1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.test.phpunit.assertions">
4 <title>Assertions</title>
7 Assertions are at the heart of Unit Testing; you use them to verify
8 that the results are what you expect. To this end,
9 <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> provides a number of
10 assertions to make testing your <acronym>MVC</acronym> apps and controllers simpler.
13 <sect3 id="zend.test.phpunit.assertions.query">
14 <title>CSS Selector Assertions</title>
17 <acronym>CSS</acronym> selectors are an easy way to verify that certain artifacts are
18 present in the response content. They also make it trivial to
19 ensure that items necessary for Javascript UIs and/or <acronym>AJAX</acronym>
20 integration will be present; most JS toolkits provide some
21 mechanism for pulling DOM elements based on <acronym>CSS</acronym> selectors, so the
22 syntax would be the same.
26 This functionality is provided via <link
27 linkend="zend.dom.query">Zend_Dom_Query</link>, and integrated
28 into a set of 'Query' assertions. Each of these assertions takes
29 as their first argument a <acronym>CSS</acronym> selector, with optionally additional
30 arguments and/or an error message, based on the assertion type. You
31 can find the rules for writing the <acronym>CSS</acronym> selectors in the <link
32 linkend="zend.dom.query.operation">Zend_Dom_Query theory of
33 operation chapter</link>. Query assertions include:
39 <methodname>assertQuery($path, $message = '')</methodname>: assert that
40 one or more DOM elements matching the given <acronym>CSS</acronym> selector are
41 present. If a <varname>$message</varname> is present, it will be
42 prepended to any failed assertion message.
48 <code>assertQueryContentContains($path, $match, $message =
49 '')</code>: assert that one or more DOM elements matching
50 the given <acronym>CSS</acronym> selector are present, and that at least one
51 contains the content provided in <varname>$match</varname>. If a
52 <varname>$message</varname> is present, it will be prepended to any
53 failed assertion message.
59 <code>assertQueryContentRegex($path, $pattern, $message =
60 '')</code>: assert that one or more DOM elements matching
61 the given <acronym>CSS</acronym> selector are present, and that at least one
62 matches the regular expression provided in
63 <varname>$pattern</varname>. If a <varname>$message</varname> is present,
64 it will be prepended to any failed assertion message.
70 <code>assertQueryCount($path, $count, $message =
71 '')</code>: assert that there are exactly
72 <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
73 selector present. If a <varname>$message</varname> is present, it
74 will be prepended to any failed assertion message.
80 <code>assertQueryCountMin($path, $count, $message =
81 '')</code>: assert that there are at least
82 <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
83 selector present. If a <varname>$message</varname> is present, it
84 will be prepended to any failed assertion message.
85 <emphasis>Note:</emphasis> specifying a value of 1 for
86 <varname>$count</varname> is the same as simply using
87 <methodname>assertQuery()</methodname>.
93 <code>assertQueryCountMax($path, $count, $message =
94 '')</code>: assert that there are no more than
95 <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
96 selector present. If a <varname>$message</varname> is present, it
97 will be prepended to any failed assertion message.
98 <emphasis>Note:</emphasis> specifying a value of 1 for
99 <varname>$count</varname> is the same as simply using
100 <methodname>assertQuery()</methodname>.
106 Additionally, each of the above has a 'Not' variant that provides a
107 negative assertion: <methodname>assertNotQuery()</methodname>,
108 <methodname>assertNotQueryContentContains()</methodname>,
109 <methodname>assertNotQueryContentRegex()</methodname>, and
110 <methodname>assertNotQueryCount()</methodname>. (Note that the min and
111 max counts do not have these variants, for what should be obvious
116 <sect3 id="zend.test.phpunit.assertions.xpath">
117 <title>XPath Assertions</title>
120 Some developers are more familiar with XPath than with <acronym>CSS</acronym>
121 selectors, and thus XPath variants of all the <link
122 linkend="zend.test.phpunit.assertions.query">Query
123 assertions</link> are also provided. These are:
129 <methodname>assertXpath($path, $message = '')</methodname>
135 <methodname>assertNotXpath($path, $message = '')</methodname>
141 <methodname>assertXpathContentContains($path, $match, $message =
148 <methodname>assertNotXpathContentContains($path, $match, $message =
155 <methodname>assertXpathContentRegex($path, $pattern, $message = '')</methodname>
161 <methodname>assertNotXpathContentRegex($path, $pattern, $message =
168 <methodname>assertXpathCount($path, $count, $message = '')</methodname>
174 <methodname>assertNotXpathCount($path, $count, $message = '')</methodname>
180 <methodname>assertXpathCountMin($path, $count, $message = '')</methodname>
186 <methodname>assertNotXpathCountMax($path, $count, $message = '')</methodname>
192 <sect3 id="zend.test.phpunit.assertions.redirect">
193 <title>Redirect Assertions</title>
196 Often an action will redirect. Instead of following the redirect,
197 <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> allows you to
198 test for redirects with a handful of assertions.
204 <methodname>assertRedirect($message = '')</methodname>: assert simply that
205 a redirect has occurred.
211 <methodname>assertNotRedirect($message = '')</methodname>: assert that no
212 redirect has occurred.
218 <methodname>assertRedirectTo($url, $message = '')</methodname>: assert that
219 a redirect has occurred, and that the value of the Location
220 header is the <varname>$url</varname> provided.
226 <methodname>assertNotRedirectTo($url, $message = '')</methodname>: assert that
227 a redirect has either NOT occurred, or that the value of the Location
228 header is NOT the <varname>$url</varname> provided.
234 <methodname>assertRedirectRegex($pattern, $message = '')</methodname>:
235 assert that a redirect has occurred, and that the value of the
236 Location header matches the regular expression provided by
237 <varname>$pattern</varname>.
243 <methodname>assertNotRedirectRegex($pattern, $message = '')</methodname>:
244 assert that a redirect has either NOT occurred, or that the value of the
245 Location header does NOT match the regular expression provided by
246 <varname>$pattern</varname>.
252 <sect3 id="zend.test.phpunit.assertions.header">
253 <title>Response Header Assertions</title>
256 In addition to checking for redirect headers, you will often need
257 to check for specific <acronym>HTTP</acronym> response codes and headers -- for
258 instance, to determine whether an action results in a 404 or 500
259 response, or to ensure that <acronym>JSON</acronym> responses contain the appropriate
260 Content-Type header. The following assertions are available.
266 <methodname>assertResponseCode($code, $message = '')</methodname>: assert
267 that the response resulted in the given <acronym>HTTP</acronym> response code.
273 <methodname>assertHeader($header, $message = '')</methodname>: assert
274 that the response contains the given header.
280 <code>assertHeaderContains($header, $match, $message = '')</code>: assert that
281 the response contains the given header and that its content contains the given
288 <code>assertHeaderRegex($header, $pattern, $message = '')</code>: assert that
289 the response contains the given header and that its content matches the given
296 Additionally, each of the above assertions have a 'Not' variant for
301 <sect3 id="zend.test.phpunit.assertions.request">
302 <title>Request Assertions</title>
305 It's often useful to assert against the last run action,
306 controller, and module; additionally, you may want to assert
307 against the route that was matched. The following assertions can
308 help you in this regard:
314 <methodname>assertModule($module, $message = '')</methodname>: Assert that
315 the given module was used in the last dispatched action.
321 <methodname>assertController($controller, $message = '')</methodname>:
322 Assert that the given controller was selected in the last
329 <methodname>assertAction($action, $message = '')</methodname>: Assert that
330 the given action was last dispatched.
336 <methodname>assertRoute($route, $message = '')</methodname>: Assert that
337 the given named route was matched by the router.
343 Each also has a 'Not' variant for negative assertions.