StandardTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_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-2009 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. public function testSetGetResponse()
  121. {
  122. $response = new Zend_Controller_Response_Cli();
  123. $this->_dispatcher->setResponse($response);
  124. $this->assertTrue($response === $this->_dispatcher->getResponse());
  125. }
  126. public function testSetGetDefaultControllerName()
  127. {
  128. $this->assertEquals('index', $this->_dispatcher->getDefaultControllerName());
  129. $this->_dispatcher->setDefaultControllerName('foo');
  130. $this->assertEquals('foo', $this->_dispatcher->getDefaultControllerName());
  131. }
  132. public function testSetGetDefaultAction()
  133. {
  134. $this->assertEquals('index', $this->_dispatcher->getDefaultAction());
  135. $this->_dispatcher->setDefaultAction('bar');
  136. $this->assertEquals('bar', $this->_dispatcher->getDefaultAction());
  137. }
  138. public function testDispatchValidControllerDefaultAction()
  139. {
  140. $request = new Zend_Controller_Request_Http();
  141. $request->setControllerName('index');
  142. $response = new Zend_Controller_Response_Cli();
  143. $this->_dispatcher->dispatch($request, $response);
  144. $this->assertContains('Index action called', $this->_dispatcher->getResponse()->getBody());
  145. }
  146. public function testDispatchValidControllerAndAction()
  147. {
  148. $request = new Zend_Controller_Request_Http();
  149. $request->setControllerName('index');
  150. $request->setActionName('index');
  151. $response = new Zend_Controller_Response_Cli();
  152. $this->_dispatcher->dispatch($request, $response);
  153. $this->assertContains('Index action called', $this->_dispatcher->getResponse()->getBody());
  154. }
  155. public function testDispatchValidControllerWithInvalidAction()
  156. {
  157. $request = new Zend_Controller_Request_Http();
  158. $request->setControllerName('index');
  159. $request->setActionName('foo');
  160. $response = new Zend_Controller_Response_Cli();
  161. try {
  162. $this->_dispatcher->dispatch($request, $response);
  163. $this->fail('Exception should be raised by __call');
  164. } catch (Exception $e) {
  165. // success
  166. }
  167. }
  168. public function testDispatchInvalidController()
  169. {
  170. $request = new Zend_Controller_Request_Http();
  171. $request->setControllerName('bogus');
  172. $response = new Zend_Controller_Response_Cli();
  173. try {
  174. $this->_dispatcher->dispatch($request, $response);
  175. $this->fail('Exception should be raised; no such controller');
  176. } catch (Exception $e) {
  177. // success
  178. }
  179. }
  180. public function testDispatchInvalidControllerUsingDefaults()
  181. {
  182. $request = new Zend_Controller_Request_Http();
  183. $request->setControllerName('bogus');
  184. $response = new Zend_Controller_Response_Cli();
  185. $this->_dispatcher->setParam('useDefaultControllerAlways', true);
  186. try {
  187. $this->_dispatcher->dispatch($request, $response);
  188. $this->assertEquals('index', $request->getControllerName());
  189. $this->assertEquals('index', $request->getActionName());
  190. } catch (Exception $e) {
  191. $this->fail('Exception should not be raised when useDefaultControllerAlways set; message: ' . $e->getMessage());
  192. }
  193. }
  194. /**
  195. * @group ZF-3465
  196. */
  197. public function testUsingDefaultControllerAlwaysShouldRewriteActionNameToDefault()
  198. {
  199. $request = new Zend_Controller_Request_Http();
  200. $request->setControllerName('bogus');
  201. $request->setActionName('really');
  202. $request->setParam('action', 'really'); // router sets action as a param
  203. $response = new Zend_Controller_Response_Cli();
  204. $this->_dispatcher->setParam('useDefaultControllerAlways', true);
  205. try {
  206. $this->_dispatcher->dispatch($request, $response);
  207. } catch (Zend_Controller_Dispatcher_Exception $e) {
  208. $this->fail('Exception should not be raised when useDefaultControllerAlways set; message: ' . $e->getMessage());
  209. }
  210. $this->assertEquals('index', $request->getControllerName());
  211. $this->assertEquals('index', $request->getActionName());
  212. }
  213. public function testDispatchInvalidControllerUsingDefaultsWithDefaultModule()
  214. {
  215. $request = new Zend_Controller_Request_Http();
  216. $request->setControllerName('bogus')
  217. ->setModuleName('default');
  218. $response = new Zend_Controller_Response_Cli();
  219. $this->_dispatcher->setParam('useDefaultControllerAlways', true);
  220. try {
  221. $this->_dispatcher->dispatch($request, $response);
  222. $this->assertSame('default', $request->getModuleName());
  223. $this->assertSame('index', $request->getControllerName());
  224. $this->assertSame('index', $request->getActionName());
  225. } catch (Exception $e) {
  226. $this->fail('Exception should not be raised when useDefaultControllerAlways set; exception: ' . $e->getMessage());
  227. }
  228. }
  229. public function testDispatchValidControllerWithPrePostDispatch()
  230. {
  231. $request = new Zend_Controller_Request_Http();
  232. $request->setControllerName('foo');
  233. $request->setActionName('bar');
  234. $response = new Zend_Controller_Response_Cli();
  235. $this->_dispatcher->dispatch($request, $response);
  236. $body = $this->_dispatcher->getResponse()->getBody();
  237. $this->assertContains('Bar action called', $body);
  238. $this->assertContains('preDispatch called', $body);
  239. $this->assertContains('postDispatch called', $body);
  240. }
  241. public function testDispatchNoControllerUsesDefaults()
  242. {
  243. $request = new Zend_Controller_Request_Http();
  244. $response = new Zend_Controller_Response_Cli();
  245. $this->_dispatcher->dispatch($request, $response);
  246. $this->assertEquals('index', $request->getControllerName());
  247. $this->assertEquals('index', $request->getActionName());
  248. }
  249. /**
  250. * Tests ZF-637 -- action names with underscores not being correctly changed to camelCase
  251. */
  252. public function testZf637()
  253. {
  254. $test = $this->_dispatcher->formatActionName('view_entry');
  255. $this->assertEquals('viewentryAction', $test);
  256. }
  257. public function testWordDelimiter()
  258. {
  259. $this->assertEquals(array('-', '.'), $this->_dispatcher->getWordDelimiter());
  260. $this->_dispatcher->setWordDelimiter(':');
  261. $this->assertEquals(array(':'), $this->_dispatcher->getWordDelimiter());
  262. }
  263. public function testPathDelimiter()
  264. {
  265. $this->assertEquals('_', $this->_dispatcher->getPathDelimiter());
  266. $this->_dispatcher->setPathDelimiter(':');
  267. $this->assertEquals(':', $this->_dispatcher->getPathDelimiter());
  268. }
  269. /**
  270. * Test that classes are found in modules, using a prefix
  271. */
  272. public function testModules()
  273. {
  274. $request = new Zend_Controller_Request_Http();
  275. $request->setModuleName('admin');
  276. $request->setControllerName('foo');
  277. $request->setActionName('bar');
  278. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  279. $response = new Zend_Controller_Response_Cli();
  280. $this->_dispatcher->dispatch($request, $response);
  281. $body = $this->_dispatcher->getResponse()->getBody();
  282. $this->assertContains("Admin_Foo::bar action called", $body, $body);
  283. }
  284. public function testModuleControllerInSubdirWithCamelCaseAction()
  285. {
  286. $request = new Zend_Controller_Request_Http();
  287. $request->setModuleName('admin');
  288. $request->setControllerName('foo-bar');
  289. $request->setActionName('baz.bat');
  290. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  291. $response = new Zend_Controller_Response_Cli();
  292. $this->_dispatcher->dispatch($request, $response);
  293. $body = $this->_dispatcher->getResponse()->getBody();
  294. $this->assertContains("Admin_FooBar::bazBat action called", $body, $body);
  295. }
  296. public function testUseModuleDefaultController()
  297. {
  298. $this->_dispatcher->setDefaultControllerName('foo')
  299. ->setParam('useDefaultControllerAlways', true);
  300. $request = new Zend_Controller_Request_Http();
  301. $request->setModuleName('admin');
  302. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  303. $response = new Zend_Controller_Response_Cli();
  304. $this->_dispatcher->dispatch($request, $response);
  305. $body = $this->_dispatcher->getResponse()->getBody();
  306. $this->assertContains("Admin_Foo::index action called", $body, $body);
  307. }
  308. public function testNoModuleOrControllerDefaultsCorrectly()
  309. {
  310. $request = new Zend_Controller_Request_Http('http://example.com/');
  311. $this->assertFalse($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  312. $response = new Zend_Controller_Response_Cli();
  313. $this->_dispatcher->dispatch($request, $response);
  314. $body = $this->_dispatcher->getResponse()->getBody();
  315. $this->assertContains("Index action called", $body, $body);
  316. }
  317. public function testOutputBuffering()
  318. {
  319. $request = new Zend_Controller_Request_Http();
  320. $request->setControllerName('ob');
  321. $request->setActionName('index');
  322. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  323. $response = new Zend_Controller_Response_Cli();
  324. $this->_dispatcher->dispatch($request, $response);
  325. $body = $this->_dispatcher->getResponse()->getBody();
  326. $this->assertContains("OB index action called", $body, $body);
  327. }
  328. public function testDisableOutputBuffering()
  329. {
  330. if (!defined('TESTS_ZEND_CONTROLLER_DISPATCHER_OB') || !TESTS_ZEND_CONTROLLER_DISPATCHER_OB) {
  331. $this->markTestSkipped('Skipping output buffer disabling in Zend_Controller_Dispatcher_Standard');
  332. }
  333. $request = new Zend_Controller_Request_Http();
  334. $request->setControllerName('ob');
  335. $request->setActionName('index');
  336. $this->_dispatcher->setParam('disableOutputBuffering', true);
  337. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  338. $response = new Zend_Controller_Response_Cli();
  339. $this->_dispatcher->dispatch($request, $response);
  340. $body = $this->_dispatcher->getResponse()->getBody();
  341. $this->assertEquals('', $body, $body);
  342. }
  343. public function testModuleSubdirControllerFound()
  344. {
  345. Zend_Controller_Front::getInstance()
  346. ->setDispatcher($this->_dispatcher)
  347. ->addControllerDirectory(
  348. dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR . 'controllers',
  349. 'foo'
  350. );
  351. $request = new Zend_Controller_Request_Http();
  352. $request->setModuleName('foo');
  353. $request->setControllerName('admin_index');
  354. $request->setActionName('index');
  355. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  356. $response = new Zend_Controller_Response_Cli();
  357. $this->_dispatcher->dispatch($request, $response);
  358. $body = $this->_dispatcher->getResponse()->getBody();
  359. $this->assertContains("Foo_Admin_IndexController::indexAction() called", $body, $body);
  360. }
  361. public function testDefaultModule()
  362. {
  363. $this->assertEquals('default', $this->_dispatcher->getDefaultModule());
  364. $this->_dispatcher->setDefaultModule('foobar');
  365. $this->assertEquals('foobar', $this->_dispatcher->getDefaultModule());
  366. }
  367. public function testModuleValid()
  368. {
  369. $this->assertTrue($this->_dispatcher->isValidModule('default'));
  370. $this->assertTrue($this->_dispatcher->isValidModule('admin'));
  371. $this->assertFalse($this->_dispatcher->isValidModule('bogus'));
  372. $this->assertFalse($this->_dispatcher->isValidModule(null));
  373. $this->assertFalse($this->_dispatcher->isValidModule($this));
  374. $this->assertFalse($this->_dispatcher->isValidModule(array()));
  375. }
  376. /**
  377. * @see ZF-3034
  378. */
  379. public function testIsValidModuleShouldNormalizeModuleName()
  380. {
  381. $this->assertTrue($this->_dispatcher->isValidModule('Admin'));
  382. }
  383. public function testSanelyDiscardOutputBufferOnException()
  384. {
  385. $request = new Zend_Controller_Request_Http();
  386. $request->setControllerName('ob');
  387. $request->setActionName('exception');
  388. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  389. $response = new Zend_Controller_Response_Cli();
  390. try {
  391. $this->_dispatcher->dispatch($request, $response);
  392. $this->fail('Exception should have been rethrown');
  393. } catch (Exception $e) {
  394. }
  395. $body = $this->_dispatcher->getResponse()->getBody();
  396. $this->assertNotContains("In exception action", $body, $body);
  397. $this->assertNotContains("Foo", $body, $body);
  398. }
  399. public function testGetDefaultControllerClassResetsRequestObject()
  400. {
  401. $request = new Zend_Controller_Request_Http();
  402. $request->setModuleName('foobar')
  403. ->setControllerName('bazbatbegone')
  404. ->setActionName('bebop');
  405. $this->_dispatcher->getDefaultControllerClass($request);
  406. $this->assertEquals($this->_dispatcher->getDefaultModule(), $request->getModuleName());
  407. $this->assertEquals($this->_dispatcher->getDefaultControllerName(), $request->getControllerName());
  408. $this->assertNull($request->getActionName());
  409. }
  410. public function testLoadClassLoadsControllerInDefaultModuleWithoutModulePrefix()
  411. {
  412. $request = new Zend_Controller_Request_Simple();
  413. $request->setControllerName('empty');
  414. $class = $this->_dispatcher->getControllerClass($request);
  415. $this->assertEquals('EmptyController', $class);
  416. $test = $this->_dispatcher->loadClass($class);
  417. $this->assertEquals($class, $test);
  418. $this->assertTrue(class_exists($class));
  419. }
  420. public function testLoadClassLoadsControllerInSpecifiedModuleWithModulePrefix()
  421. {
  422. Zend_Controller_Front::getInstance()
  423. ->setDispatcher($this->_dispatcher)
  424. ->addModuleDirectory(dirname(__FILE__) . '/../_files/modules');
  425. $request = new Zend_Controller_Request_Simple();
  426. $request->setControllerName('index')
  427. ->setModuleName('bar');
  428. $class = $this->_dispatcher->getControllerClass($request);
  429. $this->assertEquals('IndexController', $class);
  430. $test = $this->_dispatcher->loadClass($class);
  431. $this->assertEquals('Bar_IndexController', $test);
  432. $this->assertTrue(class_exists($test));
  433. }
  434. public function testLoadClassLoadsControllerInDefaultModuleWithModulePrefixWhenRequested()
  435. {
  436. Zend_Controller_Front::getInstance()
  437. ->setDispatcher($this->_dispatcher)
  438. ->addModuleDirectory(dirname(__FILE__) . '/../_files/modules');
  439. $this->_dispatcher->setDefaultModule('foo')
  440. ->setParam('prefixDefaultModule', true);
  441. $request = new Zend_Controller_Request_Simple();
  442. $request->setControllerName('index');
  443. $class = $this->_dispatcher->getControllerClass($request);
  444. $this->assertEquals('IndexController', $class);
  445. $test = $this->_dispatcher->loadClass($class);
  446. $this->assertEquals('Foo_IndexController', $test);
  447. $this->assertTrue(class_exists($test));
  448. }
  449. /**
  450. * ZF-2435
  451. */
  452. public function testCanRemoveControllerDirectory()
  453. {
  454. Zend_Controller_Front::getInstance()
  455. ->setDispatcher($this->_dispatcher)
  456. ->addModuleDirectory(dirname(__FILE__) . '/../_files/modules');
  457. $dirs = $this->_dispatcher->getControllerDirectory();
  458. $this->_dispatcher->removeControllerDirectory('foo');
  459. $test = $this->_dispatcher->getControllerDirectory();
  460. $this->assertNotEquals($dirs, $test);
  461. $this->assertFalse(array_key_exists('foo', $test));
  462. }
  463. /**
  464. * ZF-2693
  465. */
  466. public function testCamelCasedActionsNotRequestedWithWordSeparatorsShouldNotResolve()
  467. {
  468. $request = new Zend_Controller_Request_Http();
  469. $request->setModuleName('admin');
  470. $request->setControllerName('foo-bar');
  471. $request->setActionName('bazBat');
  472. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  473. $response = new Zend_Controller_Response_Cli();
  474. try {
  475. $this->_dispatcher->dispatch($request, $response);
  476. $this->fail('Invalid camelCased action should raise exception');
  477. } catch (Zend_Controller_Exception $e) {
  478. $this->assertContains('does not exist', $e->getMessage());
  479. }
  480. }
  481. /**
  482. * ZF-2693
  483. */
  484. public function testCamelCasedActionsNotRequestedWithWordSeparatorsShouldResolveIfForced()
  485. {
  486. $this->_dispatcher->setParam('useCaseSensitiveActions', true);
  487. $request = new Zend_Controller_Request_Http();
  488. $request->setModuleName('admin');
  489. $request->setControllerName('foo-bar');
  490. $request->setActionName('bazBat');
  491. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  492. $response = new Zend_Controller_Response_Cli();
  493. $oldLevel = error_reporting(0);
  494. try {
  495. $this->_dispatcher->dispatch($request, $response);
  496. $body = $this->_dispatcher->getResponse()->getBody();
  497. error_reporting($oldLevel);
  498. $this->assertContains("Admin_FooBar::bazBat action called", $body, $body);
  499. } catch (Zend_Controller_Exception $e) {
  500. error_reporting($oldLevel);
  501. $this->fail('camelCased actions should succeed when forced');
  502. }
  503. }
  504. public function handleErrors($errno, $errstr)
  505. {
  506. $this->error = $errstr;
  507. }
  508. /**
  509. * @see ZF-2693
  510. */
  511. public function testForcingCamelCasedActionsNotRequestedWithWordSeparatorsShouldRaiseNotices()
  512. {
  513. $this->_dispatcher->setParam('useCaseSensitiveActions', true);
  514. $request = new Zend_Controller_Request_Http();
  515. $request->setModuleName('admin');
  516. $request->setControllerName('foo-bar');
  517. $request->setActionName('bazBat');
  518. $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
  519. $response = new Zend_Controller_Response_Cli();
  520. set_error_handler(array($this, 'handleErrors'));
  521. try {
  522. $this->_dispatcher->dispatch($request, $response);
  523. $body = $this->_dispatcher->getResponse()->getBody();
  524. restore_error_handler();
  525. $this->assertTrue(isset($this->error));
  526. $this->assertContains('deprecated', $this->error);
  527. } catch (Zend_Controller_Exception $e) {
  528. restore_error_handler();
  529. $this->fail('camelCased actions should succeed when forced');
  530. }
  531. }
  532. /**
  533. * @see ZF-2887
  534. */
  535. public function testGetControllerClassThrowsExceptionIfNoDefaultModuleDefined()
  536. {
  537. $this->_dispatcher->setControllerDirectory(array());
  538. $request = new Zend_Controller_Request_Simple();
  539. $request->setControllerName('empty');
  540. try {
  541. $class = $this->_dispatcher->getControllerClass($request);
  542. } catch (Zend_Controller_Exception $e) {
  543. $this->assertContains('No default module', $e->getMessage());
  544. }
  545. }
  546. }
  547. // Call Zend_Controller_Dispatcher_StandardTest::main() if this source file is executed directly.
  548. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Dispatcher_StandardTest::main") {
  549. Zend_Controller_Dispatcher_StandardTest::main();
  550. }