ControllerTestCaseTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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-2010 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-2010 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->assertQueryContentContains('legend.numeric', 42, $body);
  280. $this->testCase->assertNotQueryContentContains('legend.numeric', 31, $body);
  281. $this->testCase->assertNotQueryContentContains('legend.bat', 'La do da', $body);
  282. $this->testCase->assertQueryContentRegex('legend.bat', '/d[a|i]/i', $body);
  283. $this->testCase->assertNotQueryContentRegex('legend.bat', '/d[o|e]/i', $body);
  284. $this->testCase->assertQueryCountMin('div#foo legend.bar', 2, $body);
  285. $this->testCase->assertQueryCount('div#foo legend.bar', 2, $body);
  286. $this->testCase->assertQueryCountMin('div#foo legend.bar', 2, $body);
  287. $this->testCase->assertQueryCountMax('div#foo legend.bar', 2, $body);
  288. }
  289. /**
  290. * @group ZF-4673
  291. */
  292. public function testAssertionsShouldIncreasePhpUnitAssertionCounter()
  293. {
  294. $this->testAssertQueryShouldDoNothingForValidResponseContent();
  295. $this->assertTrue(0 < $this->testCase->getNumAssertions());
  296. $this->assertTrue(12 <= $this->testCase->getNumAssertions());
  297. }
  298. public function testAssertQueryShouldThrowExceptionsForInValidResponseContent()
  299. {
  300. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  301. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  302. try {
  303. $this->testCase->assertNotQuery('div#foo legend.bar');
  304. $this->fail('Invalid assertions should throw exceptions');
  305. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  306. }
  307. try {
  308. $this->testCase->assertQuery('div#foo legend.bogus');
  309. $this->fail('Invalid assertions should throw exceptions');
  310. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  311. }
  312. try {
  313. $this->testCase->assertNotQueryContentContains('legend.bat', 'La di da');
  314. $this->fail('Invalid assertions should throw exceptions');
  315. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  316. }
  317. try {
  318. $this->testCase->assertQueryContentContains('legend.bat', 'La do da');
  319. $this->fail('Invalid assertions should throw exceptions');
  320. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  321. }
  322. try {
  323. $this->testCase->assertNotQueryContentRegex('legend.bat', '/d[a|i]/i');
  324. $this->fail('Invalid assertions should throw exceptions');
  325. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  326. }
  327. try {
  328. $this->testCase->assertQueryContentRegex('legend.bat', '/d[o|e]/i');
  329. $this->fail('Invalid assertions should throw exceptions');
  330. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  331. }
  332. try {
  333. $this->testCase->assertQueryCountMin('div#foo legend.bar', 3);
  334. $this->fail('Invalid assertions should throw exceptions');
  335. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  336. }
  337. try {
  338. $this->testCase->assertQueryCount('div#foo legend.bar', 1);
  339. $this->fail('Invalid assertions should throw exceptions');
  340. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  341. }
  342. try {
  343. $this->testCase->assertQueryCountMin('div#foo legend.bar', 3);
  344. $this->fail('Invalid assertions should throw exceptions');
  345. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  346. }
  347. try {
  348. $this->testCase->assertQueryCountMax('div#foo legend.bar', 1);
  349. $this->fail('Invalid assertions should throw exceptions');
  350. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  351. }
  352. }
  353. public function testAssertXpathShouldDoNothingForValidResponseContent()
  354. {
  355. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  356. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  357. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bar ')]");
  358. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' baz ')]");
  359. $this->testCase->assertXpath("//div[@id='foo']//legend[contains(@class, ' bat ')]");
  360. $this->testCase->assertNotXpath("//div[@id='foo']//legend[contains(@class, ' bogus ')]");
  361. $this->testCase->assertXpathContentContains("//legend[contains(@class, ' bat ')]", "La di da");
  362. $this->testCase->assertNotXpathContentContains("//legend[contains(@class, ' bat ')]", "La do da");
  363. $this->testCase->assertXpathContentRegex("//legend[contains(@class, ' bat ')]", "/d[a'i]/i");
  364. $this->testCase->assertNotXpathContentRegex("//legend[contains(@class, ' bat ')]", "/d[o'e]/i");
  365. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  366. $this->testCase->assertXpathCount("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  367. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  368. $this->testCase->assertXpathCountMax("//div[@id='foo']//legend[contains(@class, ' bar ')]", 2);
  369. }
  370. public function testAssertXpathShouldThrowExceptionsForInValidResponseContent()
  371. {
  372. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  373. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  374. try {
  375. $this->testCase->assertNotXpath("//div[@id='foo']//legend[contains(@class, ' bar ')]");
  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->assertXpath("//div[@id='foo']//legend[contains(@class, ' bogus ')]");
  381. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bogus ')] failed");
  382. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  383. }
  384. try {
  385. $this->testCase->assertNotXpathContentContains("//legend[contains(@class, ' bat ')]", "La di da");
  386. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  387. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  388. }
  389. try {
  390. $this->testCase->assertXpathContentContains("//legend[contains(@class, ' bat ')]", 'La do da');
  391. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  392. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  393. }
  394. try {
  395. $this->testCase->assertNotXpathContentRegex("//legend[contains(@class, ' bat ')]", '/d[a|i]/i');
  396. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  397. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  398. }
  399. try {
  400. $this->testCase->assertXpathContentRegex("//legend[contains(@class, ' bat ')]", '/d[o|e]/i');
  401. $this->fail("Invalid assertions should throw exceptions; assertion against //legend[contains(@class, ' bat ')] failed");
  402. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  403. }
  404. try {
  405. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 3);
  406. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  407. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  408. }
  409. try {
  410. $this->testCase->assertXpathCount("//div[@id='foo']//legend[contains(@class, ' bar ')]", 1);
  411. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  412. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  413. }
  414. try {
  415. $this->testCase->assertXpathCountMin("//div[@id='foo']//legend[contains(@class, ' bar ')]", 3);
  416. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  417. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  418. }
  419. try {
  420. $this->testCase->assertXpathCountMax("//div[@id='foo']//legend[contains(@class, ' bar ')]", 1);
  421. $this->fail("Invalid assertions should throw exceptions; assertion against //div[@id='foo']//legend[contains(@class, ' bar ')] failed");
  422. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  423. }
  424. }
  425. public function testRedirectAssertionsShouldDoNothingForValidAssertions()
  426. {
  427. $this->testCase->getResponse()->setRedirect('/foo');
  428. $this->testCase->assertRedirect();
  429. $this->testCase->assertRedirectTo('/foo', var_export($this->testCase->getResponse()->sendHeaders(), 1));
  430. $this->testCase->assertRedirectRegex('/FOO$/i');
  431. $this->testCase->reset();
  432. $this->testCase->assertNotRedirect();
  433. $this->testCase->assertNotRedirectTo('/foo');
  434. $this->testCase->assertNotRedirectRegex('/FOO$/i');
  435. $this->testCase->getResponse()->setRedirect('/foo');
  436. $this->testCase->assertNotRedirectTo('/bar');
  437. $this->testCase->assertNotRedirectRegex('/bar/i');
  438. }
  439. public function testHeaderAssertionShouldDoNothingForValidComparison()
  440. {
  441. $this->testCase->getResponse()->setHeader('Content-Type', 'x-application/my-foo');
  442. $this->testCase->assertResponseCode(200);
  443. $this->testCase->assertNotResponseCode(500);
  444. $this->testCase->assertHeader('Content-Type');
  445. $this->testCase->assertNotHeader('X-Bogus');
  446. $this->testCase->assertHeaderContains('Content-Type', 'my-foo');
  447. $this->testCase->assertNotHeaderContains('Content-Type', 'my-bar');
  448. $this->testCase->assertHeaderRegex('Content-Type', '#^[a-z-]+/[a-z-]+$#i');
  449. $this->testCase->assertNotHeaderRegex('Content-Type', '#^\d+#i');
  450. }
  451. public function testHeaderAssertionShouldThrowExceptionForInvalidComparison()
  452. {
  453. $this->testCase->getResponse()->setHeader('Content-Type', 'x-application/my-foo');
  454. try {
  455. $this->testCase->assertResponseCode(500);
  456. $this->fail();
  457. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  458. $this->assertContains('Failed', $e->getMessage());
  459. }
  460. try {
  461. $this->testCase->assertNotResponseCode(200);
  462. $this->fail();
  463. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  464. $this->assertContains('Failed', $e->getMessage());
  465. }
  466. try {
  467. $this->testCase->assertNotHeader('Content-Type');
  468. $this->fail();
  469. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  470. $this->assertContains('Failed', $e->getMessage());
  471. }
  472. try {
  473. $this->testCase->assertHeader('X-Bogus');
  474. $this->fail();
  475. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  476. $this->assertContains('Failed', $e->getMessage());
  477. }
  478. try {
  479. $this->testCase->assertNotHeaderContains('Content-Type', 'my-foo');
  480. $this->fail();
  481. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  482. $this->assertContains('Failed', $e->getMessage());
  483. }
  484. try {
  485. $this->testCase->assertHeaderContains('Content-Type', 'my-bar');
  486. $this->fail();
  487. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  488. $this->assertContains('Failed', $e->getMessage());
  489. }
  490. try {
  491. $this->testCase->assertNotHeaderRegex('Content-Type', '#^[a-z-]+/[a-z-]+$#i');
  492. $this->fail();
  493. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  494. $this->assertContains('Failed', $e->getMessage());
  495. }
  496. try {
  497. $this->testCase->assertHeaderRegex('Content-Type', '#^\d+#i');
  498. $this->fail();
  499. } catch (Zend_Test_PHPUnit_Constraint_Exception $e) {
  500. $this->assertContains('Failed', $e->getMessage());
  501. }
  502. }
  503. public function testModuleAssertionShouldDoNothingForValidComparison()
  504. {
  505. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  506. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  507. $this->testCase->assertModule('default');
  508. $this->testCase->assertNotModule('zend-test-php-unit-foo');
  509. }
  510. public function testModuleAssertionShouldThrowExceptionForInvalidComparison()
  511. {
  512. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  513. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  514. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  515. $this->testCase->assertModule('zend-test-php-unit-foo');
  516. $this->testCase->assertNotModule('default');
  517. }
  518. public function testControllerAssertionShouldDoNothingForValidComparison()
  519. {
  520. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  521. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  522. $this->testCase->assertController('zend-test-php-unit-foo');
  523. $this->testCase->assertNotController('baz');
  524. }
  525. public function testControllerAssertionShouldThrowExceptionForInvalidComparison()
  526. {
  527. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  528. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  529. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  530. $this->testCase->assertController('baz');
  531. $this->testCase->assertNotController('zend-test-php-unit-foo');
  532. }
  533. public function testActionAssertionShouldDoNothingForValidComparison()
  534. {
  535. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  536. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  537. $this->testCase->assertAction('baz');
  538. $this->testCase->assertNotAction('zend-test-php-unit-foo');
  539. }
  540. public function testActionAssertionShouldThrowExceptionForInvalidComparison()
  541. {
  542. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  543. $this->testCase->dispatch('/foo/baz');
  544. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  545. $this->testCase->assertAction('foo');
  546. $this->testCase->assertNotAction('baz');
  547. }
  548. public function testRouteAssertionShouldDoNothingForValidComparison()
  549. {
  550. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  551. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  552. $this->testCase->assertRoute('default');
  553. $this->testCase->assertNotRoute('zend-test-php-unit-foo');
  554. }
  555. public function testRouteAssertionShouldThrowExceptionForInvalidComparison()
  556. {
  557. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  558. $this->testCase->dispatch('/foo/baz');
  559. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  560. $this->testCase->assertRoute('foo');
  561. $this->testCase->assertNotRoute('default');
  562. }
  563. public function testResetShouldResetSessionArray()
  564. {
  565. $this->assertTrue(empty($_SESSION));
  566. $_SESSION = array('foo' => 'bar', 'bar' => 'baz');
  567. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $_SESSION, var_export($_SESSION, 1));
  568. $this->testCase->reset();
  569. $this->assertTrue(empty($_SESSION));
  570. }
  571. public function testResetShouldUnitTestEnableZendSession()
  572. {
  573. $this->testCase->reset();
  574. $this->assertTrue(Zend_Session::$_unitTestEnabled);
  575. }
  576. public function testResetResponseShouldClearResponseObject()
  577. {
  578. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  579. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  580. $response = $this->testCase->getResponse();
  581. $this->testCase->resetResponse();
  582. $test = $this->testCase->getResponse();
  583. $this->assertNotSame($response, $test);
  584. }
  585. /**
  586. * @group ZF-4511
  587. */
  588. public function testResetRequestShouldClearRequestObject()
  589. {
  590. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  591. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  592. $request = $this->testCase->getRequest();
  593. $this->testCase->resetRequest();
  594. $test = $this->testCase->getRequest();
  595. $this->assertNotSame($request, $test);
  596. }
  597. public function testResetResponseShouldClearAllViewPlaceholders()
  598. {
  599. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  600. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  601. $viewRenderer->initView();
  602. $view = $viewRenderer->view;
  603. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  604. $view->dojo()->setCdnVersion('1.1.0')
  605. ->requireModule('dojo.parser')
  606. ->enable();
  607. $view->headTitle('Foo');
  608. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  609. $response = $this->testCase->getResponse();
  610. $this->testCase->resetResponse();
  611. $view = new Zend_View();
  612. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  613. $this->assertFalse($view->dojo()->isEnabled(), 'Dojo is enabled? ', $view->dojo());
  614. $this->assertNotContains('Foo', $view->headTitle()->__toString(), 'Head title persisted?');
  615. }
  616. /**
  617. * @group ZF-4070
  618. */
  619. public function testQueryParametersShouldPersistFollowingDispatch()
  620. {
  621. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  622. $request = $this->testCase->request;
  623. $request->setQuery('mr', 'proper')
  624. ->setQuery('james', 'bond');
  625. $this->assertEquals('proper', $request->getQuery('mr'), '(pre) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  626. $this->assertEquals('bond', $request->getQuery('james'), '(pre) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  627. $this->testCase->dispatch('/');
  628. $this->assertEquals('proper', $request->getQuery('mr'), '(post) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  629. $this->assertEquals('bond', $request->getQuery('james'), '(post) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  630. }
  631. /**
  632. * @group ZF-4070
  633. */
  634. public function testQueryStringShouldNotOverwritePreviouslySetQueryParameters()
  635. {
  636. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  637. $request = $this->testCase->request;
  638. $request->setQuery('mr', 'proper')
  639. ->setQuery('james', 'bond');
  640. $this->assertEquals('proper', $request->getQuery('mr'), '(pre) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  641. $this->assertEquals('bond', $request->getQuery('james'), '(pre) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  642. $this->testCase->dispatch('/?spy=super');
  643. $this->assertEquals('super', $request->getQuery('spy'), '(post) Failed retrieving spy parameter: ' . var_export($request->getQuery(), 1));
  644. $this->assertEquals('proper', $request->getQuery('mr'), '(post) Failed retrieving mr parameter: ' . var_export($request->getQuery(), 1));
  645. $this->assertEquals('bond', $request->getQuery('james'), '(post) Failed retrieving james parameter: ' . var_export($request->getQuery(), 1));
  646. }
  647. /**
  648. * @group ZF-3979
  649. */
  650. public function testSuperGlobalArraysShouldBeClearedDuringSetUp()
  651. {
  652. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  653. $request = $this->testCase->request;
  654. $request->setQuery('mr', 'proper')
  655. ->setPost('foo', 'bar')
  656. ->setCookie('bar', 'baz');
  657. $this->testCase->setUp();
  658. $this->assertNull($request->getQuery('mr'), 'Retrieved mr get parameter: ' . var_export($request->getQuery(), 1));
  659. $this->assertNull($request->getPost('foo'), 'Retrieved foo post parameter: ' . var_export($request->getPost(), 1));
  660. $this->assertNull($request->getCookie('bar'), 'Retrieved bar cookie parameter: ' . var_export($request->getCookie(), 1));
  661. }
  662. /**
  663. * @group ZF-4511
  664. */
  665. public function testResetRequestShouldClearPostAndQueryParameters()
  666. {
  667. $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
  668. $this->testCase->getRequest()->setPost(array(
  669. 'foo' => 'bar',
  670. ));
  671. $this->testCase->getRequest()->setQuery(array(
  672. 'bar' => 'baz',
  673. ));
  674. $this->testCase->dispatch('/zend-test-php-unit-foo/baz');
  675. $this->testCase->resetRequest();
  676. $this->assertTrue(empty($_POST));
  677. $this->assertTrue(empty($_GET));
  678. }
  679. /**
  680. * @group ZF-7839
  681. */
  682. public function testTestCaseShouldAllowUsingApplicationObjectAsBootstrap()
  683. {
  684. require_once 'Zend/Application.php';
  685. $application = new Zend_Application('testing', array(
  686. 'resources' => array(
  687. 'frontcontroller' => array(
  688. 'controllerDirectory' => dirname(__FILE__) . '/_files/application/controllers',
  689. ),
  690. ),
  691. ));
  692. $this->testCase->bootstrap = $application;
  693. $this->testCase->bootstrap();
  694. $this->assertEquals(
  695. $application->getBootstrap()->getResource('frontcontroller'),
  696. $this->testCase->getFrontController()
  697. );
  698. }
  699. /**
  700. * @group ZF-8193
  701. */
  702. public function testWhenApplicationObjectUsedAsBootstrapTestCaseShouldExecuteBootstrapRunMethod()
  703. {
  704. require_once 'Zend/Application.php';
  705. $application = new Zend_Application('testing', array(
  706. 'resources' => array(
  707. 'frontcontroller' => array(
  708. 'controllerDirectory' => dirname(__FILE__) . '/_files/application/controllers',
  709. ),
  710. ),
  711. ));
  712. $this->testCase->bootstrap = $application;
  713. $this->testCase->bootstrap();
  714. $this->testCase->dispatch('/');
  715. $front = $application->getBootstrap()->getResource('frontcontroller');
  716. $boot = $front->getParam('bootstrap');
  717. $type = is_object($boot)
  718. ? get_class($boot)
  719. : gettype($boot);
  720. $this->assertTrue($boot === $this->testCase->bootstrap->getBootstrap(), $type);
  721. }
  722. }
  723. // Concrete test case class for testing purposes
  724. class Zend_Test_PHPUnit_ControllerTestCaseTest_Concrete extends Zend_Test_PHPUnit_ControllerTestCase
  725. {
  726. }
  727. // Call Zend_Test_PHPUnit_ControllerTestCaseTest::main() if this source file is executed directly.
  728. if (PHPUnit_MAIN_METHOD == "Zend_Test_PHPUnit_ControllerTestCaseTest::main") {
  729. Zend_Test_PHPUnit_ControllerTestCaseTest::main();
  730. }