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.
The following is a simple test case for a IndexController to verify things like HTTP code, controller and action name :
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include '/path/to/application/config/test/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/');
$this->assertResponseStatusCode(200);
$this->assertModuleName('application');
$this->assertControllerName('application_index');
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('home');
}
}
The setup of the test case can to define the application config. You can use several config to test modules dependencies or your current application config.
As noted in the previous example, all MVC test cases should extend AbstractHttpControllerTestCase. This class in turn extends PHPUnit_Framework_TestCase, and gives you all the structure and assertions you’d expect from PHPUnit – as well as some scaffolding and assertions specific to Zend Framework’s MVC implementation.
In order to test your MVC application, you will need to setup the application config. Use simply the the setApplicationConfig method :
public function setUp()
{
$this->setApplicationConfig(
include '/path/to/application/config/test/application.config.php'
);
parent::setUp();
}
Once the application is set up, you can write your tests. To help debug tests, you can activate the flag traceError to throw MVC exception during the tests writing :
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
}
Once you have your application config 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:
public function testIndexAction()
{
$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:
public function testIndexAction()
{
$this->getRequest()
->setMethod('POST')
->setPost(new Parameters(array('argument' => 'value')));
$this->dispatch('/');
}
You can populate GET or POST variables directly with the dispatch method :
public function testIndexAction()
{
$this->dispatch('/', 'POST', array('argument' => 'value'));
}
You can use directly yours query args in the url :
public function testIndexAction()
{
$this->dispatch('/tests?foo=bar&baz=foo');
}
Now that the request is made, it’s time to start making assertions against it.
Assertions are at the heart of Unit Testing; you use them to verify that the results are what you expect. To this end, Zend\Test\PHPUnit\AbstractControllerTestCase provides a number of assertions to make testing your MVC apps and controllers simpler.
It’s often useful to assert against the last run action, controller, and module; additionally, you may want to assert against the route that was matched. The following assertions can help you in this regard:
Each also has a ‘Not’ variant for negative assertions.
CSS selectors are an easy way to verify that certain artifacts are present in the response content. They also make it trivial to ensure that items necessary for Javascript UIs and/or AJAX integration will be present; most JS toolkits provide some mechanism for pulling DOM elements based on CSS selectors, so the syntax would be the same.
This functionality is provided via Zend\Dom\Query, and integrated into a set of ‘Query’ assertions. Each of these assertions takes as their first argument a CSS selector, with optionally additional arguments and/or an error message, based on the assertion type. You can find the rules for writing the CSS selectors in the Zend\Dom\Query Theory of Operation chapter. Query assertions include:
and that at least one contains the content provided in $match.
and that at least one matches the regular expression provided in $pattern. If a $message is present, it will be prepended to any failed assertion message.
Additionally, each of the above has a ‘Not’ variant that provides a negative assertion: assertNotQuery(), assertNotQueryContentContains(), assertNotQueryContentRegex(), and assertNotQueryCount(). (Note that the min and max counts do not have these variants, for what should be obvious reasons.)
Some developers are more familiar with XPath than with CSS selectors, and thus XPath variants of all the Query assertions are also provided. These are:
Often an action will redirect. Instead of following the redirect, Zend\Test\PHPUnit\ControllerTestCase allows you to test for redirects with a handful of assertions.
expression provided by $pattern.
Each also has a ‘Not’ variant for negative assertions.
In addition to checking for redirect headers, you will often need to check for specific HTTP response codes and headers – for instance, to determine whether an action results in a 404 or 500 response, or to ensure that JSON responses contain the appropriate Content-Type header. The following assertions are available.
Additionally, each of the above assertions have a ‘Not’ variant for negative assertions.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.