LayoutTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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_Layout
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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_Layout_LayoutTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Layout_LayoutTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/Layout.php';
  30. require_once 'Zend/Layout/Controller/Plugin/Layout.php';
  31. require_once 'Zend/Layout/Controller/Action/Helper/Layout.php';
  32. require_once 'Zend/Controller/Front.php';
  33. require_once 'Zend/Controller/Action/HelperBroker.php';
  34. require_once 'Zend/Filter/Inflector.php';
  35. require_once 'Zend/View/Interface.php';
  36. require_once 'Zend/View.php';
  37. /**
  38. * Test class for Zend_Layout.
  39. *
  40. * @category Zend
  41. * @package Zend_Layout
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Layout
  46. */
  47. class Zend_Layout_LayoutTest extends PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @return void
  53. */
  54. public static function main()
  55. {
  56. require_once "PHPUnit/TextUI/TestRunner.php";
  57. $suite = new PHPUnit_Framework_TestSuite("Zend_Layout_LayoutTest");
  58. $result = PHPUnit_TextUI_TestRunner::run($suite);
  59. }
  60. /**
  61. * Sets up the fixture, for example, open a network connection.
  62. * This method is called before a test is executed.
  63. *
  64. * @return void
  65. */
  66. public function setUp()
  67. {
  68. Zend_Layout_LayoutTest_Override::resetMvcInstance();
  69. Zend_Controller_Front::getInstance()->resetInstance();
  70. if (Zend_Controller_Action_HelperBroker::hasHelper('Layout')) {
  71. Zend_Controller_Action_HelperBroker::removeHelper('Layout');
  72. }
  73. if (Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
  74. Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  75. }
  76. }
  77. /**
  78. * Tears down the fixture, for example, close a network connection.
  79. * This method is called after a test is executed.
  80. *
  81. * @return void
  82. */
  83. public function tearDown()
  84. {
  85. Zend_Layout::resetMvcInstance();
  86. }
  87. public function testDefaultLayoutStatusAtInitialization()
  88. {
  89. $layout = new Zend_Layout();
  90. $this->assertEquals('layout', $layout->getLayout());
  91. $this->assertEquals('content', $layout->getContentKey());
  92. $this->assertTrue($layout->isEnabled());
  93. $this->assertTrue($layout->inflectorEnabled());
  94. $this->assertNull($layout->getLayoutPath());
  95. $this->assertFalse($layout->getMvcEnabled());
  96. }
  97. public function testDefaultLayoutStatusAtInitializationWhenInitMvcFlagPassed()
  98. {
  99. $layout = new Zend_Layout(null, true);
  100. $this->assertEquals('layout', $layout->getLayout());
  101. $this->assertEquals('content', $layout->getContentKey());
  102. $this->assertTrue($layout->isEnabled());
  103. $this->assertTrue($layout->inflectorEnabled());
  104. $this->assertNull($layout->getLayoutPath());
  105. $this->assertTrue($layout->getMvcEnabled());
  106. }
  107. /**
  108. * @return void
  109. */
  110. public function testSetConfigModifiesAttributes()
  111. {
  112. $layout = new Zend_Layout();
  113. require_once 'Zend/Config.php';
  114. $config = new Zend_Config(array(
  115. 'layout' => 'foo',
  116. 'contentKey' => 'foo',
  117. 'layoutPath' => dirname(__FILE__),
  118. 'mvcEnabled' => false,
  119. ));
  120. $layout->setConfig($config);
  121. $this->assertEquals('foo', $layout->getLayout());
  122. $this->assertEquals('foo', $layout->getContentKey());
  123. $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
  124. $this->assertFalse($layout->getMvcEnabled());
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function testSetOptionsWithConfigObjectModifiesAttributes()
  130. {
  131. $layout = new Zend_Layout();
  132. require_once 'Zend/Config.php';
  133. $config = new Zend_Config(array(
  134. 'layout' => 'foo',
  135. 'contentKey' => 'foo',
  136. 'layoutPath' => dirname(__FILE__),
  137. 'mvcEnabled' => false,
  138. ));
  139. $layout->setOptions($config);
  140. $this->assertEquals('foo', $layout->getLayout());
  141. $this->assertEquals('foo', $layout->getContentKey());
  142. $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
  143. $this->assertFalse($layout->getMvcEnabled());
  144. }
  145. /**
  146. * @return void
  147. */
  148. public function testLayoutAccessorsModifyAndRetrieveLayoutValue()
  149. {
  150. $layout = new Zend_Layout();
  151. $layout->setLayout('foo');
  152. $this->assertEquals('foo', $layout->getLayout());
  153. }
  154. /**
  155. * @return void
  156. */
  157. public function testSetLayoutEnablesLayouts()
  158. {
  159. $layout = new Zend_Layout();
  160. $layout->disableLayout();
  161. $this->assertFalse($layout->isEnabled());
  162. $layout->setLayout('foo');
  163. $this->assertTrue($layout->isEnabled());
  164. }
  165. /**
  166. * @return void
  167. */
  168. public function testDisableLayoutDisablesLayouts()
  169. {
  170. $layout = new Zend_Layout();
  171. $this->assertTrue($layout->isEnabled());
  172. $layout->disableLayout();
  173. $this->assertFalse($layout->isEnabled());
  174. }
  175. /**
  176. * @return void
  177. */
  178. public function testEnableLayoutEnablesLayouts()
  179. {
  180. $layout = new Zend_Layout();
  181. $this->assertTrue($layout->isEnabled());
  182. $layout->disableLayout();
  183. $this->assertFalse($layout->isEnabled());
  184. $layout->enableLayout();
  185. $this->assertTrue($layout->isEnabled());
  186. }
  187. /**
  188. * @return void
  189. */
  190. public function testLayoutPathAccessorsWork()
  191. {
  192. $layout = new Zend_Layout();
  193. $layout->setLayoutPath(dirname(__FILE__));
  194. $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
  195. }
  196. /**
  197. * @return void
  198. */
  199. public function testContentKeyAccessorsWork()
  200. {
  201. $layout = new Zend_Layout();
  202. $layout->setContentKey('foo');
  203. $this->assertEquals('foo', $layout->getContentKey());
  204. }
  205. /**
  206. * @return void
  207. */
  208. public function testMvcEnabledFlagFalseAfterStandardInstantiation()
  209. {
  210. $layout = new Zend_Layout();
  211. $this->assertFalse($layout->getMvcEnabled());
  212. }
  213. /**
  214. * @return void
  215. */
  216. public function testMvcEnabledFlagTrueWhenInstantiatedViaStartMvcMethod()
  217. {
  218. $layout = Zend_Layout::startMvc();
  219. $this->assertTrue($layout->getMvcEnabled());
  220. }
  221. /**
  222. * @return void
  223. */
  224. public function testGetViewRetrievesViewWhenNoneSet()
  225. {
  226. $layout = new Zend_Layout();
  227. $view = $layout->getView();
  228. $this->assertTrue($view instanceof Zend_View_Interface);
  229. }
  230. /**
  231. * @return void
  232. */
  233. public function testGetViewRetrievesViewFromViewRenderer()
  234. {
  235. $layout = new Zend_Layout();
  236. $view = $layout->getView();
  237. $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  238. $this->assertSame($vr->view, $view);
  239. }
  240. /**
  241. * @return void
  242. */
  243. public function testViewAccessorsAllowSettingView()
  244. {
  245. $layout = new Zend_Layout();
  246. $view = new Zend_View();
  247. $layout->setView($view);
  248. $received = $layout->getView();
  249. $this->assertSame($view, $received);
  250. }
  251. /**
  252. * @return void
  253. */
  254. public function testInflectorAccessorsWork()
  255. {
  256. $layout = new Zend_Layout();
  257. $inflector = new Zend_Filter_Inflector();
  258. $layout->setInflector($inflector);
  259. $this->assertSame($inflector, $layout->getInflector());
  260. }
  261. /**
  262. * @return void
  263. */
  264. public function testPluginClassAccessorsSetState()
  265. {
  266. $layout = new Zend_Layout();
  267. $layout->setPluginClass('Foo_Bar');
  268. $this->assertEquals('Foo_Bar', $layout->getPluginClass());
  269. }
  270. /**
  271. * @return void
  272. */
  273. public function testPluginClassPassedToStartMvcIsUsed()
  274. {
  275. $layout = Zend_Layout::startMvc(array('pluginClass' => 'Zend_Layout_LayoutTest_Controller_Plugin_Layout'));
  276. $this->assertTrue(Zend_Controller_Front::getInstance()->hasPlugin('Zend_Layout_LayoutTest_Controller_Plugin_Layout'));
  277. }
  278. /**
  279. * @return void
  280. */
  281. public function testHelperClassAccessorsSetState()
  282. {
  283. $layout = new Zend_Layout();
  284. $layout->setHelperClass('Foo_Bar');
  285. $this->assertEquals('Foo_Bar', $layout->getHelperClass());
  286. }
  287. /**
  288. * @return void
  289. */
  290. public function testHelperClassPassedToStartMvcIsUsed()
  291. {
  292. $layout = Zend_Layout::startMvc(array('helperClass' => 'Zend_Layout_LayoutTest_Controller_Action_Helper_Layout'));
  293. $this->assertTrue(Zend_Controller_Action_HelperBroker::hasHelper('layout'));
  294. $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('layout');
  295. $this->assertTrue($helper instanceof Zend_Layout_LayoutTest_Controller_Action_Helper_Layout);
  296. }
  297. /**
  298. * @return void
  299. */
  300. public function testEnableInflector()
  301. {
  302. $layout = new Zend_Layout();
  303. $layout->disableInflector();
  304. $this->assertFalse($layout->inflectorEnabled());
  305. $layout->enableInflector();
  306. $this->assertTrue($layout->inflectorEnabled());
  307. }
  308. /**
  309. * @return void
  310. */
  311. public function testDisableInflector()
  312. {
  313. $layout = new Zend_Layout();
  314. $layout->disableInflector();
  315. $this->assertFalse($layout->inflectorEnabled());
  316. }
  317. /**
  318. * @return void
  319. */
  320. public function testOverloadingAccessorsWork()
  321. {
  322. $layout = new Zend_Layout();
  323. $layout->foo = 'bar';
  324. $this->assertTrue(isset($layout->foo));
  325. $this->assertEquals('bar', $layout->foo);
  326. unset($layout->foo);
  327. $this->assertFalse(isset($layout->foo));
  328. }
  329. /**
  330. * @return void
  331. */
  332. public function testAssignWithKeyValuePairPopulatesPropertyAccessibleViaOverloading()
  333. {
  334. $layout = new Zend_Layout();
  335. $layout->assign('foo', 'bar');
  336. $this->assertEquals('bar', $layout->foo);
  337. }
  338. /**
  339. * @return void
  340. */
  341. public function testAssignWithArrayPopulatesPropertiesAccessibleViaOverloading()
  342. {
  343. $layout = new Zend_Layout();
  344. $layout->assign(array(
  345. 'foo' => 'bar',
  346. 'bar' => 'baz'
  347. ));
  348. $this->assertEquals('bar', $layout->foo);
  349. $this->assertEquals('baz', $layout->bar);
  350. }
  351. /**
  352. * @return void
  353. */
  354. public function testRenderWithNoInflection()
  355. {
  356. $layout = new Zend_Layout();
  357. $view = new Zend_View();
  358. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  359. ->disableInflector()
  360. ->setLayout('layout.phtml')
  361. ->setView($view);
  362. $layout->message = 'Rendered layout';
  363. $received = $layout->render();
  364. $this->assertContains('Testing layouts:', $received);
  365. $this->assertContains($layout->message, $received);
  366. }
  367. public function testRenderWithDefaultInflection()
  368. {
  369. $layout = new Zend_Layout();
  370. $view = new Zend_View();
  371. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  372. ->setView($view);
  373. $layout->message = 'Rendered layout';
  374. $received = $layout->render();
  375. $this->assertContains('Testing layouts:', $received);
  376. $this->assertContains($layout->message, $received);
  377. }
  378. public function testRenderWithCustomInflection()
  379. {
  380. $layout = new Zend_Layout();
  381. $view = new Zend_View();
  382. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  383. ->setView($view);
  384. $inflector = $layout->getInflector();
  385. $inflector->setTarget('test/:script.:suffix')
  386. ->setStaticRule('suffix', 'php');
  387. $layout->message = 'Rendered layout';
  388. $received = $layout->render();
  389. $this->assertContains('Testing layouts with custom inflection:', $received);
  390. $this->assertContains($layout->message, $received);
  391. }
  392. public function testGetMvcInstanceReturnsNullWhenStartMvcHasNotBeenCalled()
  393. {
  394. $this->assertNull(Zend_Layout::getMvcInstance());
  395. }
  396. public function testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled()
  397. {
  398. $layout = Zend_Layout::startMvc();
  399. $received = Zend_Layout::getMvcInstance();
  400. $this->assertSame($layout, $received);
  401. }
  402. public function testSubsequentCallsToStartMvcWithOptionsSetState()
  403. {
  404. $layout = Zend_Layout::startMvc();
  405. $this->assertTrue($layout->getMvcSuccessfulActionOnly());
  406. $this->assertEquals('content', $layout->getContentKey());
  407. Zend_Layout::startMvc(array(
  408. 'mvcSuccessfulActionOnly' => false,
  409. 'contentKey' => 'foobar'
  410. ));
  411. $this->assertFalse($layout->getMvcSuccessfulActionOnly());
  412. $this->assertEquals('foobar', $layout->getContentKey());
  413. }
  414. public function testGetViewSuffixRetrievesDefaultValue()
  415. {
  416. $layout = new Zend_Layout();
  417. $this->assertEquals('phtml', $layout->getViewSuffix());
  418. }
  419. public function testViewSuffixAccessorsWork()
  420. {
  421. $layout = new Zend_Layout();
  422. $layout->setViewSuffix('php');
  423. $this->assertEquals('php', $layout->getViewSuffix());
  424. }
  425. public function testSettingViewSuffixChangesInflectorSuffix()
  426. {
  427. $layout = new Zend_Layout();
  428. $inflector = $layout->getInflector();
  429. $rules = $inflector->getRules();
  430. $this->assertTrue(isset($rules['suffix']));
  431. $this->assertEquals($layout->getViewSuffix(), $rules['suffix']);
  432. $layout->setViewSuffix('php');
  433. $this->assertEquals($layout->getViewSuffix(), $rules['suffix']);
  434. }
  435. public function testGetInflectorTargetRetrievesDefaultValue()
  436. {
  437. $layout = new Zend_Layout();
  438. $this->assertEquals(':script.:suffix', $layout->getInflectorTarget());
  439. }
  440. public function testInflectorTargetAccessorsWork()
  441. {
  442. $layout = new Zend_Layout();
  443. $layout->setInflectorTarget(':script-foo.:suffix');
  444. $this->assertEquals(':script-foo.:suffix', $layout->getInflectorTarget());
  445. }
  446. public function testSettingInflectorTargetChangesInflectorSuffix()
  447. {
  448. $layout = new Zend_Layout();
  449. $inflector = $layout->getInflector();
  450. $target = $inflector->getTarget();
  451. $this->assertEquals($layout->getInflectorTarget(), $inflector->getTarget());
  452. $layout->setInflectorTarget('php');
  453. $this->assertEquals($layout->getInflectorTarget(), $inflector->getTarget());
  454. }
  455. public function testLayoutWithViewBasePath()
  456. {
  457. $layout = new Zend_Layout(array(
  458. 'viewBasePath' => dirname(__FILE__) . '/_files/layouts-basepath/')
  459. );
  460. $this->assertEquals('layout inside basePath', $layout->render());
  461. $layout->setLayout('layout2');
  462. $this->assertEquals('foobar-helper-output', $layout->render());
  463. }
  464. public function testResettingMvcInstanceUnregistersHelperAndPlugin()
  465. {
  466. $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
  467. Zend_Layout::resetMvcInstance();
  468. $front = Zend_Controller_Front::getInstance();
  469. $this->assertFalse($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'), 'Plugin not unregistered');
  470. $this->assertFalse(Zend_Controller_Action_HelperBroker::hasHelper('Layout'), 'Helper not unregistered');
  471. }
  472. public function testResettingMvcInstanceRemovesMvcSingleton()
  473. {
  474. $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
  475. Zend_Layout::resetMvcInstance();
  476. $this->assertNull(Zend_Layout::getMvcInstance());
  477. }
  478. public function testMinimalViewObjectWorks()
  479. {
  480. require_once dirname(__FILE__) . '/_files/MinimalCustomView.php';
  481. $layout = new Zend_Layout(array(
  482. 'view' => new Zend_Layout_Test_MinimalCustomView(),
  483. 'ViewScriptPath' => 'some/path'
  484. ));
  485. $layout->render();
  486. }
  487. // #ZF-5152
  488. public function testCallingStartMvcTwiceDoesntGenerateAnyUnexpectedBehavior()
  489. {
  490. Zend_Layout::startMvc('/some/path');
  491. $this->assertEquals(Zend_Layout::getMvcInstance()->getLayoutPath(),'/some/path');
  492. Zend_Layout::startMvc('/some/other/path');
  493. $this->assertEquals(Zend_Layout::getMvcInstance()->getLayoutPath(),'/some/other/path');
  494. $this->assertTrue(Zend_Layout::getMvcInstance()->isEnabled());
  495. }
  496. }
  497. /**
  498. * Zend_Layout extension to allow resetting mvcInstance static member
  499. */
  500. class Zend_Layout_LayoutTest_Override extends Zend_Layout
  501. {
  502. public static function resetMvcInstance()
  503. {
  504. self::$_mvcInstance = null;
  505. }
  506. }
  507. class Zend_Layout_LayoutTest_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
  508. {
  509. }
  510. class Zend_Layout_LayoutTest_Controller_Action_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
  511. {
  512. }
  513. // Call Zend_Layout_LayoutTest::main() if this source file is executed directly.
  514. if (PHPUnit_MAIN_METHOD == "Zend_Layout_LayoutTest::main") {
  515. Zend_Layout_LayoutTest::main();
  516. }