2
0

ControllerTestCaseTest.php 35 KB

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