Browse Source

format

master
Ilya Sigunov 13 years ago
parent
commit
acb3b8bf27
  1. 131
      tests/form/FormFieldTest.php
  2. 131
      tests/form/FormTest.php
  3. 13
      tests/form/FormViewHelperTest.php

131
tests/form/FormFieldTest.php

@ -12,9 +12,7 @@
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';
@ -22,8 +20,7 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->ff = new TestFormForFormField;
$this->obj_fields = $this->ff->getProp('fields');
/**/
}
public function testSetRequired()
@ -70,16 +67,11 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
'NotEmpty' => new NotEmptyValidator(),
'Email' => new EmailValidator()
);
$this->assertInstanceOf('iValidator', $validator['NotEmpty']);
$this->assertInstanceOf('iValidator', $validator['Email']);
$tmp_form_field = new TmpFormField;
$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);
}
@ -88,27 +80,19 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
{
$validator = new NotEmptyValidator;
$array = array('NotEmptyValidator'=>new NotEmptyValidator);
$this->assertInstanceOf('iValidator', new NotEmptyValidator);
$tmp_form_field = new TmpFormField;
$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);
}
public function testAddValidatorString()
{
$tmp_form_field = new TmpFormField;
$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);
}
@ -116,11 +100,8 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
public function testAddValidatorElse()
{
$validator = true;
$this->setExpectedException('Exception');
$tmp_form_field = new TmpFormField;
$tmp_form_field = new FormField;
$tmp_form_field->addValidator($validator);
}
@ -128,43 +109,31 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
public function testAddFilters()
{
$array = array('loginFilter' => new loginFilter(),'passwordFilter' => new passwordFilter());
$this->assertInstanceOf('iFilter', $array['loginFilter']);
$this->assertInstanceOf('iFilter', $array['passwordFilter']);
$tmp_form_field = new TmpFormField;
$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);
}
public function testAddFilterObject()
{
$filter = new loginFilter;
$filter = new loginFilter;
$array = array('loginFilter' => new loginFilter());
$this->assertInstanceOf('iFilter', new loginFilter);
$tmp_form_field = new TmpFormField;
$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);
}
public function testAddFilterString()
{
$tmp_form_field = new TmpFormField;
$tmp_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);
}
@ -172,30 +141,22 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
public function testAddFilterElse()
{
$this->setExpectedException('Exception');
$filter = new NotEmptyValidator;
$tmp_form_field = new TmpFormField;
$tmp_form_field = new FormField;
$tmp_form_field->addFilter($filter);
}
public function testGetValueArray()
{
$form = new TestFormForFormField();
$form = new FormField();
$test_array = array(
'login'=> 'login',
'password'=> 'password'
);
$this->assertSame(1, $form->isValid($test_array));
$tmp_form_field = new TmpFormField;
$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());
}
@ -206,12 +167,9 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
'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());
}
@ -221,19 +179,15 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$this->assertNotNull($test_form_field->getMessage());
}
public function testIsValidMissing1()
public function testIsValidMissingException()
{
$test_array = array(
'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));
}
@ -243,37 +197,32 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
'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));
}
public function testIsValidMissing2()
public function testIsValidMissingRequired()
{
$form_field = new FormField;
$form_field->setRequired(false);
$this->assertTrue($form_field->isValid(''));
}
public function testIsValidMissing3()
public function testIsValidMissingValid()
{
$test_array = array(
'login'=> 'login',
'password'=> 'password'
);
$validator = new NotEmptyValidator();
$form_field = new FormField;
$form_field->setRequired(false);
$form_field->isValid('');
$this->assertFalse($form_field->isRequired());
$return_object = $form_field->addValidator($validator);
$this->assertTrue($form_field->isValid($test_array));
}
public function testFilterValue()
@ -281,45 +230,11 @@ class FormFieldTest extends PHPUnit_Framework_TestCase
$form_field = new FormField;
$form_field->isValid('login');
$form_field->addFilter('login');
$lf = new loginFilter;
$this->assertSame($form_field->getValue(), $lf->filter('login'));
}
}
class TmpFormField extends FormField
{
public function getProp($name)
{
return $this->$name;
}
}
class TestFormForFormField 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);

131
tests/form/FormTest.php

@ -7,53 +7,48 @@
require_once dirname(__FILE__) . '/../../form/Form.php';
require_once dirname(__FILE__) . '/../../form/FormField.php';
require_once dirname(__FILE__) . '/../../session/Session.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';
class FormTest extends PHPUnit_Framework_TestCase
{
public function testIsValidWithNotArray()
{
$form = new NotEmptyForm();
$this->setExpectedException('Exception');
$form->isValid('');
}
public function testIsValidCorrectFields()
{
$form = new NotEmptyForm();
$test_array = array(
$form = new NotEmptyForm();
$test_array = array(
'login'=> 'login',
'password'=> 'password'
);
$this->assertSame(1, $form->isValid($test_array));
);
$this->assertSame(1, $form->isValid($test_array));
}
public function testIsValidMissingField()
{
$form = new NotEmptyForm();
$test_array = array(
$form = new NotEmptyForm();
$test_array = array(
'password'=> 'password'
);
$this->assertSame(0, $form->isValid($test_array));
);
$this->assertSame(0, $form->isValid($test_array));
}
public function testGetValues()
{
$form = new NotEmptyForm();
$fields = array(
'login'=> 'login',
'password'=>'password'
);
$this->assertSame(1, $form->isValid($fields));
$this->assertEquals($fields, $form->getValues());
}
@ -61,14 +56,11 @@ class FormTest extends PHPUnit_Framework_TestCase
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'));
}
@ -77,13 +69,10 @@ class FormTest extends PHPUnit_Framework_TestCase
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'));
}
@ -92,15 +81,11 @@ class FormTest extends PHPUnit_Framework_TestCase
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'));
}
@ -108,47 +93,35 @@ class FormTest extends PHPUnit_Framework_TestCase
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->assertEquals('error', Form::ERROR);
$this->assertEquals($form->getMessageType(), Form::ERROR);
}
@ -156,16 +129,12 @@ class FormTest extends PHPUnit_Framework_TestCase
public function testGetMessageCorrect()
{
$form = new NotEmptyForm();
$fields = array(
'login'=> 'login',
'password'=>'password'
);
$this->assertSame(1, $form->isValid($fields));
$form->setSuccessMessage('message');
$this->assertEquals('message', $form->getMessage());
}
@ -173,15 +142,11 @@ class FormTest extends PHPUnit_Framework_TestCase
public function testGetMessageMissing()
{
$form = new NotEmptyForm();
$fields = array(
'login'=> ''
);
$this->assertSame(0, $form->isValid($fields));
$form->setErrorMessage('message');
$this->assertEquals('message', $form->getMessage());
}
@ -189,87 +154,47 @@ class FormTest extends PHPUnit_Framework_TestCase
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 dirname(__FILE__) . '/../../validator/iValidator.php';
require_once dirname(__FILE__) . '/../../validator/Validator.php';
require_once dirname(__FILE__) . '/../../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;
}
}
public function init()
{
///*
$validator = new NotEmptyValidator();
$validator->setMessage('Enter login name.');
$this->addField('login')->addValidator($validator);
require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
// User password
$validator = new NotEmptyValidator();
$validator->setMessage('Enter your password.');
$this->addField('password')->addValidator($validator);
}
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;
}
public function getProp($name)
{
return $this->$name;
}
}

13
tests/form/FormViewHelperTest.php

@ -9,9 +9,6 @@
* @filesource $URL$
*/
require_once dirname(__FILE__) . '/../../form/Form.php';
require_once dirname(__FILE__) . '/../../form/FormField.php';
require_once dirname(__FILE__) . '/../../view/iView.php';
require_once dirname(__FILE__) . '/../../view/PHPView.php';
require_once dirname(__FILE__) . '/../../view/helpers/ViewHelper.php';
@ -23,11 +20,8 @@ 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'
@ -41,44 +35,37 @@ class FormViewHelperTest extends PHPUnit_Framework_TestCase
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(''));
}
}
Loading…
Cancel
Save