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.

323 lines
8.5 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->assertNotNull($form_field->setRequired(1));
}
public function testSetIgnored()
{
$form_field = new FormField;
$this->assertNotNull($form_field->setIgnored(1));
}
public function testIsIgnored()
{
$form_field = new FormField;
$this->assertNotNull($form_field->isIgnored());
}
public function testIsRequired()
{
$form_field = new FormField;
$this->assertNotNull($form_field->isRequired());
}
public function testAddValidators()
{
$fields = array(
'NotEmpty' => new NotEmptyValidator(),
'Email' => new EmailValidator()
);
$this->assertInstanceOf('iValidator', $fields['NotEmpty']);
$this->assertInstanceOf('iValidator', $fields['Email']);
$tmp_form_field = new TmpFormField;
$tmp_form_field->addValidators($fields);
$array = $tmp_form_field->getProp('validators');
$this->assertArrayHasKey('NotEmptyValidator', $array);
$this->assertArrayHasKey('EmailValidator', $array);
}
public function testAddValidatorObject()
{
$validator = new NotEmptyValidator;
$this->assertInstanceOf('iValidator', new NotEmptyValidator);
$tmp_form_field = new TmpFormField;
$tmp_form_field->addValidator($validator);
$array = $tmp_form_field->getProp('validators');
$this->assertArrayHasKey('NotEmptyValidator', $array);
}
public function testAddValidatorString()
{
$validator = 'NotEmpty';
$tmp_form_field = new TmpFormField;
$tmp_form_field->addValidator($validator);
$array = $tmp_form_field->getProp('validators');
$this->assertArrayHasKey('NotEmptyValidator', $array);
}
public function testAddValidatorElse()
{
$validator = true;
$this->setExpectedException('Exception');
$tmp_form_field = new TmpFormField;
$tmp_form_field->addValidator($validator);
$array = $tmp_form_field->getProp('validators');
$this->assertArrayHasKey('NotEmptyValidator', $array);
}
public function testAddFilters()
{
$fields = array(
'fff2' => new loginFilter(),
'fff1' => 'login'
);
$this->assertInstanceOf('iFilter', $fields['fff2']);
$tmp_form_field = new TmpFormField;
$tmp_form_field->addFilters($fields);
$array = $tmp_form_field->getProp('filters');
$this->assertArrayHasKey('loginFilter', $array);
}
public function testAddFilterObject()
{
$filter = new loginFilter;
$this->assertInstanceOf('iFilter', $filter);
$tmp_form_field = new TmpFormField;
$tmp_form_field->addFilter($filter);
$array = $tmp_form_field->getProp('filters');
$this->assertArrayHasKey('loginFilter', $array);
}
public function testAddFilterString()
{
$tmp_form_field = new TmpFormField;
$tmp_form_field->addFilter('login');
$array = $tmp_form_field->getProp('filters');
$this->assertArrayHasKey('loginFilter', $array);
}
public function testAddFilterElse()
{
$this->setExpectedException('Exception');
$filter = new NotEmptyValidator;
$tmp_form_field = new TmpFormField;
$tmp_form_field->addFilter($filter);
$array = $tmp_form_field->getProp('filters');
$this->assertArrayHasKey('NotEmptyValidator', $array);
}
public function testGetValueArray()
{
$form = new TestFormForFormField();
$test_array = array(
'login'=> 'login',
'password'=> 'password'
);
$this->assertSame(1, $form->isValid($test_array));
$this->assertTrue($this->obj_fields['login']->isValid($test_array));
$this->assertAttributeInternalType('array', 'value', $this->obj_fields['login']);
$this->assertArrayHasKey('login', $this->obj_fields['login']->getSourceValue());
}
public function testGetValueString()
{
$test_array = array(
'login'=> 'login',
'password'=> 'password'
);
$this->assertTrue($this->obj_fields['login']->isValid($test_array['login']));
$this->assertAttributeNotInternalType('array', 'value', $this->obj_fields['login']);
$this->assertEquals('login',$this->obj_fields['login']->getSourceValue());
}
public function testGetMessage()
{
$this->assertNotNull($this->obj_fields['login']->getMessage());
}
public function testIsValidMissing1()
{
$test_array = array(
'login'=> '',
'password'=> ''
);
$this->setExpectedException('Exception');
$this->assertSame(1, $this->obj_fields['login']->isValid($test_array));
}
public function testIsValidMissing11()
{
$test_array = array(
'login'=> '',
'password'=> ''
);
$this->obj_fields['login']->__construct('ssssssss');
$this->assertFalse($this->obj_fields['login']->isValid($test_array));
}
public function testIsValidMissing2()
{
$this->obj_fields2 = $this->ff->getProp('fields');
$this->assertFalse($this->obj_fields2['login']->isValid(''));
}
public function testIsValidMissing3()
{
$this->obj_fields2 = $this->ff->getProp('fields');
$this->obj_fields2['login']->setRequired(false);
$this->obj_fields2['login']->isValid('');
$this->assertFalse($this->obj_fields2['login']->isRequired());
}
public function testFilterValue()
{
$this->obj_fields['login']->isValid('login');
$this->obj_fields['login']->addFilter('login');
$lf = new loginFilter;
$this->assertSame($this->obj_fields['login']->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()
{
}
}