PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tine20/library/Zend/Test/PHPUnit/ControllerTestCase.php

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