ActionTest.php 22 KB

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