Zend_Test-PHPUnit-Assertions.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.test.phpunit.assertions">
  4. <title>Assertions</title>
  5. <para>
  6. Assertions are at the heart of Unit Testing; you use them to verify
  7. that the results are what you expect. To this end,
  8. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> provides a number of
  9. assertions to make testing your <acronym>MVC</acronym> apps and controllers simpler.
  10. </para>
  11. <sect3 id="zend.test.phpunit.assertions.query">
  12. <title>CSS Selector Assertions</title>
  13. <para>
  14. <acronym>CSS</acronym> selectors are an easy way to verify that certain artifacts are
  15. present in the response content. They also make it trivial to
  16. ensure that items necessary for Javascript UIs and/or <acronym>AJAX</acronym>
  17. integration will be present; most JS toolkits provide some
  18. mechanism for pulling DOM elements based on <acronym>CSS</acronym> selectors, so the
  19. syntax would be the same.
  20. </para>
  21. <para>
  22. This functionality is provided via <link
  23. linkend="zend.dom.query">Zend_Dom_Query</link>, and integrated
  24. into a set of 'Query' assertions. Each of these assertions takes
  25. as their first argument a <acronym>CSS</acronym> selector, with optionally additional
  26. arguments and/or an error message, based on the assertion type. You
  27. can find the rules for writing the <acronym>CSS</acronym> selectors in the <link
  28. linkend="zend.dom.query.operation">Zend_Dom_Query theory of
  29. operation chapter</link>. Query assertions include:
  30. </para>
  31. <itemizedlist>
  32. <listitem><para>
  33. <methodname>assertQuery($path, $message = '')</methodname>: assert that
  34. one or more DOM elements matching the given <acronym>CSS</acronym> selector are
  35. present. If a <varname>$message</varname> is present, it will be
  36. prepended to any failed assertion message.
  37. </para></listitem>
  38. <listitem><para>
  39. <code>assertQueryContentContains($path, $match, $message =
  40. '')</code>: assert that one or more DOM elements matching
  41. the given <acronym>CSS</acronym> selector are present, and that at least one
  42. contains the content provided in <varname>$match</varname>. If a
  43. <varname>$message</varname> is present, it will be prepended to any
  44. failed assertion message.
  45. </para></listitem>
  46. <listitem><para>
  47. <code>assertQueryContentRegex($path, $pattern, $message =
  48. '')</code>: assert that one or more DOM elements matching
  49. the given <acronym>CSS</acronym> selector are present, and that at least one
  50. matches the regular expression provided in
  51. <varname>$pattern</varname>. If a <varname>$message</varname> is present,
  52. it will be prepended to any failed assertion message.
  53. </para></listitem>
  54. <listitem><para>
  55. <code>assertQueryCount($path, $count, $message =
  56. '')</code>: assert that there are exactly
  57. <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
  58. selector present. If a <varname>$message</varname> is present, it
  59. will be prepended to any failed assertion message.
  60. </para></listitem>
  61. <listitem><para>
  62. <code>assertQueryCountMin($path, $count, $message =
  63. '')</code>: assert that there are at least
  64. <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
  65. selector present. If a <varname>$message</varname> is present, it
  66. will be prepended to any failed assertion message.
  67. <emphasis>Note:</emphasis> specifying a value of 1 for
  68. <varname>$count</varname> is the same as simply using
  69. <methodname>assertQuery()</methodname>.
  70. </para></listitem>
  71. <listitem><para>
  72. <code>assertQueryCountMax($path, $count, $message =
  73. '')</code>: assert that there are no more than
  74. <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
  75. selector present. If a <varname>$message</varname> is present, it
  76. will be prepended to any failed assertion message.
  77. <emphasis>Note:</emphasis> specifying a value of 1 for
  78. <varname>$count</varname> is the same as simply using
  79. <methodname>assertQuery()</methodname>.
  80. </para></listitem>
  81. </itemizedlist>
  82. <para>
  83. Additionally, each of the above has a 'Not' variant that provides a
  84. negative assertion: <methodname>assertNotQuery()</methodname>,
  85. <methodname>assertNotQueryContentContains()</methodname>,
  86. <methodname>assertNotQueryContentRegex()</methodname>, and
  87. <methodname>assertNotQueryCount()</methodname>. (Note that the min and
  88. max counts do not have these variants, for what should be obvious
  89. reasons.)
  90. </para>
  91. </sect3>
  92. <sect3 id="zend.test.phpunit.assertions.xpath">
  93. <title>XPath Assertions</title>
  94. <para>
  95. Some developers are more familiar with XPath than with <acronym>CSS</acronym>
  96. selectors, and thus XPath variants of all the <link
  97. linkend="zend.test.phpunit.assertions.query">Query
  98. assertions</link> are also provided. These are:
  99. </para>
  100. <itemizedlist>
  101. <listitem><para>
  102. <methodname>assertXpath($path, $message = '')</methodname>
  103. </para></listitem>
  104. <listitem><para>
  105. <methodname>assertNotXpath($path, $message = '')</methodname>
  106. </para></listitem>
  107. <listitem><para>
  108. <methodname>assertXpathContentContains($path, $match, $message = '')</methodname>
  109. </para></listitem>
  110. <listitem><para>
  111. <methodname>assertNotXpathContentContains($path, $match, $message = '')</methodname>
  112. </para></listitem>
  113. <listitem><para>
  114. <methodname>assertXpathContentRegex($path, $pattern, $message = '')</methodname>
  115. </para></listitem>
  116. <listitem><para>
  117. <methodname>assertNotXpathContentRegex($path, $pattern, $message = '')</methodname>
  118. </para></listitem>
  119. <listitem><para>
  120. <methodname>assertXpathCount($path, $count, $message = '')</methodname>
  121. </para></listitem>
  122. <listitem><para>
  123. <methodname>assertNotXpathCount($path, $count, $message = '')</methodname>
  124. </para></listitem>
  125. <listitem><para>
  126. <methodname>assertXpathCountMin($path, $count, $message = '')</methodname>
  127. </para></listitem>
  128. <listitem><para>
  129. <methodname>assertNotXpathCountMax($path, $count, $message = '')</methodname>
  130. </para></listitem>
  131. </itemizedlist>
  132. </sect3>
  133. <sect3 id="zend.test.phpunit.assertions.redirect">
  134. <title>Redirect Assertions</title>
  135. <para>
  136. Often an action will redirect. Instead of following the redirect,
  137. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> allows you to
  138. test for redirects with a handful of assertions.
  139. </para>
  140. <itemizedlist>
  141. <listitem><para>
  142. <methodname>assertRedirect($message = '')</methodname>: assert simply that
  143. a redirect has occurred.
  144. </para></listitem>
  145. <listitem><para>
  146. <methodname>assertNotRedirect($message = '')</methodname>: assert that no
  147. redirect has occurred.
  148. </para></listitem>
  149. <listitem><para>
  150. <methodname>assertRedirectTo($url, $message = '')</methodname>: assert that
  151. a redirect has occurred, and that the value of the Location
  152. header is the <varname>$url</varname> provided.
  153. </para></listitem>
  154. <listitem><para>
  155. <methodname>assertNotRedirectTo($url, $message = '')</methodname>: assert that
  156. a redirect has either NOT occurred, or that the value of the Location
  157. header is NOT the <varname>$url</varname> provided.
  158. </para></listitem>
  159. <listitem><para>
  160. <methodname>assertRedirectRegex($pattern, $message = '')</methodname>:
  161. assert that a redirect has occurred, and that the value of the
  162. Location header matches the regular expression provided by
  163. <varname>$pattern</varname>.
  164. </para></listitem>
  165. <listitem><para>
  166. <methodname>assertNotRedirectRegex($pattern, $message = '')</methodname>:
  167. assert that a redirect has either NOT occurred, or that the value of the
  168. Location header does NOT match the regular expression provided by
  169. <varname>$pattern</varname>.
  170. </para></listitem>
  171. </itemizedlist>
  172. </sect3>
  173. <sect3 id="zend.test.phpunit.assertions.header">
  174. <title>Response Header Assertions</title>
  175. <para>
  176. In addition to checking for redirect headers, you will often need
  177. to check for specific <acronym>HTTP</acronym> response codes and headers -- for
  178. instance, to determine whether an action results in a 404 or 500
  179. response, or to ensure that <acronym>JSON</acronym> responses contain the appropriate
  180. Content-Type header. The following assertions are available.
  181. </para>
  182. <itemizedlist>
  183. <listitem><para>
  184. <methodname>assertResponseCode($code, $message = '')</methodname>: assert
  185. that the response resulted in the given <acronym>HTTP</acronym> response code.
  186. </para></listitem>
  187. <listitem><para>
  188. <methodname>assertHeader($header, $message = '')</methodname>: assert
  189. that the response contains the given header.
  190. </para></listitem>
  191. <listitem><para>
  192. <code>assertHeaderContains($header, $match, $message =
  193. '')</code>: assert that the response contains the given header
  194. and that its content contains the given string.
  195. </para></listitem>
  196. <listitem><para>
  197. <code>assertHeaderRegex($header, $pattern, $message =
  198. '')</code>: assert that the response contains the given header
  199. and that its content matches the given regex.
  200. </para></listitem>
  201. </itemizedlist>
  202. <para>
  203. Additionally, each of the above assertions have a 'Not' variant for
  204. negative assertions.
  205. </para>
  206. </sect3>
  207. <sect3 id="zend.test.phpunit.assertions.request">
  208. <title>Request Assertions</title>
  209. <para>
  210. It's often useful to assert against the last run action,
  211. controller, and module; additionally, you may want to assert
  212. against the route that was matched. The following assertions can
  213. help you in this regard:
  214. </para>
  215. <itemizedlist>
  216. <listitem><para>
  217. <methodname>assertModule($module, $message = '')</methodname>: Assert that
  218. the given module was used in the last dispatched action.
  219. </para></listitem>
  220. <listitem><para>
  221. <methodname>assertController($controller, $message = '')</methodname>:
  222. Assert that the given controller was selected in the last
  223. dispatched action.
  224. </para></listitem>
  225. <listitem><para>
  226. <methodname>assertAction($action, $message = '')</methodname>: Assert that
  227. the given action was last dispatched.
  228. </para></listitem>
  229. <listitem><para>
  230. <methodname>assertRoute($route, $message = '')</methodname>: Assert that
  231. the given named route was matched by the router.
  232. </para></listitem>
  233. </itemizedlist>
  234. <para>
  235. Each also has a 'Not' variant for negative assertions.
  236. </para>
  237. </sect3>
  238. </sect2>
  239. <!--
  240. vim:se ts=4 sw=4 et:
  241. -->