Zend_Test-PHPUnit-Assertions.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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>
  33. <para>
  34. <methodname>assertQuery($path, $message = '')</methodname>: assert that
  35. one or more DOM elements matching the given <acronym>CSS</acronym> selector are
  36. present. If a <varname>$message</varname> is present, it will be
  37. prepended to any failed assertion message.
  38. </para>
  39. </listitem>
  40. <listitem>
  41. <para>
  42. <code>assertQueryContentContains($path, $match, $message =
  43. '')</code>: assert that one or more DOM elements matching
  44. the given <acronym>CSS</acronym> selector are present, and that at least one
  45. contains the content provided in <varname>$match</varname>. If a
  46. <varname>$message</varname> is present, it will be prepended to any
  47. failed assertion message.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <code>assertQueryContentRegex($path, $pattern, $message =
  53. '')</code>: assert that one or more DOM elements matching
  54. the given <acronym>CSS</acronym> selector are present, and that at least one
  55. matches the regular expression provided in
  56. <varname>$pattern</varname>. If a <varname>$message</varname> is present,
  57. it will be prepended to any failed assertion message.
  58. </para>
  59. </listitem>
  60. <listitem>
  61. <para>
  62. <code>assertQueryCount($path, $count, $message =
  63. '')</code>: assert that there are exactly
  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. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. <code>assertQueryCountMin($path, $count, $message =
  72. '')</code>: assert that there are at least
  73. <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
  74. selector present. If a <varname>$message</varname> is present, it
  75. will be prepended to any failed assertion message.
  76. <emphasis>Note:</emphasis> specifying a value of 1 for
  77. <varname>$count</varname> is the same as simply using
  78. <methodname>assertQuery()</methodname>.
  79. </para>
  80. </listitem>
  81. <listitem>
  82. <para>
  83. <code>assertQueryCountMax($path, $count, $message =
  84. '')</code>: assert that there are no more than
  85. <varname>$count</varname> DOM elements matching the given <acronym>CSS</acronym>
  86. selector present. If a <varname>$message</varname> is present, it
  87. will be prepended to any failed assertion message.
  88. <emphasis>Note:</emphasis> specifying a value of 1 for
  89. <varname>$count</varname> is the same as simply using
  90. <methodname>assertQuery()</methodname>.
  91. </para>
  92. </listitem>
  93. </itemizedlist>
  94. <para>
  95. Additionally, each of the above has a 'Not' variant that provides a
  96. negative assertion: <methodname>assertNotQuery()</methodname>,
  97. <methodname>assertNotQueryContentContains()</methodname>,
  98. <methodname>assertNotQueryContentRegex()</methodname>, and
  99. <methodname>assertNotQueryCount()</methodname>. (Note that the min and
  100. max counts do not have these variants, for what should be obvious
  101. reasons.)
  102. </para>
  103. </sect3>
  104. <sect3 id="zend.test.phpunit.assertions.xpath">
  105. <title>XPath Assertions</title>
  106. <para>
  107. Some developers are more familiar with XPath than with <acronym>CSS</acronym>
  108. selectors, and thus XPath variants of all the <link
  109. linkend="zend.test.phpunit.assertions.query">Query
  110. assertions</link> are also provided. These are:
  111. </para>
  112. <itemizedlist>
  113. <listitem>
  114. <para>
  115. <methodname>assertXpath($path, $message = '')</methodname>
  116. </para>
  117. </listitem>
  118. <listitem>
  119. <para>
  120. <methodname>assertNotXpath($path, $message = '')</methodname>
  121. </para>
  122. </listitem>
  123. <listitem>
  124. <para>
  125. <methodname>assertXpathContentContains($path, $match, $message =
  126. '')</methodname>
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <methodname>assertNotXpathContentContains($path, $match, $message =
  132. '')</methodname>
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <methodname>assertXpathContentRegex($path, $pattern, $message = '')</methodname>
  138. </para>
  139. </listitem>
  140. <listitem>
  141. <para>
  142. <methodname>assertNotXpathContentRegex($path, $pattern, $message =
  143. '')</methodname>
  144. </para>
  145. </listitem>
  146. <listitem>
  147. <para>
  148. <methodname>assertXpathCount($path, $count, $message = '')</methodname>
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. <methodname>assertNotXpathCount($path, $count, $message = '')</methodname>
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <methodname>assertXpathCountMin($path, $count, $message = '')</methodname>
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <methodname>assertNotXpathCountMax($path, $count, $message = '')</methodname>
  164. </para>
  165. </listitem>
  166. </itemizedlist>
  167. </sect3>
  168. <sect3 id="zend.test.phpunit.assertions.redirect">
  169. <title>Redirect Assertions</title>
  170. <para>
  171. Often an action will redirect. Instead of following the redirect,
  172. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> allows you to
  173. test for redirects with a handful of assertions.
  174. </para>
  175. <itemizedlist>
  176. <listitem>
  177. <para>
  178. <methodname>assertRedirect($message = '')</methodname>: assert simply that
  179. a redirect has occurred.
  180. </para>
  181. </listitem>
  182. <listitem>
  183. <para>
  184. <methodname>assertNotRedirect($message = '')</methodname>: assert that no
  185. redirect has occurred.
  186. </para>
  187. </listitem>
  188. <listitem>
  189. <para>
  190. <methodname>assertRedirectTo($url, $message = '')</methodname>: assert that
  191. a redirect has occurred, and that the value of the Location
  192. header is the <varname>$url</varname> provided.
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>
  197. <methodname>assertNotRedirectTo($url, $message = '')</methodname>: assert that
  198. a redirect has either NOT occurred, or that the value of the Location
  199. header is NOT the <varname>$url</varname> provided.
  200. </para>
  201. </listitem>
  202. <listitem>
  203. <para>
  204. <methodname>assertRedirectRegex($pattern, $message = '')</methodname>:
  205. assert that a redirect has occurred, and that the value of the
  206. Location header matches the regular expression provided by
  207. <varname>$pattern</varname>.
  208. </para>
  209. </listitem>
  210. <listitem>
  211. <para>
  212. <methodname>assertNotRedirectRegex($pattern, $message = '')</methodname>:
  213. assert that a redirect has either NOT occurred, or that the value of the
  214. Location header does NOT match the regular expression provided by
  215. <varname>$pattern</varname>.
  216. </para>
  217. </listitem>
  218. </itemizedlist>
  219. </sect3>
  220. <sect3 id="zend.test.phpunit.assertions.header">
  221. <title>Response Header Assertions</title>
  222. <para>
  223. In addition to checking for redirect headers, you will often need
  224. to check for specific <acronym>HTTP</acronym> response codes and headers -- for
  225. instance, to determine whether an action results in a 404 or 500
  226. response, or to ensure that <acronym>JSON</acronym> responses contain the appropriate
  227. Content-Type header. The following assertions are available.
  228. </para>
  229. <itemizedlist>
  230. <listitem>
  231. <para>
  232. <methodname>assertResponseCode($code, $message = '')</methodname>: assert
  233. that the response resulted in the given <acronym>HTTP</acronym> response code.
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <methodname>assertHeader($header, $message = '')</methodname>: assert
  239. that the response contains the given header.
  240. </para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. <code>assertHeaderContains($header, $match, $message = '')</code>: assert that
  245. the response contains the given header and that its content contains the given
  246. string.
  247. </para>
  248. </listitem>
  249. <listitem>
  250. <para>
  251. <code>assertHeaderRegex($header, $pattern, $message = '')</code>: assert that
  252. the response contains the given header and that its content matches the given
  253. regex.
  254. </para>
  255. </listitem>
  256. </itemizedlist>
  257. <para>
  258. Additionally, each of the above assertions have a 'Not' variant for
  259. negative assertions.
  260. </para>
  261. </sect3>
  262. <sect3 id="zend.test.phpunit.assertions.request">
  263. <title>Request Assertions</title>
  264. <para>
  265. It's often useful to assert against the last run action,
  266. controller, and module; additionally, you may want to assert
  267. against the route that was matched. The following assertions can
  268. help you in this regard:
  269. </para>
  270. <itemizedlist>
  271. <listitem>
  272. <para>
  273. <methodname>assertModule($module, $message = '')</methodname>: Assert that
  274. the given module was used in the last dispatched action.
  275. </para>
  276. </listitem>
  277. <listitem>
  278. <para>
  279. <methodname>assertController($controller, $message = '')</methodname>:
  280. Assert that the given controller was selected in the last
  281. dispatched action.
  282. </para>
  283. </listitem>
  284. <listitem>
  285. <para>
  286. <methodname>assertAction($action, $message = '')</methodname>: Assert that
  287. the given action was last dispatched.
  288. </para>
  289. </listitem>
  290. <listitem>
  291. <para>
  292. <methodname>assertRoute($route, $message = '')</methodname>: Assert that
  293. the given named route was matched by the router.
  294. </para>
  295. </listitem>
  296. </itemizedlist>
  297. <para>
  298. Each also has a 'Not' variant for negative assertions.
  299. </para>
  300. </sect3>
  301. </sect2>
  302. <!--
  303. vim:se ts=4 sw=4 et:
  304. -->