# Assertions

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_ControllerTestCase` provides a number of assertions to make testing your MVC apps and controllers simpler.

## 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 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:

- `assertQuery($path, $message)`: assert that one or more DOM elements matching the given CSS selector are present. If a `$message` is present, it will be prepended to any failed assertion message.

- `assertQueryContentContains($path, $match, $message)`: assert that one or more DOM elements matching the given CSS selector are present, and that at least one contains the content provided in `$match`. If a `$message` is present, it will be prepended to any failed assertion message.

- `assertQueryContentRegex($path, $pattern, $message)`: assert that one or more DOM elements matching the given CSS selector are present, 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.

- `assertQueryCount($path, $count, $message)`: assert that there are exactly `$count` DOM elements matching the given CSS selector present. If a `$message` is present, it will be prepended to any failed assertion message.

- `assertQueryCountMin($path, $count, $message)`: assert that there are at least `$count` DOM elements matching the given CSS selector present. If a `$message` is present, it will be prepended to any failed assertion message. Note: specifying a value of 1 for `$count` is the same as simply using `assertQuery()`.

- `assertQueryCountMax($path, $count, $message)`: assert that there are no more than `$count` DOM elements matching the given CSS selector present. If a `$message` is present, it will be prepended to any failed assertion message. Note: specifying a value of 1 for `$count` is the same as simply using `assertQuery()`.

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:

- `assertXpath($path, $message = '')`

- `assertNotXpath($path, $message = '')`

- `assertXpathContentContains($path, $match, $message = '')`

- `assertNotXpathContentContains($path, $match, $message = '')`

- `assertXpathContentRegex($path, $pattern, $message = '')`

- `assertNotXpathContentRegex($path, $pattern, $message = '')`

- `assertXpathCount($path, $count, $message = '')`

- `assertNotXpathCount($path, $count, $message = '')`

- `assertXpathCountMin($path, $count, $message = '')`

- `assertNotXpathCountMax($path, $count, $message = '')`

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

- `assertRedirect($message = '')`: assert simply that a redirect has occurred.

- `assertNotRedirect($message = '')`: assert that no redirect has occurred.

- `assertRedirectTo($url, $message = '')`: assert that a redirect has occurred, and that the value of the Location header is the `$url` provided.

- `assertNotRedirectTo($url, $message = '')`: assert that a redirect has either NOT occurred, or that the value of the Location header is NOT the `$url` provided.

- `assertRedirectRegex($pattern, $message = '')`: assert that a redirect has occurred, and that the value of the Location header matches the regular expression provided by `$pattern`.

- `assertNotRedirectRegex($pattern, $message = '')`: assert that a redirect has either NOT occurred, or that the value of the Location header does NOT match the regular expression provided by `$pattern`.

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

- `assertResponseCode($code, $message = '')`: assert that the response resulted in the given HTTP response code.

- `assertHeader($header, $message = '')`: assert that the response contains the given header.

- `assertHeaderContains($header, $match, $message)`: assert that the response contains the given header and that its content contains the given string.

- `assertHeaderRegex($header, $pattern, $message)`: assert that the response contains the given header and that its content matches the given regex.

Additionally, each of the above assertions have a 'Not' variant for negative assertions.

## 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:

- `assertModule($module, $message = '')`: Assert that the given module was used in the last dispatched action.

- `assertController($controller, $message = '')`: Assert that the given controller was selected in the last dispatched action.

- `assertAction($action, $message = '')`: Assert that the given action was last dispatched.

- `assertRoute($route, $message = '')`: Assert that the given named route was matched by the router.

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