ControllerTestCase.php 38 KB

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