2
0

ControllerTestCase.php 35 KB

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