ViewRendererTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <?php
  2. // Call Zend_Controller_Action_Helper_ViewRendererTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_ViewRendererTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  7. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  8. require_once 'Zend/Controller/Front.php';
  9. require_once 'Zend/Controller/Request/Http.php';
  10. require_once 'Zend/Controller/Response/Http.php';
  11. require_once 'Zend/Filter/Inflector.php';
  12. require_once 'Zend/View.php';
  13. require_once dirname(__FILE__) . '/../../_files/modules/foo/controllers/IndexController.php';
  14. require_once dirname(__FILE__) . '/../../_files/modules/bar/controllers/IndexController.php';
  15. /**
  16. * Test class for Zend_Controller_Action_Helper_ViewRenderer.
  17. */
  18. class Zend_Controller_Action_Helper_ViewRendererTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * Base path to controllers, views
  22. * @var string
  23. */
  24. public $basePath;
  25. /**
  26. * Front controller object
  27. * @var Zend_Controller_Front
  28. */
  29. public $front;
  30. /**
  31. * ViewRenderer helper
  32. * @var Zend_Controller_Action_Helper_ViewRenderer
  33. */
  34. public $helper;
  35. /**
  36. * Request object
  37. * @var Zend_Controller_Request_Http
  38. */
  39. public $request;
  40. /**
  41. * Response object
  42. * @var Zend_Controller_Response_Http
  43. */
  44. public $response;
  45. /**
  46. * Runs the test methods of this class.
  47. *
  48. * @access public
  49. * @static
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_ViewRendererTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Sets up the fixture, for example, open a network connection.
  58. * This method is called before a test is executed.
  59. *
  60. * @access protected
  61. */
  62. protected function setUp()
  63. {
  64. $this->basePath = realpath(dirname(__FILE__) . str_repeat(DIRECTORY_SEPARATOR . '..', 2));
  65. $this->request = new Zend_Controller_Request_Http();
  66. $this->response = new Zend_Controller_Response_Http();
  67. $this->front = Zend_Controller_Front::getInstance();
  68. $this->front->resetInstance();
  69. $this->front->addModuleDirectory($this->basePath . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules')
  70. ->setRequest($this->request)
  71. ->setResponse($this->response);
  72. $this->helper = new Zend_Controller_Action_Helper_ViewRenderer();
  73. Zend_Controller_Action_HelperBroker::addHelper($this->helper);
  74. }
  75. /**
  76. * Tears down the fixture, for example, close a network connection.
  77. * This method is called after a test is executed.
  78. *
  79. * @access protected
  80. */
  81. protected function tearDown()
  82. {
  83. Zend_Controller_Action_HelperBroker::resetHelpers();
  84. }
  85. public function testConstructorSetsViewWhenPassed()
  86. {
  87. $view = new Zend_View();
  88. $helper = new Zend_Controller_Action_Helper_ViewRenderer($view);
  89. $this->assertNotNull(isset($helper->view));
  90. $this->assertSame($view, $helper->view);
  91. }
  92. public function testConstructorSetsOptionsWhenPassed()
  93. {
  94. $helper = new Zend_Controller_Action_Helper_ViewRenderer(null, array(
  95. 'neverRender' => true,
  96. 'noRender' => true,
  97. 'noController' => true,
  98. 'viewSuffix' => 'php',
  99. 'scriptAction' => 'foo',
  100. 'responseSegment' => 'baz'
  101. ));
  102. $this->assertTrue($helper->getNeverRender());
  103. $this->assertTrue($helper->getNoRender());
  104. $this->assertTrue($helper->getNoController());
  105. $this->assertEquals('php', $helper->getViewSuffix());
  106. $this->assertEquals('foo', $helper->getScriptAction());
  107. $this->assertEquals('baz', $helper->getResponseSegment());
  108. }
  109. public function testSetView()
  110. {
  111. $view = new Zend_View();
  112. $this->helper->setView($view);
  113. $this->assertSame($view, $this->helper->view);
  114. }
  115. public function testGetFrontController()
  116. {
  117. $this->assertSame($this->front, $this->helper->getFrontController());
  118. }
  119. protected function _checkDefaults($module = 'foo', $count = 1)
  120. {
  121. $this->assertTrue(isset($this->helper->view));
  122. $this->assertTrue($this->helper->view instanceof Zend_View);
  123. $this->assertFalse($this->helper->getNeverRender());
  124. $this->assertFalse($this->helper->getNoRender());
  125. $this->assertNull($this->helper->getResponseSegment());
  126. $this->assertNull($this->helper->getScriptAction());
  127. $scriptPaths = $this->helper->view->getScriptPaths();
  128. $this->assertEquals($count, count($scriptPaths), var_export($scriptPaths, 1));
  129. $this->assertContains($module, $scriptPaths[0]);
  130. $helperPaths = $this->helper->view->getHelperPaths();
  131. $test = ucfirst($module) . '_View_Helper_';
  132. $found = false;
  133. foreach ($helperPaths as $prefix => $paths) {
  134. if ($test == $prefix) {
  135. $found = true;
  136. }
  137. }
  138. $this->assertTrue($found, 'Did not find auto-initialized helper path: ' . var_export($helperPaths, 1));
  139. $filterPaths = $this->helper->view->getFilterPaths();
  140. $test = ucfirst($module) . '_View_Filter_';
  141. $found = false;
  142. foreach ($filterPaths as $prefix => $paths) {
  143. if ($test == $prefix) {
  144. $found = true;
  145. }
  146. }
  147. $this->assertTrue($found, 'Did not find auto-initialized filter path: ' . var_export($filterPaths, 1));
  148. }
  149. public function testInitViewWithDefaults()
  150. {
  151. $this->request->setModuleName('foo')
  152. ->setControllerName('index');
  153. $controller = new Foo_IndexController($this->request, $this->response, array());
  154. $this->helper->setActionController($controller);
  155. $this->helper->initView();
  156. $this->_checkDefaults();
  157. }
  158. public function testInitViewWillNotRegisterSameViewPathTwice()
  159. {
  160. $this->request->setModuleName('foo')
  161. ->setControllerName('index');
  162. $controller = new Foo_IndexController($this->request, $this->response, array());
  163. $this->helper->setActionController($controller);
  164. $this->helper->initView();
  165. $moduleDir = dirname($this->front->getControllerDirectory('foo'));
  166. $this->helper->initView($moduleDir . '/views', 'Foo', array('encoding' => 'ISO-8858-1'));
  167. $this->_checkDefaults();
  168. }
  169. public function testInitViewCanBeCalledAfterPostDispatch()
  170. {
  171. $this->request->setModuleName('foo')
  172. ->setControllerName('index');
  173. $controller = new Foo_IndexController($this->request, $this->response, array());
  174. $this->helper->setActionController($controller);
  175. $this->helper->initView();
  176. $this->helper->setNoRender();
  177. $this->helper->postDispatch();
  178. $this->request->setModuleName('bar')
  179. ->setControllerName('index');
  180. $controller = new Bar_IndexController($this->request, $this->response, array());
  181. $this->helper->setActionController($controller);
  182. $this->helper->initView();
  183. $this->_checkDefaults('bar', 2);
  184. }
  185. public function testPreDispatchWithDefaults()
  186. {
  187. $this->request->setModuleName('foo')
  188. ->setControllerName('index');
  189. $controller = new Foo_IndexController($this->request, $this->response, array());
  190. $this->helper->setActionController($controller);
  191. $this->helper->preDispatch();
  192. $this->_checkDefaults();
  193. }
  194. public function testInitViewWithOptions()
  195. {
  196. $this->request->setModuleName('foo')
  197. ->setControllerName('index');
  198. $controller = new Foo_IndexController($this->request, $this->response, array());
  199. $this->helper->setActionController($controller);
  200. $viewDir = dirname(__FILE__) . str_repeat(DIRECTORY_SEPARATOR . '..', 2) . DIRECTORY_SEPARATOR . 'views';
  201. $this->helper->initView($viewDir, 'Baz_Bat', array(
  202. 'neverRender' => true,
  203. 'noRender' => true,
  204. 'noController' => true,
  205. 'viewSuffix' => 'php',
  206. 'scriptAction' => 'foo',
  207. 'responseSegment' => 'baz'
  208. ));
  209. $this->assertTrue($this->helper->getNeverRender());
  210. $this->assertTrue($this->helper->getNoRender());
  211. $this->assertTrue($this->helper->getNoController());
  212. $this->assertEquals('php', $this->helper->getViewSuffix());
  213. $this->assertEquals('foo', $this->helper->getScriptAction());
  214. $this->assertEquals('baz', $this->helper->getResponseSegment());
  215. $scriptPaths = $this->helper->view->getScriptPaths();
  216. $scriptPath = $scriptPaths[0];
  217. $this->assertContains($viewDir, $scriptPath);
  218. $helperPaths = $this->helper->view->getHelperPaths();
  219. $found = false;
  220. foreach ($helperPaths as $prefix => $paths) {
  221. if ('Baz_Bat_Helper_' == $prefix) {
  222. $found = true;
  223. }
  224. }
  225. $this->assertTrue($found, 'Helper prefix not set according to spec: ' . var_export($helperPaths, 1));
  226. $filterPaths = $this->helper->view->getFilterPaths();
  227. $found = false;
  228. foreach ($filterPaths as $prefix => $paths) {
  229. if ('Baz_Bat_Filter_' == $prefix) {
  230. $found = true;
  231. }
  232. }
  233. $this->assertTrue($found, 'Filter prefix not set according to spec' . var_export($filterPaths, 1));
  234. }
  235. public function testNeverRenderFlag()
  236. {
  237. $this->assertFalse($this->helper->getNeverRender());
  238. $this->helper->setNeverRender();
  239. $this->assertTrue($this->helper->getNeverRender());
  240. $this->helper->setNeverRender(false);
  241. $this->assertFalse($this->helper->getNeverRender());
  242. $this->helper->setNeverRender(true);
  243. $this->assertTrue($this->helper->getNeverRender());
  244. }
  245. public function testNeverRenderFlagDisablesRendering()
  246. {
  247. $this->helper->setNeverRender();
  248. $this->request->setModuleName('bar')
  249. ->setControllerName('index')
  250. ->setActionName('test')
  251. ->setDispatched(true);
  252. $controller = new Bar_IndexController($this->request, $this->response, array());
  253. $this->helper->setActionController($controller);
  254. $this->helper->postDispatch();
  255. $content = $this->response->getBody();
  256. $this->assertNotContains('Rendered index/test.phtml', $this->response->getBody());
  257. }
  258. public function testNoRenderFlag()
  259. {
  260. $this->assertFalse($this->helper->getNoRender());
  261. $this->helper->setNoRender();
  262. $this->assertTrue($this->helper->getNoRender());
  263. $this->helper->setNoRender(false);
  264. $this->assertFalse($this->helper->getNoRender());
  265. $this->helper->setNoRender(true);
  266. $this->assertTrue($this->helper->getNoRender());
  267. }
  268. public function testScriptActionProperty()
  269. {
  270. $this->assertNull($this->helper->getScriptAction());
  271. $this->helper->setScriptAction('foo');
  272. $this->assertEquals('foo', $this->helper->getScriptAction());
  273. $this->helper->setScriptAction('foo/bar');
  274. $this->assertEquals('foo/bar', $this->helper->getScriptAction());
  275. }
  276. public function testResponseSegmentProperty()
  277. {
  278. $this->assertNull($this->helper->getResponseSegment());
  279. $this->helper->setResponseSegment('foo');
  280. $this->assertEquals('foo', $this->helper->getResponseSegment());
  281. $this->helper->setResponseSegment('foo/bar');
  282. $this->assertEquals('foo/bar', $this->helper->getResponseSegment());
  283. }
  284. public function testNoControllerFlag()
  285. {
  286. $this->assertFalse($this->helper->getNoController());
  287. $this->helper->setNoController();
  288. $this->assertTrue($this->helper->getNoController());
  289. $this->helper->setNoController(false);
  290. $this->assertFalse($this->helper->getNoController());
  291. $this->helper->setNoController(true);
  292. $this->assertTrue($this->helper->getNoController());
  293. }
  294. public function testNeverControllerFlag()
  295. {
  296. $this->assertFalse($this->helper->getNeverController());
  297. $this->helper->setNeverController();
  298. $this->assertTrue($this->helper->getNeverController());
  299. $this->helper->setNeverController(false);
  300. $this->assertFalse($this->helper->getNeverController());
  301. $this->helper->setNeverController(true);
  302. $this->assertTrue($this->helper->getNeverController());
  303. }
  304. protected function _checkRenderProperties()
  305. {
  306. $this->assertEquals('foo', $this->helper->getScriptAction());
  307. $this->assertEquals('bar', $this->helper->getResponseSegment());
  308. $this->assertTrue($this->helper->getNoController());
  309. }
  310. public function testSetRenderSetsProperties()
  311. {
  312. $this->helper->setRender('foo', 'bar', true);
  313. $this->_checkRenderProperties();
  314. }
  315. public function testPostDispatchRendersAppropriateScript()
  316. {
  317. $this->request->setModuleName('bar')
  318. ->setControllerName('index')
  319. ->setActionName('test')
  320. ->setDispatched(true);
  321. $controller = new Bar_IndexController($this->request, $this->response, array());
  322. $this->helper->setActionController($controller);
  323. $this->helper->postDispatch();
  324. $content = $this->response->getBody();
  325. $this->assertContains('Rendered index/test.phtml in bar module', $content);
  326. }
  327. public function testPostDispatchDoesNothingOnForward()
  328. {
  329. $this->request->setModuleName('bar')
  330. ->setControllerName('index')
  331. ->setActionName('test')
  332. ->setDispatched(false);
  333. $controller = new Bar_IndexController($this->request, $this->response, array());
  334. $this->helper->setActionController($controller);
  335. $this->helper->postDispatch();
  336. $content = $this->response->getBody();
  337. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  338. $this->assertTrue(empty($content));
  339. }
  340. public function testPostDispatchDoesNothingOnRedirect()
  341. {
  342. $this->request->setModuleName('bar')
  343. ->setControllerName('index')
  344. ->setActionName('test')
  345. ->setDispatched(true);
  346. $this->response->setHttpResponseCode(302);
  347. $controller = new Bar_IndexController($this->request, $this->response, array());
  348. $this->helper->setActionController($controller);
  349. $this->helper->postDispatch();
  350. $content = $this->response->getBody();
  351. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  352. $this->assertTrue(empty($content));
  353. }
  354. public function testPostDispatchDoesNothingWithNoController()
  355. {
  356. $this->request->setModuleName('bar')
  357. ->setControllerName('index')
  358. ->setActionName('test')
  359. ->setDispatched(true);
  360. $this->helper->postDispatch();
  361. $content = $this->response->getBody();
  362. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  363. $this->assertTrue(empty($content));
  364. }
  365. public function testPostDispatchDoesNothingWithNeverController()
  366. {
  367. $this->request->setModuleName('bar')
  368. ->setControllerName('index')
  369. ->setActionName('test')
  370. ->setDispatched(true);
  371. $this->helper->setNeverController(true);
  372. $this->helper->postDispatch();
  373. $content = $this->response->getBody();
  374. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  375. $this->assertTrue(empty($content));
  376. }
  377. public function testDirectProxiesToSetRender()
  378. {
  379. $this->helper->direct('foo', 'bar', true);
  380. $this->_checkRenderProperties();
  381. }
  382. public function testViewBasePathSpecDefault()
  383. {
  384. $this->assertEquals(':moduleDir/views', $this->helper->getViewBasePathSpec());
  385. }
  386. public function testSettingViewBasePathSpec()
  387. {
  388. $this->helper->setViewBasePathSpec(':moduleDir/views/:controller');
  389. $this->assertEquals(':moduleDir/views/:controller', $this->helper->getViewBasePathSpec());
  390. }
  391. public function testViewScriptPathSpecDefault()
  392. {
  393. $this->assertEquals(':controller/:action.:suffix', $this->helper->getViewScriptPathSpec());
  394. }
  395. public function testSettingViewScriptPathSpec()
  396. {
  397. $this->helper->setViewScriptPathSpec(':moduleDir/views/:controller');
  398. $this->assertEquals(':moduleDir/views/:controller', $this->helper->getViewScriptPathSpec());
  399. }
  400. public function testViewScriptPathNoControllerSpecDefault()
  401. {
  402. $this->assertEquals(':action.:suffix', $this->helper->getViewScriptPathNoControllerSpec());
  403. }
  404. public function testSettingViewScriptPathNoControllerSpec()
  405. {
  406. $this->helper->setViewScriptPathNoControllerSpec(':module/:action.:suffix');
  407. $this->assertEquals(':module/:action.:suffix', $this->helper->getViewScriptPathNoControllerSpec());
  408. }
  409. public function testGetViewScriptWithDefaults()
  410. {
  411. $this->request->setModuleName('bar')
  412. ->setControllerName('index')
  413. ->setActionName('test');
  414. $controller = new Bar_IndexController($this->request, $this->response, array());
  415. $expected = 'index/test.phtml';
  416. $this->assertEquals($expected, $this->helper->getViewScript());
  417. }
  418. public function testGetViewScriptWithSpecifiedAction()
  419. {
  420. $this->request->setModuleName('bar')
  421. ->setControllerName('index')
  422. ->setActionName('test');
  423. $controller = new Bar_IndexController($this->request, $this->response, array());
  424. $expected = 'index/baz.phtml';
  425. $this->assertEquals($expected, $this->helper->getViewScript('baz'));
  426. }
  427. public function testGetViewScriptWithSpecifiedVars()
  428. {
  429. $this->request->setModuleName('bar')
  430. ->setControllerName('index')
  431. ->setActionName('test');
  432. $controller = new Bar_IndexController($this->request, $this->response, array());
  433. $expected = 'baz/bat.php';
  434. $this->assertEquals(
  435. $expected,
  436. $this->helper->getViewScript(
  437. null,
  438. array('controller' => 'baz', 'action' => 'bat', 'suffix' => 'php')
  439. )
  440. );
  441. }
  442. public function testGetViewScriptWithNoControllerSet()
  443. {
  444. $this->request->setModuleName('bar')
  445. ->setControllerName('index')
  446. ->setActionName('test');
  447. $controller = new Bar_IndexController($this->request, $this->response, array());
  448. $this->helper->setNoController();
  449. $expected = 'test.phtml';
  450. $this->assertEquals($expected, $this->helper->getViewScript());
  451. }
  452. public function testRenderScript()
  453. {
  454. $this->request->setModuleName('bar')
  455. ->setControllerName('index')
  456. ->setActionName('test');
  457. $controller = new Bar_IndexController($this->request, $this->response, array());
  458. $this->helper->renderScript('index/test.phtml');
  459. $body = $this->response->getBody();
  460. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  461. }
  462. public function testRenderScriptToNamedResponseSegment()
  463. {
  464. $this->request->setModuleName('bar')
  465. ->setControllerName('index')
  466. ->setActionName('test');
  467. $controller = new Bar_IndexController($this->request, $this->response, array());
  468. $this->helper->renderScript('index/test.phtml', 'foo');
  469. $body = $this->response->getBody('foo');
  470. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  471. }
  472. public function testRenderScriptToPreviouslyNamedResponseSegment()
  473. {
  474. $this->request->setModuleName('bar')
  475. ->setControllerName('index')
  476. ->setActionName('test');
  477. $controller = new Bar_IndexController($this->request, $this->response, array());
  478. $this->helper->setResponseSegment('foo');
  479. $this->helper->renderScript('index/test.phtml');
  480. $body = $this->response->getBody('foo');
  481. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  482. }
  483. public function testRenderWithDefaults()
  484. {
  485. $this->request->setModuleName('bar')
  486. ->setControllerName('index')
  487. ->setActionName('test');
  488. $controller = new Bar_IndexController($this->request, $this->response, array());
  489. $this->helper->render();
  490. $body = $this->response->getBody();
  491. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  492. }
  493. public function testRenderToSpecifiedAction()
  494. {
  495. $this->request->setModuleName('bar')
  496. ->setControllerName('index')
  497. ->setActionName('index');
  498. $controller = new Bar_IndexController($this->request, $this->response, array());
  499. $this->helper->render('test');
  500. $body = $this->response->getBody();
  501. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  502. }
  503. public function testRenderWithNoController()
  504. {
  505. $this->request->setModuleName('bar')
  506. ->setControllerName('index')
  507. ->setActionName('test');
  508. $controller = new Bar_IndexController($this->request, $this->response, array());
  509. $this->helper->render(null, null, true);
  510. $body = $this->response->getBody();
  511. $this->assertContains('Rendered test.phtml in bar module', $body);
  512. }
  513. public function testRenderToNamedSegment()
  514. {
  515. $this->request->setModuleName('bar')
  516. ->setControllerName('index')
  517. ->setActionName('test');
  518. $controller = new Bar_IndexController($this->request, $this->response, array());
  519. $this->helper->render(null, 'foo');
  520. $body = $this->response->getBody('foo');
  521. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  522. }
  523. public function testRenderBySpec()
  524. {
  525. $this->request->setModuleName('bar')
  526. ->setControllerName('index')
  527. ->setActionName('index');
  528. $controller = new Bar_IndexController($this->request, $this->response, array());
  529. $this->helper->renderBySpec('foo', array('controller' => 'test', 'suffix' => 'php'));
  530. $body = $this->response->getBody();
  531. $this->assertContains('Rendered test/foo.php', $body);
  532. }
  533. public function testRenderBySpecToNamedResponseSegment()
  534. {
  535. $this->request->setModuleName('bar')
  536. ->setControllerName('index')
  537. ->setActionName('index');
  538. $controller = new Bar_IndexController($this->request, $this->response, array());
  539. $this->helper->renderBySpec('foo', array('controller' => 'test', 'suffix' => 'php'), 'foo');
  540. $body = $this->response->getBody('foo');
  541. $this->assertContains('Rendered test/foo.php', $body);
  542. }
  543. public function testInitDoesNotInitViewWhenNoViewRendererSet()
  544. {
  545. $this->request->setModuleName('bar')
  546. ->setControllerName('index')
  547. ->setActionName('index');
  548. $this->front->setParam('noViewRenderer', true);
  549. $controller = new Bar_IndexController($this->request, $this->response, array());
  550. $this->assertNull($controller->view);
  551. }
  552. public function testPostDispatchDoesNotRenderViewWhenNoViewRendererSet()
  553. {
  554. $this->request->setModuleName('bar')
  555. ->setControllerName('index')
  556. ->setActionName('index');
  557. $this->front->setParam('noViewRenderer', true);
  558. $controller = new Bar_IndexController($this->request, $this->response, array());
  559. $this->helper->postDispatch();
  560. $body = $this->response->getBody();
  561. $this->assertTrue(empty($body));
  562. }
  563. public function testRenderNormalizationIsCorrect()
  564. {
  565. $this->request->setModuleName('default')
  566. ->setControllerName('foo')
  567. ->setActionName('myBar');
  568. $controller = new Bar_IndexController($this->request, $this->response, array());
  569. $this->helper->setActionController($controller);
  570. $scriptName = $this->helper->getViewScript();
  571. $this->assertEquals('foo/my-bar.phtml', $scriptName);
  572. $this->request->setModuleName('default')
  573. ->setControllerName('foo')
  574. ->setActionName('baz__bat');
  575. $scriptName = $this->helper->getViewScript();
  576. $this->assertEquals('foo/baz-bat.phtml', $scriptName);
  577. $this->request->setModuleName('default')
  578. ->setControllerName('Foo_Bar')
  579. ->setActionName('bar__baz');
  580. $scriptName = $this->helper->getViewScript();
  581. $this->assertEquals('foo/bar/bar-baz.phtml', $scriptName);
  582. }
  583. public function testGetInflectorGetsDefaultInflectorWhenNoneProvided()
  584. {
  585. $inflector = $this->helper->getInflector();
  586. $this->assertTrue($inflector instanceof Zend_Filter_Inflector);
  587. $rules = $inflector->getRules();
  588. $this->assertTrue(isset($rules['module']));
  589. $this->assertTrue(isset($rules['moduleDir']));
  590. $this->assertTrue(isset($rules['controller']));
  591. $this->assertTrue(isset($rules['action']));
  592. $this->assertTrue(isset($rules['suffix']));
  593. }
  594. public function testInflectorAccessorsAllowSwappingInflectors()
  595. {
  596. $inflector = $this->helper->getInflector();
  597. $this->assertTrue($inflector instanceof Zend_Filter_Inflector);
  598. $newInflector = new Zend_Filter_Inflector();
  599. $this->helper->setInflector($newInflector);
  600. $receivedInflector = $this->helper->getInflector();
  601. $this->assertSame($newInflector, $receivedInflector);
  602. $this->assertNotSame($newInflector, $inflector);
  603. }
  604. public function testCustomInflectorCanUseItsOwnTarget()
  605. {
  606. $this->request->setModuleName('bar')
  607. ->setControllerName('index')
  608. ->setActionName('index');
  609. $controller = new Bar_IndexController($this->request, $this->response, array());
  610. $this->helper->view->addBasePath($this->basePath . '/_files/modules/bar/views');
  611. $inflector = new Zend_Filter_Inflector('test.phtml');
  612. $inflector->addFilterRule(':controller', array('Word_CamelCaseToDash'));
  613. $this->helper->setInflector($inflector);
  614. $this->helper->render();
  615. $body = $this->response->getBody();
  616. $this->assertContains('Rendered test.phtml in bar module', $body);
  617. }
  618. public function testCustomInflectorUsesViewRendererTargetWhenPassedInWithReferenceFlag()
  619. {
  620. $this->request->setModuleName('bar')
  621. ->setControllerName('index')
  622. ->setActionName('test');
  623. $controller = new Bar_IndexController($this->request, $this->response, array());
  624. $this->helper->view->addBasePath($this->basePath . '/_files/modules/bar/views');
  625. require_once 'Zend/Filter/PregReplace.php';
  626. require_once 'Zend/Filter/Word/UnderscoreToSeparator.php';
  627. $inflector = new Zend_Filter_Inflector('test.phtml');
  628. $inflector->addRules(array(
  629. ':module' => array('Word_CamelCaseToDash', 'stringToLower'),
  630. ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator(DIRECTORY_SEPARATOR), 'StringToLower'),
  631. ':action' => array(
  632. 'Word_CamelCaseToDash',
  633. new Zend_Filter_PregReplace('/[^a-z0-9]+/i', '-'),
  634. 'StringToLower'
  635. ),
  636. ));
  637. $this->helper->setInflector($inflector, true);
  638. $this->helper->render();
  639. $body = $this->response->getBody();
  640. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  641. }
  642. public function testStockInflectorAllowsSubDirectoryViewScripts()
  643. {
  644. $this->request->setModuleName('bar')
  645. ->setControllerName('index')
  646. ->setActionName('layout/admin');
  647. $this->assertEquals('index/layout/admin.phtml', $this->helper->getViewScript());
  648. }
  649. /**
  650. * @see ZF-2443
  651. */
  652. public function testStockInflectorWorksWithViewBaseSpec()
  653. {
  654. $this->request->setModuleName('bar') // bar must exist so the ViewRendere doesnt throw an exception
  655. ->setControllerName('index')
  656. ->setActionName('admin');
  657. $controller = new Bar_IndexController($this->request, $this->response, array());
  658. $this->helper->setActionController($controller);
  659. $this->helper->setViewBasePathSpec(':moduleDir/:module');
  660. $this->helper->initView();
  661. $viewScriptPaths = $this->helper->view->getAllPaths();
  662. // we need this until View decides to not use DIRECTORY_SEPARATOR
  663. $expectedPathRegex = (DIRECTORY_SEPARATOR == '\\') ? '#modules\\\\bar\\\\bar\\\\scripts\\\\$#' : '#modules/bar/bar/scripts/$#';
  664. $this->assertRegExp($expectedPathRegex, $viewScriptPaths['script'][0]);
  665. $this->assertEquals($this->helper->getViewScript(), 'index/admin.phtml');
  666. }
  667. /**
  668. * @see ZF-2738
  669. */
  670. public function testStockInflectorWorksWithDottedRequestParts()
  671. {
  672. $this->request->setModuleName('foo')
  673. ->setControllerName('car.bar')
  674. ->setActionName('baz');
  675. $controller = new Bar_IndexController($this->request, $this->response, array());
  676. $this->helper->setActionController($controller);
  677. $viewScriptPaths = $this->helper->view->getAllPaths();
  678. $expectedPathRegex = (DIRECTORY_SEPARATOR == '\\') ? '#modules\\\\foo\\\\views\\\\scripts\\\\$#' : '#modules/foo/views/scripts/$#';
  679. $this->assertRegExp($expectedPathRegex, $viewScriptPaths['script'][0]);
  680. $this->assertEquals('car-bar/baz.phtml', $this->helper->getViewScript());
  681. }
  682. public function testCorrectViewHelperPathShouldBePropagatedWhenSubControllerInvoked()
  683. {
  684. require_once $this->basePath . '/_files/modules/foo/controllers/Admin/IndexController.php';
  685. $this->request->setModuleName('foo')
  686. ->setControllerName('admin_index')
  687. ->setActionName('use-helper');
  688. $controller = new Foo_Admin_IndexController($this->request, $this->response, array());
  689. $this->helper->render();
  690. $body = $this->response->getBody();
  691. $this->assertContains('fooUseHelper invoked', $body, 'Received ' . $body);
  692. }
  693. public function testCorrectViewHelperPathShouldBePropagatedWhenSubControllerInvokedInDefaultModule()
  694. {
  695. require_once $this->basePath . '/_files/modules/default/controllers/Admin/HelperController.php';
  696. $this->request->setControllerName('admin_helper')
  697. ->setActionName('render');
  698. $controller = new Admin_HelperController($this->request, $this->response, array());
  699. $this->helper->render();
  700. $body = $this->response->getBody();
  701. $this->assertContains('SampleZfHelper invoked', $body, 'Received ' . $body);
  702. }
  703. }
  704. // Call Zend_Controller_Action_Helper_ViewRendererTest::main() if this source file is executed directly.
  705. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_ViewRendererTest::main") {
  706. Zend_Controller_Action_Helper_ViewRendererTest::main();
  707. }