PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Test/PHPUnit/ControllerTestCase.php

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