From 9c4d1e2f95d6c39b97cd78fb7b7da76cbaf148f8 Mon Sep 17 00:00:00 2001 From: Ilya Sigunov Date: Thu, 20 Oct 2011 12:40:13 +0400 Subject: [PATCH] formmm --- tests/form/FormFieldTest.php | 262 +++++++++++++++++++++++++++++++++++ tests/form/FormTest.php | 282 ++++++++++++++++++++++++++++++++++++++ tests/form/FormViewHelperTest.php | 84 ++++++++++++ 3 files changed, 628 insertions(+) create mode 100644 tests/form/FormFieldTest.php create mode 100644 tests/form/FormTest.php create mode 100644 tests/form/FormViewHelperTest.php diff --git a/tests/form/FormFieldTest.php b/tests/form/FormFieldTest.php new file mode 100644 index 0000000..6d04807 --- /dev/null +++ b/tests/form/FormFieldTest.php @@ -0,0 +1,262 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage form + * @since 2010-04-25 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +require_once '/var/www/majesticV/lib/core/validator/iValidator.php'; +require_once '/var/www/majesticV/lib/core/validator/Validator.php'; +require_once '/var/www/majesticV/lib/core/validator/NotEmptyValidator.php'; +require_once '/var/www/majesticV/lib/core/validator/RegexValidator.php'; +require_once '/var/www/majesticV/lib/core/validator/EmailValidator.php'; +require_once '/var/www/majesticV/lib/core/form/Form.php'; +require_once '/var/www/majesticV/lib/core/form/FormField.php'; + + +class FormFieldTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->ff = new NotEmptyForm; + $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 NotEmptyForm(); + + $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 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); + } + + 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() + { + + } +} \ No newline at end of file diff --git a/tests/form/FormTest.php b/tests/form/FormTest.php new file mode 100644 index 0000000..11557dd --- /dev/null +++ b/tests/form/FormTest.php @@ -0,0 +1,282 @@ +setExpectedException('Exception'); + $form->isValid(''); + } + + public function testIsValidCorrectFields() + { + $form = new NotEmptyForm(); + + $test_array = array( + 'login'=> 'login', + 'password'=> 'password' + ); + + $this->assertSame(1, $form->isValid($test_array)); + } + + public function testIsValidMissingField() + { + $form = new NotEmptyForm(); + + $test_array = array( + 'password'=> 'password' + ); + + $this->assertSame(0, $form->isValid($test_array)); + } + + public function testGetValues() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> 'login', + 'password'=>'password' + ); + + $obj_fields = $form->getProp('fields'); + + $this->assertTrue(!$obj_fields['login']->isIgnored()); + $this->assertTrue(!$obj_fields['password']->isIgnored()); + + $this->assertSame(1, $form->isValid($fields)); + + $this->assertEquals($fields, $form->getValues()); + } + + + 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'=> '' + ); + + $this->assertSame(0, $form->isValid($fields)); + + $this->assertEquals('', $form->getValue('login')); + $this->assertEquals('', $form->getValue('password')); + } + + + public function testGetValueMissingFieldLogin() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> '' + ); + + $this->assertSame(0, $form->isValid($fields)); + + $this->assertFalse($form->getValue('loginw')); + + $this->assertEquals('', $form->getValue('passwordw')); + } + + + public function testGetValueMissingFieldPassword() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> '' + ); + + $this->assertSame(0, $form->isValid($fields)); + + $this->assertFalse($form->getValue('loginw')); + + $this->assertEquals('', $form->getValue('password')); + } + + public function testGetMessageTypeCorrect() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> 'login', + 'password'=>'password' + ); + + $this->assertEquals(1, $form->isValid($fields)); + + $this->assertEquals('success', Form::SUCCESS); + + $this->assertEquals($form->getMessageType(), Form::SUCCESS); + } + + public function testGetMessageTypeMissing() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> '', + 'password'=>'' + ); + + $this->assertSame(0, $form->isValid($fields)); + + $this->assertSame(0, $form->getProp('valid')); + + $this->assertEquals('error', Form::ERROR); + + $this->assertEquals($form->getMessageType(), Form::ERROR); + } + + + public function testGetMessageCorrect() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> 'login', + 'password'=>'password' + ); + + $this->assertSame(1, $form->isValid($fields)); + + $messages = $form->getProp('messages'); + + $this->assertEquals($messages[Form::SUCCESS], $form->getMessage()); + } + + + public function testGetMessageMissing() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> '' + ); + + $this->assertSame(0, $form->isValid($fields)); + + $messages = $form->getProp('messages');//attributeEq ? + + $this->assertEquals($messages[Form::ERROR], $form->getMessage()); + } + + + public function testSetSuccessMessage() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> 'login', + 'password'=>'password' + ); + + $this->assertSame(1, $form->isValid($fields)); + + $message = 'Form data valid'; + + $form->setSuccessMessage($message); + + $this->assertNotNull($form->getMessage()); + } + + public function testSetErrorMessage() + { + $form = new NotEmptyForm(); + + $fields = array( + 'login'=> '' + ); + + $this->assertSame(0, $form->isValid($fields)); + + $message = 'Form data invalid'; + + $form->setErrorMessage($message); + + $this->assertNotNull($form->getMessage()); + } +} + +require_once '/var/www/majesticV/lib/core/validator/iValidator.php'; +require_once '/var/www/majesticV/lib/core/validator/Validator.php'; +require_once '/var/www/majesticV/lib/core/validator/NotEmptyValidator.php'; + +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); + } + + public function getProp($name) + { + return $this->$name; + } +} + +require_once '/var/www/majesticV/lib/core/validator/RegexValidator.php'; +require_once '/var/www/majesticV/lib/core/validator/EmailValidator.php'; + +class DiffForm extends Form +{ + public function init() + { + ///* + $validator = new EmailValidator(); + $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; + } + +} \ No newline at end of file diff --git a/tests/form/FormViewHelperTest.php b/tests/form/FormViewHelperTest.php new file mode 100644 index 0000000..3792377 --- /dev/null +++ b/tests/form/FormViewHelperTest.php @@ -0,0 +1,84 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Form + * @since 2010-04-25 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +require_once '/var/www/majesticV/lib/core/form/Form.php'; +require_once '/var/www/majesticV/lib/core/form/FormField.php'; + +require_once '/var/www/majesticV/lib/core/view/iView.php'; +require_once '/var/www/majesticV/lib/core/view/PHPView.php'; +require_once '/var/www/majesticV/lib/core/view/helpers/ViewHelper.php'; +require_once '/var/www/majesticV/lib/core/form/FormViewHelper.php'; +require_once '/var/www/majesticV/lib/core/session/Session.php'; + +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' + ) + ); + } + + public function testForm2() + { + $this->assertNull($this->assertAttributeEquals(null,'data',$this->form)); + + $this->setExpectedException('Exception'); + + $this->form->form(null); + } + + public function testForm1() + { + $this->assertNull($this->assertAttributeEquals(null,'data',$this->form)); //аналог getProp + + $this->form->form('path'); + } + + public function testValueIsset() + { + $this->form->form('path'); + + $this->assertEquals('path', $this->form->value('path')); + } + + public function testValueNotIsset() + { + $this->form->form('path'); + + $this->assertEquals('param2', $this->form->value('param1','param2')); + } + + public function testMessageIsset() + { + $this->form->form('path'); + + $this->assertNotEmpty($this->form->message('path')); + } + + public function testMessageNotIsset() + { + $this->form->form('path'); + + $this->assertEmpty($this->form->message('')); + } +} \ No newline at end of file