You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
363 lines
10 KiB
363 lines
10 KiB
<?php
|
|
/**
|
|
* @copyright NetMonsters <team@netmonsters.ru>
|
|
* @link http://netmonsters.ru
|
|
* @package Majestic
|
|
* @subpackage form
|
|
* @since 2010-04-25
|
|
* @version SVN: $Id$
|
|
* @filesource $URL$
|
|
*/
|
|
|
|
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/Form.php';
|
|
require_once dirname(__FILE__) . '/../../form/FormField.php';
|
|
|
|
|
|
class FormFieldTest extends PHPUnit_Framework_TestCase
|
|
{
|
|
public function setUp()
|
|
{
|
|
$this->ff = new TestFormForFormField;
|
|
$this->obj_fields = $this->ff->getProp('fields');
|
|
}
|
|
|
|
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';
|
|
$form_field = new $class_name;
|
|
$this->assertFalse($form_field->isIgnored());
|
|
$form_field->setIgnored(true);
|
|
$this->assertTrue($form_field->isIgnored());
|
|
}
|
|
|
|
public function testIsRequired()
|
|
{
|
|
$class_name = 'FormField';
|
|
$form_field = new $class_name;
|
|
$this->assertTrue($form_field->isRequired());
|
|
$form_field->setRequired(false);
|
|
$this->assertFalse($form_field->isRequired());
|
|
}
|
|
|
|
public function testAddValidators()
|
|
{
|
|
$validator = array(
|
|
'NotEmpty' => new NotEmptyValidator(),
|
|
'Email' => new EmailValidator()
|
|
);
|
|
|
|
$this->assertInstanceOf('iValidator', $validator['NotEmpty']);
|
|
$this->assertInstanceOf('iValidator', $validator['Email']);
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$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);
|
|
}
|
|
|
|
public function testAddValidatorObject()
|
|
{
|
|
$validator = new NotEmptyValidator;
|
|
$array = array('NotEmptyValidator'=>new NotEmptyValidator);
|
|
|
|
$this->assertInstanceOf('iValidator', new NotEmptyValidator);
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$return_object = $tmp_form_field->addValidator($validator);
|
|
|
|
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
|
|
$this->assertSame($tmp_form_field, $return_object);
|
|
}
|
|
|
|
public function testAddValidatorString()
|
|
{
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$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);
|
|
}
|
|
|
|
public function testAddValidatorElse()
|
|
{
|
|
$validator = true;
|
|
|
|
$this->setExpectedException('Exception');
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$tmp_form_field->addValidator($validator);
|
|
}
|
|
|
|
|
|
public function testAddFilters()
|
|
{
|
|
$array = array('loginFilter' => new loginFilter(),'passwordFilter' => new passwordFilter());
|
|
|
|
$this->assertInstanceOf('iFilter', $array['loginFilter']);
|
|
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$return_object = $tmp_form_field->addFilters($array);
|
|
|
|
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
|
$this->assertSame($tmp_form_field, $return_object);
|
|
}
|
|
|
|
public function testAddFilterObject()
|
|
{
|
|
$filter = new loginFilter;
|
|
|
|
$array = array('loginFilter' => new loginFilter());
|
|
|
|
$this->assertInstanceOf('iFilter', new loginFilter);
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$return_object = $tmp_form_field->addFilter($filter);
|
|
|
|
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
|
|
$this->assertSame($tmp_form_field, $return_object);
|
|
}
|
|
|
|
public function testAddFilterString()
|
|
{
|
|
$tmp_form_field = new TmpFormField;
|
|
$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);
|
|
}
|
|
|
|
public function testAddFilterElse()
|
|
{
|
|
$this->setExpectedException('Exception');
|
|
|
|
$filter = new NotEmptyValidator;
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$tmp_form_field->addFilter($filter);
|
|
}
|
|
|
|
|
|
public function testGetValueArray()
|
|
{
|
|
$form = new TestFormForFormField();
|
|
|
|
$test_array = array(
|
|
'login'=> 'login',
|
|
'password'=> 'password'
|
|
);
|
|
|
|
$this->assertSame(1, $form->isValid($test_array));
|
|
|
|
$tmp_form_field = new TmpFormField;
|
|
|
|
$this->assertTrue($tmp_form_field->isValid($test_array));
|
|
|
|
$this->assertAttributeInternalType('array', 'value', $tmp_form_field);
|
|
$this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
|
|
}
|
|
|
|
public function testGetValueString()
|
|
{
|
|
$test_array = array(
|
|
'login'=> 'login',
|
|
'password'=> '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());
|
|
}
|
|
|
|
public function testGetMessage()
|
|
{
|
|
$test_form_field = new FormField();
|
|
$this->assertNotNull($test_form_field->getMessage());
|
|
}
|
|
|
|
public function testIsValidMissing1()
|
|
{
|
|
$test_array = array(
|
|
'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));
|
|
}
|
|
|
|
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 testIsValidMissing2()
|
|
{
|
|
$form_field = new FormField;
|
|
|
|
$form_field->setRequired(false);
|
|
|
|
$this->assertTrue($form_field->isValid(''));
|
|
}
|
|
|
|
public function testIsValidMissing3()
|
|
{
|
|
$form_field = new FormField;
|
|
$form_field->setRequired(false);
|
|
$form_field->isValid('');
|
|
|
|
$this->assertFalse($form_field->isRequired());
|
|
}
|
|
|
|
public function testFilterValue()
|
|
{
|
|
$form_field = new FormField;
|
|
$form_field->isValid('login');
|
|
$form_field->addFilter('login');
|
|
|
|
$lf = new loginFilter;
|
|
|
|
$this->assertSame($form_field->getValue(), $lf->filter('login'));
|
|
}
|
|
}
|
|
|
|
|
|
class TmpFormField extends FormField
|
|
{
|
|
public function getProp($name)
|
|
{
|
|
return $this->$name;
|
|
}
|
|
}
|
|
|
|
|
|
class TestFormForFormField extends Form
|
|
{
|
|
public function init()
|
|
{
|
|
///*
|
|
$validator = new NotEmptyValidator();
|
|
$validator->setMessage('Enter login name.');
|
|
$this->addField('login')->addValidator($validator);
|
|
|
|
// User password
|
|
$validator = new NotEmptyValidator();
|
|
$validator->setMessage('Enter your password.');
|
|
$this->addField('password')->addValidator($validator);
|
|
}
|
|
|
|
public function getProp($name)
|
|
{
|
|
return $this->$name;
|
|
}
|
|
}
|
|
|
|
|
|
interface iFilter
|
|
{
|
|
public function isValid($value, $context = null);
|
|
public function getMessage();
|
|
}
|
|
|
|
Class loginFilter implements iFilter
|
|
{
|
|
public function filter($value)
|
|
{
|
|
return $value;
|
|
}
|
|
|
|
public function isValid($value, $context = null)
|
|
{
|
|
|
|
}
|
|
|
|
public function getMessage()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
Class passwordFilter implements iFilter
|
|
{
|
|
public function filter($value)
|
|
{
|
|
return $value;
|
|
}
|
|
|
|
public function isValid($value, $context = null)
|
|
{
|
|
|
|
}
|
|
|
|
public function getMessage()
|
|
{
|
|
|
|
}
|
|
}
|