diff --git a/tests/form/FormFieldTest.php b/tests/form/FormFieldTest.php
index 925658e..29d731d 100644
--- a/tests/form/FormFieldTest.php
+++ b/tests/form/FormFieldTest.php
@@ -1,7 +1,7 @@
- * @link http://netmonsters.ru
+ * @link http://netmonsters.ru
* @package Majestic
* @subpackage form
* @since 2010-04-25
@@ -12,36 +12,34 @@
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/FormField.php';
class FormFieldTest extends PHPUnit_Framework_TestCase
{
- public function setUp()
- {
- /**/
- }
-
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';
@@ -50,7 +48,7 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$form_field->setIgnored(true);
$this->assertTrue($form_field->isIgnored());
}
-
+
public function testIsRequired()
{
$class_name = 'FormField';
@@ -59,182 +57,246 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$form_field->setRequired(false);
$this->assertFalse($form_field->isRequired());
}
-
+
public function testAddValidators()
{
- $validator = array(
- 'NotEmpty' => new NotEmptyValidator(),
- 'Email' => new EmailValidator()
+ $validators = array(
+ 'NotEmpty' => new NotEmptyValidator(),
+ 'Email' => new EmailValidator()
);
- $this->assertInstanceOf('iValidator', $validator['NotEmpty']);
- $this->assertInstanceOf('iValidator', $validator['Email']);
- $tmp_form_field = new FormField();
- $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);
+
+ $this->assertInstanceOf('iValidator', $validators['NotEmpty']);
+ $this->assertInstanceOf('iValidator', $validators['Email']);
+ $form_field = new FormField();
+ $return_object = $form_field->addValidators($validators);
+ $array = array('NotEmptyValidator' => new NotEmptyValidator(), 'EmailValidator' => new EmailValidator());
+ $this->assertAttributeEquals($array, 'validators', $form_field);
+ $this->assertSame($form_field, $return_object);
}
-
+
public function testAddValidatorObject()
{
$validator = new NotEmptyValidator();
- $array = array('NotEmptyValidator'=>new NotEmptyValidator());
- $this->assertInstanceOf('iValidator', new NotEmptyValidator());
- $tmp_form_field = new FormField();
- $return_object = $tmp_form_field->addValidator($validator);
- $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
- $this->assertSame($tmp_form_field, $return_object);
+ $array = array('NotEmptyValidator' => new NotEmptyValidator());
+ $form_field = new FormField();
+
+ $return_object = $form_field->addValidator($validator);
+ $this->assertAttributeEquals($array, 'validators', $form_field);
+ $this->assertSame($form_field, $return_object);
}
-
+
public function testAddValidatorString()
{
- $tmp_form_field = new FormField();
- $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);
+ $form_field = new FormField();
+ $return_object = $form_field->addValidator('NotEmpty');
+
+ $array = array('NotEmptyValidator' => new NotEmptyValidator());
+ $this->assertAttributeEquals($array, 'validators', $form_field);
+ $this->assertSame($form_field, $return_object);
}
-
+
public function testAddValidatorElse()
{
$validator = true;
- $this->setExpectedException('Exception');
$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);
}
-
+
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['passwordFilter']);
- $tmp_form_field = new FormField();
- $return_object = $tmp_form_field->addFilters($array);
- $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
- $this->assertSame($tmp_form_field, $return_object);
+
+ $form_field = new FormField();
+ $return_object = $form_field->addFilters($array);
+ $this->assertAttributeEquals($array, 'filters', $form_field);
+ $this->assertSame($form_field, $return_object);
}
-
+
public function testAddFilterObject()
{
$filter = new loginFilter();
$array = array('loginFilter' => new loginFilter());
$this->assertInstanceOf('iFilter', new loginFilter());
- $tmp_form_field = new FormField();
- $return_object = $tmp_form_field->addFilter($filter);
- $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
- $this->assertSame($tmp_form_field, $return_object);
+ $form_field = new FormField();
+ $return_object = $form_field->addFilter($filter);
+
+ $this->assertAttributeEquals($array, 'filters', $form_field);
+ $this->assertSame($form_field, $return_object);
}
-
+
public function testAddFilterString()
{
- $tmp_form_field = new FormField();
+ $form_field = new FormField();
$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);
+ $return_object = $form_field->addFilter('login');
+
+ $this->assertAttributeEquals($array, 'filters', $form_field);
+ $this->assertSame($form_field, $return_object);
}
-
+
public function testAddFilterElse()
{
- $this->setExpectedException('Exception');
$filter = new NotEmptyValidator();
- $tmp_form_field = new FormField();
- $tmp_form_field->addFilter($filter);
+ $form_field = new FormField();
+ // @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()
{
- $form = new FormField();
$test_array = array(
- 'login'=> 'login',
- 'password'=> 'password'
+ 'login' => 'login',
+ 'password' => 'password'
);
- $this->assertTrue($form->isValid($test_array));
- $tmp_form_field = new FormField();
- $this->assertTrue($tmp_form_field->isValid($test_array));
- $this->assertAttributeInternalType('array', 'value', $tmp_form_field);
- $this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
+ $form_field = new FormField();
+ $form_field->addFilter('Login');
+ $this->assertTrue($form_field->isValid($test_array));
+ $this->assertAttributeInternalType('array', 'value', $form_field);
+ $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_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());
+ $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 testGetMessage()
+
+ public function testGetValueStringIncorrect()
{
- $test_form_field = new FormField();
- $this->assertNotNull($test_form_field->getMessage());
+ $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 testIsValidMissingException()
+
+ 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(
- 'login'=> '',
- 'password'=> ''
+ '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));
+ $form_field = new FormField();
+ $form_field->addValidator('NotEmpty');
+ $this->setExpectedException('Exception', 'Define default message for array fields');
+ $form_field->isValid($test_array);
}
-
- public function testIsValidMissingDefaultMessage()
+
+ public function testIsValidArrayMissingCustomMessage()
{
+ $message = 'Test message';
$test_array = array(
- 'login'=> '',
- 'password'=> ''
+ '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));
+ $form_field = new FormField($message);
+ $return_object = $form_field->addValidator('NotEmpty');
+ $array = array('NotEmptyValidator' => new NotEmptyValidator());
+ $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 testIsValidMissingRequired()
+
+ public function testIsValidMissingNotRequired()
{
$form_field = new FormField();
$form_field->setRequired(false);
$this->assertTrue($form_field->isValid(''));
}
-
- public function testIsValidMissingValid()
+
+ public function testIsValidArray()
{
$test_array = array(
- 'login'=> 'login',
- 'password'=> 'password'
+ 'login' => 'login',
+ 'password' => 'password'
);
$validator = new NotEmptyValidator();
$form_field = new FormField();
$return_object = $form_field->addValidator($validator);
$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()
{
+ $input = ' login ';
$form_field = new FormField();
- $form_field->isValid('login');
+ $form_field->isValid($input);
$form_field->addFilter('login');
$lf = new loginFilter();
- $this->assertSame($form_field->getValue(), $lf->filter('login'));
+ $this->assertSame($form_field->getValue(), $lf->filter($input));
}
}
interface iFilter
{
public function isValid($value, $context = null);
+
+ public function filter($value);
+
public function getMessage();
}
@@ -242,17 +304,19 @@ class loginFilter implements iFilter
{
public function filter($value)
{
- return $value;
+ $value = trim($value);
+ if ($value === 'login') {
+ return $value;
+ }
+ return '';
}
-
+
public function isValid($value, $context = null)
{
-
}
-
+
public function getMessage()
{
-
}
}
@@ -262,14 +326,12 @@ class passwordFilter implements iFilter
{
return $value;
}
-
+
public function isValid($value, $context = null)
{
-
}
-
+
public function getMessage()
{
-
}
}
\ No newline at end of file
diff --git a/tests/form/FormTest.php b/tests/form/FormTest.php
index f18f987..4650e2f 100644
--- a/tests/form/FormTest.php
+++ b/tests/form/FormTest.php
@@ -1,8 +1,13 @@
+ * @link http://netmonsters.ru
+ * @package Majestic
+ * @subpackage UnitTests
+ * @since 2011-10-07
+ *
+ * Unit tests for Form class
*/
require_once dirname(__FILE__) . '/../../form/Form.php';
@@ -17,197 +22,213 @@ class FormTest extends PHPUnit_Framework_TestCase
{
public function testAddFieldEmptyMessage()
{
+ $form_field = new FormField();
$stub = $this->getMockForAbstractClass('Form');
$method = new ReflectionMethod('Form', 'addField');
$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()
{
- $message = 'message';
+ $message = 'Test message';
$form_field = new FormField($message);
$stub = $this->getMockForAbstractClass('Form');
$method = new ReflectionMethod('Form', 'addField');
$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->assertEquals($form_field, $return_object);
- $this->assertAttributeEquals('message', 'default_message', $return_object);
-
+ $this->assertAttributeEquals($message, 'default_message', $return_object);
}
-
+
public function testIsValidWithNotArray()
{
$form = new NotEmptyForm();
- $this->setExpectedException('Exception');
+ // @TODO Fix exception type
+ $this->setExpectedException('Exception', 'Form::Form::isValid expects an array');
$form->isValid('');
}
-
+
public function testIsValidCorrectFields()
{
$form = new NotEmptyForm();
$test_array = array(
- 'login'=> 'login',
- 'password'=> 'password'
+ 'login' => 'login',
+ 'password' => 'password'
);
+
$this->assertSame(1, $form->isValid($test_array));
}
-
+
public function testIsValidMissingField()
{
$form = new NotEmptyForm();
$test_array = array(
- 'password'=> 'password'
+ 'password' => 'password'
);
+
$this->assertSame(0, $form->isValid($test_array));
$data['messages'] = $form->getMessages();
$data['values'] = $form->getSourceValues();
$this->assertSame($_SESSION['NotEmptyForm'], $data);
}
-
- public function testFillHelperData()
+
+ public function testGetValues()
{
$form = new NotEmptyForm();
$test_array = array(
- 'password'=> 'password'
+ 'login' => 'login',
+ '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);
+
+ $this->assertSame(1, $form->isValid($test_array));
+ $this->assertSame($test_array, $form->getValues());
}
-
- public function testGetValues()
+
+
+ public function testGetValuesWithFieldIsIgnored()
{
- $form = new NotEmptyForm();
- $fields = array(
- 'login'=> 'login',
- 'password'=>'password'
+ $form = new WithIgnoredFieldForm();
+ $test_array = array(
+ 'login' => 'login',
+ 'password' => 'password',
+ 'remember' => true
);
- $this->assertSame(1, $form->isValid($fields));
- $this->assertEquals($fields, $form->getValues());
+
+ $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()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> 'login',
- 'password'=>'password'
- );
- $this->assertSame(1, $form->isValid($fields));
- $this->assertEquals($fields['login'], $form->getValue('login'));
- $this->assertEquals($fields['password'], $form->getValue('password'));
- }
-
- public function testGetValueMissing()
- {
- $form = new NotEmptyForm();
- $fields = array(
- 'login'=> ''
+ $test_array = array(
+ 'login' => 'login',
+ 'password' => 'password'
);
- $this->assertSame(0, $form->isValid($fields));
- $this->assertEquals('', $form->getValue('login'));
- $this->assertEquals('', $form->getValue('password'));
+
+ $this->assertSame(1, $form->isValid($test_array));
+ $this->assertSame($test_array['login'], $form->getValue('login'));
+ $this->assertSame($test_array['password'], $form->getValue('password'));
}
-
- public function testGetValueMissingFieldLogin()
+
+
+ public function testGetValueEmpty()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> ''
+ $test_array = array(
+ 'login' => 'login'
);
- $this->assertSame(0, $form->isValid($fields));
- $this->assertFalse($form->getValue('loginw'));
- $this->assertEquals('', $form->getValue('passwordw'));
+
+ $this->assertSame(0, $form->isValid($test_array));
+ $this->assertSame('login', $form->getValue('login'));
+ $this->assertNull($form->getValue('password'));
}
-
- public function testGetValueMissingFieldPassword()
+
+
+ public function testGetValueMissing()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> ''
+ $test_array = array(
+ 'login' => 'login',
+ 'password' => 'password'
);
- $this->assertSame(0, $form->isValid($fields));
- $this->assertFalse($form->getValue('loginw'));
- $this->assertEquals('', $form->getValue('password'));
+
+ $this->assertSame(1, $form->isValid($test_array));
+ $this->assertSame('login', $form->getValue('login'));
+ $this->assertSame('password', $form->getValue('password'));
+ $this->assertFalse($form->getValue('missing'));
}
-
+
+
public function testGetMessageTypeCorrect()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> 'login',
- 'password'=>'password'
+ $test_array = array(
+ 'login' => 'login',
+ 'password' => 'password'
);
- $this->assertEquals(1, $form->isValid($fields));
- $this->assertEquals('success', Form::SUCCESS);
- $this->assertEquals($form->getMessageType(), Form::SUCCESS);
+
+ $this->assertSame(1, $form->isValid($test_array));
+ $this->assertSame('success', Form::SUCCESS);
+ $this->assertSame($form->getMessageType(), Form::SUCCESS);
}
-
+
public function testGetMessageTypeMissing()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> '',
- 'password'=>''
+ $test_array = array(
+ 'login' => '',
+ 'password' => ''
);
- $this->assertSame(0, $form->isValid($fields));
- $this->assertEquals('error', Form::ERROR);
- $this->assertEquals($form->getMessageType(), Form::ERROR);
+
+ $this->assertSame(0, $form->isValid($test_array));
+ $this->assertSame('error', Form::ERROR);
+ $this->assertSame($form->getMessageType(), Form::ERROR);
}
-
+
public function testGetMessageCorrect()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> 'login',
- 'password'=>'password'
+ $message = 'Test message';
+ $test_array = array(
+ 'login' => 'login',
+ 'password' => 'password'
);
- $this->assertSame(1, $form->isValid($fields));
- $form->setSuccessMessage('message');
- $this->assertEquals('message', $form->getMessage());
+
+ $this->assertSame(1, $form->isValid($test_array));
+ $form->setSuccessMessage($message);
+ $this->assertSame($message, $form->getMessage());
}
-
+
public function testGetMessageMissing()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> ''
+ $message = 'Test message';
+ $test_array = array(
+ 'login' => ''
);
- $this->assertSame(0, $form->isValid($fields));
- $form->setErrorMessage('message');
- $this->assertEquals('message', $form->getMessage());
+
+ $this->assertSame(0, $form->isValid($test_array));
+ $form->setErrorMessage($message);
+ $this->assertSame($message, $form->getMessage());
}
-
+
public function testSetSuccessMessage()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> 'login',
- 'password'=>'password'
+ $test_array = array(
+ 'login' => 'login',
+ 'password' => 'password'
);
- $this->assertSame(1, $form->isValid($fields));
+
+ $this->assertSame(1, $form->isValid($test_array));
$message = 'Form data valid';
$form->setSuccessMessage($message);
- $this->assertNotNull($form->getMessage());
+ $this->assertSame($message, $form->getMessage());
}
-
+
public function testSetErrorMessage()
{
$form = new NotEmptyForm();
- $fields = array(
- 'login'=> ''
+ $test_array = array(
+ 'login' => ''
);
- $this->assertSame(0, $form->isValid($fields));
+
+ $this->assertSame(0, $form->isValid($test_array));
$message = 'Form data invalid';
$form->setErrorMessage($message);
- $this->assertNotNull($form->getMessage());
+ $this->assertSame($message, $form->getMessage());
}
}
@@ -216,14 +237,30 @@ class NotEmptyForm 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);
}
-}
\ No newline at end of file
+}
+
+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);
+ }
+}
diff --git a/tests/form/FormViewHelperTest.php b/tests/form/FormViewHelperTest.php
index 980f569..f8b15b1 100644
--- a/tests/form/FormViewHelperTest.php
+++ b/tests/form/FormViewHelperTest.php
@@ -1,12 +1,12 @@
* @link http://netmonsters.ru
* @package Majestic
- * @subpackage Form
- * @since 2010-04-25
- * @version SVN: $Id$
- * @filesource $URL$
+ * @subpackage UnitTests
+ * @since 2011-10-07
+ *
+ * Unit tests for Form class
*/
require_once dirname(__FILE__) . '/../../view/iView.php';
@@ -19,53 +19,79 @@ class FormViewHelperTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
- Session::start();
- $phpview = new PHPView(array('path' => 'document_root'));
- $this->form = new FormViewHelper($phpview);
- $_SESSION['path'] = array(
- 'values'=>array(
- 'path'=>'path'
- ),
- 'messages'=>array(
- 'path'=>'path'
- )
- );
+ $this->view = new PHPView(array('path' => '/path/to/templates'));
+ $this->formname = 'formname';
+ $this->test_array = array('values' => array('key1' => 'val"ue1'), 'messages' => array('field1' => 'Can\'t serialize "value"'));
+ Session::set($this->formname, $this->test_array);
}
-
- public function testForm2()
+
+ public function testFormWithNotViewInstance()
{
- $this->assertNull($this->assertAttributeEquals(null, 'data', $this->form));
- $this->setExpectedException('Exception');
- $this->form->form(null);
+ // @TODO check, that iView used in construct
+ $form = new FormViewHelper('something');
+ $this->assertInstanceOf('FormViewHelper', $form);
}
-
- public function testForm1()
+
+ public function testFormUnsetFormName()
+ {
+ $helper = new FormViewHelper($this->view);
+ $this->setExpectedException('Exception', 'Form name required for helper init');
+ // @TODO Refactor for form name is required param?
+ $helper->form();
+ }
+
+ public function testFormEmptyFormName()
{
- $this->assertNull($this->assertAttributeEquals(null, 'data', $this->form)); //аналог getProp
- $this->form->form('path');
+ $helper = new FormViewHelper($this->view);
+ $this->setExpectedException('Exception', 'Form name required for helper init');
+ $helper->form('');
+ }
+
+ public function testFillData()
+ {
+ $helper = new FormViewHelper($this->view);
+
+ $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 testValueIsset()
+
+ public function testValueSet()
{
- $this->form->form('path');
- $this->assertEquals(htmlentities('path', ENT_QUOTES, 'UTF-8'), $this->form->value('path'));
+ $helper = new FormViewHelper($this->view);
+ $helper->form($this->formname);
+
+ $value = $helper->value('key1');
+ $this->assertSame($this->view->escape('val"ue1'), $value);
}
-
- public function testValueNotIsset()
+
+ public function testValueDefault()
{
- $this->form->form('path');
- $this->assertEquals(htmlentities('param2', ENT_QUOTES, 'UTF-8'), $this->form->value('param1','param2'));
+ $helper = new FormViewHelper($this->view);
+ $helper->form($this->formname);
+
+ $value = $helper->value('key_not_exist', 'default"');
+ $this->assertSame($this->view->escape('default"'), $value);
}
-
- public function testMessageIsset()
+
+ public function testMessageSet()
{
- $this->form->form('path');
- $this->assertEquals('' . htmlentities('path', ENT_QUOTES, 'UTF-8') . '', $this->form->message('path'));
+ $helper = new FormViewHelper($this->view);
+ $helper->form($this->formname);
+
+ $value = $helper->message('field1');
+ $this->assertSame('' . $this->view->escape('Can\'t serialize "value"') . '', $value);
}
-
- public function testMessageNotIsset()
+
+ public function testMessageNotSet()
{
- $this->form->form('path');
- $this->assertEmpty($this->form->message(''));
+
+ $helper = new FormViewHelper($this->view);
+ $helper->form($this->formname);
+
+ $value = $helper->message('key_not_exist', 'default"');
+ $this->assertSame('', $value);
}
}
\ No newline at end of file