2
0

ControllerTestCase.php 36 KB

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