Zend_Test-PHPUnit-Assertions.xml 14 KB

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