| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Layout
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_Layout_LayoutTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_Layout_LayoutTest::main");
- }
- require_once 'Zend/Layout.php';
- require_once 'Zend/Layout/Controller/Plugin/Layout.php';
- require_once 'Zend/Layout/Controller/Action/Helper/Layout.php';
- require_once 'Zend/Controller/Front.php';
- require_once 'Zend/Controller/Action/HelperBroker.php';
- require_once 'Zend/Filter/Inflector.php';
- require_once 'Zend/View/Interface.php';
- require_once 'Zend/View.php';
- /**
- * Test class for Zend_Layout.
- *
- * @category Zend
- * @package Zend_Layout
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Layout
- */
- class Zend_Layout_LayoutTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Runs the test methods of this class.
- *
- * @return void
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_Layout_LayoutTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Sets up the fixture, for example, open a network connection.
- * This method is called before a test is executed.
- *
- * @return void
- */
- public function setUp()
- {
- Zend_Layout_LayoutTest_Override::resetMvcInstance();
- Zend_Controller_Front::getInstance()->resetInstance();
- if (Zend_Controller_Action_HelperBroker::hasHelper('Layout')) {
- Zend_Controller_Action_HelperBroker::removeHelper('Layout');
- }
- if (Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
- Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
- }
- }
- /**
- * Tears down the fixture, for example, close a network connection.
- * This method is called after a test is executed.
- *
- * @return void
- */
- public function tearDown()
- {
- Zend_Layout::resetMvcInstance();
- }
- public function testDefaultLayoutStatusAtInitialization()
- {
- $layout = new Zend_Layout();
- $this->assertEquals('layout', $layout->getLayout());
- $this->assertEquals('content', $layout->getContentKey());
- $this->assertTrue($layout->isEnabled());
- $this->assertTrue($layout->inflectorEnabled());
- $this->assertNull($layout->getLayoutPath());
- $this->assertFalse($layout->getMvcEnabled());
- }
- public function testDefaultLayoutStatusAtInitializationWhenInitMvcFlagPassed()
- {
- $layout = new Zend_Layout(null, true);
- $this->assertEquals('layout', $layout->getLayout());
- $this->assertEquals('content', $layout->getContentKey());
- $this->assertTrue($layout->isEnabled());
- $this->assertTrue($layout->inflectorEnabled());
- $this->assertNull($layout->getLayoutPath());
- $this->assertTrue($layout->getMvcEnabled());
- }
- /**
- * @return void
- */
- public function testSetConfigModifiesAttributes()
- {
- $layout = new Zend_Layout();
- require_once 'Zend/Config.php';
- $config = new Zend_Config(array(
- 'layout' => 'foo',
- 'contentKey' => 'foo',
- 'layoutPath' => dirname(__FILE__),
- 'mvcEnabled' => false,
- ));
- $layout->setConfig($config);
- $this->assertEquals('foo', $layout->getLayout());
- $this->assertEquals('foo', $layout->getContentKey());
- $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
- $this->assertFalse($layout->getMvcEnabled());
- }
- /**
- * @return void
- */
- public function testSetOptionsWithConfigObjectModifiesAttributes()
- {
- $layout = new Zend_Layout();
- require_once 'Zend/Config.php';
- $config = new Zend_Config(array(
- 'layout' => 'foo',
- 'contentKey' => 'foo',
- 'layoutPath' => dirname(__FILE__),
- 'mvcEnabled' => false,
- ));
- $layout->setOptions($config);
- $this->assertEquals('foo', $layout->getLayout());
- $this->assertEquals('foo', $layout->getContentKey());
- $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
- $this->assertFalse($layout->getMvcEnabled());
- }
- /**
- * @return void
- */
- public function testLayoutAccessorsModifyAndRetrieveLayoutValue()
- {
- $layout = new Zend_Layout();
- $layout->setLayout('foo');
- $this->assertEquals('foo', $layout->getLayout());
- }
- /**
- * @return void
- */
- public function testSetLayoutEnablesLayouts()
- {
- $layout = new Zend_Layout();
- $layout->disableLayout();
- $this->assertFalse($layout->isEnabled());
- $layout->setLayout('foo');
- $this->assertTrue($layout->isEnabled());
- }
- /**
- * @return void
- */
- public function testDisableLayoutDisablesLayouts()
- {
- $layout = new Zend_Layout();
- $this->assertTrue($layout->isEnabled());
- $layout->disableLayout();
- $this->assertFalse($layout->isEnabled());
- }
- /**
- * @return void
- */
- public function testEnableLayoutEnablesLayouts()
- {
- $layout = new Zend_Layout();
- $this->assertTrue($layout->isEnabled());
- $layout->disableLayout();
- $this->assertFalse($layout->isEnabled());
- $layout->enableLayout();
- $this->assertTrue($layout->isEnabled());
- }
- /**
- * @return void
- */
- public function testLayoutPathAccessorsWork()
- {
- $layout = new Zend_Layout();
- $layout->setLayoutPath(dirname(__FILE__));
- $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
- }
- /**
- * @return void
- */
- public function testContentKeyAccessorsWork()
- {
- $layout = new Zend_Layout();
- $layout->setContentKey('foo');
- $this->assertEquals('foo', $layout->getContentKey());
- }
- /**
- * @return void
- */
- public function testMvcEnabledFlagFalseAfterStandardInstantiation()
- {
- $layout = new Zend_Layout();
- $this->assertFalse($layout->getMvcEnabled());
- }
- /**
- * @return void
- */
- public function testMvcEnabledFlagTrueWhenInstantiatedViaStartMvcMethod()
- {
- $layout = Zend_Layout::startMvc();
- $this->assertTrue($layout->getMvcEnabled());
- }
- /**
- * @return void
- */
- public function testGetViewRetrievesViewWhenNoneSet()
- {
- $layout = new Zend_Layout();
- $view = $layout->getView();
- $this->assertTrue($view instanceof Zend_View_Interface);
- }
- /**
- * @return void
- */
- public function testGetViewRetrievesViewFromViewRenderer()
- {
- $layout = new Zend_Layout();
- $view = $layout->getView();
- $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- $this->assertSame($vr->view, $view);
- }
- /**
- * @return void
- */
- public function testViewAccessorsAllowSettingView()
- {
- $layout = new Zend_Layout();
- $view = new Zend_View();
- $layout->setView($view);
- $received = $layout->getView();
- $this->assertSame($view, $received);
- }
- /**
- * @return void
- */
- public function testInflectorAccessorsWork()
- {
- $layout = new Zend_Layout();
- $inflector = new Zend_Filter_Inflector();
- $layout->setInflector($inflector);
- $this->assertSame($inflector, $layout->getInflector());
- }
- /**
- * @return void
- */
- public function testPluginClassAccessorsSetState()
- {
- $layout = new Zend_Layout();
- $layout->setPluginClass('Foo_Bar');
- $this->assertEquals('Foo_Bar', $layout->getPluginClass());
- }
- /**
- * @return void
- */
- public function testPluginClassPassedToStartMvcIsUsed()
- {
- $layout = Zend_Layout::startMvc(array('pluginClass' => 'Zend_Layout_LayoutTest_Controller_Plugin_Layout'));
- $this->assertTrue(Zend_Controller_Front::getInstance()->hasPlugin('Zend_Layout_LayoutTest_Controller_Plugin_Layout'));
- }
- /**
- * @return void
- */
- public function testHelperClassAccessorsSetState()
- {
- $layout = new Zend_Layout();
- $layout->setHelperClass('Foo_Bar');
- $this->assertEquals('Foo_Bar', $layout->getHelperClass());
- }
- /**
- * @return void
- */
- public function testHelperClassPassedToStartMvcIsUsed()
- {
- $layout = Zend_Layout::startMvc(array('helperClass' => 'Zend_Layout_LayoutTest_Controller_Action_Helper_Layout'));
- $this->assertTrue(Zend_Controller_Action_HelperBroker::hasHelper('layout'));
- $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('layout');
- $this->assertTrue($helper instanceof Zend_Layout_LayoutTest_Controller_Action_Helper_Layout);
- }
- /**
- * @return void
- */
- public function testEnableInflector()
- {
- $layout = new Zend_Layout();
- $layout->disableInflector();
- $this->assertFalse($layout->inflectorEnabled());
- $layout->enableInflector();
- $this->assertTrue($layout->inflectorEnabled());
- }
- /**
- * @return void
- */
- public function testDisableInflector()
- {
- $layout = new Zend_Layout();
- $layout->disableInflector();
- $this->assertFalse($layout->inflectorEnabled());
- }
- /**
- * @return void
- */
- public function testOverloadingAccessorsWork()
- {
- $layout = new Zend_Layout();
- $layout->foo = 'bar';
- $this->assertTrue(isset($layout->foo));
- $this->assertEquals('bar', $layout->foo);
- unset($layout->foo);
- $this->assertFalse(isset($layout->foo));
- }
- /**
- * @return void
- */
- public function testAssignWithKeyValuePairPopulatesPropertyAccessibleViaOverloading()
- {
- $layout = new Zend_Layout();
- $layout->assign('foo', 'bar');
- $this->assertEquals('bar', $layout->foo);
- }
- /**
- * @return void
- */
- public function testAssignWithArrayPopulatesPropertiesAccessibleViaOverloading()
- {
- $layout = new Zend_Layout();
- $layout->assign(array(
- 'foo' => 'bar',
- 'bar' => 'baz'
- ));
- $this->assertEquals('bar', $layout->foo);
- $this->assertEquals('baz', $layout->bar);
- }
- /**
- * @return void
- */
- public function testRenderWithNoInflection()
- {
- $layout = new Zend_Layout();
- $view = new Zend_View();
- $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
- ->disableInflector()
- ->setLayout('layout.phtml')
- ->setView($view);
- $layout->message = 'Rendered layout';
- $received = $layout->render();
- $this->assertContains('Testing layouts:', $received);
- $this->assertContains($layout->message, $received);
- }
- public function testRenderWithDefaultInflection()
- {
- $layout = new Zend_Layout();
- $view = new Zend_View();
- $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
- ->setView($view);
- $layout->message = 'Rendered layout';
- $received = $layout->render();
- $this->assertContains('Testing layouts:', $received);
- $this->assertContains($layout->message, $received);
- }
- public function testRenderWithCustomInflection()
- {
- $layout = new Zend_Layout();
- $view = new Zend_View();
- $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
- ->setView($view);
- $inflector = $layout->getInflector();
- $inflector->setTarget('test/:script.:suffix')
- ->setStaticRule('suffix', 'php');
- $layout->message = 'Rendered layout';
- $received = $layout->render();
- $this->assertContains('Testing layouts with custom inflection:', $received);
- $this->assertContains($layout->message, $received);
- }
- public function testGetMvcInstanceReturnsNullWhenStartMvcHasNotBeenCalled()
- {
- $this->assertNull(Zend_Layout::getMvcInstance());
- }
- public function testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled()
- {
- $layout = Zend_Layout::startMvc();
- $received = Zend_Layout::getMvcInstance();
- $this->assertSame($layout, $received);
- }
- public function testSubsequentCallsToStartMvcWithOptionsSetState()
- {
- $layout = Zend_Layout::startMvc();
- $this->assertTrue($layout->getMvcSuccessfulActionOnly());
- $this->assertEquals('content', $layout->getContentKey());
- Zend_Layout::startMvc(array(
- 'mvcSuccessfulActionOnly' => false,
- 'contentKey' => 'foobar'
- ));
- $this->assertFalse($layout->getMvcSuccessfulActionOnly());
- $this->assertEquals('foobar', $layout->getContentKey());
- }
- public function testGetViewSuffixRetrievesDefaultValue()
- {
- $layout = new Zend_Layout();
- $this->assertEquals('phtml', $layout->getViewSuffix());
- }
- public function testViewSuffixAccessorsWork()
- {
- $layout = new Zend_Layout();
- $layout->setViewSuffix('php');
- $this->assertEquals('php', $layout->getViewSuffix());
- }
- public function testSettingViewSuffixChangesInflectorSuffix()
- {
- $layout = new Zend_Layout();
- $inflector = $layout->getInflector();
- $rules = $inflector->getRules();
- $this->assertTrue(isset($rules['suffix']));
- $this->assertEquals($layout->getViewSuffix(), $rules['suffix']);
- $layout->setViewSuffix('php');
- $this->assertEquals($layout->getViewSuffix(), $rules['suffix']);
- }
- public function testGetInflectorTargetRetrievesDefaultValue()
- {
- $layout = new Zend_Layout();
- $this->assertEquals(':script.:suffix', $layout->getInflectorTarget());
- }
- public function testInflectorTargetAccessorsWork()
- {
- $layout = new Zend_Layout();
- $layout->setInflectorTarget(':script-foo.:suffix');
- $this->assertEquals(':script-foo.:suffix', $layout->getInflectorTarget());
- }
- public function testSettingInflectorTargetChangesInflectorSuffix()
- {
- $layout = new Zend_Layout();
- $inflector = $layout->getInflector();
- $target = $inflector->getTarget();
- $this->assertEquals($layout->getInflectorTarget(), $inflector->getTarget());
- $layout->setInflectorTarget('php');
- $this->assertEquals($layout->getInflectorTarget(), $inflector->getTarget());
- }
- public function testLayoutWithViewBasePath()
- {
- $layout = new Zend_Layout(array(
- 'viewBasePath' => dirname(__FILE__) . '/_files/layouts-basepath/')
- );
- $this->assertEquals('layout inside basePath', $layout->render());
- $layout->setLayout('layout2');
- $this->assertEquals('foobar-helper-output', $layout->render());
- }
- public function testResettingMvcInstanceUnregistersHelperAndPlugin()
- {
- $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
- Zend_Layout::resetMvcInstance();
- $front = Zend_Controller_Front::getInstance();
- $this->assertFalse($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'), 'Plugin not unregistered');
- $this->assertFalse(Zend_Controller_Action_HelperBroker::hasHelper('Layout'), 'Helper not unregistered');
- }
- public function testResettingMvcInstanceRemovesMvcSingleton()
- {
- $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
- Zend_Layout::resetMvcInstance();
- $this->assertNull(Zend_Layout::getMvcInstance());
- }
- public function testMinimalViewObjectWorks()
- {
- require_once dirname(__FILE__) . '/_files/MinimalCustomView.php';
- $layout = new Zend_Layout(array(
- 'view' => new Zend_Layout_Test_MinimalCustomView(),
- 'ViewScriptPath' => 'some/path'
- ));
- $layout->render();
- }
- /**
- * @group ZF-5152
- */
- public function testCallingStartMvcTwiceDoesntGenerateAnyUnexpectedBehavior()
- {
- Zend_Layout::startMvc('/some/path');
- $this->assertEquals(Zend_Layout::getMvcInstance()->getLayoutPath(),'/some/path');
- Zend_Layout::startMvc('/some/other/path');
- $this->assertEquals(Zend_Layout::getMvcInstance()->getLayoutPath(),'/some/other/path');
- $this->assertTrue(Zend_Layout::getMvcInstance()->isEnabled());
- }
- /**
- * @group ZF-5891
- */
- public function testSetLayoutWithDisabledFlag()
- {
- $layout = new Zend_Layout();
- $layout->disableLayout();
- $layout->setLayout('foo', false);
- $this->assertEquals('foo', $layout->getLayout());
- $this->assertFalse($layout->isEnabled());
- }
- }
- /**
- * Zend_Layout extension to allow resetting mvcInstance static member
- */
- class Zend_Layout_LayoutTest_Override extends Zend_Layout
- {
- public static function resetMvcInstance()
- {
- self::$_mvcInstance = null;
- }
- }
- class Zend_Layout_LayoutTest_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
- {
- }
- class Zend_Layout_LayoutTest_Controller_Action_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
- {
- }
- // Call Zend_Layout_LayoutTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Layout_LayoutTest::main") {
- Zend_Layout_LayoutTest::main();
- }
|