PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Controller/FrontTest.php

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