PageRenderTime 132ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Test/PHPUnit/ControllerTestCase.php

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