2
0

FrontTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. // Call Zend_Controller_FrontTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_FrontTest::main");
  5. $basePath = realpath(dirname(__FILE__) . str_repeat(DIRECTORY_SEPARATOR . '..', 3));
  6. set_include_path(
  7. $basePath . DIRECTORY_SEPARATOR . 'tests'
  8. . PATH_SEPARATOR . $basePath . DIRECTORY_SEPARATOR . 'library'
  9. . PATH_SEPARATOR . get_include_path()
  10. );
  11. }
  12. require_once "PHPUnit/Framework/TestCase.php";
  13. require_once "PHPUnit/Framework/TestSuite.php";
  14. require_once 'Zend/Controller/Front.php';
  15. require_once 'Zend/Controller/Request/Http.php';
  16. require_once 'Zend/Controller/Response/Cli.php';
  17. require_once 'Zend/Controller/Dispatcher/Standard.php';
  18. require_once 'Zend/Controller/Router/Rewrite.php';
  19. require_once 'Zend/Controller/Action/HelperBroker.php';
  20. require_once 'Zend/Controller/Action/Helper/Url.php';
  21. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  22. class Zend_Controller_FrontTest extends PHPUnit_Framework_TestCase
  23. {
  24. protected $_controller = null;
  25. /**
  26. * Runs the test methods of this class.
  27. *
  28. * @access public
  29. * @static
  30. */
  31. public static function main()
  32. {
  33. require_once "PHPUnit/TextUI/TestRunner.php";
  34. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_FrontTest");
  35. $result = PHPUnit_TextUI_TestRunner::run($suite);
  36. }
  37. public function setUp()
  38. {
  39. $this->_controller = Zend_Controller_Front::getInstance();
  40. $this->_controller->resetInstance();
  41. $this->_controller->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files')
  42. ->setParam('noErrorHandler', true)
  43. ->setParam('noViewRenderer', true)
  44. ->returnResponse(true)
  45. ->throwExceptions(false);
  46. Zend_Controller_Action_HelperBroker::resetHelpers();
  47. }
  48. public function tearDown()
  49. {
  50. unset($this->_controller);
  51. }
  52. public function testResetInstance()
  53. {
  54. $this->_controller->setParam('foo', 'bar');
  55. $this->assertEquals('bar', $this->_controller->getParam('foo'));
  56. $this->_controller->resetInstance();
  57. $this->assertNull($this->_controller->getParam('bar'));
  58. $this->assertSame(array(), $this->_controller->getParams());
  59. $this->assertSame(array(), $this->_controller->getControllerDirectory());
  60. }
  61. /**
  62. * @see ZF-3145
  63. */
  64. public function testResetInstanceShouldResetHelperBroker()
  65. {
  66. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  67. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_Url());
  68. $helpers = Zend_Controller_Action_HelperBroker::getExistingHelpers();
  69. $this->assertTrue(is_array($helpers));
  70. $this->assertFalse(empty($helpers));
  71. $this->_controller->resetInstance();
  72. $helpers = Zend_Controller_Action_HelperBroker::getExistingHelpers();
  73. $this->assertTrue(is_array($helpers));
  74. $this->assertTrue(empty($helpers));
  75. }
  76. public function testSetGetRequest()
  77. {
  78. $request = new Zend_Controller_Request_Http();
  79. $this->_controller->setRequest($request);
  80. $this->assertTrue($request === $this->_controller->getRequest());
  81. $this->_controller->resetInstance();
  82. $this->_controller->setRequest('Zend_Controller_Request_Http');
  83. $request = $this->_controller->getRequest();
  84. $this->assertTrue($request instanceof Zend_Controller_Request_Http);
  85. }
  86. public function testSetRequestThrowsExceptionWithBadRequest()
  87. {
  88. try {
  89. $this->_controller->setRequest('Zend_Controller_Response_Cli');
  90. $this->fail('Should not be able to set invalid request class');
  91. } catch (Exception $e) {
  92. // success
  93. }
  94. }
  95. public function testSetGetResponse()
  96. {
  97. $response = new Zend_Controller_Response_Cli();
  98. $this->_controller->setResponse($response);
  99. $this->assertTrue($response === $this->_controller->getResponse());
  100. $this->_controller->resetInstance();
  101. $this->_controller->setResponse('Zend_Controller_Response_Cli');
  102. $response = $this->_controller->getResponse();
  103. $this->assertTrue($response instanceof Zend_Controller_Response_Cli);
  104. }
  105. public function testSetResponseThrowsExceptionWithBadResponse()
  106. {
  107. try {
  108. $this->_controller->setResponse('Zend_Controller_Request_Http');
  109. $this->fail('Should not be able to set invalid response class');
  110. } catch (Exception $e) {
  111. // success
  112. }
  113. }
  114. public function testSetGetRouter()
  115. {
  116. $router = new Zend_Controller_Router_Rewrite();
  117. $this->_controller->setRouter($router);
  118. $this->assertTrue($router === $this->_controller->getRouter());
  119. $this->_controller->resetInstance();
  120. $this->_controller->setRouter('Zend_Controller_Router_Rewrite');
  121. $router = $this->_controller->getRouter();
  122. $this->assertTrue($router instanceof Zend_Controller_Router_Rewrite);
  123. }
  124. public function testSetRouterThrowsExceptionWithBadRouter()
  125. {
  126. try {
  127. $this->_controller->setRouter('Zend_Controller_Request_Http');
  128. $this->fail('Should not be able to set invalid router class');
  129. } catch (Exception $e) {
  130. // success
  131. }
  132. }
  133. public function testSetGetDispatcher()
  134. {
  135. $dispatcher = new Zend_Controller_Dispatcher_Standard();
  136. $this->_controller->setDispatcher($dispatcher);
  137. $this->assertTrue($dispatcher === $this->_controller->getDispatcher());
  138. }
  139. public function testSetGetControllerDirectory()
  140. {
  141. $test = $this->_controller->getControllerDirectory();
  142. $expected = array('default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  143. $this->assertSame($expected, $test);
  144. }
  145. public function testGetSetParam()
  146. {
  147. $this->_controller->setParam('foo', 'bar');
  148. $this->assertEquals('bar', $this->_controller->getParam('foo'));
  149. $this->_controller->setParam('bar', 'baz');
  150. $this->assertEquals('baz', $this->_controller->getParam('bar'));
  151. }
  152. public function testGetSetParams()
  153. {
  154. $this->_controller->setParams(array('foo' => 'bar'));
  155. $params = $this->_controller->getParams();
  156. $this->assertTrue(isset($params['foo']));
  157. $this->assertEquals('bar', $params['foo']);
  158. $this->_controller->setParam('baz', 'bat');
  159. $params = $this->_controller->getParams();
  160. $this->assertTrue(isset($params['foo']));
  161. $this->assertEquals('bar', $params['foo']);
  162. $this->assertTrue(isset($params['baz']));
  163. $this->assertEquals('bat', $params['baz']);
  164. $this->_controller->setParams(array('foo' => 'bug'));
  165. $params = $this->_controller->getParams();
  166. $this->assertTrue(isset($params['foo']));
  167. $this->assertEquals('bug', $params['foo']);
  168. $this->assertTrue(isset($params['baz']));
  169. $this->assertEquals('bat', $params['baz']);
  170. }
  171. public function testClearParams()
  172. {
  173. $this->_controller->setParams(array('foo' => 'bar', 'baz' => 'bat'));
  174. $params = $this->_controller->getParams();
  175. $this->assertTrue(isset($params['foo']));
  176. $this->assertTrue(isset($params['baz']));
  177. $this->_controller->clearParams('foo');
  178. $params = $this->_controller->getParams();
  179. $this->assertFalse(isset($params['foo']));
  180. $this->assertTrue(isset($params['baz']));
  181. $this->_controller->clearParams();
  182. $this->assertSame(array(), $this->_controller->getParams());
  183. $this->_controller->setParams(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat'));
  184. $this->assertSame(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat'), $this->_controller->getParams());
  185. $this->_controller->clearParams(array('foo', 'baz'));
  186. $this->assertSame(array('bar' => 'baz'), $this->_controller->getParams());
  187. }
  188. public function testSetGetDefaultControllerName()
  189. {
  190. $this->assertEquals('index', $this->_controller->getDefaultControllerName());
  191. $this->_controller->setDefaultControllerName('foo');
  192. $this->assertEquals('foo', $this->_controller->getDefaultControllerName());
  193. }
  194. public function testSetGetDefaultAction()
  195. {
  196. $this->assertEquals('index', $this->_controller->getDefaultAction());
  197. $this->_controller->setDefaultAction('bar');
  198. $this->assertEquals('bar', $this->_controller->getDefaultAction());
  199. }
  200. /**
  201. * Test default action on valid controller
  202. */
  203. public function testDispatch()
  204. {
  205. $request = new Zend_Controller_Request_Http('http://example.com/index');
  206. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  207. $response = $this->_controller->dispatch($request);
  208. $this->assertContains('Index action called', $response->getBody());
  209. }
  210. /**
  211. * Test valid action on valid controller
  212. */
  213. public function testDispatch1()
  214. {
  215. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  216. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  217. $response = $this->_controller->dispatch($request);
  218. $this->assertContains('Index action called', $response->getBody());
  219. }
  220. /**
  221. * Test invalid action on valid controller
  222. */
  223. /*
  224. public function testDispatch2()
  225. {
  226. $request = new Zend_Controller_Request_Http('http://example.com/index/foo');
  227. try {
  228. $this->_controller->dispatch($request);
  229. $this->fail('Exception should be raised by __call');
  230. } catch (Exception $e) {
  231. // success
  232. }
  233. }
  234. */
  235. /**
  236. * Test invalid controller
  237. */
  238. /*
  239. public function testDispatch3()
  240. {
  241. $request = new Zend_Controller_Request_Http('http://example.com/baz');
  242. try {
  243. $this->_controller->dispatch($request);
  244. $this->fail('Exception should be raised; no such controller');
  245. } catch (Exception $e) {
  246. // success
  247. }
  248. }
  249. */
  250. /**
  251. * Test valid action on valid controller; test pre/postDispatch
  252. */
  253. public function testDispatch4()
  254. {
  255. $request = new Zend_Controller_Request_Http('http://example.com/foo/bar');
  256. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  257. $response = $this->_controller->dispatch($request);
  258. $body = $response->getBody();
  259. $this->assertContains('Bar action called', $body, $body);
  260. $this->assertContains('preDispatch called', $body, $body);
  261. $this->assertContains('postDispatch called', $body, $body);
  262. }
  263. /**
  264. * Test that extra arguments get passed
  265. */
  266. public function testDispatch5()
  267. {
  268. $request = new Zend_Controller_Request_Http('http://example.com/index/args');
  269. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  270. $this->_controller->setParam('foo', 'bar');
  271. $this->_controller->setParam('baz', 'bat');
  272. $response = $this->_controller->dispatch($request);
  273. $body = $response->getBody();
  274. $this->assertContains('foo: bar', $body, $body);
  275. $this->assertContains('baz: bat', $body);
  276. }
  277. /**
  278. * Test using router
  279. */
  280. public function testDispatch6()
  281. {
  282. $request = new Zend_Controller_Request_Http('http://framework.zend.com/foo/bar/var1/baz');
  283. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  284. $this->_controller->setRouter(new Zend_Controller_Router_Rewrite());
  285. $response = $this->_controller->dispatch($request);
  286. $body = $response->getBody();
  287. $this->assertContains('Bar action called', $body);
  288. $params = $request->getParams();
  289. $this->assertTrue(isset($params['var1']));
  290. $this->assertEquals('baz', $params['var1']);
  291. }
  292. /**
  293. * Test without router, using GET params
  294. */
  295. public function testDispatch7()
  296. {
  297. if ('cli' == strtolower(php_sapi_name())) {
  298. $this->markTestSkipped('Issues with $_GET in CLI interface prevents test from passing');
  299. }
  300. $request = new Zend_Controller_Request_Http('http://framework.zend.com/index.php?controller=foo&action=bar');
  301. $response = new Zend_Controller_Response_Cli();
  302. $response = $this->_controller->dispatch($request, $response);
  303. $body = $response->getBody();
  304. $this->assertContains('Bar action called', $body);
  305. }
  306. /**
  307. * Test that run() throws exception when called from object instance
  308. */
  309. public function _testRunThrowsException()
  310. {
  311. try {
  312. $this->_controller->run(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  313. $this->fail('Should not be able to call run() from object instance');
  314. } catch (Exception $e) {
  315. // success
  316. }
  317. }
  318. /**
  319. * Test that set/getBaseUrl() functionality works
  320. */
  321. public function testSetGetBaseUrl()
  322. {
  323. $this->assertNull($this->_controller->getBaseUrl());
  324. $this->_controller->setBaseUrl('/index.php');
  325. $this->assertEquals('/index.php', $this->_controller->getBaseUrl());
  326. }
  327. public function testSetGetBaseUrlPopulatesRequest()
  328. {
  329. $request = new Zend_Controller_Request_Http();
  330. $this->_controller->setRequest($request);
  331. $this->_controller->setBaseUrl('/index.php');
  332. $this->assertEquals('/index.php', $request->getBaseUrl());
  333. $this->assertEquals($request->getBaseUrl(), $this->_controller->getBaseUrl());
  334. }
  335. public function testSetBaseUrlThrowsExceptionOnNonString()
  336. {
  337. try {
  338. $this->_controller->setBaseUrl(array());
  339. $this->fail('Should not be able to set non-string base URL');
  340. } catch (Exception $e) {
  341. // success
  342. }
  343. }
  344. /**
  345. * Test that a set base URL is pushed to the request during the dispatch
  346. * process
  347. */
  348. public function testBaseUrlPushedToRequest()
  349. {
  350. $this->_controller->setBaseUrl('/index.php');
  351. $request = new Zend_Controller_Request_Http('http://example.com/index');
  352. $response = new Zend_Controller_Response_Cli();
  353. $response = $this->_controller->dispatch($request, $response);
  354. $this->assertContains('index.php', $request->getBaseUrl());
  355. }
  356. /**
  357. * Test that throwExceptions() sets and returns value properly
  358. */
  359. public function testThrowExceptions()
  360. {
  361. $this->_controller->throwExceptions(true);
  362. $this->assertTrue($this->_controller->throwExceptions());
  363. $this->_controller->throwExceptions(false);
  364. $this->assertFalse($this->_controller->throwExceptions());
  365. }
  366. public function testThrowExceptionsFluentInterface()
  367. {
  368. $result = $this->_controller->throwExceptions(true);
  369. $this->assertSame($this->_controller, $result);
  370. }
  371. /**
  372. * Test that with throwExceptions() set, an exception is thrown
  373. */
  374. public function testThrowExceptionsThrows()
  375. {
  376. $this->_controller->throwExceptions(true);
  377. $this->_controller->setControllerDirectory(dirname(__FILE__));
  378. $request = new Zend_Controller_Request_Http('http://framework.zend.com/bogus/baz');
  379. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  380. $this->_controller->setRouter(new Zend_Controller_Router_Rewrite());
  381. try {
  382. $response = $this->_controller->dispatch($request);
  383. $this->fail('Invalid controller should throw exception');
  384. } catch (Exception $e) {
  385. // success
  386. }
  387. }
  388. /**
  389. * Test that returnResponse() sets and returns value properly
  390. */
  391. public function testReturnResponse()
  392. {
  393. $this->_controller->returnResponse(true);
  394. $this->assertTrue($this->_controller->returnResponse());
  395. $this->_controller->returnResponse(false);
  396. $this->assertFalse($this->_controller->returnResponse());
  397. }
  398. public function testReturnResponseFluentInterface()
  399. {
  400. $result = $this->_controller->returnResponse(true);
  401. $this->assertSame($this->_controller, $result);
  402. }
  403. /**
  404. * Test that with returnResponse set to false, output is echoed and equals that in the response
  405. */
  406. public function testReturnResponseReturnsResponse()
  407. {
  408. $request = new Zend_Controller_Request_Http('http://framework.zend.com/foo/bar/var1/baz');
  409. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  410. $this->_controller->setRouter(new Zend_Controller_Router_Rewrite());
  411. $this->_controller->returnResponse(false);
  412. ob_start();
  413. $this->_controller->dispatch($request);
  414. $body = ob_get_clean();
  415. $actual = $this->_controller->getResponse()->getBody();
  416. $this->assertContains($actual, $body);
  417. }
  418. public function testRunStatically()
  419. {
  420. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  421. $this->_controller->setRequest($request);
  422. Zend_Controller_Front::run(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  423. }
  424. public function testRunDynamically()
  425. {
  426. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  427. $this->_controller->setRequest($request);
  428. $this->_controller->run(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  429. }
  430. public function testModulePathDispatched()
  431. {
  432. $this->_controller->addControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . '/Admin', 'admin');
  433. $request = new Zend_Controller_Request_Http('http://example.com/admin/foo/bar');
  434. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  435. $response = $this->_controller->dispatch($request);
  436. $body = $response->getBody();
  437. $this->assertContains('Admin_Foo::bar action called', $body, $body);
  438. }
  439. public function testModuleControllerDirectoryName()
  440. {
  441. $this->assertEquals('controllers', $this->_controller->getModuleControllerDirectoryName());
  442. $this->_controller->setModuleControllerDirectoryName('foobar');
  443. $this->assertEquals('foobar', $this->_controller->getModuleControllerDirectoryName());
  444. }
  445. public function testAddModuleDirectory()
  446. {
  447. $moduleDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules';
  448. $this->_controller->addModuleDirectory($moduleDir);
  449. $controllerDirs = $this->_controller->getControllerDirectory();
  450. $this->assertTrue(isset($controllerDirs['foo']));
  451. $this->assertTrue(isset($controllerDirs['bar']));
  452. $this->assertTrue(isset($controllerDirs['default']));
  453. $this->assertFalse(isset($controllerDirs['.svn']));
  454. $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'foo', $controllerDirs['foo']);
  455. $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'bar', $controllerDirs['bar']);
  456. $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'default', $controllerDirs['default']);
  457. }
  458. /**#@+
  459. * @see ZF-2910
  460. */
  461. public function testShouldAllowRetrievingCurrentModuleDirectory()
  462. {
  463. $this->testAddModuleDirectory();
  464. $request = new Zend_Controller_Request_Http();
  465. $request->setModuleName('bar');
  466. $this->_controller->setRequest($request);
  467. $dir = $this->_controller->getModuleDirectory();
  468. $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'bar', $dir);
  469. $this->assertNotContains('controllers', $dir);
  470. }
  471. public function testShouldAllowRetrievingSpecifiedModuleDirectory()
  472. {
  473. $this->testAddModuleDirectory();
  474. $dir = $this->_controller->getModuleDirectory('foo');
  475. $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'foo', $dir);
  476. $this->assertNotContains('controllers', $dir);
  477. }
  478. public function testShouldReturnNullWhenRetrievingNonexistentModuleDirectory()
  479. {
  480. $this->testAddModuleDirectory();
  481. $this->assertNull($this->_controller->getModuleDirectory('bogus-foo-bar'));
  482. }
  483. /**#@-*/
  484. /**
  485. * ZF-2435
  486. */
  487. public function testCanRemoveIndividualModuleDirectory()
  488. {
  489. $moduleDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules';
  490. $this->_controller->addModuleDirectory($moduleDir);
  491. $controllerDirs = $this->_controller->getControllerDirectory();
  492. $this->_controller->removeControllerDirectory('foo');
  493. $test = $this->_controller->getControllerDirectory();
  494. $this->assertNotEquals($controllerDirs, $test);
  495. $this->assertFalse(array_key_exists('foo', $test));
  496. }
  497. public function testAddModuleDirectoryThrowsExceptionForInvalidDirectory()
  498. {
  499. $moduleDir = 'doesntexist';
  500. try {
  501. $this->_controller->addModuleDirectory($moduleDir);
  502. $this->fail('Exception expected but not thrown');
  503. }catch(Exception $e){
  504. $this->assertType('Zend_Exception',$e);
  505. $this->assertRegExp('/Directory \w+ not readable/',$e->getMessage());
  506. }
  507. }
  508. public function testGetControllerDirectoryByModuleName()
  509. {
  510. $moduleDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules';
  511. $this->_controller->addModuleDirectory($moduleDir);
  512. $barDir = $this->_controller->getControllerDirectory('bar');
  513. $this->assertNotNull($barDir);
  514. $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'bar', $barDir);
  515. }
  516. public function testGetControllerDirectoryByModuleNameReturnsNullOnBadModule()
  517. {
  518. $moduleDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules';
  519. $this->_controller->addModuleDirectory($moduleDir);
  520. $dir = $this->_controller->getControllerDirectory('_bazbat');
  521. $this->assertNull($dir);
  522. }
  523. public function testDefaultModule()
  524. {
  525. $dispatcher = $this->_controller->getDispatcher();
  526. $this->assertEquals($dispatcher->getDefaultModule(), $this->_controller->getDefaultModule());
  527. $this->_controller->setDefaultModule('foobar');
  528. $this->assertEquals('foobar', $this->_controller->getDefaultModule());
  529. $this->assertEquals($dispatcher->getDefaultModule(), $this->_controller->getDefaultModule());
  530. }
  531. public function testErrorHandlerPluginRegisteredWhenDispatched()
  532. {
  533. $this->assertFalse($this->_controller->hasPlugin('Zend_Controller_Plugin_ErrorHandler'));
  534. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  535. $this->_controller->setParam('noErrorHandler', false)
  536. ->setResponse(new Zend_Controller_Response_Cli());
  537. $response = $this->_controller->dispatch($request);
  538. $this->assertTrue($this->_controller->hasPlugin('Zend_Controller_Plugin_ErrorHandler'));
  539. }
  540. public function testErrorHandlerPluginNotRegisteredIfNoErrorHandlerSet()
  541. {
  542. $this->assertFalse($this->_controller->hasPlugin('Zend_Controller_Plugin_ErrorHandler'));
  543. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  544. $this->_controller->setParam('noErrorHandler', true)
  545. ->setResponse(new Zend_Controller_Response_Cli());
  546. $response = $this->_controller->dispatch($request);
  547. $this->assertFalse($this->_controller->hasPlugin('Zend_Controller_Plugin_ErrorHandler'));
  548. }
  549. public function testReplaceRequestAndResponseMidStream()
  550. {
  551. $request = new Zend_Controller_Request_Http('http://example.com/index/replace');
  552. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  553. $response = new Zend_Controller_Response_Http();
  554. $responsePost = $this->_controller->dispatch($request, $response);
  555. $requestPost = $this->_controller->getRequest();
  556. $this->assertNotSame($request, $requestPost);
  557. $this->assertNotSame($response, $responsePost);
  558. $this->assertContains('Reset action called', $responsePost->getBody());
  559. $this->assertNotContains('Reset action called', $response->getBody());
  560. }
  561. public function testViewRendererHelperRegisteredWhenDispatched()
  562. {
  563. $this->assertFalse(Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer'));
  564. $this->_controller->setParam('noViewRenderer', false);
  565. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  566. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  567. $response = $this->_controller->dispatch($request);
  568. $this->assertTrue(Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer'));
  569. }
  570. public function testViewRendererHelperNotRegisteredIfNoViewRendererSet()
  571. {
  572. $this->assertFalse(Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer'));
  573. $this->_controller->setParam('noViewRenderer', true);
  574. $request = new Zend_Controller_Request_Http('http://example.com/index/index');
  575. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  576. $response = $this->_controller->dispatch($request);
  577. $this->assertFalse(Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer'));
  578. }
  579. }
  580. // Call Zend_Controller_FrontTest::main() if this source file is executed directly.
  581. if (PHPUnit_MAIN_METHOD == "Zend_Controller_FrontTest::main") {
  582. Zend_Controller_FrontTest::main();
  583. }