2
0

ControllerTestCase.php 35 KB

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