ControllerTestCase.php 36 KB

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