FrontTest.php 26 KB

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