ControllerTestCase.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Test
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** @see PHPUnit_Framework_TestCase */
  22. require_once 'PHPUnit/Framework/TestCase.php';
  23. /** @see PHPUnit_Runner_Version */
  24. require_once 'PHPUnit/Runner/Version.php';
  25. /** @see Zend_Controller_Front */
  26. require_once 'Zend/Controller/Front.php';
  27. /** @see Zend_Controller_Action_HelperBroker */
  28. require_once 'Zend/Controller/Action/HelperBroker.php';
  29. /** @see Zend_Layout */
  30. require_once 'Zend/Layout.php';
  31. /** @see Zend_Session */
  32. require_once 'Zend/Session.php';
  33. /** @see Zend_Registry */
  34. require_once 'Zend/Registry.php';
  35. /**
  36. * Functional testing scaffold for MVC applications
  37. *
  38. * @uses PHPUnit_Framework_TestCase
  39. * @category Zend
  40. * @package Zend_Test
  41. * @subpackage PHPUnit
  42. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * @var mixed Bootstrap file path or callback
  49. */
  50. public $bootstrap;
  51. /**
  52. * @var Zend_Controller_Front
  53. */
  54. protected $_frontController;
  55. /**
  56. * @var Zend_Dom_Query
  57. */
  58. protected $_query;
  59. /**
  60. * @var Zend_Controller_Request_Abstract
  61. */
  62. protected $_request;
  63. /**
  64. * @var Zend_Controller_Response_Abstract
  65. */
  66. protected $_response;
  67. /**
  68. * XPath namespaces
  69. * @var array
  70. */
  71. protected $_xpathNamespaces = array();
  72. /**
  73. * Overloading: prevent overloading to special properties
  74. *
  75. * @param string $name
  76. * @param mixed $value
  77. * @return void
  78. */
  79. public function __set($name, $value)
  80. {
  81. if (in_array($name, array('request', 'response', 'frontController'))) {
  82. require_once 'Zend/Exception.php';
  83. throw new Zend_Exception(sprintf('Setting %s object manually is not allowed', $name));
  84. }
  85. $this->$name = $value;
  86. }
  87. /**
  88. * Overloading for common properties
  89. *
  90. * Provides overloading for request, response, and frontController objects.
  91. *
  92. * @param mixed $name
  93. * @return void
  94. */
  95. public function __get($name)
  96. {
  97. switch ($name) {
  98. case 'request':
  99. return $this->getRequest();
  100. case 'response':
  101. return $this->getResponse();
  102. case 'frontController':
  103. return $this->getFrontController();
  104. }
  105. return null;
  106. }
  107. /**
  108. * Set up MVC app
  109. *
  110. * Calls {@link bootstrap()} by default
  111. *
  112. * @return void
  113. */
  114. protected function setUp()
  115. {
  116. $this->bootstrap();
  117. }
  118. /**
  119. * Bootstrap the front controller
  120. *
  121. * Resets the front controller, and then bootstraps it.
  122. *
  123. * If {@link $bootstrap} is a callback, executes it; if it is a file, it include's
  124. * it. When done, sets the test case request and response objects into the
  125. * front controller.
  126. *
  127. * @return void
  128. */
  129. final public function bootstrap()
  130. {
  131. $this->reset();
  132. if (null !== $this->bootstrap) {
  133. if ($this->bootstrap instanceof Zend_Application) {
  134. $this->bootstrap->bootstrap();
  135. $this->_frontController = $this->bootstrap->getBootstrap()->getResource('frontcontroller');
  136. } elseif (is_callable($this->bootstrap)) {
  137. call_user_func($this->bootstrap);
  138. } elseif (is_string($this->bootstrap)) {
  139. require_once 'Zend/Loader.php';
  140. if (Zend_Loader::isReadable($this->bootstrap)) {
  141. include $this->bootstrap;
  142. }
  143. }
  144. }
  145. $this->frontController
  146. ->setRequest($this->getRequest())
  147. ->setResponse($this->getResponse());
  148. }
  149. /**
  150. * Dispatch the MVC
  151. *
  152. * If a URL is provided, sets it as the request URI in the request object.
  153. * Then sets test case request and response objects in front controller,
  154. * disables throwing exceptions, and disables returning the response.
  155. * Finally, dispatches the front controller.
  156. *
  157. * @param string|null $url
  158. * @return void
  159. */
  160. public function dispatch($url = null)
  161. {
  162. // redirector should not exit
  163. $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
  164. $redirector->setExit(false);
  165. // json helper should not exit
  166. $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
  167. $json->suppressExit = true;
  168. $request = $this->getRequest();
  169. if (null !== $url) {
  170. $request->setRequestUri($url);
  171. }
  172. $request->setPathInfo(null);
  173. $controller = $this->getFrontController();
  174. $this->frontController
  175. ->setRequest($request)
  176. ->setResponse($this->getResponse())
  177. ->throwExceptions(false)
  178. ->returnResponse(false);
  179. if ($this->bootstrap instanceof Zend_Application) {
  180. $this->bootstrap->run();
  181. } else {
  182. $this->frontController->dispatch();
  183. }
  184. }
  185. /**
  186. * Reset MVC state
  187. *
  188. * Creates new request/response objects, resets the front controller
  189. * instance, and resets the action helper broker.
  190. *
  191. * @todo Need to update Zend_Layout to add a resetInstance() method
  192. * @return void
  193. */
  194. public function reset()
  195. {
  196. $_SESSION = array();
  197. $_GET = array();
  198. $_POST = array();
  199. $_COOKIE = array();
  200. $this->resetRequest();
  201. $this->resetResponse();
  202. Zend_Layout::resetMvcInstance();
  203. Zend_Controller_Action_HelperBroker::resetHelpers();
  204. $this->frontController->resetInstance();
  205. Zend_Session::$_unitTestEnabled = true;
  206. }
  207. /**
  208. * Rest all view placeholders
  209. *
  210. * @return void
  211. */
  212. protected function _resetPlaceholders()
  213. {
  214. $registry = Zend_Registry::getInstance();
  215. $remove = array();
  216. foreach ($registry as $key => $value) {
  217. if (strstr($key, '_View_')) {
  218. $remove[] = $key;
  219. }
  220. }
  221. foreach ($remove as $key) {
  222. unset($registry[$key]);
  223. }
  224. }
  225. /**
  226. * Reset the request object
  227. *
  228. * Useful for test cases that need to test multiple trips to the server.
  229. *
  230. * @return Zend_Test_PHPUnit_ControllerTestCase
  231. */
  232. public function resetRequest()
  233. {
  234. if ($this->_request instanceof Zend_Controller_Request_HttpTestCase) {
  235. $this->_request->clearQuery()
  236. ->clearPost();
  237. }
  238. $this->_request = null;
  239. return $this;
  240. }
  241. /**
  242. * Reset the response object
  243. *
  244. * Useful for test cases that need to test multiple trips to the server.
  245. *
  246. * @return Zend_Test_PHPUnit_ControllerTestCase
  247. */
  248. public function resetResponse()
  249. {
  250. $this->_response = null;
  251. $this->_resetPlaceholders();
  252. return $this;
  253. }
  254. /**
  255. * Assert against DOM selection
  256. *
  257. * @param string $path CSS selector path
  258. * @param string $message
  259. * @return void
  260. */
  261. public function assertQuery($path, $message = '')
  262. {
  263. $this->_incrementAssertionCount();
  264. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  265. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  266. $content = $this->response->outputBody();
  267. if (!$constraint->evaluate($content, __FUNCTION__)) {
  268. $constraint->fail($path, $message);
  269. }
  270. }
  271. /**
  272. * Assert against DOM selection
  273. *
  274. * @param string $path CSS selector path
  275. * @param string $message
  276. * @return void
  277. */
  278. public function assertNotQuery($path, $message = '')
  279. {
  280. $this->_incrementAssertionCount();
  281. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  282. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  283. $content = $this->response->outputBody();
  284. if (!$constraint->evaluate($content, __FUNCTION__)) {
  285. $constraint->fail($path, $message);
  286. }
  287. }
  288. /**
  289. * Assert against DOM selection; node should contain content
  290. *
  291. * @param string $path CSS selector path
  292. * @param string $match content that should be contained in matched nodes
  293. * @param string $message
  294. * @return void
  295. */
  296. public function assertQueryContentContains($path, $match, $message = '')
  297. {
  298. $this->_incrementAssertionCount();
  299. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  300. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  301. $content = $this->response->outputBody();
  302. if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
  303. $constraint->fail($path, $message);
  304. }
  305. }
  306. /**
  307. * Assert against DOM selection; node should NOT contain content
  308. *
  309. * @param string $path CSS selector path
  310. * @param string $match content that should NOT be contained in matched nodes
  311. * @param string $message
  312. * @return void
  313. */
  314. public function assertNotQueryContentContains($path, $match, $message = '')
  315. {
  316. $this->_incrementAssertionCount();
  317. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  318. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  319. $content = $this->response->outputBody();
  320. if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
  321. $constraint->fail($path, $message);
  322. }
  323. }
  324. /**
  325. * Assert against DOM selection; node should match content
  326. *
  327. * @param string $path CSS selector path
  328. * @param string $pattern Pattern that should be contained in matched nodes
  329. * @param string $message
  330. * @return void
  331. */
  332. public function assertQueryContentRegex($path, $pattern, $message = '')
  333. {
  334. $this->_incrementAssertionCount();
  335. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  336. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  337. $content = $this->response->outputBody();
  338. if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
  339. $constraint->fail($path, $message);
  340. }
  341. }
  342. /**
  343. * Assert against DOM selection; node should NOT match content
  344. *
  345. * @param string $path CSS selector path
  346. * @param string $pattern pattern that should NOT be contained in matched nodes
  347. * @param string $message
  348. * @return void
  349. */
  350. public function assertNotQueryContentRegex($path, $pattern, $message = '')
  351. {
  352. $this->_incrementAssertionCount();
  353. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  354. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  355. $content = $this->response->outputBody();
  356. if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
  357. $constraint->fail($path, $message);
  358. }
  359. }
  360. /**
  361. * Assert against DOM selection; should contain exact number of nodes
  362. *
  363. * @param string $path CSS selector path
  364. * @param string $count Number of nodes that should match
  365. * @param string $message
  366. * @return void
  367. */
  368. public function assertQueryCount($path, $count, $message = '')
  369. {
  370. $this->_incrementAssertionCount();
  371. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  372. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  373. $content = $this->response->outputBody();
  374. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  375. $constraint->fail($path, $message);
  376. }
  377. }
  378. /**
  379. * Assert against DOM selection; should NOT contain exact number of nodes
  380. *
  381. * @param string $path CSS selector path
  382. * @param string $count Number of nodes that should NOT match
  383. * @param string $message
  384. * @return void
  385. */
  386. public function assertNotQueryCount($path, $count, $message = '')
  387. {
  388. $this->_incrementAssertionCount();
  389. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  390. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  391. $content = $this->response->outputBody();
  392. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  393. $constraint->fail($path, $message);
  394. }
  395. }
  396. /**
  397. * Assert against DOM selection; should contain at least this number of nodes
  398. *
  399. * @param string $path CSS selector path
  400. * @param string $count Minimum number of nodes that should match
  401. * @param string $message
  402. * @return void
  403. */
  404. public function assertQueryCountMin($path, $count, $message = '')
  405. {
  406. $this->_incrementAssertionCount();
  407. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  408. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  409. $content = $this->response->outputBody();
  410. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  411. $constraint->fail($path, $message);
  412. }
  413. }
  414. /**
  415. * Assert against DOM selection; should contain no more than this number of nodes
  416. *
  417. * @param string $path CSS selector path
  418. * @param string $count Maximum number of nodes that should match
  419. * @param string $message
  420. * @return void
  421. */
  422. public function assertQueryCountMax($path, $count, $message = '')
  423. {
  424. $this->_incrementAssertionCount();
  425. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  426. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  427. $content = $this->response->outputBody();
  428. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  429. $constraint->fail($path, $message);
  430. }
  431. }
  432. /**
  433. * Register XPath namespaces
  434. *
  435. * @param array $xpathNamespaces
  436. * @return void
  437. */
  438. public function registerXpathNamespaces($xpathNamespaces)
  439. {
  440. $this->_xpathNamespaces = $xpathNamespaces;
  441. }
  442. /**
  443. * Assert against XPath selection
  444. *
  445. * @param string $path XPath path
  446. * @param string $message
  447. * @return void
  448. */
  449. public function assertXpath($path, $message = '')
  450. {
  451. $this->_incrementAssertionCount();
  452. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  453. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  454. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  455. $content = $this->response->outputBody();
  456. if (!$constraint->evaluate($content, __FUNCTION__)) {
  457. $constraint->fail($path, $message);
  458. }
  459. }
  460. /**
  461. * Assert against XPath selection
  462. *
  463. * @param string $path XPath path
  464. * @param string $message
  465. * @return void
  466. */
  467. public function assertNotXpath($path, $message = '')
  468. {
  469. $this->_incrementAssertionCount();
  470. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  471. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  472. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  473. $content = $this->response->outputBody();
  474. if (!$constraint->evaluate($content, __FUNCTION__)) {
  475. $constraint->fail($path, $message);
  476. }
  477. }
  478. /**
  479. * Assert against XPath selection; node should contain content
  480. *
  481. * @param string $path XPath path
  482. * @param string $match content that should be contained in matched nodes
  483. * @param string $message
  484. * @return void
  485. */
  486. public function assertXpathContentContains($path, $match, $message = '')
  487. {
  488. $this->_incrementAssertionCount();
  489. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  490. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  491. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  492. $content = $this->response->outputBody();
  493. if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
  494. $constraint->fail($path, $message);
  495. }
  496. }
  497. /**
  498. * Assert against XPath selection; node should NOT contain content
  499. *
  500. * @param string $path XPath path
  501. * @param string $match content that should NOT be contained in matched nodes
  502. * @param string $message
  503. * @return void
  504. */
  505. public function assertNotXpathContentContains($path, $match, $message = '')
  506. {
  507. $this->_incrementAssertionCount();
  508. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  509. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  510. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  511. $content = $this->response->outputBody();
  512. if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
  513. $constraint->fail($path, $message);
  514. }
  515. }
  516. /**
  517. * Assert against XPath selection; node should match content
  518. *
  519. * @param string $path XPath path
  520. * @param string $pattern Pattern that should be contained in matched nodes
  521. * @param string $message
  522. * @return void
  523. */
  524. public function assertXpathContentRegex($path, $pattern, $message = '')
  525. {
  526. $this->_incrementAssertionCount();
  527. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  528. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  529. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  530. $content = $this->response->outputBody();
  531. if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
  532. $constraint->fail($path, $message);
  533. }
  534. }
  535. /**
  536. * Assert against XPath selection; node should NOT match content
  537. *
  538. * @param string $path XPath path
  539. * @param string $pattern pattern that should NOT be contained in matched nodes
  540. * @param string $message
  541. * @return void
  542. */
  543. public function assertNotXpathContentRegex($path, $pattern, $message = '')
  544. {
  545. $this->_incrementAssertionCount();
  546. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  547. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  548. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  549. $content = $this->response->outputBody();
  550. if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
  551. $constraint->fail($path, $message);
  552. }
  553. }
  554. /**
  555. * Assert against XPath selection; should contain exact number of nodes
  556. *
  557. * @param string $path XPath path
  558. * @param string $count Number of nodes that should match
  559. * @param string $message
  560. * @return void
  561. */
  562. public function assertXpathCount($path, $count, $message = '')
  563. {
  564. $this->_incrementAssertionCount();
  565. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  566. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  567. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  568. $content = $this->response->outputBody();
  569. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  570. $constraint->fail($path, $message);
  571. }
  572. }
  573. /**
  574. * Assert against XPath selection; should NOT contain exact number of nodes
  575. *
  576. * @param string $path XPath path
  577. * @param string $count Number of nodes that should NOT match
  578. * @param string $message
  579. * @return void
  580. */
  581. public function assertNotXpathCount($path, $count, $message = '')
  582. {
  583. $this->_incrementAssertionCount();
  584. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  585. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  586. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  587. $content = $this->response->outputBody();
  588. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  589. $constraint->fail($path, $message);
  590. }
  591. }
  592. /**
  593. * Assert against XPath selection; should contain at least this number of nodes
  594. *
  595. * @param string $path XPath path
  596. * @param string $count Minimum number of nodes that should match
  597. * @param string $message
  598. * @return void
  599. */
  600. public function assertXpathCountMin($path, $count, $message = '')
  601. {
  602. $this->_incrementAssertionCount();
  603. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  604. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  605. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  606. $content = $this->response->outputBody();
  607. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  608. $constraint->fail($path, $message);
  609. }
  610. }
  611. /**
  612. * Assert against XPath selection; should contain no more than this number of nodes
  613. *
  614. * @param string $path XPath path
  615. * @param string $count Maximum number of nodes that should match
  616. * @param string $message
  617. * @return void
  618. */
  619. public function assertXpathCountMax($path, $count, $message = '')
  620. {
  621. $this->_incrementAssertionCount();
  622. require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
  623. $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
  624. $constraint->registerXpathNamespaces($this->_xpathNamespaces);
  625. $content = $this->response->outputBody();
  626. if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
  627. $constraint->fail($path, $message);
  628. }
  629. }
  630. /**
  631. * Assert that response is a redirect
  632. *
  633. * @param string $message
  634. * @return void
  635. */
  636. public function assertRedirect($message = '')
  637. {
  638. $this->_incrementAssertionCount();
  639. require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
  640. $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
  641. $response = $this->response;
  642. if (!$constraint->evaluate($response, __FUNCTION__)) {
  643. $constraint->fail($response, $message);
  644. }
  645. }
  646. /**
  647. * Assert that response is NOT a redirect
  648. *
  649. * @param string $message
  650. * @return void
  651. */
  652. public function assertNotRedirect($message = '')
  653. {
  654. $this->_incrementAssertionCount();
  655. require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
  656. $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
  657. $response = $this->response;
  658. if (!$constraint->evaluate($response, __FUNCTION__)) {
  659. $constraint->fail($response, $message);
  660. }
  661. }
  662. /**
  663. * Assert that response redirects to given URL
  664. *
  665. * @param string $url
  666. * @param string $message
  667. * @return void
  668. */
  669. public function assertRedirectTo($url, $message = '')
  670. {
  671. $this->_incrementAssertionCount();
  672. require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
  673. $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
  674. $response = $this->response;
  675. if (!$constraint->evaluate($response, __FUNCTION__, $url)) {
  676. $constraint->fail($response, $message);
  677. }
  678. }
  679. /**
  680. * Assert that response does not redirect to given URL
  681. *
  682. * @param string $url
  683. * @param string $message
  684. * @return void
  685. */
  686. public function assertNotRedirectTo($url, $message = '')
  687. {
  688. $this->_incrementAssertionCount();
  689. require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
  690. $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
  691. $response = $this->response;
  692. if (!$constraint->evaluate($response, __FUNCTION__, $url)) {
  693. $constraint->fail($response, $message);
  694. }
  695. }
  696. /**
  697. * Assert that redirect location matches pattern
  698. *
  699. * @param string $pattern
  700. * @param string $message
  701. * @return void
  702. */
  703. public function assertRedirectRegex($pattern, $message = '')
  704. {
  705. $this->_incrementAssertionCount();
  706. require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
  707. $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
  708. $response = $this->response;
  709. if (!$constraint->evaluate($response, __FUNCTION__, $pattern)) {
  710. $constraint->fail($response, $message);
  711. }
  712. }
  713. /**
  714. * Assert that redirect location does not match pattern
  715. *
  716. * @param string $pattern
  717. * @param string $message
  718. * @return void
  719. */
  720. public function assertNotRedirectRegex($pattern, $message = '')
  721. {
  722. $this->_incrementAssertionCount();
  723. require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
  724. $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
  725. $response = $this->response;
  726. if (!$constraint->evaluate($response, __FUNCTION__, $pattern)) {
  727. $constraint->fail($response, $message);
  728. }
  729. }
  730. /**
  731. * Assert response code
  732. *
  733. * @param int $code
  734. * @param string $message
  735. * @return void
  736. */
  737. public function assertResponseCode($code, $message = '')
  738. {
  739. $this->_incrementAssertionCount();
  740. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  741. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  742. $response = $this->response;
  743. if (!$constraint->evaluate($response, __FUNCTION__, $code)) {
  744. $constraint->fail($response, $message);
  745. }
  746. }
  747. /**
  748. * Assert response code
  749. *
  750. * @param int $code
  751. * @param string $message
  752. * @return void
  753. */
  754. public function assertNotResponseCode($code, $message = '')
  755. {
  756. $this->_incrementAssertionCount();
  757. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  758. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  759. $constraint->setNegate(true);
  760. $response = $this->response;
  761. if (!$constraint->evaluate($response, __FUNCTION__, $code)) {
  762. $constraint->fail($response, $message);
  763. }
  764. }
  765. /**
  766. * Assert response header exists
  767. *
  768. * @param string $header
  769. * @param string $message
  770. * @return void
  771. */
  772. public function assertHeader($header, $message = '')
  773. {
  774. $this->_incrementAssertionCount();
  775. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  776. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  777. $response = $this->response;
  778. if (!$constraint->evaluate($response, __FUNCTION__, $header)) {
  779. $constraint->fail($response, $message);
  780. }
  781. }
  782. /**
  783. * Assert response header does not exist
  784. *
  785. * @param string $header
  786. * @param string $message
  787. * @return void
  788. */
  789. public function assertNotHeader($header, $message = '')
  790. {
  791. $this->_incrementAssertionCount();
  792. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  793. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  794. $constraint->setNegate(true);
  795. $response = $this->response;
  796. if (!$constraint->evaluate($response, __FUNCTION__, $header)) {
  797. $constraint->fail($response, $message);
  798. }
  799. }
  800. /**
  801. * Assert response header exists and contains the given string
  802. *
  803. * @param string $header
  804. * @param string $match
  805. * @param string $message
  806. * @return void
  807. */
  808. public function assertHeaderContains($header, $match, $message = '')
  809. {
  810. $this->_incrementAssertionCount();
  811. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  812. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  813. $response = $this->response;
  814. if (!$constraint->evaluate($response, __FUNCTION__, $header, $match)) {
  815. $constraint->fail($response, $message);
  816. }
  817. }
  818. /**
  819. * Assert response header does not exist and/or does not contain the given string
  820. *
  821. * @param string $header
  822. * @param string $match
  823. * @param string $message
  824. * @return void
  825. */
  826. public function assertNotHeaderContains($header, $match, $message = '')
  827. {
  828. $this->_incrementAssertionCount();
  829. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  830. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  831. $constraint->setNegate(true);
  832. $response = $this->response;
  833. if (!$constraint->evaluate($response, __FUNCTION__, $header, $match)) {
  834. $constraint->fail($response, $message);
  835. }
  836. }
  837. /**
  838. * Assert response header exists and matches the given pattern
  839. *
  840. * @param string $header
  841. * @param string $pattern
  842. * @param string $message
  843. * @return void
  844. */
  845. public function assertHeaderRegex($header, $pattern, $message = '')
  846. {
  847. $this->_incrementAssertionCount();
  848. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  849. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  850. $response = $this->response;
  851. if (!$constraint->evaluate($response, __FUNCTION__, $header, $pattern)) {
  852. $constraint->fail($response, $message);
  853. }
  854. }
  855. /**
  856. * Assert response header does not exist and/or does not match the given regex
  857. *
  858. * @param string $header
  859. * @param string $pattern
  860. * @param string $message
  861. * @return void
  862. */
  863. public function assertNotHeaderRegex($header, $pattern, $message = '')
  864. {
  865. $this->_incrementAssertionCount();
  866. require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
  867. $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
  868. $constraint->setNegate(true);
  869. $response = $this->response;
  870. if (!$constraint->evaluate($response, __FUNCTION__, $header, $pattern)) {
  871. $constraint->fail($response, $message);
  872. }
  873. }
  874. /**
  875. * Assert that the last handled request used the given module
  876. *
  877. * @param string $module
  878. * @param string $message
  879. * @return void
  880. */
  881. public function assertModule($module, $message = '')
  882. {
  883. $this->_incrementAssertionCount();
  884. if ($module != $this->request->getModuleName()) {
  885. $msg = sprintf('Failed asserting last module used <"%s"> was "%s"',
  886. $this->request->getModuleName(),
  887. $module
  888. );
  889. if (!empty($message)) {
  890. $msg = $message . "\n" . $msg;
  891. }
  892. $this->fail($msg);
  893. }
  894. }
  895. /**
  896. * Assert that the last handled request did NOT use the given module
  897. *
  898. * @param string $module
  899. * @param string $message
  900. * @return void
  901. */
  902. public function assertNotModule($module, $message = '')
  903. {
  904. $this->_incrementAssertionCount();
  905. if ($module == $this->request->getModuleName()) {
  906. $msg = sprintf('Failed asserting last module used was NOT "%s"', $module);
  907. if (!empty($message)) {
  908. $msg = $message . "\n" . $msg;
  909. }
  910. $this->fail($msg);
  911. }
  912. }
  913. /**
  914. * Assert that the last handled request used the given controller
  915. *
  916. * @param string $controller
  917. * @param string $message
  918. * @return void
  919. */
  920. public function assertController($controller, $message = '')
  921. {
  922. $this->_incrementAssertionCount();
  923. if ($controller != $this->request->getControllerName()) {
  924. $msg = sprintf('Failed asserting last controller used <"%s"> was "%s"',
  925. $this->request->getControllerName(),
  926. $controller
  927. );
  928. if (!empty($message)) {
  929. $msg = $message . "\n" . $msg;
  930. }
  931. $this->fail($msg);
  932. }
  933. }
  934. /**
  935. * Assert that the last handled request did NOT use the given controller
  936. *
  937. * @param string $controller
  938. * @param string $message
  939. * @return void
  940. */
  941. public function assertNotController($controller, $message = '')
  942. {
  943. $this->_incrementAssertionCount();
  944. if ($controller == $this->request->getControllerName()) {
  945. $msg = sprintf('Failed asserting last controller used <"%s"> was NOT "%s"',
  946. $this->request->getControllerName(),
  947. $controller
  948. );
  949. if (!empty($message)) {
  950. $msg = $message . "\n" . $msg;
  951. }
  952. $this->fail($msg);
  953. }
  954. }
  955. /**
  956. * Assert that the last handled request used the given action
  957. *
  958. * @param string $action
  959. * @param string $message
  960. * @return void
  961. */
  962. public function assertAction($action, $message = '')
  963. {
  964. $this->_incrementAssertionCount();
  965. if ($action != $this->request->getActionName()) {
  966. $msg = sprintf('Failed asserting last action used <"%s"> was "%s"', $this->request->getActionName(), $action);
  967. if (!empty($message)) {
  968. $msg = $message . "\n" . $msg;
  969. }
  970. $this->fail($msg);
  971. }
  972. }
  973. /**
  974. * Assert that the last handled request did NOT use the given action
  975. *
  976. * @param string $action
  977. * @param string $message
  978. * @return void
  979. */
  980. public function assertNotAction($action, $message = '')
  981. {
  982. $this->_incrementAssertionCount();
  983. if ($action == $this->request->getActionName()) {
  984. $msg = sprintf('Failed asserting last action used <"%s"> was NOT "%s"', $this->request->getActionName(), $action);
  985. if (!empty($message)) {
  986. $msg = $message . "\n" . $msg;
  987. }
  988. $this->fail($msg);
  989. }
  990. }
  991. /**
  992. * Assert that the specified route was used
  993. *
  994. * @param string $route
  995. * @param string $message
  996. * @return void
  997. */
  998. public function assertRoute($route, $message = '')
  999. {
  1000. $this->_incrementAssertionCount();
  1001. $router = $this->frontController->getRouter();
  1002. if ($route != $router->getCurrentRouteName()) {
  1003. $msg = sprintf('Failed asserting matched route was "%s", actual route is %s',
  1004. $route,
  1005. $router->getCurrentRouteName()
  1006. );
  1007. if (!empty($message)) {
  1008. $msg = $message . "\n" . $msg;
  1009. }
  1010. $this->fail($msg);
  1011. }
  1012. }
  1013. /**
  1014. * Assert that the route matched is NOT as specified
  1015. *
  1016. * @param string $route
  1017. * @param string $message
  1018. * @return void
  1019. */
  1020. public function assertNotRoute($route, $message = '')
  1021. {
  1022. $this->_incrementAssertionCount();
  1023. $router = $this->frontController->getRouter();
  1024. if ($route == $router->getCurrentRouteName()) {
  1025. $msg = sprintf('Failed asserting route matched was NOT "%s"', $route);
  1026. if (!empty($message)) {
  1027. $msg = $message . "\n" . $msg;
  1028. }
  1029. $this->fail($msg);
  1030. }
  1031. }
  1032. /**
  1033. * Retrieve front controller instance
  1034. *
  1035. * @return Zend_Controller_Front
  1036. */
  1037. public function getFrontController()
  1038. {
  1039. if (null === $this->_frontController) {
  1040. $this->_frontController = Zend_Controller_Front::getInstance();
  1041. }
  1042. return $this->_frontController;
  1043. }
  1044. /**
  1045. * Retrieve test case request object
  1046. *
  1047. * @return Zend_Controller_Request_Abstract
  1048. */
  1049. public function getRequest()
  1050. {
  1051. if (null === $this->_request) {
  1052. require_once 'Zend/Controller/Request/HttpTestCase.php';
  1053. $this->_request = new Zend_Controller_Request_HttpTestCase;
  1054. }
  1055. return $this->_request;
  1056. }
  1057. /**
  1058. * Retrieve test case response object
  1059. *
  1060. * @return Zend_Controller_Response_Abstract
  1061. */
  1062. public function getResponse()
  1063. {
  1064. if (null === $this->_response) {
  1065. require_once 'Zend/Controller/Response/HttpTestCase.php';
  1066. $this->_response = new Zend_Controller_Response_HttpTestCase;
  1067. }
  1068. return $this->_response;
  1069. }
  1070. /**
  1071. * Retrieve DOM query object
  1072. *
  1073. * @return Zend_Dom_Query
  1074. */
  1075. public function getQuery()
  1076. {
  1077. if (null === $this->_query) {
  1078. require_once 'Zend/Dom/Query.php';
  1079. $this->_query = new Zend_Dom_Query;
  1080. }
  1081. return $this->_query;
  1082. }
  1083. /**
  1084. * Increment assertion count
  1085. *
  1086. * @return void
  1087. */
  1088. protected function _incrementAssertionCount()
  1089. {
  1090. $stack = debug_backtrace();
  1091. foreach (debug_backtrace() as $step) {
  1092. if (isset($step['object'])
  1093. && $step['object'] instanceof PHPUnit_Framework_TestCase
  1094. ) {
  1095. if (version_compare(PHPUnit_Runner_Version::id(), '3.3.0', 'lt')) {
  1096. break;
  1097. } elseif (version_compare(PHPUnit_Runner_Version::id(), '3.3.3', 'lt')) {
  1098. $step['object']->incrementAssertionCounter();
  1099. } else {
  1100. $step['object']->addToAssertionCount(1);
  1101. }
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. }