getMockForAbstractClass(Field::class, ['testName']); $this->assertEquals('testName', $stub->getName()); $this->assertInstanceOf(Field::class, $stub); } /** * @depends testFoundation */ public function testName() { // Set the name originally $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $this->assertEquals('originalName', $stub->getName()); } /** * @depends testFoundation */ public function testLabel() { // Set the label originally $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // First test the label if none is set $this->assertEquals('OriginalName', $stub->getLabel()); // Set the label $stub->setLabel('newLabel'); $this->assertEquals('newLabel', $stub->getLabel()); } /** * @depends testFoundation */ public function testIdentifier() { // Set the label originally $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // This test presumes that the field always has a formName $stub->setFormName('testForm'); // First test the label if none is set $this->assertEquals('testForm_originalName', $stub->getId()); // Set the label $stub->setId('newIdentifier'); // Test if the identifier is set $this->assertEquals('testForm_newIdentifier', $stub->getId()); } /** * @depends testFoundation */ public function testNote() { // Set the label originally $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // First test the label if none is set $this->assertNull($stub->getNote()); // Set the label $stub->setNote('newNote'); $this->assertEquals('newNote', $stub->getNote()); } /** * @depends testFoundation */ public function testValue() { // Set the label originally $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // First test the label if none is set $this->assertNull($stub->getValue()); // Set the label $stub->setValue('newValue'); $this->assertEquals('newValue', $stub->getValue()); } /** * @depends testValue */ public function testLock() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // Set the value $stub->setValue('newValue'); $this->assertEquals('newValue', $stub->getValue()); // Lock the field $stub->lock(); // Try to change the value $stub->setValue('newValue2'); // And assert it hasn't changed $this->assertEquals('newValue', $stub->getValue()); } /** * @depends testFoundation */ public function testErrors() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // First test the label if none is set $this->assertEmpty($stub->getErrors()); // Set the label $stub->addError('newError'); $this->assertEquals(['newError'], $stub->getErrors()); } /** * @depends testValue, testErrors */ public function testBaseValidation() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); // And set a value $stub->setValue('newValue'); // Test that the field isn't validated yet $this->assertFalse($stub->isValidated()); $this->assertFalse($stub->isValid()); // Validate the field $this->assertTrue($stub->validate()); // Assert that all is well $this->assertTrue($stub->isValidated()); $this->assertTrue($stub->isValid()); } /** * @depends testBaseValidation */ public function testMissingValue() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); // Test that the field isn't validated yet $this->assertFalse($stub->isValidated()); $this->assertFalse($stub->isValid()); // Validate the field $this->assertFalse($stub->validate()); // Assert that all is wrong $this->assertTrue($stub->isValidated()); $this->assertFalse($stub->isValid()); // And assert that the error is set $this->assertEquals([$stub->getLabel() . " may not be empty."], $stub->getErrors()); } /** * @depends testMissingValue */ public function testEmptyErrorString() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); $stub->emptyErrorString("Value may not be missing."); // Test that the field isn't validated yet $this->assertFalse($stub->isValidated()); $this->assertFalse($stub->isValid()); // Validate the field $this->assertFalse($stub->validate()); // Assert that all is wrong $this->assertTrue($stub->isValidated()); $this->assertFalse($stub->isValid()); // And assert that the error is set $this->assertEquals(["Value may not be missing."], $stub->getErrors()); } /** * @depends testMissingValue */ public function testOptionalField() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); // Set the field as optional $stub->optional(); // Validate the field $this->assertTrue($stub->validate()); // Assert that all is well $this->assertTrue($stub->isValidated()); $this->assertTrue($stub->isValid()); } /** * @depends testBaseValidation */ public function testInvalidField() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); // Stub expects validateField to be called, return true and adds an error to $this->errors[] $stub->expects($this->any())->method("validateField")->will($this->returnCallback(function() use ($stub) { $stub->addError("Error"); return false; })); // And set a value $stub->setValue('newValue'); // Validate the field $this->assertFalse($stub->validate()); // Assert that all is wrong $this->assertTrue($stub->isValidated()); $this->assertFalse($stub->isValid()); // And assert that the error is set $this->assertEquals(["Error"], $stub->getErrors()); } /** * @depends testInvalidField */ public function testInvalidate() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); // And set a value $stub->setValue('newValue'); // Validate the field $this->assertTrue($stub->validate()); // Assert that all is well $this->assertTrue($stub->isValidated()); $this->assertTrue($stub->isValid()); // Invalidate the field $stub->invalidate(); // Assert that all is wrong $this->assertTrue($stub->isValidated()); $this->assertFalse($stub->isValid()); } /** * @depends testInvalidField */ public function testCondition() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); // Set a value $stub->setValue('newValue'); // Add a condition $stub->condition(function(Field $field) { $field->addError("Condition error."); return false; }); // Validate the field $this->assertFalse($stub->validate()); // Assert that all is wrong $this->assertTrue($stub->isValidated()); $this->assertFalse($stub->isValid()); // And assert that the error is set $this->assertEquals(["Condition error."], $stub->getErrors()); } /** * @depends testCondition */ public function testValidCondition() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("validateField")->will($this->returnValue(true)); // Set a value $stub->setValue('newValue'); // Add a condition $stub->condition(function(Field $field) { return true; }); // Validate the field $this->assertTrue($stub->validate()); // Assert that all is well $this->assertTrue($stub->isValidated()); $this->assertTrue($stub->isValid()); // And assert that no error is set $this->assertEquals([], $stub->getErrors()); } /** * @depends testFoundation */ public function testGenerate() { // Create the field $stub = $this->getMockForAbstractClass(Field::class, ['originalName']); $stub->expects($this->any())->method("generateHtml")->will($this->returnValue("html")); // Generate the field $this->assertEquals("html", $stub->generateHtml()); $this->assertEquals("html", (string) $stub); } }