PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/application/libraries/Zend/Test/PHPUnit/ControllerTestCase.php

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