ControllerTestCaseTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <?php
  2. // Call Zend_Test_PHPUnit_ControllerTestCaseTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Test_PHPUnit_ControllerTestCaseTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. /** Zend_Test_PHPUnit_ControllerTestCase */
  8. require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
  9. /** Zend_Registry */
  10. require_once 'Zend/Registry.php';
  11. /** Zend_Session */
  12. require_once 'Zend/Session.php';
  13. /** Zend_Controller_Action */
  14. require_once 'Zend/Controller/Action.php';
  15. /**
  16. * Test class for Zend_Test_PHPUnit_ControllerTestCase.
  17. */
  18. class Zend_Test_PHPUnit_ControllerTestCaseTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * Runs the test methods of this class.
  22. *
  23. * @return void
  24. */
  25. public static function main()
  26. {
  27. $suite = new PHPUnit_Framework_TestSuite("Zend_Test_PHPUnit_ControllerTestCaseTest");
  28. $result = PHPUnit_TextUI_TestRunner::run($suite);
  29. }
  30. /**
  31. * Sets up the fixture, for example, open a network connection.
  32. * This method is called before a test is executed.
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. $_SESSION = array();
  39. $this->setExpectedException(null);
  40. $this->testCase = new Zend_Test_PHPUnit_ControllerTestCaseTest_Concrete();
  41. $this->testCase->reset();
  42. $this->testCase->bootstrap = array($this, 'bootstrap');
  43. }
  44. /**
  45. * Tears down the fixture, for example, close a network connection.
  46. * This method is called after a test is executed.
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. $registry = Zend_Registry::getInstance();
  53. if (isset($registry['router'])) {
  54. unset($registry['router']);
  55. }
  56. if (isset($registry['dispatcher'])) {
  57. unset($registry['dispatcher']);
  58. }
  59. if (isset($registry['plugin'])) {
  60. unset($registry['plugin']);
  61. }
  62. if (isset($registry['viewRenderer'])) {
  63. unset($registry['viewRenderer']);
  64. }
  65. Zend_Session::$_unitTestEnabled = false;
  66. session_id(uniqid());
  67. }
  68. public function bootstrap()
  69. {
  70. }
  71. public function testGetFrontControllerShouldReturnFrontController()
  72. {
  73. $controller = $this->testCase->getFrontController();
  74. $this->assertTrue($controller instanceof Zend_Controller_Front);
  75. }
  76. public function testGetFrontControllerShouldReturnSameFrontControllerObjectOnRepeatedCalls()
  77. {
  78. $controller = $this->testCase->getFrontController();
  79. $this->assertTrue($controller instanceof Zend_Controller_Front);
  80. $test = $this->testCase->getFrontController();
  81. $this->assertSame($controller, $test);
  82. }
  83. public function testGetRequestShouldReturnRequestTestCase()
  84. {
  85. $request = $this->testCase->getRequest();
  86. $this->assertTrue($request instanceof Zend_Controller_Request_HttpTestCase);
  87. }
  88. public function testGetRequestShouldReturnSameRequestObjectOnRepeatedCalls()
  89. {
  90. $request = $this->testCase->getRequest();
  91. $this->assertTrue($request instanceof Zend_Controller_Request_HttpTestCase);
  92. $test = $this->testCase->getRequest();
  93. $this->assertSame($request, $test);
  94. }
  95. public function testGetResponseShouldReturnResponseTestCase()
  96. {
  97. $response = $this->testCase->getResponse();
  98. $this->assertTrue($response instanceof Zend_Controller_Response_HttpTestCase);
  99. }
  100. public function testGetResponseShouldReturnSameResponseObjectOnRepeatedCalls()
  101. {
  102. $response = $this->testCase->getResponse();
  103. $this->assertTrue($response instanceof Zend_Controller_Response_HttpTestCase);
  104. $test = $this->testCase->getResponse();
  105. $this->assertSame($response, $test);
  106. }
  107. public function testGetQueryShouldReturnQueryTestCase()
  108. {
  109. $query = $this->testCase->getQuery();
  110. $this->assertTrue($query instanceof Zend_Dom_Query);
  111. }
  112. public function testGetQueryShouldReturnSameQueryObjectOnRepeatedCalls()
  113. {
  114. $query = $this->testCase->getQuery();
  115. $this->assertTrue($query instanceof Zend_Dom_Query);
  116. $test = $this->testCase->getQuery();
  117. $this->assertSame($query, $test);
  118. }
  119. public function testOverloadingShouldReturnRequestResponseAndFrontControllerObjects()
  120. {
  121. $request = $this->testCase->getRequest();
  122. $response = $this->testCase->getResponse();
  123. $frontController = $this->testCase->getFrontController();
  124. $this->assertSame($request, $this->testCase->request);
  125. $this->assertSame($response, $this->testCase->response);
  126. $this->assertSame($frontController, $this->testCase->frontController);
  127. }
  128. public function testOverloadingShouldPreventSettingRequestResponseAndFrontControllerObjects()
  129. {
  130. try {
  131. $this->testCase->request = new Zend_Controller_Request_Http();
  132. $this->fail('Setting request object as public property should raise exception');
  133. } catch (Exception $e) {
  134. $this->assertContains('not allow', $e->getMessage());
  135. }
  136. try {
  137. $this->testCase->response = new Zend_Controller_Response_Http();
  138. $this->fail('Setting response object as public property should raise exception');
  139. } catch (Exception $e) {
  140. $this->assertContains('not allow', $e->getMessage());
  141. }
  142. try {
  143. $this->testCase->frontController = Zend_Controller_Front::getInstance();
  144. $this->fail('Setting front controller as public property should raise exception');
  145. } catch (Exception $e) {
  146. $this->assertContains('not allow', $e->getMessage());
  147. }
  148. }
  149. public function testResetShouldResetMvcState()
  150. {
  151. require_once 'Zend/Controller/Action/HelperBroker.php';
  152. require_once 'Zend/Controller/Dispatcher/Standard.php';
  153. require_once 'Zend/Controller/Plugin/ErrorHandler.php';
  154. require_once 'Zend/Controller/Router/Rewrite.php';
  155. $request = $this->testCase->getRequest();
  156. $response = $this->testCase->getResponse();
  157. $router = new Zend_Controller_Router_Rewrite();
  158. $dispatcher = new Zend_Controller_Dispatcher_Standard();
  159. $plugin = new Zend_Controller_Plugin_ErrorHandler();
  160. $controller = $this->testCase->getFrontController();
  161. $controller->setParam('foo', 'bar')
  162. ->registerPlugin($plugin)
  163. ->setRouter($router)
  164. ->setDispatcher($dispatcher);
  165. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  166. $this->testCase->reset();
  167. $test = $controller->getRouter();
  168. $this->assertNotSame($router, $test);
  169. $test = $controller->getDispatcher();
  170. $this->assertNotSame($dispatcher, $test);
  171. $this->assertFalse($controller->getPlugin('Zend_Controller_Plugin_ErrorHandler'));
  172. $test = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  173. $this->assertNotSame($viewRenderer, $test);
  174. $this->assertNull($controller->getRequest());
  175. $this->assertNull($controller->getResponse());
  176. $this->assertNotSame($request, $this->testCase->getRequest());
  177. $this->assertNotSame($response, $this->testCase->getResponse());
  178. }
  179. public function testBootstrapShouldSetRequestAndResponseTestCaseObjects()
  180. {
  181. $this->testCase->bootstrap();
  182. $controller = $this->testCase->getFrontController();
  183. $request = $controller->getRequest();
  184. $response = $controller->getResponse();
  185. $this->assertSame($this->testCase->getRequest(), $request);
  186. $this->assertSame($this->testCase->getResponse(), $response);
  187. }
  188. public function testBootstrapShouldIncludeBootstrapFileSpecifiedInPublicBootstrapProperty()
  189. {
  190. $this->testCase->bootstrap = dirname(__FILE__) . '/_files/bootstrap.php';
  191. $this->testCase->bootstrap();
  192. $controller = $this->testCase->getFrontController();
  193. $this->assertSame(Zend_Registry::get('router'), $controller->getRouter());
  194. $this->assertSame(Zend_Registry::get('dispatcher'), $controller->getDispatcher());
  195. $this->assertSame(Zend_Registry::get('plugin'), $controller->getPlugin('Zend_Controller_Plugin_ErrorHandler'));
  196. $this->assertSame(Zend_Registry::get('viewRenderer'), Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'));
  197. }
  198. public function testBootstrapShouldInvokeCallbackSpecifiedInPublicBootstrapProperty()
  199. {
  200. $this->testCase->bootstrap = array($this, 'bootstrapCallback');
  201. $this->testCase->bootstrap();
  202. $controller = $this->testCase->getFrontController();
  203. $this->assertSame(Zend_Registry::get('router'), $controller->getRouter());
  204. $this->assertSame(Zend_Registry::get('dispatcher'), $controller->getDispatcher());
  205. $this->assertSame(Zend_Registry::get('plugin'), $controller->getPlugin('Zend_Controller_Plugin_ErrorHandler'));
  206. $this->assertSame(Zend_Registry::get('viewRenderer'), Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'));
  207. }
  208. public function bootstrapCallback()
  209. {
  210. require_once 'Zend/Controller/Action/HelperBroker.php';
  211. require_once 'Zend/Controller/Dispatcher/Standard.php';
  212. require_once 'Zend/Controller/Front.php';
  213. require_once 'Zend/Controller/Plugin/ErrorHandler.php';
  214. require_once 'Zend/Controller/Router/Rewrite.php';
  215. require_once 'Zend/Registry.php';
  216. $router = new Zend_Controller_Router_Rewrite();
  217. $dispatcher = new Zend_Controller_Dispatcher_Standard();
  218. $plugin = new Zend_Controller_Plugin_ErrorHandler();
  219. $controller = Zend_Controller_Front::getInstance();
  220. $controller->setParam('foo', 'bar')
  221. ->registerPlugin($plugin)
  222. ->setRouter($router)
  223. ->setDispatcher($dispatcher);
  224. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  225. Zend_Registry::set('router', $router);
  226. Zend_Registry::set('dispatcher', $dispatcher);
  227. Zend_Registry::set('plugin', $plugin);
  228. Zend_Registry::set('viewRenderer', $viewRenderer);
  229. }
  230. public function testDispatchShouldDispatchSpecifiedUrl()
  231. {
  232. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  233. $this->testCase->dispatch('/zend-test-php-unit-foo/bar');
  234. $request = $this->testCase->getRequest();
  235. $response = $this->testCase->getResponse();
  236. $content = $response->getBody();
  237. $this->assertEquals('zend-test-php-unit-foo', $request->getControllerName(), $content);
  238. $this->assertEquals('bar', $request->getActionName());
  239. $this->assertContains('FooController::barAction', $content, $content);
  240. }
  241. public function testAssertQueryShouldDoNothingForValidResponseContent()
  242. {
  243. $this->testCase->getFrontController()->setControllerDirectory(realpath(dirname(__FILE__)) . '/_files/application/controllers', 'default');
  244. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  245. $body = $this->testCase->getResponse()->getBody();
  246. $this->testCase->assertQuery('div#foo legend.bar', $body);
  247. $this->testCase->assertQuery('div#foo legend.baz', $body);
  248. $this->testCase->assertQuery('div#foo legend.bat', $body);
  249. $this->testCase->assertNotQuery('div#foo legend.bogus', $body);
  250. $this->testCase->assertQueryContentContains('legend.bat', 'La di da', $body);
  251. $this->testCase->assertNotQueryContentContains('legend.bat', 'La do da', $body);
  252. $this->testCase->assertQueryContentRegex('legend.bat', '/d[a|i]/i', $body);
  253. $this->testCase->assertNotQueryContentRegex('legend.bat', '/d[o|e]/i', $body);
  254. $this->testCase->assertQueryCountMin('div#foo legend.bar', 2, $body);
  255. $this->testCase->assertQueryCount('div#foo legend.bar', 2, $body);
  256. $this->testCase->assertQueryCountMin('div#foo legend.bar', 2, $body);
  257. $this->testCase->assertQueryCountMax('div#foo legend.bar', 2, $body);
  258. }
  259. /**
  260. * @group ZF-4673
  261. */
  262. public function testAssertionsShouldIncreasePhpUnitAssertionCounter()
  263. {
  264. $this->testAssertQueryShouldDoNothingForValidResponseContent();
  265. $this->assertTrue(0 < $this->testCase->getNumAssertions());
  266. $this->assertTrue(12 <= $this->testCase->getNumAssertions());
  267. }
  268. public function testAssertQueryShouldThrowExceptionsForInValidResponseContent()
  269. {
  270. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  271. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  272. try {
  273. $this->testCase->assertNotQuery('div#foo legend.bar');
  274. $this->fail('Invalid assertions should throw exceptions');
  275. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  276. }
  277. try {
  278. $this->testCase->assertQuery('div#foo legend.bogus');
  279. $this->fail('Invalid assertions should throw exceptions');
  280. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  281. }
  282. try {
  283. $this->testCase->assertNotQueryContentContains('legend.bat', 'La di da');
  284. $this->fail('Invalid assertions should throw exceptions');
  285. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  286. }
  287. try {
  288. $this->testCase->assertQueryContentContains('legend.bat', 'La do da');
  289. $this->fail('Invalid assertions should throw exceptions');
  290. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  291. }
  292. try {
  293. $this->testCase->assertNotQueryContentRegex('legend.bat', '/d[a|i]/i');
  294. $this->fail('Invalid assertions should throw exceptions');
  295. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  296. }
  297. try {
  298. $this->testCase->assertQueryContentRegex('legend.bat', '/d[o|e]/i');
  299. $this->fail('Invalid assertions should throw exceptions');
  300. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  301. }
  302. try {
  303. $this->testCase->assertQueryCountMin('div#foo legend.bar', 3);
  304. $this->fail('Invalid assertions should throw exceptions');
  305. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  306. }
  307. try {
  308. $this->testCase->assertQueryCount('div#foo legend.bar', 1);
  309. $this->fail('Invalid assertions should throw exceptions');
  310. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  311. }
  312. try {
  313. $this->testCase->assertQueryCountMin('div#foo legend.bar', 3);
  314. $this->fail('Invalid assertions should throw exceptions');
  315. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  316. }
  317. try {
  318. $this->testCase->assertQueryCountMax('div#foo legend.bar', 1);
  319. $this->fail('Invalid assertions should throw exceptions');
  320. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  321. }
  322. }
  323. public function testAssertXpathShouldDoNothingForValidResponseContent()
  324. {
  325. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  326. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  327. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bar ')]");
  328. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' baz ')]");
  329. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bat ')]");
  330. $this->testCase->assertNotXpath("//div[@id='foo']//legend[contains(@class, ' bogus ')]");
  331. $this->testCase->assertXpathContentContains("//legend[contains(@class, ' bat ')]", "La di da");
  332. $this->testCase->assertNotXpathContentContains("//legend[contains(@class, ' bat ')]", "La do da");
  333. $this->testCase->assertXpathContentRegex("//legend[contains(@class, ' bat ')]", "/d[a'i]/i");
  334. $this->testCase->assertNotXpathContentRegex("//legend[contains(@class, ' bat ')]", "/d[o'e]/i");
  335. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  336. $this->testCase->assertXpathCount("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  337. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  338. $this->testCase->assertXpathCountMax("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  339. }
  340. public function testAssertXpathShouldThrowExceptionsForInValidResponseContent()
  341. {
  342. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  343. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  344. try {
  345. $this->testCase->assertNotXpath("//div[@id='foo']//legend[contains(@class, ' bar ')]");
  346. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  347. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  348. }
  349. try {
  350. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bogus ')]");
  351. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bogus ')] failed");
  352. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  353. }
  354. try {
  355. $this->testCase->assertNotXpathContentContains("//legend[contains(@class, ' bat ')]", "La di da");
  356. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  357. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  358. }
  359. try {
  360. $this->testCase->assertXpathContentContains("//legend[contains(@class, ' bat ')]", 'La do da');
  361. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  362. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  363. }
  364. try {
  365. $this->testCase->assertNotXpathContentRegex("//legend[contains(@class, ' bat ')]", '/d[a|i]/i');
  366. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  367. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  368. }
  369. try {
  370. $this->testCase->assertXpathContentRegex("//legend[contains(@class, ' bat ')]", '/d[o|e]/i');
  371. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  372. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  373. }
  374. try {
  375. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 3);
  376. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  377. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  378. }
  379. try {
  380. $this->testCase->assertXpathCount("//div[@id='foo']//legend[contains(@class, ' bar ')]", 1);
  381. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  382. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  383. }
  384. try {
  385. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 3);
  386. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  387. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  388. }
  389. try {
  390. $this->testCase->assertXpathCountMax("//div[@id='foo']//legend[contains(@class, ' bar ')]", 1);
  391. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  392. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  393. }
  394. }
  395. public function testRedirectAssertionsShouldDoNothingForValidAssertions()
  396. {
  397. $this->testCase->getResponse()->setRedirect('/foo');
  398. $this->testCase->assertRedirect();
  399. $this->testCase->assertRedirectTo('/foo', var_export($this->testCase->getResponse()->sendHeaders(), 1));
  400. $this->testCase->assertRedirectRegex('/FOO$/i');
  401. $this->testCase->reset();
  402. $this->testCase->assertNotRedirect();
  403. $this->testCase->assertNotRedirectTo('/foo');
  404. $this->testCase->assertNotRedirectRegex('/FOO$/i');
  405. $this->testCase->getResponse()->setRedirect('/foo');
  406. $this->testCase->assertNotRedirectTo('/bar');
  407. $this->testCase->assertNotRedirectRegex('/bar/i');
  408. }
  409. public function testHeaderAssertionShouldDoNothingForValidComparison()
  410. {
  411. $this->testCase->getResponse()->setHeader('Content-Type', 'x-application/my-foo');
  412. $this->testCase->assertResponseCode(200);
  413. $this->testCase->assertNotResponseCode(500);
  414. $this->testCase->assertHeader('Content-Type');
  415. $this->testCase->assertNotHeader('X-Bogus');
  416. $this->testCase->assertHeaderContains('Content-Type', 'my-foo');
  417. $this->testCase->assertNotHeaderContains('Content-Type', 'my-bar');
  418. $this->testCase->assertHeaderRegex('Content-Type', '#^[a-z-]+/[a-z-]+$#i');
  419. $this->testCase->assertNotHeaderRegex('Content-Type', '#^\d+#i');
  420. }
  421. public function testHeaderAssertionShouldThrowExceptionForInvalidComparison()
  422. {
  423. $this->testCase->getResponse()->setHeader('Content-Type', 'x-application/my-foo');
  424. try {
  425. $this->testCase->assertResponseCode(500);
  426. $this->fail();
  427. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  428. $this->assertContains('Failed', $e->getMessage());
  429. }
  430. try {
  431. $this->testCase->assertNotResponseCode(200);
  432. $this->fail();
  433. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  434. $this->assertContains('Failed', $e->getMessage());
  435. }
  436. try {
  437. $this->testCase->assertNotHeader('Content-Type');
  438. $this->fail();
  439. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  440. $this->assertContains('Failed', $e->getMessage());
  441. }
  442. try {
  443. $this->testCase->assertHeader('X-Bogus');
  444. $this->fail();
  445. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  446. $this->assertContains('Failed', $e->getMessage());
  447. }
  448. try {
  449. $this->testCase->assertNotHeaderContains('Content-Type', 'my-foo');
  450. $this->fail();
  451. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  452. $this->assertContains('Failed', $e->getMessage());
  453. }
  454. try {
  455. $this->testCase->assertHeaderContains('Content-Type', 'my-bar');
  456. $this->fail();
  457. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  458. $this->assertContains('Failed', $e->getMessage());
  459. }
  460. try {
  461. $this->testCase->assertNotHeaderRegex('Content-Type', '#^[a-z-]+/[a-z-]+$#i');
  462. $this->fail();
  463. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  464. $this->assertContains('Failed', $e->getMessage());
  465. }
  466. try {
  467. $this->testCase->assertHeaderRegex('Content-Type', '#^\d+#i');
  468. $this->fail();
  469. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  470. $this->assertContains('Failed', $e->getMessage());
  471. }
  472. }
  473. public function testModuleAssertionShouldDoNothingForValidComparison()
  474. {
  475. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  476. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  477. $this->testCase->assertModule('default');
  478. $this->testCase->assertNotModule('zend-test-php-unit-foo');
  479. }
  480. public function testModuleAssertionShouldThrowExceptionForInvalidComparison()
  481. {
  482. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  483. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  484. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  485. $this->testCase->assertModule('zend-test-php-unit-foo');
  486. $this->testCase->assertNotModule('default');
  487. }
  488. public function testControllerAssertionShouldDoNothingForValidComparison()
  489. {
  490. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  491. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  492. $this->testCase->assertController('zend-test-php-unit-foo');
  493. $this->testCase->assertNotController('baz');
  494. }
  495. public function testControllerAssertionShouldThrowExceptionForInvalidComparison()
  496. {
  497. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  498. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  499. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  500. $this->testCase->assertController('baz');
  501. $this->testCase->assertNotController('zend-test-php-unit-foo');
  502. }
  503. public function testActionAssertionShouldDoNothingForValidComparison()
  504. {
  505. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  506. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  507. $this->testCase->assertAction('baz');
  508. $this->testCase->assertNotAction('zend-test-php-unit-foo');
  509. }
  510. public function testActionAssertionShouldThrowExceptionForInvalidComparison()
  511. {
  512. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  513. $this->testCase->dispatch('/foo/baz');
  514. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  515. $this->testCase->assertAction('foo');
  516. $this->testCase->assertNotAction('baz');
  517. }
  518. public function testRouteAssertionShouldDoNothingForValidComparison()
  519. {
  520. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  521. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  522. $this->testCase->assertRoute('default');
  523. $this->testCase->assertNotRoute('zend-test-php-unit-foo');
  524. }
  525. public function testRouteAssertionShouldThrowExceptionForInvalidComparison()
  526. {
  527. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  528. $this->testCase->dispatch('/foo/baz');
  529. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  530. $this->testCase->assertRoute('foo');
  531. $this->testCase->assertNotRoute('default');
  532. }
  533. public function testResetShouldResetSessionArray()
  534. {
  535. $this->assertTrue(empty($_SESSION));
  536. $_SESSION = array('foo' => 'bar', 'bar' => 'baz');
  537. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $_SESSION, var_export($_SESSION, 1));
  538. $this->testCase->reset();
  539. $this->assertTrue(empty($_SESSION));
  540. }
  541. public function testResetShouldUnitTestEnableZendSession()
  542. {
  543. $this->testCase->reset();
  544. $this->assertTrue(Zend_Session::$_unitTestEnabled);
  545. }
  546. public function testResetResponseShouldClearResponseObject()
  547. {
  548. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  549. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  550. $response = $this->testCase->getResponse();
  551. $this->testCase->resetResponse();
  552. $test = $this->testCase->getResponse();
  553. $this->assertNotSame($response, $test);
  554. }
  555. /**
  556. * @group ZF-4511
  557. */
  558. public function testResetRequestShouldClearRequestObject()
  559. {
  560. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  561. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  562. $request = $this->testCase->getRequest();
  563. $this->testCase->resetRequest();
  564. $test = $this->testCase->getRequest();
  565. $this->assertNotSame($request, $test);
  566. }
  567. public function testResetResponseShouldClearAllViewPlaceholders()
  568. {
  569. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  570. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  571. $viewRenderer->initView();
  572. $view = $viewRenderer->view;
  573. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  574. $view->dojo()->setCdnVersion('1.1.0')
  575. ->requireModule('dojo.parser')
  576. ->enable();
  577. $view->headTitle('Foo');
  578. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  579. $response = $this->testCase->getResponse();
  580. $this->testCase->resetResponse();
  581. $view = new Zend_View();
  582. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  583. $this->assertFalse($view->dojo()->isEnabled(), 'Dojo is enabled? ', $view->dojo());
  584. $this->assertNotContains('Foo', $view->headTitle()->__toString(), 'Head title persisted?');
  585. }
  586. /**
  587. * @group ZF-4070
  588. */
  589. public function testQueryParametersShouldPersistFollowingDispatch()
  590. {
  591. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  592. $request = $this->testCase->request;
  593. $request->setQuery('mr', 'proper')
  594. ->setQuery('james', 'bond');
  595. $this->assertEquals('proper', $request->getQuery('mr'), '(pre) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  596. $this->assertEquals('bond', $request->getQuery('james'), '(pre) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  597. $this->testCase->dispatch('/');
  598. $this->assertEquals('proper', $request->getQuery('mr'), '(post) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  599. $this->assertEquals('bond', $request->getQuery('james'), '(post) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  600. }
  601. /**
  602. * @group ZF-4070
  603. */
  604. public function testQueryStringShouldNotOverwritePreviouslySetQueryParameters()
  605. {
  606. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  607. $request = $this->testCase->request;
  608. $request->setQuery('mr', 'proper')
  609. ->setQuery('james', 'bond');
  610. $this->assertEquals('proper', $request->getQuery('mr'), '(pre) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  611. $this->assertEquals('bond', $request->getQuery('james'), '(pre) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  612. $this->testCase->dispatch('/?spy=super');
  613. $this->assertEquals('super', $request->getQuery('spy'), '(post) Failed retrieving spy parameter: ' . var_export($request->getQuery(), 1));
  614. $this->assertEquals('proper', $request->getQuery('mr'), '(post) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  615. $this->assertEquals('bond', $request->getQuery('james'), '(post) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  616. }
  617. /**
  618. * @group ZF-3979
  619. */
  620. public function testSuperGlobalArraysShouldBeClearedDuringSetUp()
  621. {
  622. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  623. $request = $this->testCase->request;
  624. $request->setQuery('mr', 'proper')
  625. ->setPost('foo', 'bar')
  626. ->setCookie('bar', 'baz');
  627. $this->testCase->setUp();
  628. $this->assertNull($request->getQuery('mr'), 'Retrieved mr get parameter: ' . var_export($request->getQuery(), 1));
  629. $this->assertNull($request->getPost('foo'), 'Retrieved foo post parameter: ' . var_export($request->getPost(), 1));
  630. $this->assertNull($request->getCookie('bar'), 'Retrieved bar cookie parameter: ' . var_export($request->getCookie(), 1));
  631. }
  632. }
  633. // Concrete test case class for testing purposes
  634. class Zend_Test_PHPUnit_ControllerTestCaseTest_Concrete extends Zend_Test_PHPUnit_ControllerTestCase
  635. {
  636. }
  637. // Call Zend_Test_PHPUnit_ControllerTestCaseTest::main() if this source file is executed directly.
  638. if (PHPUnit_MAIN_METHOD == "Zend_Test_PHPUnit_ControllerTestCaseTest::main") {
  639. Zend_Test_PHPUnit_ControllerTestCaseTest::main();
  640. }