Forms/test/base/FieldTest.php

386 lines
11 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 PHPUnit\Framework\TestCase;
use FuzeWorks\Forms\Field;
class FieldTest extends TestCase
{
public function testFoundation()
{
// Create a new field
$stub = $this->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);
}
}