PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend_Test_PHPUnit

Zend_Test_PHPUnit provides a TestCase for MVC applications that contains assertions for testing against a variety of responsibilities. Probably the easiest way to understand what it can do is to see an example.

Example 956. Application Login TestCase example

The following is a simple test case for a UserController to verify several things:

  • The login form should be displayed to non-authenticated users.

  • When a user logs in, they should be redirected to their profile page, and that profile page should show relevant information.

This particular example assumes a few things. First, we're moving most of our bootstrapping to a plugin. This simplifies setup of the test case as it allows us to specify our environment succinctly, and also allows us to bootstrap the application in a single line. Also, our particular example is assuming that autoloading is setup so we do not need to worry about requiring the appropriate classes (such as the correct controller, plugin, etc).

<?php
class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function 
setUp()
    {
        
$this->bootstrap = array($this'appBootstrap');
        
parent::setUp();
    }

    public function 
appBootstrap()
    {
        
$this->frontController
             
->registerPlugin(new Bugapp_Plugin_Initialize('development'));
    }

    public function 
testCallWithoutActionShouldPullFromIndexAction()
    {
        
$this->dispatch('/user');
        
$this->assertController('user');
        
$this->assertAction('index');
    }

    public function 
testIndexActionShouldContainLoginForm()
    {
        
$this->dispatch('/user');
        
$this->assertAction('index');
        
$this->assertQueryCount('form#loginForm'1);
    }

    public function 
testValidLoginShouldGoToProfilePage()
    {
        
$this->request->setMethod('POST')
              ->
setPost(array(
                  
'username' => 'foobar',
                  
'password' => 'foobar'
              
));
        
$this->dispatch('/user/login');
        
$this->assertRedirectTo('/user/view');

        
$this->resetRequest()
             ->
resetResponse();

        
$this->request->setMethod('GET')
             ->
setPost(array());
        
$this->dispatch('/user/view');
        
$this->assertRoute('default');
        
$this->assertModule('default');
        
$this->assertController('user');
        
$this->assertAction('view');
        
$this->assertNotRedirect();
        
$this->assertQuery('dl');
        
$this->assertQueryContentContains('h2''User: foobar');
    }
}

This example could be written somewhat simpler -- not all the assertions shown are necessary, and are provided for illustration purposes only. Hopefully, it shows how simple it can be to test your applications.


Zend Framework