Add namespace.

This commit is contained in:
2014-06-02 18:58:49 +04:00
parent aec1a60985
commit 1ba341b064
159 changed files with 265 additions and 264 deletions

View File

@ -0,0 +1,336 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage form
* @since 2010-04-25
*/
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';
require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
class FormFieldTest extends PHPUnit_Framework_TestCase
{
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';
$form_field = new $class_name();
$this->assertFalse($form_field->isIgnored());
$form_field->setIgnored(true);
$this->assertTrue($form_field->isIgnored());
}
public function testIsRequired()
{
$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()
{
$validators = array(
'NotEmpty' => new NotEmptyValidator(),
'Email' => new EmailValidator()
);
$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());
$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()
{
$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;
$tmp_form_field = new FormField();
// @TODO Fix exception type
$this->setExpectedException('InitializationException', '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());
$this->assertInstanceOf('iFilter', $array['loginFilter']);
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
$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());
$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()
{
$form_field = new FormField();
$array = array('loginFilter' => new loginFilter());
$this->assertInstanceOf('iFilter', new loginFilter());
$return_object = $form_field->addFilter('login');
$this->assertAttributeEquals($array, 'filters', $form_field);
$this->assertSame($form_field, $return_object);
}
public function testAddFilterElse()
{
$filter = new NotEmptyValidator();
$form_field = new FormField();
// @TODO Fix exception type
$this->setExpectedException('InitializationException', 'Invalid filter provided to addFilter; must be string or iFilter'); // Text of exception
$form_field->addFilter($filter);
}
public function testGetValueArray()
{
$test_array = array(
'login' => 'login',
'password' => 'password'
);
$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 testGetValueStringCorrect()
{
$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->assertSame('login', $form_field->getValue());
}
public function testGetValueStringIncorrect()
{
$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->assertSame('', $form_field->getValue());
}
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' => ''
);
$form_field = new FormField();
$form_field->addValidator('NotEmpty');
$this->setExpectedException('InitializationException', 'Define default message for array fields');
$form_field->isValid($test_array);
}
public function testIsValidArrayMissingCustomMessage()
{
$message = 'Test message';
$test_array = array(
'login' => '',
'password' => ''
);
$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 testIsValidMissingNotRequired()
{
$form_field = new FormField();
$form_field->setRequired(false);
$this->assertTrue($form_field->isValid(''));
}
public function testIsValidArray()
{
$test_array = array(
'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($input);
$form_field->addFilter('login');
$lf = new loginFilter();
$this->assertSame($form_field->getValue(), $lf->filter($input));
}
}
interface iFilter
{
public function isValid($value, $context = null);
public function filter($value);
public function getMessage();
}
class loginFilter implements iFilter
{
public function filter($value)
{
$value = trim($value);
if ($value === 'login') {
return $value;
}
return '';
}
public function isValid($value, $context = null)
{
}
public function getMessage()
{
}
}
class passwordFilter implements iFilter
{
public function filter($value)
{
return $value;
}
public function isValid($value, $context = null)
{
}
public function getMessage()
{
}
}

266
Tests/form/FormTest.php Normal file
View File

@ -0,0 +1,266 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-07
*
* Unit tests for Form class
*/
require_once dirname(__FILE__) . '/../../form/Form.php';
require_once dirname(__FILE__) . '/../../form/FormField.php';
require_once dirname(__FILE__) . '/../../validator/iValidator.php';
require_once dirname(__FILE__) . '/../../validator/Validator.php';
require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
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);
$return_object = $method->invokeArgs($stub, array('login'));
$this->assertInstanceOf('FormField', $return_object);
$this->assertEquals($form_field, $return_object);
}
public function testAddFieldWithMessage()
{
$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));
$this->assertInstanceOf('FormField', $return_object);
$this->assertEquals($form_field, $return_object);
$this->assertAttributeEquals($message, 'default_message', $return_object);
}
public function testIsValidWithNotArray()
{
$form = new NotEmptyForm();
// @TODO Fix exception type
$this->setExpectedException('InitializationException', 'Form::Form::isValid expects an array');
$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));
$data['messages'] = $form->getMessages();
$data['values'] = $form->getSourceValues();
$this->assertSame($_SESSION['NotEmptyForm'], $data);
}
public function testGetValues()
{
$form = new NotEmptyForm();
$test_array = array(
'login' => 'login',
'password' => 'password'
);
$this->assertSame(1, $form->isValid($test_array));
$this->assertSame($test_array, $form->getValues());
}
public function testGetValuesWithFieldIsIgnored()
{
$form = new WithIgnoredFieldForm();
$test_array = array(
'login' => 'login',
'password' => 'password',
'remember' => true
);
$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();
$test_array = array(
'login' => 'login',
'password' => '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 testGetValueEmpty()
{
$form = new NotEmptyForm();
$test_array = array(
'login' => 'login'
);
$this->assertSame(0, $form->isValid($test_array));
$this->assertSame('login', $form->getValue('login'));
$this->assertNull($form->getValue('password'));
}
public function testGetValueMissing()
{
$form = new NotEmptyForm();
$test_array = array(
'login' => 'login',
'password' => '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();
$test_array = array(
'login' => 'login',
'password' => 'password'
);
$this->assertSame(1, $form->isValid($test_array));
$this->assertSame('success', Form::SUCCESS);
$this->assertSame($form->getMessageType(), Form::SUCCESS);
}
public function testGetMessageTypeMissing()
{
$form = new NotEmptyForm();
$test_array = array(
'login' => '',
'password' => ''
);
$this->assertSame(0, $form->isValid($test_array));
$this->assertSame('error', Form::ERROR);
$this->assertSame($form->getMessageType(), Form::ERROR);
}
public function testGetMessageCorrect()
{
$form = new NotEmptyForm();
$message = 'Test message';
$test_array = array(
'login' => 'login',
'password' => 'password'
);
$this->assertSame(1, $form->isValid($test_array));
$form->setSuccessMessage($message);
$this->assertSame($message, $form->getMessage());
}
public function testGetMessageMissing()
{
$form = new NotEmptyForm();
$message = 'Test message';
$test_array = array(
'login' => ''
);
$this->assertSame(0, $form->isValid($test_array));
$form->setErrorMessage($message);
$this->assertSame($message, $form->getMessage());
}
public function testSetSuccessMessage()
{
$form = new NotEmptyForm();
$test_array = array(
'login' => 'login',
'password' => 'password'
);
$this->assertSame(1, $form->isValid($test_array));
$message = 'Form data valid';
$form->setSuccessMessage($message);
$this->assertSame($message, $form->getMessage());
}
public function testSetErrorMessage()
{
$form = new NotEmptyForm();
$test_array = array(
'login' => ''
);
$this->assertSame(0, $form->isValid($test_array));
$message = 'Form data invalid';
$form->setErrorMessage($message);
$this->assertSame($message, $form->getMessage());
}
}
class NotEmptyForm 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);
}
}
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);
}
}

View File

@ -0,0 +1,95 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-07
*
* Unit tests for Form class
*/
require_once dirname(__FILE__) . '/../../view/iView.php';
require_once dirname(__FILE__) . '/../../view/PHPView.php';
require_once dirname(__FILE__) . '/../../view/helpers/ViewHelper.php';
require_once dirname(__FILE__) . '/../../form/FormViewHelper.php';
require_once dirname(__FILE__) . '/../../session/Session.php';
require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
class FormViewHelperTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$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 testFormWithNotViewInstance()
{
// @TODO check, that iView used in construct
$form = new FormViewHelper('something');
$this->assertInstanceOf('FormViewHelper', $form);
}
public function testFormUnsetFormName()
{
$helper = new FormViewHelper($this->view);
$this->setExpectedException('InitializationException', 'Form name required for helper init');
// @TODO Refactor for form name is required param?
$helper->form();
}
public function testFormEmptyFormName()
{
$helper = new FormViewHelper($this->view);
$this->setExpectedException('InitializationException', '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 testValueSet()
{
$helper = new FormViewHelper($this->view);
$helper->form($this->formname);
$value = $helper->value('key1');
$this->assertSame($this->view->escape('val"ue1'), $value);
}
public function testValueDefault()
{
$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 testMessageSet()
{
$helper = new FormViewHelper($this->view);
$helper->form($this->formname);
$value = $helper->message('field1');
$this->assertSame('<span class="error">' . $this->view->escape('Can\'t serialize "value"') . '</span>', $value);
}
public function testMessageNotSet()
{
$helper = new FormViewHelper($this->view);
$helper->form($this->formname);
$value = $helper->message('key_not_exist', 'default"');
$this->assertSame('', $value);
}
}