PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/www/system/library/Zend/Test/PHPUnit/ControllerTestCase.php

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