You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
7.9 KiB

<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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';
class FormTest extends PHPUnit_Framework_TestCase
{
public function testAddFieldEmptyMessage()
{
$stub = $this->getMockForAbstractClass('Form');
$method = new ReflectionMethod('Form', 'addField');
$method->setAccessible(true);
$this->assertInstanceOf('FormField', $method->invoke($stub, 'login'));
}
public function testAddFieldWithMessage()
{
$message = '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();
$this->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));
$data['messages'] = $form->getMessages();
$data['values'] = $form->getSourceValues();
$this->assertSame($_SESSION['NotEmptyForm'], $data);
}
public function testFillHelperData()
{
$form = new NotEmptyForm();
$test_array = array(
'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);
}
public function testGetValues()
{
$form = new NotEmptyForm();
$fields = array(
'login'=> 'login',
'password'=>'password'
);
$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->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));
$form->setSuccessMessage('message');
$this->assertEquals('message', $form->getMessage());
}
public function testGetMessageMissing()
{
$form = new NotEmptyForm();
$fields = array(
'login'=> ''
);
$this->assertSame(0, $form->isValid($fields));
$form->setErrorMessage('message');
$this->assertEquals('message', $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());
}
}
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);
}
}
class NullForm extends Form
{
public function init()
{
/**/
}
}
class FooTest extends PHPUnit_Framework_TestCase
{
/**
* @covers Foo::doSomethingPrivate
*/
public function testPrivateMethod()
{
$method = new ReflectionMethod(
'Foo', 'doSomethingPrivate'
);
$method->setAccessible(TRUE);
$this->assertEquals(
'blah', $method->invoke(new Foo)
);
}
}