2
0

ViewRendererTest.php 33 KB

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