ActionTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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-2010 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_ActionTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. require_once dirname(__FILE__) . '/../../TestHelper.php';
  25. define("PHPUnit_MAIN_METHOD", "Zend_Controller_ActionTest::main");
  26. }
  27. require_once 'Zend/Controller/Action.php';
  28. require_once 'Zend/Controller/Action/Helper/Redirector.php';
  29. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  30. require_once 'Zend/Controller/Request/Http.php';
  31. require_once 'Zend/Controller/Response/Cli.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Controller
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Controller
  39. * @group Zend_Controller_Action
  40. */
  41. class Zend_Controller_ActionTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @access public
  47. * @static
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_ActionTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. Zend_Controller_Action_HelperBroker::resetHelpers();
  57. $front = Zend_Controller_Front::getInstance();
  58. $front->resetInstance();
  59. $front->setControllerDirectory('.', 'default');
  60. $this->_controller = new Zend_Controller_ActionTest_TestController(
  61. new Zend_Controller_Request_Http(),
  62. new Zend_Controller_Response_Cli(),
  63. array(
  64. 'foo' => 'bar',
  65. 'bar' => 'baz'
  66. )
  67. );
  68. $redirector = $this->_controller->getHelper('redirector');
  69. $redirector->setExit(false);
  70. }
  71. public function tearDown()
  72. {
  73. unset($this->_controller);
  74. }
  75. public function testInit()
  76. {
  77. $this->assertEquals('bar', $this->_controller->initArgs['foo']);
  78. $this->assertEquals('baz', $this->_controller->initArgs['bar']);
  79. }
  80. public function testPreRun()
  81. {
  82. $this->_controller->preDispatch();
  83. $this->assertNotContains('Prerun ran', $this->_controller->getResponse()->getBody());
  84. $this->_controller->getRequest()->setParam('prerun', true);
  85. $this->_controller->preDispatch();
  86. $this->assertContains('Prerun ran', $this->_controller->getResponse()->getBody());
  87. }
  88. public function testPostRun()
  89. {
  90. $this->_controller->postDispatch();
  91. $this->assertNotContains('Postrun ran', $this->_controller->getResponse()->getBody());
  92. $this->_controller->getRequest()->setParam('postrun', true);
  93. $this->_controller->postDispatch();
  94. $this->assertContains('Postrun ran', $this->_controller->getResponse()->getBody());
  95. }
  96. public function testGetRequest()
  97. {
  98. $this->assertTrue($this->_controller->getRequest() instanceof Zend_Controller_Request_Abstract);
  99. }
  100. public function testGetResponse()
  101. {
  102. $this->assertTrue($this->_controller->getResponse() instanceof Zend_Controller_Response_Abstract);
  103. }
  104. public function testGetInvokeArgs()
  105. {
  106. $expected = array('foo' => 'bar', 'bar' => 'baz');
  107. $this->assertSame($expected, $this->_controller->getInvokeArgs());
  108. }
  109. public function testGetInvokeArg()
  110. {
  111. $this->assertSame('bar', $this->_controller->getInvokeArg('foo'));
  112. $this->assertSame('baz', $this->_controller->getInvokeArg('bar'));
  113. }
  114. public function testForwardActionOnly()
  115. {
  116. $this->_controller->forward('forwarded');
  117. $this->assertEquals('forwarded', $this->_controller->getRequest()->getActionName());
  118. $this->assertFalse($this->_controller->getRequest()->isDispatched());
  119. }
  120. public function testForwardActionKeepsController()
  121. {
  122. $request = $this->_controller->getRequest();
  123. $request->setControllerName('foo')
  124. ->setActionName('bar');
  125. $this->_controller->forward('forwarded');
  126. $this->assertEquals('forwarded', $request->getActionName());
  127. $this->assertEquals('foo', $request->getControllerName());
  128. $this->assertFalse($request->isDispatched());
  129. }
  130. public function testForwardActionAndController()
  131. {
  132. $request = $this->_controller->getRequest();
  133. $request->setControllerName('foo')
  134. ->setActionName('bar');
  135. $this->_controller->forward('forwarded', 'bar');
  136. $this->assertEquals('forwarded', $request->getActionName());
  137. $this->assertEquals('bar', $request->getControllerName());
  138. $this->assertFalse($request->isDispatched());
  139. }
  140. public function testForwardActionControllerAndModule()
  141. {
  142. $request = $this->_controller->getRequest();
  143. $request->setControllerName('foo')
  144. ->setActionName('bar')
  145. ->setModuleName('admin');
  146. $this->_controller->forward('forwarded', 'bar');
  147. $this->assertEquals('forwarded', $request->getActionName());
  148. $this->assertEquals('bar', $request->getControllerName());
  149. $this->assertEquals('admin', $request->getModuleName());
  150. $this->assertFalse($request->isDispatched());
  151. }
  152. public function testForwardCanSetParams()
  153. {
  154. $request = $this->_controller->getRequest();
  155. $request->setParams(array('admin' => 'batman'));
  156. $this->_controller->forward('forwarded', null, null, array('foo' => 'bar'));
  157. $this->assertEquals('forwarded', $request->getActionName());
  158. $received = $request->getParams();
  159. $this->assertTrue(isset($received['foo']));
  160. $this->assertEquals('bar', $received['foo']);
  161. $this->assertFalse($request->isDispatched());
  162. }
  163. public function testRun()
  164. {
  165. $response = $this->_controller->run();
  166. $body = $response->getBody();
  167. $this->assertContains('In the index action', $body, var_export($this->_controller->getRequest(), 1));
  168. $this->assertNotContains('Prerun ran', $body, $body);
  169. }
  170. public function testRun2()
  171. {
  172. $this->_controller->getRequest()->setActionName('bar');
  173. try {
  174. $response = $this->_controller->run();
  175. $this->fail('Should not be able to call bar as action');
  176. } catch (Exception $e) {
  177. //success!
  178. }
  179. }
  180. public function testRun3()
  181. {
  182. $this->_controller->getRequest()->setActionName('foo');
  183. $response = $this->_controller->run();
  184. $this->assertContains('In the foo action', $response->getBody());
  185. $this->assertNotContains('Prerun ran', $this->_controller->getResponse()->getBody());
  186. }
  187. public function testHasParam()
  188. {
  189. $request = $this->_controller->getRequest();
  190. $request->setParam('foo', 'bar');
  191. $request->setParam('baz', 'bal');
  192. $this->assertTrue($this->_controller->hasParam('foo'));
  193. $this->assertTrue($this->_controller->hasParam('baz'));
  194. }
  195. public function testSetParam()
  196. {
  197. $this->_controller->setParam('foo', 'bar');
  198. $params = $this->_controller->getParams();
  199. $this->assertTrue(isset($params['foo']));
  200. $this->assertEquals('bar', $params['foo']);
  201. }
  202. /**
  203. * @group ZF-5163
  204. */
  205. public function testGetParamForZeroValues()
  206. {
  207. $this->_controller->setParam('foo', 'bar');
  208. $this->_controller->setParam('bar', 0);
  209. $this->_controller->setParam('baz', null);
  210. $this->assertEquals('bar', $this->_controller->getParam('foo', -1));
  211. $this->assertEquals(0, $this->_controller->getParam('bar', -1));
  212. $this->assertEquals(-1, $this->_controller->getParam('baz', -1));
  213. }
  214. /**
  215. * @group ZF-9179
  216. */
  217. public function testGetParamForEmptyString()
  218. {
  219. $this->_controller->setParam('lang', '');
  220. $this->assertEquals('en', $this->_controller->getParam('lang', 'en'));
  221. }
  222. public function testGetParams()
  223. {
  224. $this->_controller->setParam('foo', 'bar');
  225. $this->_controller->setParam('bar', 'baz');
  226. $this->_controller->setParam('boo', 'bah');
  227. $params = $this->_controller->getParams();
  228. $this->assertEquals('bar', $params['foo']);
  229. $this->assertEquals('baz', $params['bar']);
  230. $this->assertEquals('bah', $params['boo']);
  231. }
  232. public function testRedirect()
  233. {
  234. $response = $this->_controller->getResponse();
  235. $response->headersSentThrowsException = false;
  236. $this->_controller->redirect('/baz/foo');
  237. $this->_controller->redirect('/foo/bar');
  238. $headers = $response->getHeaders();
  239. $found = 0;
  240. $url = '';
  241. foreach ($headers as $header) {
  242. if ('Location' == $header['name']) {
  243. ++$found;
  244. $url = $header['value'];
  245. break;
  246. }
  247. }
  248. $this->assertEquals(1, $found);
  249. $this->assertContains('/foo/bar', $url);
  250. }
  251. public function testInitView()
  252. {
  253. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  254. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  255. $controller = new ViewController(
  256. new Zend_Controller_Request_Http(),
  257. new Zend_Controller_Response_Cli()
  258. );
  259. $view = $controller->initView();
  260. $this->assertTrue($view instanceof Zend_View);
  261. $scriptPath = $view->getScriptPaths();
  262. $this->assertTrue(is_array($scriptPath));
  263. $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'scripts/', $scriptPath[0]);
  264. }
  265. public function testRender()
  266. {
  267. $request = new Zend_Controller_Request_Http();
  268. $request->setControllerName('view')
  269. ->setActionName('index');
  270. $response = new Zend_Controller_Response_Cli();
  271. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  272. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  273. $controller = new ViewController($request, $response);
  274. $controller->indexAction();
  275. $this->assertContains('In the index action view', $response->getBody());
  276. }
  277. public function testRenderByName()
  278. {
  279. $request = new Zend_Controller_Request_Http();
  280. $request->setControllerName('view')
  281. ->setActionName('test');
  282. $response = new Zend_Controller_Response_Cli();
  283. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  284. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  285. $controller = new ViewController($request, $response);
  286. $controller->testAction();
  287. $this->assertContains('In the index action view', $response->getBody());
  288. }
  289. public function testRenderOutsideControllerSubdir()
  290. {
  291. $request = new Zend_Controller_Request_Http();
  292. $request->setControllerName('view')
  293. ->setActionName('site');
  294. $response = new Zend_Controller_Response_Cli();
  295. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  296. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  297. $controller = new ViewController($request, $response);
  298. $controller->siteAction();
  299. $this->assertContains('In the sitewide view', $response->getBody());
  300. }
  301. public function testRenderNamedSegment()
  302. {
  303. $request = new Zend_Controller_Request_Http();
  304. $request->setControllerName('view')
  305. ->setActionName('name');
  306. $response = new Zend_Controller_Response_Cli();
  307. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  308. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  309. $controller = new ViewController($request, $response);
  310. $controller->nameAction();
  311. $this->assertContains('In the name view', $response->getBody('name'));
  312. }
  313. public function testRenderNormalizesScriptName()
  314. {
  315. $request = new Zend_Controller_Request_Http();
  316. $request->setControllerName('foo.bar')
  317. ->setActionName('baz_bat');
  318. $response = new Zend_Controller_Response_Cli();
  319. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  320. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'FooBarController.php';
  321. $controller = new FooBarController($request, $response);
  322. $controller->bazBatAction();
  323. $this->assertContains('Inside foo-bar/baz-bat.phtml', $response->getBody());
  324. }
  325. public function testGetViewScript()
  326. {
  327. $request = new Zend_Controller_Request_Http();
  328. $request->setControllerName('view')
  329. ->setActionName('test');
  330. $response = new Zend_Controller_Response_Cli();
  331. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  332. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  333. $controller = new ViewController($request, $response);
  334. $script = $controller->getViewScript();
  335. $this->assertContains('view' . DIRECTORY_SEPARATOR . 'test.phtml', $script);
  336. $script = $controller->getViewScript('foo');
  337. $this->assertContains('view' . DIRECTORY_SEPARATOR . 'foo.phtml', $script);
  338. }
  339. public function testGetViewScriptDoesNotOverwriteNoControllerFlagWhenNullPassed()
  340. {
  341. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  342. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  343. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  344. $request = new Zend_Controller_Request_Http();
  345. $request->setControllerName('view')
  346. ->setActionName('test');
  347. $response = new Zend_Controller_Response_Cli();
  348. $controller = new ViewController($request, $response);
  349. $this->assertSame($viewRenderer->getActionController(), $controller);
  350. $viewRenderer->setNoController(true);
  351. $this->assertTrue($viewRenderer->getNoController());
  352. $script = $controller->getViewScript();
  353. $this->assertTrue($viewRenderer->getNoController());
  354. }
  355. public function testRenderScript()
  356. {
  357. $request = new Zend_Controller_Request_Http();
  358. $request->setControllerName('view')
  359. ->setActionName('script');
  360. $response = new Zend_Controller_Response_Cli();
  361. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  362. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  363. $controller = new ViewController($request, $response);
  364. $controller->scriptAction();
  365. $this->assertContains('Inside custom/renderScript.php', $response->getBody());
  366. }
  367. public function testRenderScriptToNamedResponseSegment()
  368. {
  369. $request = new Zend_Controller_Request_Http();
  370. $request->setControllerName('view')
  371. ->setActionName('script-name');
  372. $response = new Zend_Controller_Response_Cli();
  373. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  374. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  375. $controller = new ViewController($request, $response);
  376. $controller->scriptNameAction();
  377. $this->assertContains('Inside custom/renderScript.php', $response->getBody('foo'));
  378. }
  379. public function testGetHelper()
  380. {
  381. $redirector = $this->_controller->getHelper('redirector');
  382. $this->assertTrue($redirector instanceof Zend_Controller_Action_Helper_Abstract);
  383. $this->assertTrue($redirector instanceof Zend_Controller_Action_Helper_Redirector);
  384. }
  385. public function testGetHelperCopy()
  386. {
  387. $redirector = $this->_controller->getHelper('redirector');
  388. $copy = $this->_controller->getHelperCopy('redirector');
  389. $this->assertNotSame($redirector, $copy);
  390. $this->assertTrue($copy instanceof Zend_Controller_Action_Helper_Redirector);
  391. }
  392. public function testViewInjectionUsingViewRenderer()
  393. {
  394. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  395. $request = new Zend_Controller_Request_Http();
  396. $request->setControllerName('view')
  397. ->setActionName('script');
  398. $response = new Zend_Controller_Response_Cli();
  399. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  400. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  401. $controller = new ViewController($request, $response);
  402. $this->assertNotNull($controller->view);
  403. }
  404. public function testRenderUsingViewRenderer()
  405. {
  406. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  407. $request = new Zend_Controller_Request_Http();
  408. $request->setControllerName('view')
  409. ->setActionName('script');
  410. $response = new Zend_Controller_Response_Cli();
  411. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  412. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  413. $controller = new ViewController($request, $response);
  414. $controller->scriptAction();
  415. $this->assertContains('Inside custom/renderScript.php', $response->getBody());
  416. }
  417. public function testMissingActionExceptionsDifferFromMissingMethods()
  418. {
  419. try {
  420. $this->_controller->bogusAction();
  421. $this->fail('Invalid action should throw exception');
  422. } catch (Zend_Controller_Exception $e) {
  423. $this->assertRegexp('/^Action.*?(does not exist and was not trapped in __call\(\))$/', $e->getMessage());
  424. $this->assertContains('bogus', $e->getMessage());
  425. $this->assertNotContains('bogusAction', $e->getMessage());
  426. $this->assertEquals(404, $e->getCode());
  427. }
  428. try {
  429. $this->_controller->bogus();
  430. $this->fail('Invalid method should throw exception');
  431. } catch (Zend_Controller_Exception $e) {
  432. $this->assertRegexp('/^Method.*?(does not exist and was not trapped in __call\(\))$/', $e->getMessage());
  433. $this->assertContains('bogus', $e->getMessage());
  434. $this->assertEquals(500, $e->getCode());
  435. }
  436. }
  437. }
  438. class Zend_Controller_ActionTest_TestController extends Zend_Controller_Action
  439. {
  440. public $initArgs = array();
  441. public function init()
  442. {
  443. $this->initArgs['foo'] = $this->getInvokeArg('foo');
  444. $this->initArgs['bar'] = $this->getInvokeArg('bar');
  445. }
  446. public function preDispatch()
  447. {
  448. if (false !== ($param = $this->_getParam('prerun', false))) {
  449. $this->getResponse()->appendBody("Prerun ran\n");
  450. }
  451. }
  452. public function postDispatch()
  453. {
  454. if (false !== ($param = $this->_getParam('postrun', false))) {
  455. $this->getResponse()->appendBody("Postrun ran\n");
  456. }
  457. }
  458. public function noRouteAction()
  459. {
  460. return $this->indexAction();
  461. }
  462. public function indexAction()
  463. {
  464. $this->getResponse()->appendBody("In the index action\n");
  465. }
  466. public function fooAction()
  467. {
  468. $this->getResponse()->appendBody("In the foo action\n");
  469. }
  470. public function bar()
  471. {
  472. $this->getResponse()->setBody("Should never see this\n");
  473. }
  474. public function forward($action, $controller = null, $module = null, array $params = null)
  475. {
  476. $this->_forward($action, $controller, $module, $params);
  477. }
  478. public function hasParam($param)
  479. {
  480. return $this->_hasParam($param);
  481. }
  482. public function getParams()
  483. {
  484. return $this->_getAllParams();
  485. }
  486. public function setParam($key, $value)
  487. {
  488. $this->_setParam($key, $value);
  489. return $this;
  490. }
  491. public function getParam($key, $default)
  492. {
  493. return $this->_getParam($key, $default);
  494. }
  495. public function redirect($url, $code = 302, $prependBase = true)
  496. {
  497. $this->_redirect($url, array('code' => $code, 'prependBase' => $prependBase));
  498. }
  499. }
  500. // Call Zend_Controller_ActionTest::main() if this source file is executed directly.
  501. if (PHPUnit_MAIN_METHOD == "Zend_Controller_ActionTest::main") {
  502. Zend_Controller_ActionTest::main();
  503. }