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.

Request Assertions

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 Selector 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 manipulating 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 Theory of Operation chapter. Query assertions include:

All queries above also allow an optional $message argument; when provided, that message will be used when displaying assertion failures.

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.)

XPath Assertions

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:

Redirect Assertions

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.

Each also has a 'Not' variant for negative assertions.

Response Header 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.