ActionTest.php 21 KB

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