Fixed trash on Form tests
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage form
|
||||
* @since 2010-04-25
|
||||
@ -12,36 +12,34 @@
|
||||
require_once dirname(__FILE__) . '/../../validator/iValidator.php';
|
||||
require_once dirname(__FILE__) . '/../../validator/Validator.php';
|
||||
require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
|
||||
require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
|
||||
require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
|
||||
require_once dirname(__FILE__) . '/../../form/FormField.php';
|
||||
|
||||
class FormFieldTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
/**/
|
||||
}
|
||||
|
||||
public function testSetRequired()
|
||||
{
|
||||
$form_field = new FormField();
|
||||
|
||||
$this->assertTrue($form_field->isRequired());
|
||||
$return_object = $form_field->setRequired(false);
|
||||
$this->assertInstanceOf('FormField', $return_object);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
$this->assertFalse($form_field->isRequired());
|
||||
}
|
||||
|
||||
|
||||
public function testSetIgnored()
|
||||
{
|
||||
$form_field = new FormField();
|
||||
|
||||
$this->assertFalse($form_field->isIgnored());
|
||||
$return_object = $form_field->setIgnored(true);
|
||||
$this->assertInstanceOf('FormField', $return_object);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
$this->assertTrue($form_field->isIgnored());
|
||||
}
|
||||
|
||||
|
||||
public function testIsIgnored()
|
||||
{
|
||||
$class_name = 'FormField';
|
||||
@ -50,7 +48,7 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
|
||||
$form_field->setIgnored(true);
|
||||
$this->assertTrue($form_field->isIgnored());
|
||||
}
|
||||
|
||||
|
||||
public function testIsRequired()
|
||||
{
|
||||
$class_name = 'FormField';
|
||||
@ -59,182 +57,246 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
|
||||
$form_field->setRequired(false);
|
||||
$this->assertFalse($form_field->isRequired());
|
||||
}
|
||||
|
||||
|
||||
public function testAddValidators()
|
||||
{
|
||||
$validator = array(
|
||||
'NotEmpty' => new NotEmptyValidator(),
|
||||
'Email' => new EmailValidator()
|
||||
$validators = array(
|
||||
'NotEmpty' => new NotEmptyValidator(),
|
||||
'Email' => new EmailValidator()
|
||||
);
|
||||
$this->assertInstanceOf('iValidator', $validator['NotEmpty']);
|
||||
$this->assertInstanceOf('iValidator', $validator['Email']);
|
||||
$tmp_form_field = new FormField();
|
||||
$return_object = $tmp_form_field->addValidators($validator);
|
||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator(),'EmailValidator'=>new EmailValidator());
|
||||
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
||||
$this->assertSame($tmp_form_field, $return_object);
|
||||
|
||||
$this->assertInstanceOf('iValidator', $validators['NotEmpty']);
|
||||
$this->assertInstanceOf('iValidator', $validators['Email']);
|
||||
$form_field = new FormField();
|
||||
$return_object = $form_field->addValidators($validators);
|
||||
$array = array('NotEmptyValidator' => new NotEmptyValidator(), 'EmailValidator' => new EmailValidator());
|
||||
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
}
|
||||
|
||||
|
||||
public function testAddValidatorObject()
|
||||
{
|
||||
$validator = new NotEmptyValidator();
|
||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator());
|
||||
$this->assertInstanceOf('iValidator', new NotEmptyValidator());
|
||||
$tmp_form_field = new FormField();
|
||||
$return_object = $tmp_form_field->addValidator($validator);
|
||||
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
||||
$this->assertSame($tmp_form_field, $return_object);
|
||||
$array = array('NotEmptyValidator' => new NotEmptyValidator());
|
||||
$form_field = new FormField();
|
||||
|
||||
$return_object = $form_field->addValidator($validator);
|
||||
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
}
|
||||
|
||||
|
||||
public function testAddValidatorString()
|
||||
{
|
||||
$tmp_form_field = new FormField();
|
||||
$return_object = $tmp_form_field->addValidator('NotEmpty');
|
||||
$this->assertInstanceOf('iValidator', new NotEmptyValidator());
|
||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator());
|
||||
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
||||
$this->assertSame($tmp_form_field, $return_object);
|
||||
$form_field = new FormField();
|
||||
$return_object = $form_field->addValidator('NotEmpty');
|
||||
|
||||
$array = array('NotEmptyValidator' => new NotEmptyValidator());
|
||||
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
}
|
||||
|
||||
|
||||
public function testAddValidatorElse()
|
||||
{
|
||||
$validator = true;
|
||||
$this->setExpectedException('Exception');
|
||||
$tmp_form_field = new FormField();
|
||||
// @TODO Fix exception type
|
||||
$this->setExpectedException('Exception', 'Invalid validator provided to addValidator; must be string or iValidator'); // Text of Exception
|
||||
$tmp_form_field->addValidator($validator);
|
||||
}
|
||||
|
||||
|
||||
public function testAddFilters()
|
||||
{
|
||||
$array = array('loginFilter' => new loginFilter(),'passwordFilter' => new passwordFilter());
|
||||
$array = array('loginFilter' => new loginFilter(), 'passwordFilter' => new passwordFilter());
|
||||
$this->assertInstanceOf('iFilter', $array['loginFilter']);
|
||||
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
|
||||
$tmp_form_field = new FormField();
|
||||
$return_object = $tmp_form_field->addFilters($array);
|
||||
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
||||
$this->assertSame($tmp_form_field, $return_object);
|
||||
|
||||
$form_field = new FormField();
|
||||
$return_object = $form_field->addFilters($array);
|
||||
$this->assertAttributeEquals($array, 'filters', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
}
|
||||
|
||||
|
||||
public function testAddFilterObject()
|
||||
{
|
||||
$filter = new loginFilter();
|
||||
$array = array('loginFilter' => new loginFilter());
|
||||
$this->assertInstanceOf('iFilter', new loginFilter());
|
||||
$tmp_form_field = new FormField();
|
||||
$return_object = $tmp_form_field->addFilter($filter);
|
||||
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
||||
$this->assertSame($tmp_form_field, $return_object);
|
||||
$form_field = new FormField();
|
||||
$return_object = $form_field->addFilter($filter);
|
||||
|
||||
$this->assertAttributeEquals($array, 'filters', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
}
|
||||
|
||||
|
||||
public function testAddFilterString()
|
||||
{
|
||||
$tmp_form_field = new FormField();
|
||||
$form_field = new FormField();
|
||||
$array = array('loginFilter' => new loginFilter());
|
||||
$this->assertInstanceOf('iFilter', new loginFilter());
|
||||
$return_object = $tmp_form_field->addFilter('login');
|
||||
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
||||
$this->assertSame($tmp_form_field, $return_object);
|
||||
$return_object = $form_field->addFilter('login');
|
||||
|
||||
$this->assertAttributeEquals($array, 'filters', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
}
|
||||
|
||||
|
||||
public function testAddFilterElse()
|
||||
{
|
||||
$this->setExpectedException('Exception');
|
||||
$filter = new NotEmptyValidator();
|
||||
$tmp_form_field = new FormField();
|
||||
$tmp_form_field->addFilter($filter);
|
||||
$form_field = new FormField();
|
||||
// @TODO Fix exception type
|
||||
$this->setExpectedException('Exception', 'Invalid filter provided to addFilter; must be string or iFilter'); // Text of exception
|
||||
$form_field->addFilter($filter);
|
||||
}
|
||||
|
||||
|
||||
public function testGetValueArray()
|
||||
{
|
||||
$form = new FormField();
|
||||
$test_array = array(
|
||||
'login'=> 'login',
|
||||
'password'=> 'password'
|
||||
'login' => 'login',
|
||||
'password' => 'password'
|
||||
);
|
||||
$this->assertTrue($form->isValid($test_array));
|
||||
$tmp_form_field = new FormField();
|
||||
$this->assertTrue($tmp_form_field->isValid($test_array));
|
||||
$this->assertAttributeInternalType('array', 'value', $tmp_form_field);
|
||||
$this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
|
||||
$form_field = new FormField();
|
||||
$form_field->addFilter('Login');
|
||||
$this->assertTrue($form_field->isValid($test_array));
|
||||
$this->assertAttributeInternalType('array', 'value', $form_field);
|
||||
$value = $form_field->getValue();
|
||||
|
||||
$this->assertArrayHasKey('login', $value);
|
||||
$this->assertArrayHasKey('password', $value);
|
||||
$this->assertSame(array('login' => 'login',
|
||||
'password' => ''), $value);
|
||||
}
|
||||
|
||||
public function testGetValueString()
|
||||
|
||||
public function testGetValueStringCorrect()
|
||||
{
|
||||
$test_string = 'login';
|
||||
$form_field = new FormField();
|
||||
$form_field->addFilter('Login');
|
||||
|
||||
$this->assertTrue($form_field->isValid($test_string));
|
||||
$this->assertAttributeNotInternalType('array', 'value', $form_field);
|
||||
$this->assertEquals('login', $form_field->getValue());
|
||||
}
|
||||
|
||||
public function testGetValueStringIncorrect()
|
||||
{
|
||||
$test_string = 'password';
|
||||
$form_field = new FormField();
|
||||
$form_field->addFilter('Login');
|
||||
|
||||
$this->assertTrue($form_field->isValid($test_string));
|
||||
$this->assertAttributeNotInternalType('array', 'value', $form_field);
|
||||
$this->assertEquals('', $form_field->getValue());
|
||||
}
|
||||
|
||||
public function testGetMessageDefault()
|
||||
{
|
||||
$form_field = new FormField();
|
||||
$this->assertFalse($form_field->getMessage());
|
||||
$form_field->addValidator('NotEmpty');
|
||||
|
||||
$this->assertFalse($form_field->isValid(''));
|
||||
$this->assertSame('Value is required and can\'t be empty', $form_field->getMessage());
|
||||
}
|
||||
|
||||
public function testGetMessageCustom()
|
||||
{
|
||||
$message = 'Test message';
|
||||
$form_field = new FormField($message);
|
||||
$this->assertFalse($form_field->getMessage());
|
||||
$form_field->addValidator('NotEmpty');
|
||||
|
||||
$this->assertFalse($form_field->isValid(''));
|
||||
$this->assertSame($message, $form_field->getMessage());
|
||||
}
|
||||
|
||||
public function testIsValidArrayMissingDefaultMessage()
|
||||
{
|
||||
$test_array = array(
|
||||
'login'=> 'login',
|
||||
'password'=> 'password'
|
||||
'login' => '',
|
||||
'password' => ''
|
||||
);
|
||||
$test_form_field = new FormField();
|
||||
$this->assertTrue($test_form_field->isValid($test_array['login']));
|
||||
$this->assertAttributeNotInternalType('array', 'value', $test_form_field);
|
||||
$this->assertEquals('login', $test_form_field->getSourceValue());
|
||||
$form_field = new FormField();
|
||||
$form_field->addValidator('NotEmpty');
|
||||
$this->setExpectedException('Exception', 'Define default message for array fields');
|
||||
$form_field->isValid($test_array);
|
||||
}
|
||||
|
||||
public function testGetMessage()
|
||||
{
|
||||
$test_form_field = new FormField();
|
||||
$this->assertNotNull($test_form_field->getMessage());
|
||||
}
|
||||
|
||||
public function testIsValidMissingException()
|
||||
|
||||
public function testIsValidArrayMissingCustomMessage()
|
||||
{
|
||||
$message = 'Test message';
|
||||
$test_array = array(
|
||||
'login'=> '',
|
||||
'password'=> ''
|
||||
'login' => '',
|
||||
'password' => ''
|
||||
);
|
||||
$test_form_field = new FormField();
|
||||
$test_form_field->addValidator('NotEmpty');
|
||||
$this->setExpectedException('Exception','Define default message for array fields');
|
||||
$this->assertTrue($test_form_field->isValid($test_array));
|
||||
$form_field = new FormField($message);
|
||||
$return_object = $form_field->addValidator('NotEmpty');
|
||||
$array = array('NotEmptyValidator' => new NotEmptyValidator());
|
||||
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||
$this->assertSame($form_field, $return_object);
|
||||
$this->assertFalse($form_field->isValid($test_array));
|
||||
$this->assertSame($message, $form_field->getMessage());
|
||||
}
|
||||
|
||||
public function testIsValidMissingDefaultMessage()
|
||||
{
|
||||
$test_array = array(
|
||||
'login'=> '',
|
||||
'password'=> ''
|
||||
);
|
||||
$test_form_field = new FormField('ssssssss');
|
||||
$return_object = $test_form_field->addValidator('NotEmpty');
|
||||
$this->assertInstanceOf('iValidator', new NotEmptyValidator());
|
||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator());
|
||||
$this->assertAttributeEquals($array, 'validators', $test_form_field);
|
||||
$this->assertSame($test_form_field, $return_object);
|
||||
$this->assertFalse($test_form_field->isValid($test_array));
|
||||
}
|
||||
|
||||
public function testIsValidMissingRequired()
|
||||
|
||||
public function testIsValidMissingNotRequired()
|
||||
{
|
||||
$form_field = new FormField();
|
||||
$form_field->setRequired(false);
|
||||
$this->assertTrue($form_field->isValid(''));
|
||||
}
|
||||
|
||||
public function testIsValidMissingValid()
|
||||
|
||||
public function testIsValidArray()
|
||||
{
|
||||
$test_array = array(
|
||||
'login'=> 'login',
|
||||
'password'=> 'password'
|
||||
'login' => 'login',
|
||||
'password' => 'password'
|
||||
);
|
||||
$validator = new NotEmptyValidator();
|
||||
$form_field = new FormField();
|
||||
$return_object = $form_field->addValidator($validator);
|
||||
$this->assertTrue($form_field->isValid($test_array));
|
||||
}
|
||||
|
||||
|
||||
public function testIsValidScalar()
|
||||
{
|
||||
$test = 'password';
|
||||
$validator = new NotEmptyValidator();
|
||||
$form_field = new FormField();
|
||||
$return_object = $form_field->addValidator($validator);
|
||||
$this->assertTrue($form_field->isValid($test));
|
||||
}
|
||||
|
||||
public function testGetSourceValue()
|
||||
{
|
||||
$test_array = array(
|
||||
'login' => ' login ',
|
||||
'password' => ''
|
||||
);
|
||||
$form_field = new FormField('Custom message');
|
||||
$form_field->addFilter('login');
|
||||
$form_field->addValidator('NotEmpty');
|
||||
$this->assertFalse($form_field->isValid($test_array));
|
||||
$this->assertSame(array('login' => 'login', 'password' => ''), $form_field->getValue());
|
||||
$this->assertNotSame($test_array, $form_field->getValue());
|
||||
$this->assertSame($test_array, $form_field->getSourceValue());
|
||||
}
|
||||
|
||||
public function testFilterValue()
|
||||
{
|
||||
$input = ' login ';
|
||||
$form_field = new FormField();
|
||||
$form_field->isValid('login');
|
||||
$form_field->isValid($input);
|
||||
$form_field->addFilter('login');
|
||||
$lf = new loginFilter();
|
||||
$this->assertSame($form_field->getValue(), $lf->filter('login'));
|
||||
$this->assertSame($form_field->getValue(), $lf->filter($input));
|
||||
}
|
||||
}
|
||||
|
||||
interface iFilter
|
||||
{
|
||||
public function isValid($value, $context = null);
|
||||
|
||||
public function filter($value);
|
||||
|
||||
public function getMessage();
|
||||
}
|
||||
|
||||
@ -242,17 +304,19 @@ class loginFilter implements iFilter
|
||||
{
|
||||
public function filter($value)
|
||||
{
|
||||
return $value;
|
||||
$value = trim($value);
|
||||
if ($value === 'login') {
|
||||
return $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -262,14 +326,12 @@ class passwordFilter implements iFilter
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user