PageRenderTime 158ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/Test/PHPUnit/ControllerTestCase.php

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