2
0

StandardTest.php 26 KB

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