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.

262 lines
7.1 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 NotEmptyFormField;
$this->obj_fields = $this->ff->getProp('fields');
}
public function testSetRequired()
{
$this->assertNotNull($this->obj_fields['login']->setRequired(1));
}
public function testSetIgnored()
{
$this->assertNotNull($this->obj_fields['login']->setIgnored(1));
}
public function testIsIgnored()
{
$this->assertNotNull($this->obj_fields['login']->isIgnored());
}
public function testIsRequired()
{
$this->assertNotNull($this->obj_fields['login']->isRequired());
}
public function testAddValidators()
{
$fields = array(
'NotEmpty' => new NotEmptyValidator(),
'Email' => new EmailValidator()
);
$this->assertInstanceOf('iValidator', $fields['NotEmpty']);
$this->assertInstanceOf('iValidator', $fields['Email']);
$this->obj_fields['login']->addValidators($fields);
}
public function testAddValidatorObject()
{
$validator = new NotEmptyValidator;
$this->assertInstanceOf('iValidator', new NotEmptyValidator);
$this->obj_fields['login']->addValidator($validator);
}
public function testAddValidatorString()
{
$validator = 'NotEmpty';
$this->obj_fields['login']->addValidator($validator);
$this->assertAttributeContainsOnly($validator . 'Validator', 'validators', $this->obj_fields['login']);
}
public function testAddValidatorElse()
{
$validator = true;
$this->setExpectedException('Exception');
$this->obj_fields['login']->addValidator($validator);
}
public function testAddFilters()
{
$fields = array(
'fff2' => new loginFilter(),
'fff1' => 'login'
);
$this->assertInstanceOf('iFilter', $fields['fff2']);
$this->obj_fields['login']->addFilters($fields);
}
public function testAddFilterObject()
{
$filter = new loginFilter;
$this->assertInstanceOf('iFilter', $filter);
$this->obj_fields['login']->addFilter($filter);
}
public function testAddFilterString()
{
$this->obj_fields['login']->addFilter('login');
$this->assertAttributeContainsOnly('loginFilter', 'filters', $this->obj_fields['login']);
}
public function testAddFilterElse()
{
$this->setExpectedException('Exception');
$filter = new NotEmptyValidator;
$this->obj_fields['login']->addFilter($filter);
}
public function testGetValueArray()
{
$form = new NotEmptyFormField();
$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 NotEmptyFormField 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()
{
}
}