How can I test the forms in zend framework? I have a login form in my zend project, the Login.php is:
<?php
class DEMO_Form_Login extends Zend_Form {
public function init() {
$this
->setMethod('post')
->addElementPrefixPaths(array(
'decorator' => array('DEMO_Decorator' => '../application/decorators'),
));
$this
->addElement('text', 'username', array(
'label' => _T('USERNAME'),
'required' => true,
'value' => '',
'filters' => array('StringTrim'),
'decorators' => array('ViewHelper')
))
->addElement('password', 'password', array(
'label' => _T('PASSWORD'),
'required' => true,
'value' => '',
'decorators' => array('ViewHelper')
))
->addElement('submit', 'submit', array(
开发者_如何学Python 'label' => _T('LOG_INTO'),
'ignore' => true,
'decorators' => array(
array('Submit', array('separator'=>'<br />')))
));
}
}
How can I test it? Can anyone provide some resource about it?
I cannot think of any resource, but I can give you one example of how I would do it.
So, I would create a FormTestCase class as follows:
class FormTestCase extends PHPUnit_Framework_TestCase {
private $_form;
public function setUp() {
parent::setUp();
}
}
Then each form could be tested as follows:
class DemoFormTest extends FormTestCase {
public function setUp() {
parent::setUp();
$this->_form = new My_Form_Demo();
}
public function testCorrectData() {
$mockInputData = array(
'username' => 'somename',
'password' => 'somepass',
'submit' => 'LOG_INTO'
);
$this->assertTrue($this->_form->isValid($mockInputData));
}
public function testInCorrectData() {
$mockInputData = array(
'username' => 'somename',
// password not given
'submit' => 'LOG_INTO'
);
$this->assertFalse($this->_form->isValid($mockInputData));
}
// some other tests
}
In the above example My_Form_Demo
is simplified version of your form. I needed to simplify it, because I do not have your custom decorators and I could not run the test. The setup that I used for this example, can be seen here (along with all my other tests).
Hope this will help you.
精彩评论