2
0

ActionTest.php 22 KB

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