Forms/test/base/FormTest.php
Abel Hoogeveen 8c7eb5f8aa
All checks were successful
continuous-integration/drone/push Build is passing
Implemented unit tests for Forms, Form and Field.
Many more unit tests are needed, everything related to specific Fields.
2022-12-28 14:29:34 +01:00

321 lines
9.6 KiB
PHP

<?php
/**
* FuzeWorks Forms Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2022 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2022, i15. (http://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link https://i15.nl
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
use FuzeWorks\Forms\Field;
use PHPUnit\Framework\TestCase;
use FuzeWorks\Forms\Form;
class FormTest extends TestCase
{
public function testFoundation()
{
$form = new Form('testName', 'testLabel');
$this->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());
}
}