Browse Source

new object

master
Ilya Sigunov 13 years ago
parent
commit
45c89d3f00
  1. 188
      tests/form/FormFieldTest.php

188
tests/form/FormFieldTest.php

@ -29,77 +29,88 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
public function testSetRequired()
{
$form_field = new FormField;
$this->assertNotNull($form_field->setRequired(1));
$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->assertNotNull($form_field->setIgnored(1));
$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()
{
$form_field = new FormField;
$this->assertNotNull($form_field->isIgnored());
$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()
{
$form_field = new FormField;
$this->assertNotNull($form_field->isRequired());
$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()
{
$fields = array(
$validator = array(
'NotEmpty' => new NotEmptyValidator(),
'Email' => new EmailValidator()
);
$this->assertInstanceOf('iValidator', $fields['NotEmpty']);
$this->assertInstanceOf('iValidator', $fields['Email']);
$this->assertInstanceOf('iValidator', $validator['NotEmpty']);
$this->assertInstanceOf('iValidator', $validator['Email']);
$tmp_form_field = new TmpFormField;
$tmp_form_field->addValidators($fields);
$return_object = $tmp_form_field->addValidators($validator);
$array = $tmp_form_field->getProp('validators');
$array = array('NotEmptyValidator'=>new NotEmptyValidator,'EmailValidator'=>new EmailValidator);
$this->assertArrayHasKey('NotEmptyValidator', $array);
$this->assertArrayHasKey('EmailValidator', $array);
$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;
$tmp_form_field->addValidator($validator);
$array = $tmp_form_field->getProp('validators');
$return_object = $tmp_form_field->addValidator($validator);
$this->assertArrayHasKey('NotEmptyValidator', $array);
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
$this->assertSame($tmp_form_field, $return_object);
}
public function testAddValidatorString()
{
$validator = 'NotEmpty';
$tmp_form_field = new TmpFormField;
$tmp_form_field->addValidator($validator);
$return_object = $tmp_form_field->addValidator('NotEmpty');
$array = $tmp_form_field->getProp('validators');
$this->assertInstanceOf('iValidator', new NotEmptyValidator);
$this->assertArrayHasKey('NotEmptyValidator', $array);
$array = array('NotEmptyValidator'=>new NotEmptyValidator);
$this->assertAttributeEquals($array, 'validators', $tmp_form_field);
$this->assertSame($tmp_form_field, $return_object);
}
public function testAddValidatorElse()
@ -111,56 +122,51 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$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'
);
$array = array('loginFilter' => new loginFilter(),'passwordFilter' => new passwordFilter());
$this->assertInstanceOf('iFilter', $fields['fff2']);
$this->assertInstanceOf('iFilter', $array['loginFilter']);
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
$tmp_form_field = new TmpFormField;
$tmp_form_field->addFilters($fields);
$return_object = $tmp_form_field->addFilters($array);
$array = $tmp_form_field->getProp('filters');
$this->assertArrayHasKey('loginFilter', $array);
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
$this->assertSame($tmp_form_field, $return_object);
}
public function testAddFilterObject()
{
$filter = new loginFilter;
$this->assertInstanceOf('iFilter', $filter);
$array = array('loginFilter' => new loginFilter());
$tmp_form_field = new TmpFormField;
$this->assertInstanceOf('iFilter', new loginFilter);
$tmp_form_field->addFilter($filter);
$tmp_form_field = new TmpFormField;
$array = $tmp_form_field->getProp('filters');
$return_object = $tmp_form_field->addFilter($filter);
$this->assertArrayHasKey('loginFilter', $array);
$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());
$tmp_form_field->addFilter('login');
$this->assertInstanceOf('iFilter', new loginFilter);
$array = $tmp_form_field->getProp('filters');
$return_object = $tmp_form_field->addFilter('login');
$this->assertArrayHasKey('loginFilter', $array);
$this->assertAttributeEquals($array, 'filters', $tmp_form_field);
$this->assertSame($tmp_form_field, $return_object);
}
public function testAddFilterElse()
@ -172,10 +178,6 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$tmp_form_field = new TmpFormField;
$tmp_form_field->addFilter($filter);
$array = $tmp_form_field->getProp('filters');
$this->assertArrayHasKey('NotEmptyValidator', $array);
}
@ -190,10 +192,12 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$this->assertSame(1, $form->isValid($test_array));
$this->assertTrue($this->obj_fields['login']->isValid($test_array));
$tmp_form_field = new TmpFormField;
$this->assertTrue($tmp_form_field->isValid($test_array));
$this->assertAttributeInternalType('array', 'value', $this->obj_fields['login']);
$this->assertArrayHasKey('login', $this->obj_fields['login']->getSourceValue());
$this->assertAttributeInternalType('array', 'value', $tmp_form_field);
$this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
}
public function testGetValueString()
@ -203,15 +207,18 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
'password'=> 'password'
);
$this->assertTrue($this->obj_fields['login']->isValid($test_array['login']));
$this->assertAttributeNotInternalType('array', 'value', $this->obj_fields['login']);
$test_form_field = new FormField();
$this->assertTrue($test_form_field->isValid($test_array['login']));
$this->assertAttributeNotInternalType('array', 'value', $test_form_field);
$this->assertEquals('login',$this->obj_fields['login']->getSourceValue());
$this->assertEquals('login',$test_form_field->getSourceValue());
}
public function testGetMessage()
{
$this->assertNotNull($this->obj_fields['login']->getMessage());
$test_form_field = new FormField();
$this->assertNotNull($test_form_field->getMessage());
}
public function testIsValidMissing1()
@ -221,48 +228,63 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
'password'=> ''
);
$this->setExpectedException('Exception');
$test_form_field = new FormField();
$test_form_field->addValidator('NotEmpty');
$this->setExpectedException('Exception','Define default message for array fields');
$this->assertSame(1, $this->obj_fields['login']->isValid($test_array));
$this->assertTrue($test_form_field->isValid($test_array));
}
public function testIsValidMissing11()
public function testIsValidMissingDefaultMessage()
{
$test_array = array(
'login'=> '',
'password'=> ''
);
$this->obj_fields['login']->__construct('ssssssss');
$test_form_field = new FormField('ssssssss');
$this->assertFalse($this->obj_fields['login']->isValid($test_array));
$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()
{
$this->obj_fields2 = $this->ff->getProp('fields');
$form_field = new FormField;
$form_field->setRequired(false);
$this->assertFalse($this->obj_fields2['login']->isValid(''));
$this->assertTrue($form_field->isValid(''));
}
public function testIsValidMissing3()
{
$this->obj_fields2 = $this->ff->getProp('fields');
$this->obj_fields2['login']->setRequired(false);
$this->obj_fields2['login']->isValid('');
$form_field = new FormField;
$form_field->setRequired(false);
$form_field->isValid('');
$this->assertFalse($this->obj_fields2['login']->isRequired());
$this->assertFalse($form_field->isRequired());
}
public function testFilterValue()
{
$this->obj_fields['login']->isValid('login');
$this->obj_fields['login']->addFilter('login');
$form_field = new FormField;
$form_field->isValid('login');
$form_field->addFilter('login');
$lf = new loginFilter;
$this->assertSame($this->obj_fields['login']->getValue(), $lf->filter('login'));
$this->assertSame($form_field->getValue(), $lf->filter('login'));
}
}
@ -321,3 +343,21 @@ Class loginFilter implements iFilter
}
}
Class passwordFilter implements iFilter
{
public function filter($value)
{
return $value;
}
public function isValid($value, $context = null)
{
}
public function getMessage()
{
}
}
Loading…
Cancel
Save