ActionTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. public function testGetParams()
  218. {
  219. $this->_controller->setParam('foo', 'bar');
  220. $this->_controller->setParam('bar', 'baz');
  221. $this->_controller->setParam('boo', 'bah');
  222. $params = $this->_controller->getParams();
  223. $this->assertEquals('bar', $params['foo']);
  224. $this->assertEquals('baz', $params['bar']);
  225. $this->assertEquals('bah', $params['boo']);
  226. }
  227. public function testRedirect()
  228. {
  229. $response = $this->_controller->getResponse();
  230. $response->headersSentThrowsException = false;
  231. $this->_controller->redirect('/baz/foo');
  232. $this->_controller->redirect('/foo/bar');
  233. $headers = $response->getHeaders();
  234. $found = 0;
  235. $url = '';
  236. foreach ($headers as $header) {
  237. if ('Location' == $header['name']) {
  238. ++$found;
  239. $url = $header['value'];
  240. break;
  241. }
  242. }
  243. $this->assertEquals(1, $found);
  244. $this->assertContains('/foo/bar', $url);
  245. }
  246. public function testInitView()
  247. {
  248. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  249. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  250. $controller = new ViewController(
  251. new Zend_Controller_Request_Http(),
  252. new Zend_Controller_Response_Cli()
  253. );
  254. $view = $controller->initView();
  255. $this->assertTrue($view instanceof Zend_View);
  256. $scriptPath = $view->getScriptPaths();
  257. $this->assertTrue(is_array($scriptPath));
  258. $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR, $scriptPath[0]);
  259. }
  260. public function testRender()
  261. {
  262. $request = new Zend_Controller_Request_Http();
  263. $request->setControllerName('view')
  264. ->setActionName('index');
  265. $response = new Zend_Controller_Response_Cli();
  266. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  267. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  268. $controller = new ViewController($request, $response);
  269. $controller->indexAction();
  270. $this->assertContains('In the index action view', $response->getBody());
  271. }
  272. public function testRenderByName()
  273. {
  274. $request = new Zend_Controller_Request_Http();
  275. $request->setControllerName('view')
  276. ->setActionName('test');
  277. $response = new Zend_Controller_Response_Cli();
  278. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  279. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  280. $controller = new ViewController($request, $response);
  281. $controller->testAction();
  282. $this->assertContains('In the index action view', $response->getBody());
  283. }
  284. public function testRenderOutsideControllerSubdir()
  285. {
  286. $request = new Zend_Controller_Request_Http();
  287. $request->setControllerName('view')
  288. ->setActionName('site');
  289. $response = new Zend_Controller_Response_Cli();
  290. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  291. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  292. $controller = new ViewController($request, $response);
  293. $controller->siteAction();
  294. $this->assertContains('In the sitewide view', $response->getBody());
  295. }
  296. public function testRenderNamedSegment()
  297. {
  298. $request = new Zend_Controller_Request_Http();
  299. $request->setControllerName('view')
  300. ->setActionName('name');
  301. $response = new Zend_Controller_Response_Cli();
  302. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  303. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  304. $controller = new ViewController($request, $response);
  305. $controller->nameAction();
  306. $this->assertContains('In the name view', $response->getBody('name'));
  307. }
  308. public function testRenderNormalizesScriptName()
  309. {
  310. $request = new Zend_Controller_Request_Http();
  311. $request->setControllerName('foo.bar')
  312. ->setActionName('baz_bat');
  313. $response = new Zend_Controller_Response_Cli();
  314. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  315. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'FooBarController.php';
  316. $controller = new FooBarController($request, $response);
  317. $controller->bazBatAction();
  318. $this->assertContains('Inside foo-bar/baz-bat.phtml', $response->getBody());
  319. }
  320. public function testGetViewScript()
  321. {
  322. $request = new Zend_Controller_Request_Http();
  323. $request->setControllerName('view')
  324. ->setActionName('test');
  325. $response = new Zend_Controller_Response_Cli();
  326. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  327. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  328. $controller = new ViewController($request, $response);
  329. $script = $controller->getViewScript();
  330. $this->assertContains('view' . DIRECTORY_SEPARATOR . 'test.phtml', $script);
  331. $script = $controller->getViewScript('foo');
  332. $this->assertContains('view' . DIRECTORY_SEPARATOR . 'foo.phtml', $script);
  333. }
  334. public function testGetViewScriptDoesNotOverwriteNoControllerFlagWhenNullPassed()
  335. {
  336. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  337. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  338. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  339. $request = new Zend_Controller_Request_Http();
  340. $request->setControllerName('view')
  341. ->setActionName('test');
  342. $response = new Zend_Controller_Response_Cli();
  343. $controller = new ViewController($request, $response);
  344. $this->assertSame($viewRenderer->getActionController(), $controller);
  345. $viewRenderer->setNoController(true);
  346. $this->assertTrue($viewRenderer->getNoController());
  347. $script = $controller->getViewScript();
  348. $this->assertTrue($viewRenderer->getNoController());
  349. }
  350. public function testRenderScript()
  351. {
  352. $request = new Zend_Controller_Request_Http();
  353. $request->setControllerName('view')
  354. ->setActionName('script');
  355. $response = new Zend_Controller_Response_Cli();
  356. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  357. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  358. $controller = new ViewController($request, $response);
  359. $controller->scriptAction();
  360. $this->assertContains('Inside custom/renderScript.php', $response->getBody());
  361. }
  362. public function testRenderScriptToNamedResponseSegment()
  363. {
  364. $request = new Zend_Controller_Request_Http();
  365. $request->setControllerName('view')
  366. ->setActionName('script-name');
  367. $response = new Zend_Controller_Response_Cli();
  368. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  369. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  370. $controller = new ViewController($request, $response);
  371. $controller->scriptNameAction();
  372. $this->assertContains('Inside custom/renderScript.php', $response->getBody('foo'));
  373. }
  374. public function testGetHelper()
  375. {
  376. $redirector = $this->_controller->getHelper('redirector');
  377. $this->assertTrue($redirector instanceof Zend_Controller_Action_Helper_Abstract);
  378. $this->assertTrue($redirector instanceof Zend_Controller_Action_Helper_Redirector);
  379. }
  380. public function testGetHelperCopy()
  381. {
  382. $redirector = $this->_controller->getHelper('redirector');
  383. $copy = $this->_controller->getHelperCopy('redirector');
  384. $this->assertNotSame($redirector, $copy);
  385. $this->assertTrue($copy instanceof Zend_Controller_Action_Helper_Redirector);
  386. }
  387. public function testViewInjectionUsingViewRenderer()
  388. {
  389. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  390. $request = new Zend_Controller_Request_Http();
  391. $request->setControllerName('view')
  392. ->setActionName('script');
  393. $response = new Zend_Controller_Response_Cli();
  394. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  395. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  396. $controller = new ViewController($request, $response);
  397. $this->assertNotNull($controller->view);
  398. }
  399. public function testRenderUsingViewRenderer()
  400. {
  401. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  402. $request = new Zend_Controller_Request_Http();
  403. $request->setControllerName('view')
  404. ->setActionName('script');
  405. $response = new Zend_Controller_Response_Cli();
  406. Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  407. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
  408. $controller = new ViewController($request, $response);
  409. $controller->scriptAction();
  410. $this->assertContains('Inside custom/renderScript.php', $response->getBody());
  411. }
  412. public function testMissingActionExceptionsDifferFromMissingMethods()
  413. {
  414. try {
  415. $this->_controller->bogusAction();
  416. $this->fail('Invalid action should throw exception');
  417. } catch (Zend_Controller_Exception $e) {
  418. $this->assertRegexp('/^Action.*?(does not exist and was not trapped in __call\(\))$/', $e->getMessage());
  419. $this->assertContains('bogus', $e->getMessage());
  420. $this->assertNotContains('bogusAction', $e->getMessage());
  421. $this->assertEquals(404, $e->getCode());
  422. }
  423. try {
  424. $this->_controller->bogus();
  425. $this->fail('Invalid method should throw exception');
  426. } catch (Zend_Controller_Exception $e) {
  427. $this->assertRegexp('/^Method.*?(does not exist and was not trapped in __call\(\))$/', $e->getMessage());
  428. $this->assertContains('bogus', $e->getMessage());
  429. $this->assertEquals(500, $e->getCode());
  430. }
  431. }
  432. }
  433. class Zend_Controller_ActionTest_TestController extends Zend_Controller_Action
  434. {
  435. public $initArgs = array();
  436. public function init()
  437. {
  438. $this->initArgs['foo'] = $this->getInvokeArg('foo');
  439. $this->initArgs['bar'] = $this->getInvokeArg('bar');
  440. }
  441. public function preDispatch()
  442. {
  443. if (false !== ($param = $this->_getParam('prerun', false))) {
  444. $this->getResponse()->appendBody("Prerun ran\n");
  445. }
  446. }
  447. public function postDispatch()
  448. {
  449. if (false !== ($param = $this->_getParam('postrun', false))) {
  450. $this->getResponse()->appendBody("Postrun ran\n");
  451. }
  452. }
  453. public function noRouteAction()
  454. {
  455. return $this->indexAction();
  456. }
  457. public function indexAction()
  458. {
  459. $this->getResponse()->appendBody("In the index action\n");
  460. }
  461. public function fooAction()
  462. {
  463. $this->getResponse()->appendBody("In the foo action\n");
  464. }
  465. public function bar()
  466. {
  467. $this->getResponse()->setBody("Should never see this\n");
  468. }
  469. public function forward($action, $controller = null, $module = null, array $params = null)
  470. {
  471. $this->_forward($action, $controller, $module, $params);
  472. }
  473. public function hasParam($param)
  474. {
  475. return $this->_hasParam($param);
  476. }
  477. public function getParams()
  478. {
  479. return $this->_getAllParams();
  480. }
  481. public function setParam($key, $value)
  482. {
  483. $this->_setParam($key, $value);
  484. return $this;
  485. }
  486. public function getParam($key, $default)
  487. {
  488. return $this->_getParam($key, $default);
  489. }
  490. public function redirect($url, $code = 302, $prependBase = true)
  491. {
  492. $this->_redirect($url, array('code' => $code, 'prependBase' => $prependBase));
  493. }
  494. }
  495. // Call Zend_Controller_ActionTest::main() if this source file is executed directly.
  496. if (PHPUnit_MAIN_METHOD == "Zend_Controller_ActionTest::main") {
  497. Zend_Controller_ActionTest::main();
  498. }