PhpRiot
Follow phpriot on Twitter
Sponsored Link
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
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Testing your Controllers and MVC Applications

Once you have your bootstrap in place, you can begin testing. Testing is basically as you would expect in an PHPUnit test suite, with a few minor differences.

First, you will need to dispatch a URL to test, using the dispatch() method of the TestCase:

<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    
// ...

    
public function testHomePage()
    {
        
$this->dispatch('/');
        
// ...
    
}
}

There will be times, however, that you need to provide extra information -- GET and POST variables, COOKIE information, etc. You can populate the request with that information:

<?php
class FooControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    
// ...

    
public function testBarActionShouldReceiveAllParameters()
    {
        
// Set GET variables:
        
$this->request->setQuery(array(
            
'foo' => 'bar',
            
'bar' => 'baz',
        ));

        
// Set POST variables:
        
$this->request->setPost(array(
            
'baz'  => 'bat',
            
'lame' => 'bogus',
        ));

        
// Set a cookie value:
        
$this->request->setCookie('user''matthew');
        
// or many:
        
$this->request->setCookies(array(
            
'timestamp' => time(),
            
'host'      => 'foobar',
        ));

        
// Set headers, even:
        
$this->request->setHeader('X-Requested-With''XmlHttpRequest');

        
// Set the request method:
        
$this->request->setMethod('POST');

        
// Dispatch:
        
$this->dispatch('/foo/bar');

        
// ...
    
}
}

Now that the request is made, it's time to start making assertions against it.

Controller Tests and the Redirector Action Helper

Important

The redirect action helper issues an exit() statement when using the method gotoAndExit() and will then obviously also stops a test running for this method. For testability of your application dont use that method on the redirector!

Due to its nature the redirector action helper plugin issues a redirect and exists after this. Because you cannot test parts of an application that issue exit calls Zend_Test_PHPUnit_ControllerTestCase automatically disables the exit part of the redirector which can cause different behaviours in tests and the real application. To make sure redirect work correctly you should it them in the following way:

<?php
class MyController extends Zend_Controller_Action
{
    public function 
indexAction()
    {
        if(
$someCondition == true) {
            return 
$this->_redirect(...);
        } else if(
$anotherCondition == true) {
            
$this->_redirector->gotoSimple("foo");
            return;
        }

        
// do some stuff here
    
}
}

Important

Depending on your application this is not enough as additional action, preDispatch() or postDispatch() logic might be executed. This cannot be handled in a good way with Zend Test currently.

Zend Framework