StandardTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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_Controller
  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_Controller_Dispatcher_StandardTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Dispatcher_StandardTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/Controller/Dispatcher/Standard.php';
  28. require_once 'Zend/Controller/Action/HelperBroker.php';
  29. require_once 'Zend/Controller/Front.php';
  30. require_once 'Zend/Controller/Request/Http.php';
  31. require_once 'Zend/Controller/Request/Simple.php';
  32. require_once 'Zend/Controller/Response/Cli.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Controller
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Controller
  40. * @group Zend_Controller_Dispatcher
  41. */
  42. class Zend_Controller_Dispatcher_StandardTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_dispatcher;
  45. /**
  46. * Runs the test methods of this class.
  47. *
  48. * @access public
  49. * @static
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Dispatcher_StandardTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. public function setUp()
  57. {
  58. if (isset($this->error)) {
  59. unset($this->error);
  60. }
  61. $front = Zend_Controller_Front::getInstance();
  62. $front->resetInstance();
  63. Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  64. $this->_dispatcher = new Zend_Controller_Dispatcher_Standard();
  65. $this->_dispatcher->setControllerDirectory(array(
  66. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  67. 'admin' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin'
  68. ));
  69. }
  70. public function tearDown()
  71. {
  72. unset($this->_dispatcher);
  73. }
  74. public function testFormatControllerName()
  75. {
  76. $this->assertEquals('IndexController', $this->_dispatcher->formatControllerName('index'));
  77. $this->assertEquals('Site_CustomController', $this->_dispatcher->formatControllerName('site_custom'));
  78. }
  79. public function testFormatActionName()
  80. {
  81. $this->assertEquals('indexAction', $this->_dispatcher->formatActionName('index'));
  82. $this->assertEquals('myindexAction', $this->_dispatcher->formatActionName('myIndex'));
  83. $this->assertEquals('myindexAction', $this->_dispatcher->formatActionName('my_index'));
  84. $this->assertEquals('myIndexAction', $this->_dispatcher->formatActionName('my.index'));
  85. $this->assertEquals('myIndexAction', $this->_dispatcher->formatActionName('my-index'));
  86. }
  87. public function testSetGetControllerDirectory()
  88. {
  89. $expected = array(
  90. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  91. 'admin' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin'
  92. );
  93. $dirs = $this->_dispatcher->getControllerDirectory();
  94. $this->assertEquals($expected, $dirs);
  95. }
  96. public function testIsDispatchable()
  97. {
  98. $request = new Zend_Controller_Request_Http();
  99. $this->assertFalse($this->_dispatcher->isDispatchable($request));
  100. $request->setControllerName('index');
  101. $this->assertTrue($this->_dispatcher->isDispatchable($request));
  102. $request->setControllerName('foo');
  103. $this->assertTrue($this->_dispatcher->isDispatchable($request));
  104. // True, because it will dispatch to default controller
  105. $request->setControllerName('bogus');
  106. $this->assertFalse($this->_dispatcher->isDispatchable($request));
  107. }
  108. public function testModuleIsDispatchable()
  109. {
  110. $request = new Zend_Controller_Request_Http();
  111. $request->setModuleName('admin');
  112. $request->setControllerName('foo');
  113. $request->setActionName('bar');
  114. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  115. $request->setModuleName('bogus');
  116. $request->setControllerName('bogus');
  117. $request->setActionName('bar');
  118. $this->assertFalse($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  119. }
  120. /**
  121. * @group ZF-8222
  122. */
  123. public function testIsDispatchableManuallyIncludedController()
  124. {
  125. require_once dirname(__FILE__) . '/../_files/ManuallyIncludedControllers.php';
  126. $request = new Zend_Controller_Request_Http();
  127. $this->_dispatcher->setParam('prefixDefaultModule', true);
  128. $request->setControllerName('included');
  129. $this->assertFalse($this->_dispatcher->isDispatchable($request));
  130. $request->setControllerName('included-prefix');
  131. $this->assertTrue($this->_dispatcher->isDispatchable($request));
  132. $this->_dispatcher->setParam('prefixDefaultModule', false);
  133. $request->setModuleName('admin');
  134. $request->setControllerName('included-admin');
  135. $this->assertTrue($this->_dispatcher->isDispatchable($request));
  136. }
  137. public function testSetGetResponse()
  138. {
  139. $response = new Zend_Controller_Response_Cli();
  140. $this->_dispatcher->setResponse($response);
  141. $this->assertTrue($response === $this->_dispatcher->getResponse());
  142. }
  143. public function testSetGetDefaultControllerName()
  144. {
  145. $this->assertEquals('index', $this->_dispatcher->getDefaultControllerName());
  146. $this->_dispatcher->setDefaultControllerName('foo');
  147. $this->assertEquals('foo', $this->_dispatcher->getDefaultControllerName());
  148. }
  149. public function testSetGetDefaultAction()
  150. {
  151. $this->assertEquals('index', $this->_dispatcher->getDefaultAction());
  152. $this->_dispatcher->setDefaultAction('bar');
  153. $this->assertEquals('bar', $this->_dispatcher->getDefaultAction());
  154. }
  155. public function testDispatchValidControllerDefaultAction()
  156. {
  157. $request = new Zend_Controller_Request_Http();
  158. $request->setControllerName('index');
  159. $response = new Zend_Controller_Response_Cli();
  160. $this->_dispatcher->dispatch($request, $response);
  161. $this->assertContains('Index action called', $this->_dispatcher->getResponse()->getBody());
  162. }
  163. public function testDispatchValidControllerAndAction()
  164. {
  165. $request = new Zend_Controller_Request_Http();
  166. $request->setControllerName('index');
  167. $request->setActionName('index');
  168. $response = new Zend_Controller_Response_Cli();
  169. $this->_dispatcher->dispatch($request, $response);
  170. $this->assertContains('Index action called', $this->_dispatcher->getResponse()->getBody());
  171. }
  172. public function testDispatchValidControllerWithInvalidAction()
  173. {
  174. $request = new Zend_Controller_Request_Http();
  175. $request->setControllerName('index');
  176. $request->setActionName('foo');
  177. $response = new Zend_Controller_Response_Cli();
  178. try {
  179. $this->_dispatcher->dispatch($request, $response);
  180. $this->fail('Exception should be raised by __call');
  181. } catch (Exception $e) {
  182. // success
  183. }
  184. }
  185. public function testDispatchInvalidController()
  186. {
  187. $request = new Zend_Controller_Request_Http();
  188. $request->setControllerName('bogus');
  189. $response = new Zend_Controller_Response_Cli();
  190. try {
  191. $this->_dispatcher->dispatch($request, $response);
  192. $this->fail('Exception should be raised; no such controller');
  193. } catch (Exception $e) {
  194. // success
  195. }
  196. }
  197. public function testDispatchInvalidControllerUsingDefaults()
  198. {
  199. $request = new Zend_Controller_Request_Http();
  200. $request->setControllerName('bogus');
  201. $response = new Zend_Controller_Response_Cli();
  202. $this->_dispatcher->setParam('useDefaultControllerAlways', true);
  203. try {
  204. $this->_dispatcher->dispatch($request, $response);
  205. $this->assertEquals('index', $request->getControllerName());
  206. $this->assertEquals('index', $request->getActionName());
  207. } catch (Exception $e) {
  208. $this->fail('Exception should not be raised when useDefaultControllerAlways set; message: ' . $e->getMessage());
  209. }
  210. }
  211. /**
  212. * @group ZF-3465
  213. */
  214. public function testUsingDefaultControllerAlwaysShouldRewriteActionNameToDefault()
  215. {
  216. $request = new Zend_Controller_Request_Http();
  217. $request->setControllerName('bogus');
  218. $request->setActionName('really');
  219. $request->setParam('action', 'really'); // router sets action as a param
  220. $response = new Zend_Controller_Response_Cli();
  221. $this->_dispatcher->setParam('useDefaultControllerAlways', true);
  222. try {
  223. $this->_dispatcher->dispatch($request, $response);
  224. } catch (Zend_Controller_Dispatcher_Exception $e) {
  225. $this->fail('Exception should not be raised when useDefaultControllerAlways set; message: ' . $e->getMessage());
  226. }
  227. $this->assertEquals('index', $request->getControllerName());
  228. $this->assertEquals('index', $request->getActionName());
  229. }
  230. public function testDispatchInvalidControllerUsingDefaultsWithDefaultModule()
  231. {
  232. $request = new Zend_Controller_Request_Http();
  233. $request->setControllerName('bogus')
  234. ->setModuleName('default');
  235. $response = new Zend_Controller_Response_Cli();
  236. $this->_dispatcher->setParam('useDefaultControllerAlways', true);
  237. try {
  238. $this->_dispatcher->dispatch($request, $response);
  239. $this->assertSame('default', $request->getModuleName());
  240. $this->assertSame('index', $request->getControllerName());
  241. $this->assertSame('index', $request->getActionName());
  242. } catch (Exception $e) {
  243. $this->fail('Exception should not be raised when useDefaultControllerAlways set; exception: ' . $e->getMessage());
  244. }
  245. }
  246. public function testDispatchValidControllerWithPrePostDispatch()
  247. {
  248. $request = new Zend_Controller_Request_Http();
  249. $request->setControllerName('foo');
  250. $request->setActionName('bar');
  251. $response = new Zend_Controller_Response_Cli();
  252. $this->_dispatcher->dispatch($request, $response);
  253. $body = $this->_dispatcher->getResponse()->getBody();
  254. $this->assertContains('Bar action called', $body);
  255. $this->assertContains('preDispatch called', $body);
  256. $this->assertContains('postDispatch called', $body);
  257. }
  258. public function testDispatchNoControllerUsesDefaults()
  259. {
  260. $request = new Zend_Controller_Request_Http();
  261. $response = new Zend_Controller_Response_Cli();
  262. $this->_dispatcher->dispatch($request, $response);
  263. $this->assertEquals('index', $request->getControllerName());
  264. $this->assertEquals('index', $request->getActionName());
  265. }
  266. /**
  267. * Tests ZF-637 -- action names with underscores not being correctly changed to camelCase
  268. */
  269. public function testZf637()
  270. {
  271. $test = $this->_dispatcher->formatActionName('view_entry');
  272. $this->assertEquals('viewentryAction', $test);
  273. }
  274. public function testWordDelimiter()
  275. {
  276. $this->assertEquals(array('-', '.'), $this->_dispatcher->getWordDelimiter());
  277. $this->_dispatcher->setWordDelimiter(':');
  278. $this->assertEquals(array(':'), $this->_dispatcher->getWordDelimiter());
  279. }
  280. public function testPathDelimiter()
  281. {
  282. $this->assertEquals('_', $this->_dispatcher->getPathDelimiter());
  283. $this->_dispatcher->setPathDelimiter(':');
  284. $this->assertEquals(':', $this->_dispatcher->getPathDelimiter());
  285. }
  286. /**
  287. * Test that classes are found in modules, using a prefix
  288. */
  289. public function testModules()
  290. {
  291. $request = new Zend_Controller_Request_Http();
  292. $request->setModuleName('admin');
  293. $request->setControllerName('foo');
  294. $request->setActionName('bar');
  295. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  296. $response = new Zend_Controller_Response_Cli();
  297. $this->_dispatcher->dispatch($request, $response);
  298. $body = $this->_dispatcher->getResponse()->getBody();
  299. $this->assertContains("Admin_Foo::bar action called", $body, $body);
  300. }
  301. public function testModuleControllerInSubdirWithCamelCaseAction()
  302. {
  303. $request = new Zend_Controller_Request_Http();
  304. $request->setModuleName('admin');
  305. $request->setControllerName('foo-bar');
  306. $request->setActionName('baz.bat');
  307. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  308. $response = new Zend_Controller_Response_Cli();
  309. $this->_dispatcher->dispatch($request, $response);
  310. $body = $this->_dispatcher->getResponse()->getBody();
  311. $this->assertContains("Admin_FooBar::bazBat action called", $body, $body);
  312. }
  313. public function testUseModuleDefaultController()
  314. {
  315. $this->_dispatcher->setDefaultControllerName('foo')
  316. ->setParam('useDefaultControllerAlways', true);
  317. $request = new Zend_Controller_Request_Http();
  318. $request->setModuleName('admin');
  319. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  320. $response = new Zend_Controller_Response_Cli();
  321. $this->_dispatcher->dispatch($request, $response);
  322. $body = $this->_dispatcher->getResponse()->getBody();
  323. $this->assertContains("Admin_Foo::index action called", $body, $body);
  324. }
  325. public function testNoModuleOrControllerDefaultsCorrectly()
  326. {
  327. $request = new Zend_Controller_Request_Http('http://example.com/');
  328. $this->assertFalse($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  329. $response = new Zend_Controller_Response_Cli();
  330. $this->_dispatcher->dispatch($request, $response);
  331. $body = $this->_dispatcher->getResponse()->getBody();
  332. $this->assertContains("Index action called", $body, $body);
  333. }
  334. public function testOutputBuffering()
  335. {
  336. $request = new Zend_Controller_Request_Http();
  337. $request->setControllerName('ob');
  338. $request->setActionName('index');
  339. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  340. $response = new Zend_Controller_Response_Cli();
  341. $this->_dispatcher->dispatch($request, $response);
  342. $body = $this->_dispatcher->getResponse()->getBody();
  343. $this->assertContains("OB index action called", $body, $body);
  344. }
  345. public function testDisableOutputBuffering()
  346. {
  347. if (!defined('TESTS_ZEND_CONTROLLER_DISPATCHER_OB') || !TESTS_ZEND_CONTROLLER_DISPATCHER_OB) {
  348. $this->markTestSkipped('Skipping output buffer disabling in Zend_Controller_Dispatcher_Standard');
  349. }
  350. $request = new Zend_Controller_Request_Http();
  351. $request->setControllerName('ob');
  352. $request->setActionName('index');
  353. $this->_dispatcher->setParam('disableOutputBuffering', true);
  354. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  355. $response = new Zend_Controller_Response_Cli();
  356. $this->_dispatcher->dispatch($request, $response);
  357. $body = $this->_dispatcher->getResponse()->getBody();
  358. $this->assertEquals('', $body, $body);
  359. }
  360. public function testModuleSubdirControllerFound()
  361. {
  362. Zend_Controller_Front::getInstance()
  363. ->setDispatcher($this->_dispatcher)
  364. ->addControllerDirectory(
  365. dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR . 'controllers',
  366. 'foo'
  367. );
  368. $request = new Zend_Controller_Request_Http();
  369. $request->setModuleName('foo');
  370. $request->setControllerName('admin_index');
  371. $request->setActionName('index');
  372. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  373. $response = new Zend_Controller_Response_Cli();
  374. $this->_dispatcher->dispatch($request, $response);
  375. $body = $this->_dispatcher->getResponse()->getBody();
  376. $this->assertContains("Foo_Admin_IndexController::indexAction() called", $body, $body);
  377. }
  378. public function testDefaultModule()
  379. {
  380. $this->assertEquals('default', $this->_dispatcher->getDefaultModule());
  381. $this->_dispatcher->setDefaultModule('foobar');
  382. $this->assertEquals('foobar', $this->_dispatcher->getDefaultModule());
  383. }
  384. public function testModuleValid()
  385. {
  386. $this->assertTrue($this->_dispatcher->isValidModule('default'));
  387. $this->assertTrue($this->_dispatcher->isValidModule('admin'));
  388. $this->assertFalse($this->_dispatcher->isValidModule('bogus'));
  389. $this->assertFalse($this->_dispatcher->isValidModule(null));
  390. $this->assertFalse($this->_dispatcher->isValidModule($this));
  391. $this->assertFalse($this->_dispatcher->isValidModule(array()));
  392. }
  393. /**
  394. * @see ZF-3034
  395. */
  396. public function testIsValidModuleShouldNormalizeModuleName()
  397. {
  398. $this->assertTrue($this->_dispatcher->isValidModule('Admin'));
  399. }
  400. public function testSanelyDiscardOutputBufferOnException()
  401. {
  402. $request = new Zend_Controller_Request_Http();
  403. $request->setControllerName('ob');
  404. $request->setActionName('exception');
  405. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  406. $response = new Zend_Controller_Response_Cli();
  407. try {
  408. $this->_dispatcher->dispatch($request, $response);
  409. $this->fail('Exception should have been rethrown');
  410. } catch (Exception $e) {
  411. }
  412. $body = $this->_dispatcher->getResponse()->getBody();
  413. $this->assertNotContains("In exception action", $body, $body);
  414. $this->assertNotContains("Foo", $body, $body);
  415. }
  416. public function testGetDefaultControllerClassResetsRequestObject()
  417. {
  418. $request = new Zend_Controller_Request_Http();
  419. $request->setModuleName('foobar')
  420. ->setControllerName('bazbatbegone')
  421. ->setActionName('bebop');
  422. $this->_dispatcher->getDefaultControllerClass($request);
  423. $this->assertEquals($this->_dispatcher->getDefaultModule(), $request->getModuleName());
  424. $this->assertEquals($this->_dispatcher->getDefaultControllerName(), $request->getControllerName());
  425. $this->assertNull($request->getActionName());
  426. }
  427. public function testLoadClassLoadsControllerInDefaultModuleWithoutModulePrefix()
  428. {
  429. $request = new Zend_Controller_Request_Simple();
  430. $request->setControllerName('empty');
  431. $class = $this->_dispatcher->getControllerClass($request);
  432. $this->assertEquals('EmptyController', $class);
  433. $test = $this->_dispatcher->loadClass($class);
  434. $this->assertEquals($class, $test);
  435. $this->assertTrue(class_exists($class));
  436. }
  437. public function testLoadClassLoadsControllerInSpecifiedModuleWithModulePrefix()
  438. {
  439. Zend_Controller_Front::getInstance()
  440. ->setDispatcher($this->_dispatcher)
  441. ->addModuleDirectory(dirname(__FILE__) . '/../_files/modules');
  442. $request = new Zend_Controller_Request_Simple();
  443. $request->setControllerName('index')
  444. ->setModuleName('bar');
  445. $class = $this->_dispatcher->getControllerClass($request);
  446. $this->assertEquals('IndexController', $class);
  447. $test = $this->_dispatcher->loadClass($class);
  448. $this->assertEquals('Bar_IndexController', $test);
  449. $this->assertTrue(class_exists($test));
  450. }
  451. public function testLoadClassLoadsControllerInDefaultModuleWithModulePrefixWhenRequested()
  452. {
  453. Zend_Controller_Front::getInstance()
  454. ->setDispatcher($this->_dispatcher)
  455. ->addModuleDirectory(dirname(__FILE__) . '/../_files/modules');
  456. $this->_dispatcher->setDefaultModule('foo')
  457. ->setParam('prefixDefaultModule', true);
  458. $request = new Zend_Controller_Request_Simple();
  459. $request->setControllerName('index');
  460. $class = $this->_dispatcher->getControllerClass($request);
  461. $this->assertEquals('IndexController', $class);
  462. $test = $this->_dispatcher->loadClass($class);
  463. $this->assertEquals('Foo_IndexController', $test);
  464. $this->assertTrue(class_exists($test));
  465. }
  466. /**
  467. * ZF-2435
  468. */
  469. public function testCanRemoveControllerDirectory()
  470. {
  471. Zend_Controller_Front::getInstance()
  472. ->setDispatcher($this->_dispatcher)
  473. ->addModuleDirectory(dirname(__FILE__) . '/../_files/modules');
  474. $dirs = $this->_dispatcher->getControllerDirectory();
  475. $this->_dispatcher->removeControllerDirectory('foo');
  476. $test = $this->_dispatcher->getControllerDirectory();
  477. $this->assertNotEquals($dirs, $test);
  478. $this->assertFalse(array_key_exists('foo', $test));
  479. }
  480. /**
  481. * ZF-2693
  482. */
  483. public function testCamelCasedActionsNotRequestedWithWordSeparatorsShouldNotResolve()
  484. {
  485. $request = new Zend_Controller_Request_Http();
  486. $request->setModuleName('admin');
  487. $request->setControllerName('foo-bar');
  488. $request->setActionName('bazBat');
  489. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  490. $response = new Zend_Controller_Response_Cli();
  491. try {
  492. $this->_dispatcher->dispatch($request, $response);
  493. $this->fail('Invalid camelCased action should raise exception');
  494. } catch (Zend_Controller_Exception $e) {
  495. $this->assertContains('does not exist', $e->getMessage());
  496. }
  497. }
  498. /**
  499. * ZF-2693
  500. */
  501. public function testCamelCasedActionsNotRequestedWithWordSeparatorsShouldResolveIfForced()
  502. {
  503. $this->_dispatcher->setParam('useCaseSensitiveActions', true);
  504. $request = new Zend_Controller_Request_Http();
  505. $request->setModuleName('admin');
  506. $request->setControllerName('foo-bar');
  507. $request->setActionName('bazBat');
  508. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  509. $response = new Zend_Controller_Response_Cli();
  510. $oldLevel = error_reporting(0);
  511. try {
  512. $this->_dispatcher->dispatch($request, $response);
  513. $body = $this->_dispatcher->getResponse()->getBody();
  514. error_reporting($oldLevel);
  515. $this->assertContains("Admin_FooBar::bazBat action called", $body, $body);
  516. } catch (Zend_Controller_Exception $e) {
  517. error_reporting($oldLevel);
  518. $this->fail('camelCased actions should succeed when forced');
  519. }
  520. }
  521. public function handleErrors($errno, $errstr)
  522. {
  523. $this->error = $errstr;
  524. }
  525. /**
  526. * @see ZF-2693
  527. */
  528. public function testForcingCamelCasedActionsNotRequestedWithWordSeparatorsShouldRaiseNotices()
  529. {
  530. $this->_dispatcher->setParam('useCaseSensitiveActions', true);
  531. $request = new Zend_Controller_Request_Http();
  532. $request->setModuleName('admin');
  533. $request->setControllerName('foo-bar');
  534. $request->setActionName('bazBat');
  535. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  536. $response = new Zend_Controller_Response_Cli();
  537. set_error_handler(array($this, 'handleErrors'));
  538. try {
  539. $this->_dispatcher->dispatch($request, $response);
  540. $body = $this->_dispatcher->getResponse()->getBody();
  541. restore_error_handler();
  542. $this->assertTrue(isset($this->error));
  543. $this->assertContains('deprecated', $this->error);
  544. } catch (Zend_Controller_Exception $e) {
  545. restore_error_handler();
  546. $this->fail('camelCased actions should succeed when forced');
  547. }
  548. }
  549. /**
  550. * @see ZF-2887
  551. */
  552. public function testGetControllerClassThrowsExceptionIfNoDefaultModuleDefined()
  553. {
  554. $this->_dispatcher->setControllerDirectory(array());
  555. $request = new Zend_Controller_Request_Simple();
  556. $request->setControllerName('empty');
  557. try {
  558. $class = $this->_dispatcher->getControllerClass($request);
  559. } catch (Zend_Controller_Exception $e) {
  560. $this->assertContains('No default module', $e->getMessage());
  561. }
  562. }
  563. }
  564. // Call Zend_Controller_Dispatcher_StandardTest::main() if this source file is executed directly.
  565. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Dispatcher_StandardTest::main") {
  566. Zend_Controller_Dispatcher_StandardTest::main();
  567. }