#native_company# #native_desc#
#native_cta#

Testing with the Zend Framework: How to Get Started Page 2

By W. Jason Gilmore
on September 29, 2010

Testing a Form

Now that you know how to create and execute a test, let’s look at a simple test for a typical login form. Presume the login form, located at /account/login, asks the user to provide an account’s associated email address and password. If the information is deemed valid, the user is redirected to /account/profile, otherwise an error message is returned and the form is displayed anew.
We can write a test that verifies this is working as expected. Presuming the account controller is in place and the associated action, view and login form created, open the associated AccountControllerTest.php file and replace its contents with the following:

<?php class AccountControllerTest extends ControllerTestCase { public function 
testValidLoginShouldInitializeSessionAndRedirectToHomePage() { $this->getRequest()-
>setMethod('POST'); $this->getRequest()->setPost( array( 'email' =>
'[email protected]', 'pswd' => 'secret' ) ); $this->dispatch('/account/login'); $this-
>assertRedirectTo('/account/profile'); } }

Run your tests anew and if the provided email address and password matches a record as retrieved by your authentication mechanism (I use Zend_Auth), then the test should pass!

Conclusion

This brief tutorial only scratches the surface of what’s at your disposal when using the Zend Framework and powerful PHPUnit test framework together. However, it should be enough to help you introduce some semblance of sanity into your approach to Web development, not to mention save you bunches of time otherwise spent testing tedious features. As always, if you have any thoughts or questions, please add them in the comments, or ping me on Twitter at @wjgilmore!

About the Author

Jason Gilmore is the founder of WJGilmore.com and the author of several popular books, including “Easy PHP Websites with the Zend Framework”, “Easy PayPal with PHP”, and “Beginning PHP and MySQL, Third Edition”.