assertEquals('testName', $form->getName()); $this->assertEquals('testLabel', $form->getLabel()); $this->assertInstanceOf(Form::class, $form); } public function testNames() { // Set the name originally $form = new Form('originalName', 'originalLabel'); $this->assertEquals('originalName', $form->getName()); // Change the name $form->setName('newName'); $this->assertEquals('newName', $form->getName()); // Change the name back $form->setName('originalName'); $this->assertEquals('originalName', $form->getName()); } public function testLabels() { // Set the label originally $form = new Form('originalName', 'originalLabel'); $this->assertEquals('originalLabel', $form->getLabel()); // Change the label $form->setLabel('newLabel'); $this->assertEquals('newLabel', $form->getLabel()); // Change the label back $form->setLabel('originalLabel'); $this->assertEquals('originalLabel', $form->getLabel()); } public function testFormErrors() { // Generate the original form $form = new Form('originalName', 'originalLabel'); $this->assertEquals([], $form->getFormErrors()); // Add some errors $form->addFormError('error1'); $form->addFormError('error2'); $this->assertEquals(['error1', 'error2'], $form->getFormErrors()); // Add some warnings $form->addFormWarning('warning1'); $form->addFormWarning('warning2'); $this->assertEquals(['warning1', 'warning2'], $form->getFormWarnings()); } /** * @depends FieldTest::testValidCondition */ public function testFields() { // Generate the original form $form = new Form('formName', 'formLabel'); // Create a field $stub = $this->getMockForAbstractClass(Field::class, ['fieldName']); // Add the field to the form $form->field($stub); // Check if the field is in the form $extractedField = $form->getField('fieldName'); $this->assertEquals($stub, $extractedField); // Check the identifier $this->assertEquals('formName_fieldName', $extractedField->getId()); // Check if the field can also be extracted through getFields $this->assertEquals([$form->getCsrfField(), $stub], $form->getFields()); } /** * @depends testFields */ public function testValidate() { // Generate the original form $form = new Form('formName', 'formLabel'); // Create a field $stub = $this->getMockForAbstractClass(Field::class, ['fieldName']); $stub->expects($this->any()) ->method('validateField') ->will($this->returnValue(true)); // Add the field to the form $form->field($stub); // Create a source for input $form->setSource(function (string $fieldName){ return match ($fieldName) { 'fieldName' => 'fieldValue', default => null, }; }); // Test that the form isn't validated yet $this->assertFalse($form->isValidated()); $this->assertFalse($form->isValid()); // And validate $this->assertTrue($form->validate()); // Assert that all is well $this->assertTrue($form->isValidated()); $this->assertTrue($form->isValid()); } /** * @depends testValidate */ public function testValidateMissingValues() { // generate the original form $form = new Form('formName', 'formLabel'); // Create a field $stub = $this->getMockForAbstractClass(Field::class, ['fieldName']); $stub->expects($this->any()) ->method('validateField') ->will($this->returnValue(true)); // Add the field to the form $form->field($stub); // Test that the form isn't validated yet $this->assertFalse($form->isValidated()); $this->assertFalse($form->isValid()); // And validate $this->assertFalse($form->validate()); // Assert that all is wrong $this->assertFalse($form->isValidated()); $this->assertFalse($form->isValid()); } /** * @depends testValidateMissingValues */ public function testValidateInvalidField() { // Create the original form $form = new Form('formName', 'formLabel'); // Create a field $stub = $this->getMockForAbstractClass(Field::class, ['fieldName']); $stub->expects($this->any()) ->method('validateField') ->will($this->returnCallback(function() use ($stub) { $stub->addError("Error"); return false; })); // Add the field to the form $form->field($stub); // Create a source for input $form->setSource(function (string $fieldName){ return match ($fieldName) { 'fieldName' => 'fieldValue', default => null, }; }); // Test that the form isn't validated yet $this->assertFalse($form->isValidated()); $this->assertFalse($form->isValid()); // And validate $this->assertFalse($form->validate()); // Assert that all is wrong $this->assertTrue($form->isValidated()); $this->assertFalse($form->isValid()); // Assert that the field has an error $this->assertEquals(['fieldName' => ['Error']], $form->getFieldErrors()); } /** * @depends testValidateInvalidField */ public function testInvalidate() { // Create the original form $form = new Form('formName', 'formLabel'); // Create a field $stub = $this->getMockForAbstractClass(Field::class, ['fieldName']); $stub->expects($this->any()) ->method('validateField') ->will($this->returnValue(true)); // Add the field to the form $form->field($stub); // Create a source for input $form->setSource(function (string $fieldName){ return match ($fieldName) { 'fieldName' => 'fieldValue', default => null, }; }); // Test that the form isn't validated yet $this->assertFalse($form->isValidated()); $this->assertFalse($form->isValid()); // And validate $this->assertTrue($form->validate()); // Assert that all is well $this->assertTrue($form->isValidated()); $this->assertTrue($form->isValid()); // Invalidate the form $form->invalidate(); // Assert that the form is invalidated $this->assertTrue($form->isValidated()); $this->assertFalse($form->isValid()); } /** * @depends testValidateInvalidField */ public function testCondition() { // Create the original form $form = new Form('formName', 'formLabel'); // Create a field $stub = $this->getMockForAbstractClass(Field::class, ['fieldName']); $stub->expects($this->any()) ->method('validateField') ->will($this->returnValue(true)); // Add the field to the form $form->field($stub); // Create a source for input $form->setSource(function (string $fieldName){ return match ($fieldName) { 'fieldName' => 'fieldValue', default => null, }; }); // Add a condition $form->condition(function (Form $form) { return false; }); // Test that the form isn't validated yet $this->assertFalse($form->isValidated()); $this->assertFalse($form->isValid()); // And validate $this->assertFalse($form->validate()); // Assert that all is wrong $this->assertTrue($form->isValidated()); $this->assertFalse($form->isValid()); } }