ControllerTestCaseTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Test_PHPUnit_ControllerTestCaseTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Test_PHPUnit_ControllerTestCaseTest::main");
  25. }
  26. /** Zend_Test_PHPUnit_ControllerTestCase */
  27. require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
  28. /** Zend_Registry */
  29. require_once 'Zend/Registry.php';
  30. /** Zend_Session */
  31. require_once 'Zend/Session.php';
  32. /** Zend_Controller_Action */
  33. require_once 'Zend/Controller/Action.php';
  34. /**
  35. * Test class for Zend_Test_PHPUnit_ControllerTestCase.
  36. *
  37. * @category Zend
  38. * @package Zend_Test
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Test
  43. * @group Zend_Test_PHPUnit
  44. */
  45. class Zend_Test_PHPUnit_ControllerTestCaseTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Test_PHPUnit_ControllerTestCaseTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. /**
  58. * Sets up the fixture, for example, open a network connection.
  59. * This method is called before a test is executed.
  60. *
  61. * @return void
  62. */
  63. public function setUp()
  64. {
  65. $_SESSION = array();
  66. $this->setExpectedException(null);
  67. $this->testCase = new Zend_Test_PHPUnit_ControllerTestCaseTest_Concrete();
  68. $this->testCase->reset();
  69. $this->testCase->bootstrap = array($this, 'bootstrap');
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. $registry = Zend_Registry::getInstance();
  80. if (isset($registry['router'])) {
  81. unset($registry['router']);
  82. }
  83. if (isset($registry['dispatcher'])) {
  84. unset($registry['dispatcher']);
  85. }
  86. if (isset($registry['plugin'])) {
  87. unset($registry['plugin']);
  88. }
  89. if (isset($registry['viewRenderer'])) {
  90. unset($registry['viewRenderer']);
  91. }
  92. Zend_Session::$_unitTestEnabled = false;
  93. session_id(uniqid());
  94. }
  95. public function bootstrap()
  96. {
  97. }
  98. public function testGetFrontControllerShouldReturnFrontController()
  99. {
  100. $controller = $this->testCase->getFrontController();
  101. $this->assertTrue($controller instanceof Zend_Controller_Front);
  102. }
  103. public function testGetFrontControllerShouldReturnSameFrontControllerObjectOnRepeatedCalls()
  104. {
  105. $controller = $this->testCase->getFrontController();
  106. $this->assertTrue($controller instanceof Zend_Controller_Front);
  107. $test = $this->testCase->getFrontController();
  108. $this->assertSame($controller, $test);
  109. }
  110. public function testGetRequestShouldReturnRequestTestCase()
  111. {
  112. $request = $this->testCase->getRequest();
  113. $this->assertTrue($request instanceof Zend_Controller_Request_HttpTestCase);
  114. }
  115. public function testGetRequestShouldReturnSameRequestObjectOnRepeatedCalls()
  116. {
  117. $request = $this->testCase->getRequest();
  118. $this->assertTrue($request instanceof Zend_Controller_Request_HttpTestCase);
  119. $test = $this->testCase->getRequest();
  120. $this->assertSame($request, $test);
  121. }
  122. public function testGetResponseShouldReturnResponseTestCase()
  123. {
  124. $response = $this->testCase->getResponse();
  125. $this->assertTrue($response instanceof Zend_Controller_Response_HttpTestCase);
  126. }
  127. public function testGetResponseShouldReturnSameResponseObjectOnRepeatedCalls()
  128. {
  129. $response = $this->testCase->getResponse();
  130. $this->assertTrue($response instanceof Zend_Controller_Response_HttpTestCase);
  131. $test = $this->testCase->getResponse();
  132. $this->assertSame($response, $test);
  133. }
  134. public function testGetQueryShouldReturnQueryTestCase()
  135. {
  136. $query = $this->testCase->getQuery();
  137. $this->assertTrue($query instanceof Zend_Dom_Query);
  138. }
  139. public function testGetQueryShouldReturnSameQueryObjectOnRepeatedCalls()
  140. {
  141. $query = $this->testCase->getQuery();
  142. $this->assertTrue($query instanceof Zend_Dom_Query);
  143. $test = $this->testCase->getQuery();
  144. $this->assertSame($query, $test);
  145. }
  146. public function testOverloadingShouldReturnRequestResponseAndFrontControllerObjects()
  147. {
  148. $request = $this->testCase->getRequest();
  149. $response = $this->testCase->getResponse();
  150. $frontController = $this->testCase->getFrontController();
  151. $this->assertSame($request, $this->testCase->request);
  152. $this->assertSame($response, $this->testCase->response);
  153. $this->assertSame($frontController, $this->testCase->frontController);
  154. }
  155. public function testOverloadingShouldPreventSettingRequestResponseAndFrontControllerObjects()
  156. {
  157. try {
  158. $this->testCase->request = new Zend_Controller_Request_Http();
  159. $this->fail('Setting request object as public property should raise exception');
  160. } catch (Exception $e) {
  161. $this->assertContains('not allow', $e->getMessage());
  162. }
  163. try {
  164. $this->testCase->response = new Zend_Controller_Response_Http();
  165. $this->fail('Setting response object as public property should raise exception');
  166. } catch (Exception $e) {
  167. $this->assertContains('not allow', $e->getMessage());
  168. }
  169. try {
  170. $this->testCase->frontController = Zend_Controller_Front::getInstance();
  171. $this->fail('Setting front controller as public property should raise exception');
  172. } catch (Exception $e) {
  173. $this->assertContains('not allow', $e->getMessage());
  174. }
  175. }
  176. public function testResetShouldResetMvcState()
  177. {
  178. require_once 'Zend/Controller/Action/HelperBroker.php';
  179. require_once 'Zend/Controller/Dispatcher/Standard.php';
  180. require_once 'Zend/Controller/Plugin/ErrorHandler.php';
  181. require_once 'Zend/Controller/Router/Rewrite.php';
  182. $request = $this->testCase->getRequest();
  183. $response = $this->testCase->getResponse();
  184. $router = new Zend_Controller_Router_Rewrite();
  185. $dispatcher = new Zend_Controller_Dispatcher_Standard();
  186. $plugin = new Zend_Controller_Plugin_ErrorHandler();
  187. $controller = $this->testCase->getFrontController();
  188. $controller->setParam('foo', 'bar')
  189. ->registerPlugin($plugin)
  190. ->setRouter($router)
  191. ->setDispatcher($dispatcher);
  192. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  193. $this->testCase->reset();
  194. $test = $controller->getRouter();
  195. $this->assertNotSame($router, $test);
  196. $test = $controller->getDispatcher();
  197. $this->assertNotSame($dispatcher, $test);
  198. $this->assertFalse($controller->getPlugin('Zend_Controller_Plugin_ErrorHandler'));
  199. $test = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  200. $this->assertNotSame($viewRenderer, $test);
  201. $this->assertNull($controller->getRequest());
  202. $this->assertNull($controller->getResponse());
  203. $this->assertNotSame($request, $this->testCase->getRequest());
  204. $this->assertNotSame($response, $this->testCase->getResponse());
  205. }
  206. public function testBootstrapShouldSetRequestAndResponseTestCaseObjects()
  207. {
  208. $this->testCase->bootstrap();
  209. $controller = $this->testCase->getFrontController();
  210. $request = $controller->getRequest();
  211. $response = $controller->getResponse();
  212. $this->assertSame($this->testCase->getRequest(), $request);
  213. $this->assertSame($this->testCase->getResponse(), $response);
  214. }
  215. public function testBootstrapShouldIncludeBootstrapFileSpecifiedInPublicBootstrapProperty()
  216. {
  217. $this->testCase->bootstrap = dirname(__FILE__) . '/_files/bootstrap.php';
  218. $this->testCase->bootstrap();
  219. $controller = $this->testCase->getFrontController();
  220. $this->assertSame(Zend_Registry::get('router'), $controller->getRouter());
  221. $this->assertSame(Zend_Registry::get('dispatcher'), $controller->getDispatcher());
  222. $this->assertSame(Zend_Registry::get('plugin'), $controller->getPlugin('Zend_Controller_Plugin_ErrorHandler'));
  223. $this->assertSame(Zend_Registry::get('viewRenderer'), Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'));
  224. }
  225. public function testBootstrapShouldInvokeCallbackSpecifiedInPublicBootstrapProperty()
  226. {
  227. $this->testCase->bootstrap = array($this, 'bootstrapCallback');
  228. $this->testCase->bootstrap();
  229. $controller = $this->testCase->getFrontController();
  230. $this->assertSame(Zend_Registry::get('router'), $controller->getRouter());
  231. $this->assertSame(Zend_Registry::get('dispatcher'), $controller->getDispatcher());
  232. $this->assertSame(Zend_Registry::get('plugin'), $controller->getPlugin('Zend_Controller_Plugin_ErrorHandler'));
  233. $this->assertSame(Zend_Registry::get('viewRenderer'), Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'));
  234. }
  235. public function bootstrapCallback()
  236. {
  237. require_once 'Zend/Controller/Action/HelperBroker.php';
  238. require_once 'Zend/Controller/Dispatcher/Standard.php';
  239. require_once 'Zend/Controller/Front.php';
  240. require_once 'Zend/Controller/Plugin/ErrorHandler.php';
  241. require_once 'Zend/Controller/Router/Rewrite.php';
  242. require_once 'Zend/Registry.php';
  243. $router = new Zend_Controller_Router_Rewrite();
  244. $dispatcher = new Zend_Controller_Dispatcher_Standard();
  245. $plugin = new Zend_Controller_Plugin_ErrorHandler();
  246. $controller = Zend_Controller_Front::getInstance();
  247. $controller->setParam('foo', 'bar')
  248. ->registerPlugin($plugin)
  249. ->setRouter($router)
  250. ->setDispatcher($dispatcher);
  251. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  252. Zend_Registry::set('router', $router);
  253. Zend_Registry::set('dispatcher', $dispatcher);
  254. Zend_Registry::set('plugin', $plugin);
  255. Zend_Registry::set('viewRenderer', $viewRenderer);
  256. }
  257. public function testDispatchShouldDispatchSpecifiedUrl()
  258. {
  259. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  260. $this->testCase->dispatch('/zend-test-php-unit-foo/bar');
  261. $request = $this->testCase->getRequest();
  262. $response = $this->testCase->getResponse();
  263. $content = $response->getBody();
  264. $this->assertEquals('zend-test-php-unit-foo', $request->getControllerName(), $content);
  265. $this->assertEquals('bar', $request->getActionName());
  266. $this->assertContains('FooController::barAction', $content, $content);
  267. }
  268. public function testAssertQueryShouldDoNothingForValidResponseContent()
  269. {
  270. $this->testCase->getFrontController()->setControllerDirectory(realpath(dirname(__FILE__)) . '/_files/application/controllers', 'default');
  271. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  272. $body = $this->testCase->getResponse()->getBody();
  273. $this->testCase->assertQuery('div#foo legend.bar', $body);
  274. $this->testCase->assertQuery('div#foo legend.baz', $body);
  275. $this->testCase->assertQuery('div#foo legend.bat', $body);
  276. $this->testCase->assertNotQuery('div#foo legend.bogus', $body);
  277. $this->testCase->assertQueryContentContains('legend.bat', 'La di da', $body);
  278. $this->testCase->assertQueryContentContains('legend.numeric', 42, $body);
  279. $this->testCase->assertNotQueryContentContains('legend.numeric', 31, $body);
  280. $this->testCase->assertNotQueryContentContains('legend.bat', 'La do da', $body);
  281. $this->testCase->assertQueryContentRegex('legend.bat', '/d[a|i]/i', $body);
  282. $this->testCase->assertNotQueryContentRegex('legend.bat', '/d[o|e]/i', $body);
  283. $this->testCase->assertQueryCountMin('div#foo legend.bar', 2, $body);
  284. $this->testCase->assertQueryCount('div#foo legend.bar', 2, $body);
  285. $this->testCase->assertQueryCountMin('div#foo legend.bar', 2, $body);
  286. $this->testCase->assertQueryCountMax('div#foo legend.bar', 2, $body);
  287. }
  288. /**
  289. * @group ZF-4673
  290. */
  291. public function testAssertionsShouldIncreasePhpUnitAssertionCounter()
  292. {
  293. $this->testAssertQueryShouldDoNothingForValidResponseContent();
  294. $this->assertTrue(0 < $this->testCase->getNumAssertions());
  295. $this->assertTrue(12 <= $this->testCase->getNumAssertions());
  296. }
  297. public function testAssertQueryShouldThrowExceptionsForInValidResponseContent()
  298. {
  299. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  300. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  301. try {
  302. $this->testCase->assertNotQuery('div#foo legend.bar');
  303. $this->fail('Invalid assertions should throw exceptions');
  304. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  305. }
  306. try {
  307. $this->testCase->assertQuery('div#foo legend.bogus');
  308. $this->fail('Invalid assertions should throw exceptions');
  309. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  310. }
  311. try {
  312. $this->testCase->assertNotQueryContentContains('legend.bat', 'La di da');
  313. $this->fail('Invalid assertions should throw exceptions');
  314. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  315. }
  316. try {
  317. $this->testCase->assertQueryContentContains('legend.bat', 'La do da');
  318. $this->fail('Invalid assertions should throw exceptions');
  319. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  320. }
  321. try {
  322. $this->testCase->assertNotQueryContentRegex('legend.bat', '/d[a|i]/i');
  323. $this->fail('Invalid assertions should throw exceptions');
  324. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  325. }
  326. try {
  327. $this->testCase->assertQueryContentRegex('legend.bat', '/d[o|e]/i');
  328. $this->fail('Invalid assertions should throw exceptions');
  329. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  330. }
  331. try {
  332. $this->testCase->assertQueryCountMin('div#foo legend.bar', 3);
  333. $this->fail('Invalid assertions should throw exceptions');
  334. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  335. }
  336. try {
  337. $this->testCase->assertQueryCount('div#foo legend.bar', 1);
  338. $this->fail('Invalid assertions should throw exceptions');
  339. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  340. }
  341. try {
  342. $this->testCase->assertQueryCountMin('div#foo legend.bar', 3);
  343. $this->fail('Invalid assertions should throw exceptions');
  344. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  345. }
  346. try {
  347. $this->testCase->assertQueryCountMax('div#foo legend.bar', 1);
  348. $this->fail('Invalid assertions should throw exceptions');
  349. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  350. }
  351. }
  352. public function testAssertXpathShouldDoNothingForValidResponseContent()
  353. {
  354. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  355. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  356. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bar ')]");
  357. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' baz ')]");
  358. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bat ')]");
  359. $this->testCase->assertNotXpath("//div[@id='foo']//legend[contains(@class, ' bogus ')]");
  360. $this->testCase->assertXpathContentContains("//legend[contains(@class, ' bat ')]", "La di da");
  361. $this->testCase->assertNotXpathContentContains("//legend[contains(@class, ' bat ')]", "La do da");
  362. $this->testCase->assertXpathContentRegex("//legend[contains(@class, ' bat ')]", "/d[a'i]/i");
  363. $this->testCase->assertNotXpathContentRegex("//legend[contains(@class, ' bat ')]", "/d[o'e]/i");
  364. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  365. $this->testCase->assertXpathCount("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  366. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  367. $this->testCase->assertXpathCountMax("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  368. }
  369. public function testAssertXpathShouldThrowExceptionsForInValidResponseContent()
  370. {
  371. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  372. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  373. try {
  374. $this->testCase->assertNotXpath("//div[@id='foo']//legend[contains(@class, ' bar ')]");
  375. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  376. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  377. }
  378. try {
  379. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bogus ')]");
  380. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bogus ')] failed");
  381. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  382. }
  383. try {
  384. $this->testCase->assertNotXpathContentContains("//legend[contains(@class, ' bat ')]", "La di da");
  385. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  386. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  387. }
  388. try {
  389. $this->testCase->assertXpathContentContains("//legend[contains(@class, ' bat ')]", 'La do da');
  390. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  391. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  392. }
  393. try {
  394. $this->testCase->assertNotXpathContentRegex("//legend[contains(@class, ' bat ')]", '/d[a|i]/i');
  395. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  396. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  397. }
  398. try {
  399. $this->testCase->assertXpathContentRegex("//legend[contains(@class, ' bat ')]", '/d[o|e]/i');
  400. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  401. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  402. }
  403. try {
  404. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 3);
  405. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  406. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  407. }
  408. try {
  409. $this->testCase->assertXpathCount("//div[@id='foo']//legend[contains(@class, ' bar ')]", 1);
  410. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  411. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  412. }
  413. try {
  414. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 3);
  415. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  416. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  417. }
  418. try {
  419. $this->testCase->assertXpathCountMax("//div[@id='foo']//legend[contains(@class, ' bar ')]", 1);
  420. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  421. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  422. }
  423. }
  424. public function testRedirectAssertionsShouldDoNothingForValidAssertions()
  425. {
  426. $this->testCase->getResponse()->setRedirect('/foo');
  427. $this->testCase->assertRedirect();
  428. $this->testCase->assertRedirectTo('/foo', var_export($this->testCase->getResponse()->sendHeaders(), 1));
  429. $this->testCase->assertRedirectRegex('/FOO$/i');
  430. $this->testCase->reset();
  431. $this->testCase->assertNotRedirect();
  432. $this->testCase->assertNotRedirectTo('/foo');
  433. $this->testCase->assertNotRedirectRegex('/FOO$/i');
  434. $this->testCase->getResponse()->setRedirect('/foo');
  435. $this->testCase->assertNotRedirectTo('/bar');
  436. $this->testCase->assertNotRedirectRegex('/bar/i');
  437. }
  438. public function testHeaderAssertionShouldDoNothingForValidComparison()
  439. {
  440. $this->testCase->getResponse()->setHeader('Content-Type', 'x-application/my-foo');
  441. $this->testCase->assertResponseCode(200);
  442. $this->testCase->assertNotResponseCode(500);
  443. $this->testCase->assertHeader('Content-Type');
  444. $this->testCase->assertNotHeader('X-Bogus');
  445. $this->testCase->assertHeaderContains('Content-Type', 'my-foo');
  446. $this->testCase->assertNotHeaderContains('Content-Type', 'my-bar');
  447. $this->testCase->assertHeaderRegex('Content-Type', '#^[a-z-]+/[a-z-]+$#i');
  448. $this->testCase->assertNotHeaderRegex('Content-Type', '#^\d+#i');
  449. }
  450. public function testHeaderAssertionShouldThrowExceptionForInvalidComparison()
  451. {
  452. $this->testCase->getResponse()->setHeader('Content-Type', 'x-application/my-foo');
  453. try {
  454. $this->testCase->assertResponseCode(500);
  455. $this->fail();
  456. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  457. $this->assertContains('Failed', $e->getMessage());
  458. }
  459. try {
  460. $this->testCase->assertNotResponseCode(200);
  461. $this->fail();
  462. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  463. $this->assertContains('Failed', $e->getMessage());
  464. }
  465. try {
  466. $this->testCase->assertNotHeader('Content-Type');
  467. $this->fail();
  468. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  469. $this->assertContains('Failed', $e->getMessage());
  470. }
  471. try {
  472. $this->testCase->assertHeader('X-Bogus');
  473. $this->fail();
  474. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  475. $this->assertContains('Failed', $e->getMessage());
  476. }
  477. try {
  478. $this->testCase->assertNotHeaderContains('Content-Type', 'my-foo');
  479. $this->fail();
  480. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  481. $this->assertContains('Failed', $e->getMessage());
  482. }
  483. try {
  484. $this->testCase->assertHeaderContains('Content-Type', 'my-bar');
  485. $this->fail();
  486. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  487. $this->assertContains('Failed', $e->getMessage());
  488. }
  489. try {
  490. $this->testCase->assertNotHeaderRegex('Content-Type', '#^[a-z-]+/[a-z-]+$#i');
  491. $this->fail();
  492. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  493. $this->assertContains('Failed', $e->getMessage());
  494. }
  495. try {
  496. $this->testCase->assertHeaderRegex('Content-Type', '#^\d+#i');
  497. $this->fail();
  498. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  499. $this->assertContains('Failed', $e->getMessage());
  500. }
  501. }
  502. public function testModuleAssertionShouldDoNothingForValidComparison()
  503. {
  504. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  505. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  506. $this->testCase->assertModule('default');
  507. $this->testCase->assertNotModule('zend-test-php-unit-foo');
  508. }
  509. public function testModuleAssertionShouldThrowExceptionForInvalidComparison()
  510. {
  511. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  512. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  513. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  514. $this->testCase->assertModule('zend-test-php-unit-foo');
  515. $this->testCase->assertNotModule('default');
  516. }
  517. public function testControllerAssertionShouldDoNothingForValidComparison()
  518. {
  519. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  520. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  521. $this->testCase->assertController('zend-test-php-unit-foo');
  522. $this->testCase->assertNotController('baz');
  523. }
  524. public function testControllerAssertionShouldThrowExceptionForInvalidComparison()
  525. {
  526. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  527. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  528. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  529. $this->testCase->assertController('baz');
  530. $this->testCase->assertNotController('zend-test-php-unit-foo');
  531. }
  532. public function testActionAssertionShouldDoNothingForValidComparison()
  533. {
  534. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  535. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  536. $this->testCase->assertAction('baz');
  537. $this->testCase->assertNotAction('zend-test-php-unit-foo');
  538. }
  539. public function testActionAssertionShouldThrowExceptionForInvalidComparison()
  540. {
  541. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  542. $this->testCase->dispatch('/foo/baz');
  543. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  544. $this->testCase->assertAction('foo');
  545. $this->testCase->assertNotAction('baz');
  546. }
  547. public function testRouteAssertionShouldDoNothingForValidComparison()
  548. {
  549. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  550. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  551. $this->testCase->assertRoute('default');
  552. $this->testCase->assertNotRoute('zend-test-php-unit-foo');
  553. }
  554. public function testRouteAssertionShouldThrowExceptionForInvalidComparison()
  555. {
  556. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  557. $this->testCase->dispatch('/foo/baz');
  558. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  559. $this->testCase->assertRoute('foo');
  560. $this->testCase->assertNotRoute('default');
  561. }
  562. public function testResetShouldResetSessionArray()
  563. {
  564. $this->assertTrue(empty($_SESSION));
  565. $_SESSION = array('foo' => 'bar', 'bar' => 'baz');
  566. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $_SESSION, var_export($_SESSION, 1));
  567. $this->testCase->reset();
  568. $this->assertTrue(empty($_SESSION));
  569. }
  570. public function testResetShouldUnitTestEnableZendSession()
  571. {
  572. $this->testCase->reset();
  573. $this->assertTrue(Zend_Session::$_unitTestEnabled);
  574. }
  575. public function testResetResponseShouldClearResponseObject()
  576. {
  577. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  578. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  579. $response = $this->testCase->getResponse();
  580. $this->testCase->resetResponse();
  581. $test = $this->testCase->getResponse();
  582. $this->assertNotSame($response, $test);
  583. }
  584. /**
  585. * @group ZF-4511
  586. */
  587. public function testResetRequestShouldClearRequestObject()
  588. {
  589. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  590. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  591. $request = $this->testCase->getRequest();
  592. $this->testCase->resetRequest();
  593. $test = $this->testCase->getRequest();
  594. $this->assertNotSame($request, $test);
  595. }
  596. public function testResetResponseShouldClearAllViewPlaceholders()
  597. {
  598. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  599. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  600. $viewRenderer->initView();
  601. $view = $viewRenderer->view;
  602. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  603. $view->dojo()->setCdnVersion('1.1.0')
  604. ->requireModule('dojo.parser')
  605. ->enable();
  606. $view->headTitle('Foo');
  607. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  608. $response = $this->testCase->getResponse();
  609. $this->testCase->resetResponse();
  610. $view = new Zend_View();
  611. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  612. $this->assertFalse($view->dojo()->isEnabled(), 'Dojo is enabled? ', $view->dojo());
  613. $this->assertNotContains('Foo', $view->headTitle()->__toString(), 'Head title persisted?');
  614. }
  615. /**
  616. * @group ZF-4070
  617. */
  618. public function testQueryParametersShouldPersistFollowingDispatch()
  619. {
  620. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  621. $request = $this->testCase->request;
  622. $request->setQuery('mr', 'proper')
  623. ->setQuery('james', 'bond');
  624. $this->assertEquals('proper', $request->getQuery('mr'), '(pre) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  625. $this->assertEquals('bond', $request->getQuery('james'), '(pre) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  626. $this->testCase->dispatch('/');
  627. $this->assertEquals('proper', $request->getQuery('mr'), '(post) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  628. $this->assertEquals('bond', $request->getQuery('james'), '(post) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  629. }
  630. /**
  631. * @group ZF-4070
  632. */
  633. public function testQueryStringShouldNotOverwritePreviouslySetQueryParameters()
  634. {
  635. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  636. $request = $this->testCase->request;
  637. $request->setQuery('mr', 'proper')
  638. ->setQuery('james', 'bond');
  639. $this->assertEquals('proper', $request->getQuery('mr'), '(pre) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  640. $this->assertEquals('bond', $request->getQuery('james'), '(pre) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  641. $this->testCase->dispatch('/?spy=super');
  642. $this->assertEquals('super', $request->getQuery('spy'), '(post) Failed retrieving spy parameter: ' . var_export($request->getQuery(), 1));
  643. $this->assertEquals('proper', $request->getQuery('mr'), '(post) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  644. $this->assertEquals('bond', $request->getQuery('james'), '(post) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  645. }
  646. /**
  647. * @group ZF-3979
  648. */
  649. public function testSuperGlobalArraysShouldBeClearedDuringSetUp()
  650. {
  651. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  652. $request = $this->testCase->request;
  653. $request->setQuery('mr', 'proper')
  654. ->setPost('foo', 'bar')
  655. ->setCookie('bar', 'baz');
  656. $this->testCase->setUp();
  657. $this->assertNull($request->getQuery('mr'), 'Retrieved mr get parameter: ' . var_export($request->getQuery(), 1));
  658. $this->assertNull($request->getPost('foo'), 'Retrieved foo post parameter: ' . var_export($request->getPost(), 1));
  659. $this->assertNull($request->getCookie('bar'), 'Retrieved bar cookie parameter: ' . var_export($request->getCookie(), 1));
  660. }
  661. /**
  662. * @group ZF-4511
  663. */
  664. public function testResetRequestShouldClearPostAndQueryParameters()
  665. {
  666. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  667. $this->testCase->getRequest()->setPost(array(
  668. 'foo' => 'bar',
  669. ));
  670. $this->testCase->getRequest()->setQuery(array(
  671. 'bar' => 'baz',
  672. ));
  673. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  674. $this->testCase->resetRequest();
  675. $this->assertTrue(empty($_POST));
  676. $this->assertTrue(empty($_GET));
  677. }
  678. /**
  679. * @group ZF-7839
  680. */
  681. public function testTestCaseShouldAllowUsingApplicationObjectAsBootstrap()
  682. {
  683. require_once 'Zend/Application.php';
  684. $application = new Zend_Application('testing', array(
  685. 'resources' => array(
  686. 'frontcontroller' => array(
  687. 'controllerDirectory' => dirname(__FILE__) . '/_files/application/controllers',
  688. ),
  689. ),
  690. ));
  691. $this->testCase->bootstrap = $application;
  692. $this->testCase->bootstrap();
  693. $this->assertEquals(
  694. $application->getBootstrap()->getResource('frontcontroller'),
  695. $this->testCase->getFrontController()
  696. );
  697. }
  698. /**
  699. * @group ZF-8193
  700. */
  701. public function testWhenApplicationObjectUsedAsBootstrapTestCaseShouldExecuteBootstrapRunMethod()
  702. {
  703. require_once 'Zend/Application.php';
  704. $application = new Zend_Application('testing', array(
  705. 'resources' => array(
  706. 'frontcontroller' => array(
  707. 'controllerDirectory' => dirname(__FILE__) . '/_files/application/controllers',
  708. ),
  709. ),
  710. ));
  711. $this->testCase->bootstrap = $application;
  712. $this->testCase->bootstrap();
  713. $this->testCase->dispatch('/');
  714. $front = $application->getBootstrap()->getResource('frontcontroller');
  715. $boot = $front->getParam('bootstrap');
  716. $type = is_object($boot)
  717. ? get_class($boot)
  718. : gettype($boot);
  719. $this->assertTrue($boot === $this->testCase->bootstrap->getBootstrap(), $type);
  720. }
  721. /**
  722. * @group ZF-7496
  723. * @dataProvider providerRedirectWorksAsExpectedFromHookMethodsInActionController
  724. */
  725. public function testRedirectWorksAsExpectedFromHookMethodsInActionController($dispatchTo)
  726. {
  727. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  728. $this->testCase->dispatch($dispatchTo);
  729. $this->testCase->assertRedirectTo('/login');
  730. $this->assertNotEquals('action body', $this->testCase->getResponse()->getBody());
  731. }
  732. /**
  733. * Data provider for testRedirectWorksAsExpectedFromHookMethodsInActionController
  734. * @return array
  735. */
  736. public function providerRedirectWorksAsExpectedFromHookMethodsInActionController()
  737. {
  738. return array(
  739. array('/zend-test-redirect-from-init/baz'),
  740. array('/zend-test-redirect-from-pre-dispatch/baz')
  741. );
  742. }
  743. /**
  744. * @group ZF-7496
  745. * @dataProvider providerRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin
  746. */
  747. public function testRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin($pluginName)
  748. {
  749. require_once dirname(__FILE__) . "/_files/application/plugins/RedirectFrom{$pluginName}.php";
  750. $className = "Application_Plugin_RedirectFrom{$pluginName}";
  751. $fc = $this->testCase->getFrontController();
  752. $fc->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers')
  753. ->registerPlugin(new $className());
  754. $this->testCase->dispatch('/');
  755. $this->testCase->assertRedirectTo('/login');
  756. $this->assertNotEquals('action body', $this->testCase->getResponse()->getBody());
  757. }
  758. /**
  759. * @group ZF-12492
  760. * @internal Since header value is being cast into a string, we should only
  761. * need to check 0 and 0.0
  762. */
  763. public function testHeaderAssertionShouldDoNothingForValidComparisonWithZeroForValue()
  764. {
  765. $this->testCase->getResponse()->setHeader('Expires', '0', true);
  766. $this->testCase->assertResponseCode(200);
  767. $this->testCase->assertNotResponseCode(500);
  768. $this->testCase->assertHeader('Expires');
  769. $this->testCase->assertNotHeader('X-Bogus');
  770. $this->testCase->assertHeaderContains('Expires', '0');
  771. $this->testCase->assertNotHeaderContains('Expires', 'my-bar');
  772. $this->testCase->assertHeaderRegex('Expires', '#^\d#i');
  773. $this->testCase->assertNotHeaderRegex(
  774. 'Expires', '#^[a-z-]+/[a-z-]+$#i'
  775. );
  776. $this->testCase->getResponse()->setHeader('Expires', '0.0', true);
  777. $this->testCase->assertResponseCode(200);
  778. $this->testCase->assertNotResponseCode(500);
  779. $this->testCase->assertHeader('Expires');
  780. $this->testCase->assertNotHeader('X-Bogus');
  781. $this->testCase->assertHeaderContains('Expires', '0.0');
  782. $this->testCase->assertNotHeaderContains('Expires', 'my-bar');
  783. $this->testCase->assertHeaderRegex('Expires', '#^\d+#i');
  784. $this->testCase->assertNotHeaderRegex(
  785. 'Expires', '#^[a-z-]+/[a-z-]+$#i'
  786. );
  787. }
  788. /**
  789. * Data provider for testRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin
  790. * @return array
  791. */
  792. public function providerRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin()
  793. {
  794. return array(
  795. array('RouteStartup'),
  796. array('RouteShutdown'),
  797. array('DispatchLoopStartup'),
  798. array('PreDispatch')
  799. );
  800. }
  801. }
  802. // Concrete test case class for testing purposes
  803. class Zend_Test_PHPUnit_ControllerTestCaseTest_Concrete extends Zend_Test_PHPUnit_ControllerTestCase
  804. {
  805. }
  806. // Call Zend_Test_PHPUnit_ControllerTestCaseTest::main() if this source file is executed directly.
  807. if (PHPUnit_MAIN_METHOD == "Zend_Test_PHPUnit_ControllerTestCaseTest::main") {
  808. Zend_Test_PHPUnit_ControllerTestCaseTest::main();
  809. }