ControllerTestCase.php 35 KB

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