Fixed trash on Form tests
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @copyright NetMonsters <team@netmonsters.ru>
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
* @link http://netmonsters.ru
|
* @link http://netmonsters.ru
|
||||||
* @package Majestic
|
* @package Majestic
|
||||||
* @subpackage form
|
* @subpackage form
|
||||||
* @since 2010-04-25
|
* @since 2010-04-25
|
||||||
@ -12,36 +12,34 @@
|
|||||||
require_once dirname(__FILE__) . '/../../validator/iValidator.php';
|
require_once dirname(__FILE__) . '/../../validator/iValidator.php';
|
||||||
require_once dirname(__FILE__) . '/../../validator/Validator.php';
|
require_once dirname(__FILE__) . '/../../validator/Validator.php';
|
||||||
require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.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__) . '/../../validator/EmailValidator.php';
|
||||||
require_once dirname(__FILE__) . '/../../form/FormField.php';
|
require_once dirname(__FILE__) . '/../../form/FormField.php';
|
||||||
|
|
||||||
class FormFieldTest extends PHPUnit_Framework_TestCase
|
class FormFieldTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function setUp()
|
|
||||||
{
|
|
||||||
/**/
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSetRequired()
|
public function testSetRequired()
|
||||||
{
|
{
|
||||||
$form_field = new FormField();
|
$form_field = new FormField();
|
||||||
|
|
||||||
$this->assertTrue($form_field->isRequired());
|
$this->assertTrue($form_field->isRequired());
|
||||||
$return_object = $form_field->setRequired(false);
|
$return_object = $form_field->setRequired(false);
|
||||||
$this->assertInstanceOf('FormField', $return_object);
|
$this->assertInstanceOf('FormField', $return_object);
|
||||||
$this->assertSame($form_field, $return_object);
|
$this->assertSame($form_field, $return_object);
|
||||||
$this->assertFalse($form_field->isRequired());
|
$this->assertFalse($form_field->isRequired());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetIgnored()
|
public function testSetIgnored()
|
||||||
{
|
{
|
||||||
$form_field = new FormField();
|
$form_field = new FormField();
|
||||||
|
|
||||||
$this->assertFalse($form_field->isIgnored());
|
$this->assertFalse($form_field->isIgnored());
|
||||||
$return_object = $form_field->setIgnored(true);
|
$return_object = $form_field->setIgnored(true);
|
||||||
$this->assertInstanceOf('FormField', $return_object);
|
$this->assertInstanceOf('FormField', $return_object);
|
||||||
$this->assertSame($form_field, $return_object);
|
$this->assertSame($form_field, $return_object);
|
||||||
$this->assertTrue($form_field->isIgnored());
|
$this->assertTrue($form_field->isIgnored());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsIgnored()
|
public function testIsIgnored()
|
||||||
{
|
{
|
||||||
$class_name = 'FormField';
|
$class_name = 'FormField';
|
||||||
@ -50,7 +48,7 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
|
|||||||
$form_field->setIgnored(true);
|
$form_field->setIgnored(true);
|
||||||
$this->assertTrue($form_field->isIgnored());
|
$this->assertTrue($form_field->isIgnored());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsRequired()
|
public function testIsRequired()
|
||||||
{
|
{
|
||||||
$class_name = 'FormField';
|
$class_name = 'FormField';
|
||||||
@ -59,182 +57,246 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
|
|||||||
$form_field->setRequired(false);
|
$form_field->setRequired(false);
|
||||||
$this->assertFalse($form_field->isRequired());
|
$this->assertFalse($form_field->isRequired());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddValidators()
|
public function testAddValidators()
|
||||||
{
|
{
|
||||||
$validator = array(
|
$validators = array(
|
||||||
'NotEmpty' => new NotEmptyValidator(),
|
'NotEmpty' => new NotEmptyValidator(),
|
||||||
'Email' => new EmailValidator()
|
'Email' => new EmailValidator()
|
||||||
);
|
);
|
||||||
$this->assertInstanceOf('iValidator', $validator['NotEmpty']);
|
|
||||||
$this->assertInstanceOf('iValidator', $validator['Email']);
|
$this->assertInstanceOf('iValidator', $validators['NotEmpty']);
|
||||||
$tmp_form_field = new FormField();
|
$this->assertInstanceOf('iValidator', $validators['Email']);
|
||||||
$return_object = $tmp_form_field->addValidators($validator);
|
$form_field = new FormField();
|
||||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator(),'EmailValidator'=>new EmailValidator());
|
$return_object = $form_field->addValidators($validators);
|
||||||
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
$array = array('NotEmptyValidator' => new NotEmptyValidator(), 'EmailValidator' => new EmailValidator());
|
||||||
$this->assertSame($tmp_form_field, $return_object);
|
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||||
|
$this->assertSame($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddValidatorObject()
|
public function testAddValidatorObject()
|
||||||
{
|
{
|
||||||
$validator = new NotEmptyValidator();
|
$validator = new NotEmptyValidator();
|
||||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator());
|
$array = array('NotEmptyValidator' => new NotEmptyValidator());
|
||||||
$this->assertInstanceOf('iValidator', new NotEmptyValidator());
|
$form_field = new FormField();
|
||||||
$tmp_form_field = new FormField();
|
|
||||||
$return_object = $tmp_form_field->addValidator($validator);
|
$return_object = $form_field->addValidator($validator);
|
||||||
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||||
$this->assertSame($tmp_form_field, $return_object);
|
$this->assertSame($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddValidatorString()
|
public function testAddValidatorString()
|
||||||
{
|
{
|
||||||
$tmp_form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$return_object = $tmp_form_field->addValidator('NotEmpty');
|
$return_object = $form_field->addValidator('NotEmpty');
|
||||||
$this->assertInstanceOf('iValidator', new NotEmptyValidator());
|
|
||||||
$array = array('NotEmptyValidator'=>new NotEmptyValidator());
|
$array = array('NotEmptyValidator' => new NotEmptyValidator());
|
||||||
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
$this->assertAttributeEquals($array, 'validators', $form_field);
|
||||||
$this->assertSame($tmp_form_field, $return_object);
|
$this->assertSame($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddValidatorElse()
|
public function testAddValidatorElse()
|
||||||
{
|
{
|
||||||
$validator = true;
|
$validator = true;
|
||||||
$this->setExpectedException('Exception');
|
|
||||||
$tmp_form_field = new FormField();
|
$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);
|
$tmp_form_field->addValidator($validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddFilters()
|
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['loginFilter']);
|
||||||
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
|
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
|
||||||
$tmp_form_field = new FormField();
|
|
||||||
$return_object = $tmp_form_field->addFilters($array);
|
$form_field = new FormField();
|
||||||
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
$return_object = $form_field->addFilters($array);
|
||||||
$this->assertSame($tmp_form_field, $return_object);
|
$this->assertAttributeEquals($array, 'filters', $form_field);
|
||||||
|
$this->assertSame($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddFilterObject()
|
public function testAddFilterObject()
|
||||||
{
|
{
|
||||||
$filter = new loginFilter();
|
$filter = new loginFilter();
|
||||||
$array = array('loginFilter' => new loginFilter());
|
$array = array('loginFilter' => new loginFilter());
|
||||||
$this->assertInstanceOf('iFilter', new loginFilter());
|
$this->assertInstanceOf('iFilter', new loginFilter());
|
||||||
$tmp_form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$return_object = $tmp_form_field->addFilter($filter);
|
$return_object = $form_field->addFilter($filter);
|
||||||
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
|
||||||
$this->assertSame($tmp_form_field, $return_object);
|
$this->assertAttributeEquals($array, 'filters', $form_field);
|
||||||
|
$this->assertSame($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddFilterString()
|
public function testAddFilterString()
|
||||||
{
|
{
|
||||||
$tmp_form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$array = array('loginFilter' => new loginFilter());
|
$array = array('loginFilter' => new loginFilter());
|
||||||
$this->assertInstanceOf('iFilter', new loginFilter());
|
$this->assertInstanceOf('iFilter', new loginFilter());
|
||||||
$return_object = $tmp_form_field->addFilter('login');
|
$return_object = $form_field->addFilter('login');
|
||||||
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
|
||||||
$this->assertSame($tmp_form_field, $return_object);
|
$this->assertAttributeEquals($array, 'filters', $form_field);
|
||||||
|
$this->assertSame($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddFilterElse()
|
public function testAddFilterElse()
|
||||||
{
|
{
|
||||||
$this->setExpectedException('Exception');
|
|
||||||
$filter = new NotEmptyValidator();
|
$filter = new NotEmptyValidator();
|
||||||
$tmp_form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$tmp_form_field->addFilter($filter);
|
// @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()
|
public function testGetValueArray()
|
||||||
{
|
{
|
||||||
$form = new FormField();
|
|
||||||
$test_array = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=> 'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertTrue($form->isValid($test_array));
|
$form_field = new FormField();
|
||||||
$tmp_form_field = new FormField();
|
$form_field->addFilter('Login');
|
||||||
$this->assertTrue($tmp_form_field->isValid($test_array));
|
$this->assertTrue($form_field->isValid($test_array));
|
||||||
$this->assertAttributeInternalType('array', 'value', $tmp_form_field);
|
$this->assertAttributeInternalType('array', 'value', $form_field);
|
||||||
$this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
|
$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(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => '',
|
||||||
'password'=> 'password'
|
'password' => ''
|
||||||
);
|
);
|
||||||
$test_form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$this->assertTrue($test_form_field->isValid($test_array['login']));
|
$form_field->addValidator('NotEmpty');
|
||||||
$this->assertAttributeNotInternalType('array', 'value', $test_form_field);
|
$this->setExpectedException('Exception', 'Define default message for array fields');
|
||||||
$this->assertEquals('login', $test_form_field->getSourceValue());
|
$form_field->isValid($test_array);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetMessage()
|
public function testIsValidArrayMissingCustomMessage()
|
||||||
{
|
|
||||||
$test_form_field = new FormField();
|
|
||||||
$this->assertNotNull($test_form_field->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testIsValidMissingException()
|
|
||||||
{
|
{
|
||||||
|
$message = 'Test message';
|
||||||
$test_array = array(
|
$test_array = array(
|
||||||
'login'=> '',
|
'login' => '',
|
||||||
'password'=> ''
|
'password' => ''
|
||||||
);
|
);
|
||||||
$test_form_field = new FormField();
|
$form_field = new FormField($message);
|
||||||
$test_form_field->addValidator('NotEmpty');
|
$return_object = $form_field->addValidator('NotEmpty');
|
||||||
$this->setExpectedException('Exception','Define default message for array fields');
|
$array = array('NotEmptyValidator' => new NotEmptyValidator());
|
||||||
$this->assertTrue($test_form_field->isValid($test_array));
|
$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()
|
public function testIsValidMissingNotRequired()
|
||||||
{
|
|
||||||
$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()
|
|
||||||
{
|
{
|
||||||
$form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$form_field->setRequired(false);
|
$form_field->setRequired(false);
|
||||||
$this->assertTrue($form_field->isValid(''));
|
$this->assertTrue($form_field->isValid(''));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsValidMissingValid()
|
public function testIsValidArray()
|
||||||
{
|
{
|
||||||
$test_array = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=> 'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$validator = new NotEmptyValidator();
|
$validator = new NotEmptyValidator();
|
||||||
$form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$return_object = $form_field->addValidator($validator);
|
$return_object = $form_field->addValidator($validator);
|
||||||
$this->assertTrue($form_field->isValid($test_array));
|
$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()
|
public function testFilterValue()
|
||||||
{
|
{
|
||||||
|
$input = ' login ';
|
||||||
$form_field = new FormField();
|
$form_field = new FormField();
|
||||||
$form_field->isValid('login');
|
$form_field->isValid($input);
|
||||||
$form_field->addFilter('login');
|
$form_field->addFilter('login');
|
||||||
$lf = new loginFilter();
|
$lf = new loginFilter();
|
||||||
$this->assertSame($form_field->getValue(), $lf->filter('login'));
|
$this->assertSame($form_field->getValue(), $lf->filter($input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface iFilter
|
interface iFilter
|
||||||
{
|
{
|
||||||
public function isValid($value, $context = null);
|
public function isValid($value, $context = null);
|
||||||
|
|
||||||
|
public function filter($value);
|
||||||
|
|
||||||
public function getMessage();
|
public function getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,17 +304,19 @@ class loginFilter implements iFilter
|
|||||||
{
|
{
|
||||||
public function filter($value)
|
public function filter($value)
|
||||||
{
|
{
|
||||||
return $value;
|
$value = trim($value);
|
||||||
|
if ($value === 'login') {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValid($value, $context = null)
|
public function isValid($value, $context = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMessage()
|
public function getMessage()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,14 +326,12 @@ class passwordFilter implements iFilter
|
|||||||
{
|
{
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValid($value, $context = null)
|
public function isValid($value, $context = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMessage()
|
public function getMessage()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* To change this template, choose Tools | Templates
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
* and open the template in the editor.
|
* @link http://netmonsters.ru
|
||||||
|
* @package Majestic
|
||||||
|
* @subpackage UnitTests
|
||||||
|
* @since 2011-10-07
|
||||||
|
*
|
||||||
|
* Unit tests for Form class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../form/Form.php';
|
require_once dirname(__FILE__) . '/../../form/Form.php';
|
||||||
@ -17,197 +22,213 @@ class FormTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
public function testAddFieldEmptyMessage()
|
public function testAddFieldEmptyMessage()
|
||||||
{
|
{
|
||||||
|
$form_field = new FormField();
|
||||||
$stub = $this->getMockForAbstractClass('Form');
|
$stub = $this->getMockForAbstractClass('Form');
|
||||||
$method = new ReflectionMethod('Form', 'addField');
|
$method = new ReflectionMethod('Form', 'addField');
|
||||||
$method->setAccessible(true);
|
$method->setAccessible(true);
|
||||||
$this->assertInstanceOf('FormField', $method->invoke($stub, 'login'));
|
|
||||||
|
$return_object = $method->invokeArgs($stub, array('login'));
|
||||||
|
|
||||||
|
$this->assertInstanceOf('FormField', $return_object);
|
||||||
|
$this->assertEquals($form_field, $return_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddFieldWithMessage()
|
public function testAddFieldWithMessage()
|
||||||
{
|
{
|
||||||
$message = 'message';
|
$message = 'Test message';
|
||||||
$form_field = new FormField($message);
|
$form_field = new FormField($message);
|
||||||
$stub = $this->getMockForAbstractClass('Form');
|
$stub = $this->getMockForAbstractClass('Form');
|
||||||
$method = new ReflectionMethod('Form', 'addField');
|
$method = new ReflectionMethod('Form', 'addField');
|
||||||
$method->setAccessible(true);
|
$method->setAccessible(true);
|
||||||
$return_object = $method->invokeArgs($stub, array('login',$message));
|
|
||||||
|
$return_object = $method->invokeArgs($stub, array('login', $message));
|
||||||
$this->assertInstanceOf('FormField', $return_object);
|
$this->assertInstanceOf('FormField', $return_object);
|
||||||
$this->assertEquals($form_field, $return_object);
|
$this->assertEquals($form_field, $return_object);
|
||||||
$this->assertAttributeEquals('message', 'default_message', $return_object);
|
$this->assertAttributeEquals($message, 'default_message', $return_object);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsValidWithNotArray()
|
public function testIsValidWithNotArray()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$this->setExpectedException('Exception');
|
// @TODO Fix exception type
|
||||||
|
$this->setExpectedException('Exception', 'Form::Form::isValid expects an array');
|
||||||
$form->isValid('');
|
$form->isValid('');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsValidCorrectFields()
|
public function testIsValidCorrectFields()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$test_array = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=> 'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertSame(1, $form->isValid($test_array));
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsValidMissingField()
|
public function testIsValidMissingField()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$test_array = array(
|
$test_array = array(
|
||||||
'password'=> 'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertSame(0, $form->isValid($test_array));
|
$this->assertSame(0, $form->isValid($test_array));
|
||||||
$data['messages'] = $form->getMessages();
|
$data['messages'] = $form->getMessages();
|
||||||
$data['values'] = $form->getSourceValues();
|
$data['values'] = $form->getSourceValues();
|
||||||
$this->assertSame($_SESSION['NotEmptyForm'], $data);
|
$this->assertSame($_SESSION['NotEmptyForm'], $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFillHelperData()
|
|
||||||
{
|
|
||||||
$form = new NotEmptyForm();
|
|
||||||
$test_array = array(
|
|
||||||
'password'=> 'password'
|
|
||||||
);
|
|
||||||
$form->isValid($test_array);
|
|
||||||
$data['messages'] = $form->getMessages();
|
|
||||||
$data['values'] = $form->getSourceValues();
|
|
||||||
$stub = $this->getMockForAbstractClass('Form');
|
|
||||||
$method = new ReflectionMethod('Form', 'fillHelperData');
|
|
||||||
$method->setAccessible(true);
|
|
||||||
$method->invoke($stub);
|
|
||||||
$this->assertSame($_SESSION['NotEmptyForm'], $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetValues()
|
public function testGetValues()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=>'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertSame(1, $form->isValid($fields));
|
|
||||||
$this->assertEquals($fields, $form->getValues());
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
|
$this->assertSame($test_array, $form->getValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testGetValuesWithFieldIsIgnored()
|
||||||
|
{
|
||||||
|
$form = new WithIgnoredFieldForm();
|
||||||
|
$test_array = array(
|
||||||
|
'login' => 'login',
|
||||||
|
'password' => 'password',
|
||||||
|
'remember' => true
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
|
$this->assertSame(array(
|
||||||
|
'login' => 'login',
|
||||||
|
'password' => 'password'), $form->getValues());
|
||||||
|
$this->assertSame($test_array, $form->getSourceValues());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testGetValueCorrect()
|
public function testGetValueCorrect()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=>'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertSame(1, $form->isValid($fields));
|
|
||||||
$this->assertEquals($fields['login'], $form->getValue('login'));
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
$this->assertEquals($fields['password'], $form->getValue('password'));
|
$this->assertSame($test_array['login'], $form->getValue('login'));
|
||||||
|
$this->assertSame($test_array['password'], $form->getValue('password'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testGetValueEmpty()
|
||||||
|
{
|
||||||
|
$form = new NotEmptyForm();
|
||||||
|
$test_array = array(
|
||||||
|
'login' => 'login'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(0, $form->isValid($test_array));
|
||||||
|
$this->assertSame('login', $form->getValue('login'));
|
||||||
|
$this->assertNull($form->getValue('password'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testGetValueMissing()
|
public function testGetValueMissing()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> ''
|
'login' => 'login',
|
||||||
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertSame(0, $form->isValid($fields));
|
|
||||||
$this->assertEquals('', $form->getValue('login'));
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
$this->assertEquals('', $form->getValue('password'));
|
$this->assertSame('login', $form->getValue('login'));
|
||||||
|
$this->assertSame('password', $form->getValue('password'));
|
||||||
|
$this->assertFalse($form->getValue('missing'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetValueMissingFieldLogin()
|
|
||||||
{
|
|
||||||
$form = new NotEmptyForm();
|
|
||||||
$fields = array(
|
|
||||||
'login'=> ''
|
|
||||||
);
|
|
||||||
$this->assertSame(0, $form->isValid($fields));
|
|
||||||
$this->assertFalse($form->getValue('loginw'));
|
|
||||||
$this->assertEquals('', $form->getValue('passwordw'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetValueMissingFieldPassword()
|
|
||||||
{
|
|
||||||
$form = new NotEmptyForm();
|
|
||||||
$fields = array(
|
|
||||||
'login'=> ''
|
|
||||||
);
|
|
||||||
$this->assertSame(0, $form->isValid($fields));
|
|
||||||
$this->assertFalse($form->getValue('loginw'));
|
|
||||||
$this->assertEquals('', $form->getValue('password'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetMessageTypeCorrect()
|
public function testGetMessageTypeCorrect()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=>'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertEquals(1, $form->isValid($fields));
|
|
||||||
$this->assertEquals('success', Form::SUCCESS);
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
$this->assertEquals($form->getMessageType(), Form::SUCCESS);
|
$this->assertSame('success', Form::SUCCESS);
|
||||||
|
$this->assertSame($form->getMessageType(), Form::SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetMessageTypeMissing()
|
public function testGetMessageTypeMissing()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> '',
|
'login' => '',
|
||||||
'password'=>''
|
'password' => ''
|
||||||
);
|
);
|
||||||
$this->assertSame(0, $form->isValid($fields));
|
|
||||||
$this->assertEquals('error', Form::ERROR);
|
$this->assertSame(0, $form->isValid($test_array));
|
||||||
$this->assertEquals($form->getMessageType(), Form::ERROR);
|
$this->assertSame('error', Form::ERROR);
|
||||||
|
$this->assertSame($form->getMessageType(), Form::ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetMessageCorrect()
|
public function testGetMessageCorrect()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$message = 'Test message';
|
||||||
'login'=> 'login',
|
$test_array = array(
|
||||||
'password'=>'password'
|
'login' => 'login',
|
||||||
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertSame(1, $form->isValid($fields));
|
|
||||||
$form->setSuccessMessage('message');
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
$this->assertEquals('message', $form->getMessage());
|
$form->setSuccessMessage($message);
|
||||||
|
$this->assertSame($message, $form->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetMessageMissing()
|
public function testGetMessageMissing()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$message = 'Test message';
|
||||||
'login'=> ''
|
$test_array = array(
|
||||||
|
'login' => ''
|
||||||
);
|
);
|
||||||
$this->assertSame(0, $form->isValid($fields));
|
|
||||||
$form->setErrorMessage('message');
|
$this->assertSame(0, $form->isValid($test_array));
|
||||||
$this->assertEquals('message', $form->getMessage());
|
$form->setErrorMessage($message);
|
||||||
|
$this->assertSame($message, $form->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetSuccessMessage()
|
public function testSetSuccessMessage()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> 'login',
|
'login' => 'login',
|
||||||
'password'=>'password'
|
'password' => 'password'
|
||||||
);
|
);
|
||||||
$this->assertSame(1, $form->isValid($fields));
|
|
||||||
|
$this->assertSame(1, $form->isValid($test_array));
|
||||||
$message = 'Form data valid';
|
$message = 'Form data valid';
|
||||||
$form->setSuccessMessage($message);
|
$form->setSuccessMessage($message);
|
||||||
$this->assertNotNull($form->getMessage());
|
$this->assertSame($message, $form->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetErrorMessage()
|
public function testSetErrorMessage()
|
||||||
{
|
{
|
||||||
$form = new NotEmptyForm();
|
$form = new NotEmptyForm();
|
||||||
$fields = array(
|
$test_array = array(
|
||||||
'login'=> ''
|
'login' => ''
|
||||||
);
|
);
|
||||||
$this->assertSame(0, $form->isValid($fields));
|
|
||||||
|
$this->assertSame(0, $form->isValid($test_array));
|
||||||
$message = 'Form data invalid';
|
$message = 'Form data invalid';
|
||||||
$form->setErrorMessage($message);
|
$form->setErrorMessage($message);
|
||||||
$this->assertNotNull($form->getMessage());
|
$this->assertSame($message, $form->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,14 +237,30 @@ class NotEmptyForm extends Form
|
|||||||
{
|
{
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
///*
|
|
||||||
$validator = new NotEmptyValidator();
|
$validator = new NotEmptyValidator();
|
||||||
$validator->setMessage('Enter login name.');
|
$validator->setMessage('Enter login name.');
|
||||||
$this->addField('login')->addValidator($validator);
|
$this->addField('login')->addValidator($validator);
|
||||||
|
|
||||||
// User password
|
|
||||||
$validator = new NotEmptyValidator();
|
$validator = new NotEmptyValidator();
|
||||||
$validator->setMessage('Enter your password.');
|
$validator->setMessage('Enter your password.');
|
||||||
$this->addField('password')->addValidator($validator);
|
$this->addField('password')->addValidator($validator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class WithIgnoredFieldForm extends Form
|
||||||
|
{
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$validator = new NotEmptyValidator();
|
||||||
|
$validator->setMessage('Enter login name.');
|
||||||
|
$this->addField('login')->addValidator($validator);
|
||||||
|
|
||||||
|
$validator = new NotEmptyValidator();
|
||||||
|
$validator->setMessage('Enter your password.');
|
||||||
|
$this->addField('password')->addValidator($validator);
|
||||||
|
|
||||||
|
$validator = new NotEmptyValidator();
|
||||||
|
$validator->setMessage('Remember');
|
||||||
|
$this->addField('remember')->addValidator($validator)->setIgnored(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/*
|
||||||
* @copyright NetMonsters <team@netmonsters.ru>
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
* @link http://netmonsters.ru
|
* @link http://netmonsters.ru
|
||||||
* @package Majestic
|
* @package Majestic
|
||||||
* @subpackage Form
|
* @subpackage UnitTests
|
||||||
* @since 2010-04-25
|
* @since 2011-10-07
|
||||||
* @version SVN: $Id$
|
*
|
||||||
* @filesource $URL$
|
* Unit tests for Form class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../view/iView.php';
|
require_once dirname(__FILE__) . '/../../view/iView.php';
|
||||||
@ -19,53 +19,79 @@ class FormViewHelperTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
Session::start();
|
$this->view = new PHPView(array('path' => '/path/to/templates'));
|
||||||
$phpview = new PHPView(array('path' => 'document_root'));
|
$this->formname = 'formname';
|
||||||
$this->form = new FormViewHelper($phpview);
|
$this->test_array = array('values' => array('key1' => 'val"ue1'), 'messages' => array('field1' => 'Can\'t serialize "value"'));
|
||||||
$_SESSION['path'] = array(
|
Session::set($this->formname, $this->test_array);
|
||||||
'values'=>array(
|
|
||||||
'path'=>'path'
|
|
||||||
),
|
|
||||||
'messages'=>array(
|
|
||||||
'path'=>'path'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testForm2()
|
public function testFormWithNotViewInstance()
|
||||||
{
|
{
|
||||||
$this->assertNull($this->assertAttributeEquals(null, 'data', $this->form));
|
// @TODO check, that iView used in construct
|
||||||
$this->setExpectedException('Exception');
|
$form = new FormViewHelper('something');
|
||||||
$this->form->form(null);
|
$this->assertInstanceOf('FormViewHelper', $form);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testForm1()
|
public function testFormUnsetFormName()
|
||||||
{
|
{
|
||||||
$this->assertNull($this->assertAttributeEquals(null, 'data', $this->form)); //аналог getProp
|
$helper = new FormViewHelper($this->view);
|
||||||
$this->form->form('path');
|
$this->setExpectedException('Exception', 'Form name required for helper init');
|
||||||
|
// @TODO Refactor for form name is required param?
|
||||||
|
$helper->form();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValueIsset()
|
public function testFormEmptyFormName()
|
||||||
{
|
{
|
||||||
$this->form->form('path');
|
$helper = new FormViewHelper($this->view);
|
||||||
$this->assertEquals(htmlentities('path', ENT_QUOTES, 'UTF-8'), $this->form->value('path'));
|
$this->setExpectedException('Exception', 'Form name required for helper init');
|
||||||
|
$helper->form('');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValueNotIsset()
|
public function testFillData()
|
||||||
{
|
{
|
||||||
$this->form->form('path');
|
$helper = new FormViewHelper($this->view);
|
||||||
$this->assertEquals(htmlentities('param2', ENT_QUOTES, 'UTF-8'), $this->form->value('param1','param2'));
|
|
||||||
|
$this->assertAttributeSame(null, 'data', $helper);
|
||||||
|
$return_obj = $helper->form($this->formname);
|
||||||
|
$this->assertAttributeSame($this->test_array, 'data', $helper);
|
||||||
|
$this->assertNull(Session::get($this->formname));
|
||||||
|
$this->assertSame($helper, $return_obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMessageIsset()
|
public function testValueSet()
|
||||||
{
|
{
|
||||||
$this->form->form('path');
|
$helper = new FormViewHelper($this->view);
|
||||||
$this->assertEquals('<span class="error">' . htmlentities('path', ENT_QUOTES, 'UTF-8') . '</span>', $this->form->message('path'));
|
$helper->form($this->formname);
|
||||||
|
|
||||||
|
$value = $helper->value('key1');
|
||||||
|
$this->assertSame($this->view->escape('val"ue1'), $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMessageNotIsset()
|
public function testValueDefault()
|
||||||
{
|
{
|
||||||
$this->form->form('path');
|
$helper = new FormViewHelper($this->view);
|
||||||
$this->assertEmpty($this->form->message(''));
|
$helper->form($this->formname);
|
||||||
|
|
||||||
|
$value = $helper->value('key_not_exist', 'default"');
|
||||||
|
$this->assertSame($this->view->escape('default"'), $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMessageSet()
|
||||||
|
{
|
||||||
|
$helper = new FormViewHelper($this->view);
|
||||||
|
$helper->form($this->formname);
|
||||||
|
|
||||||
|
$value = $helper->message('field1');
|
||||||
|
$this->assertSame('<span class="error">' . $this->view->escape('Can\'t serialize "value"') . '</span>', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMessageNotSet()
|
||||||
|
{
|
||||||
|
|
||||||
|
$helper = new FormViewHelper($this->view);
|
||||||
|
$helper->form($this->formname);
|
||||||
|
|
||||||
|
$value = $helper->message('key_not_exist', 'default"');
|
||||||
|
$this->assertSame('', $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user