view = $this->getView(); $this->helper = new Zend_Dojo_View_Helper_Dojo_Container(); $this->helper->setView($this->view); Zend_Registry::set('Zend_Dojo_View_Helper_Dojo', $this->helper); Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(); } /** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. * * @return void */ public function tearDown() { } public function getView() { require_once 'Zend/View.php'; $view = new Zend_View(); $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper'); return $view; } public function testViewPropertyShouldBeNullByDefault() { $helper = new Zend_Dojo_View_Helper_Dojo(); $this->assertNull($helper->view); } public function testShouldBeAbleToSetViewProperty() { $this->assertTrue($this->helper->view instanceof Zend_View_Interface); } public function testNoModulesShouldBeRegisteredByDefault() { $modules = $this->helper->getModules(); $this->assertTrue(empty($modules)); } public function testShouldBeAbleToRequireModules() { $this->helper->requireModule('foo.bar'); $modules = $this->helper->getModules(); $this->assertContains('foo.bar', $modules); } /** * @group ZF-3914 */ public function testShouldAllowRequiringMultipleModulesAtOnce() { $modules = array('foo.bar', 'bar.baz', 'baz.bat'); $this->helper->requireModule($modules); $test = $this->helper->getModules(); foreach ($modules as $module) { $this->assertTrue(in_array($module, $test)); } } public function testInvalidModuleNameShouldThrowExceptionDuringRegistration() { try { $this->helper->requireModule('foo#$!bar'); $this->fail('Invalid module name should throw exception during registration'); } catch (Zend_Dojo_View_Exception $e) { $this->assertContains('invalid character', $e->getMessage()); } } /** * @group ZF-3916 */ public function testRequireModuleShouldAllowDashAndUnderscoreCharacters() { $this->helper->requireModule('dojox.highlight.language._www'); $this->helper->requireModule('dojo.NodeList-fx'); } public function testShouldNotRegisterDuplicateModules() { $this->helper->requireModule('foo.bar'); $this->helper->requireModule('foo.bar'); $modules = $this->helper->getModules(); $this->assertContains('foo.bar', $modules); $this->assertEquals(1, count($modules)); } public function testModulePathsShouldBeEmptyByDefault() { $paths = $this->helper->getModulePaths(); $this->assertTrue(empty($paths)); } public function testShouldBeAbleToRegisterModulePaths() { $this->helper->registerModulePath('custom', '../custom'); $paths = $this->helper->getModulePaths(); $this->assertTrue(array_key_exists('custom', $paths), var_export($paths, 1)); $this->assertContains('../custom', $paths); } public function testShouldNotBeAbleToRegisterDuplicateModulePaths() { $this->helper->registerModulePath('custom', '../custom'); $this->helper->registerModulePath('custom', '../custom'); $paths = $this->helper->getModulePaths(); $this->assertEquals(1, count($paths)); $this->assertTrue(array_key_exists('custom', $paths)); $this->assertContains('../custom', $paths); } public function testShouldBeDisabledByDefault() { $this->assertFalse($this->helper->isEnabled()); } public function testCallingAUseMethodShouldEnableHelper() { $this->testShouldBeDisabledByDefault(); $this->helper->setCdnVersion('1.0'); $this->assertTrue($this->helper->isEnabled()); $this->helper->disable(); $this->assertFalse($this->helper->isEnabled()); $this->helper->setLocalPath('/js/dojo/dojo.js'); $this->assertTrue($this->helper->isEnabled()); } public function testShouldUtilizeCdnByDefault() { $this->helper->enable(); $this->assertTrue($this->helper->useCdn()); } public function testShouldUseGoogleCdnByDefault() { $this->assertEquals(Zend_Dojo::CDN_BASE_GOOGLE, $this->helper->getCdnBase()); } public function testShouldAllowSpecifyingCdnBasePath() { $this->testShouldUseGoogleCdnByDefault(); $this->helper->setCdnBase(Zend_Dojo::CDN_BASE_AOL); $this->assertEquals(Zend_Dojo::CDN_BASE_AOL, $this->helper->getCdnBase()); } public function testShouldUseLatestVersionWhenUsingCdnByDefault() { $this->helper->enable(); $this->assertEquals('1.3.2', $this->helper->getCdnVersion()); } public function testShouldAllowSpecifyingDojoVersionWhenUtilizingCdn() { $this->helper->setCdnVersion('1.0'); $this->assertEquals('1.0', $this->helper->getCdnVersion()); } public function testShouldUseAolCdnDojoPathByDefault() { $this->assertEquals(Zend_Dojo::CDN_DOJO_PATH_AOL, $this->helper->getCdnDojoPath()); } public function testShouldAllowSpecifyingCdnDojoPath() { $this->testShouldUseAolCdnDojoPathByDefault(); $this->helper->setCdnDojoPath(Zend_Dojo::CDN_DOJO_PATH_GOOGLE); $this->assertEquals(Zend_Dojo::CDN_DOJO_PATH_GOOGLE, $this->helper->getCdnDojoPath()); } public function testShouldAllowSpecifyingLocalDojoInstall() { $this->helper->setLocalPath('/js/dojo/dojo.js'); $this->assertTrue($this->helper->useLocalPath()); } public function testShouldAllowSpecifyingDjConfig() { $this->helper->setDjConfig(array('parseOnLoad' => 'true')); $config = $this->helper->getDjConfig(); $this->assertTrue(is_array($config)); $this->assertTrue(array_key_exists('parseOnLoad', $config)); $this->assertEquals('true', $config['parseOnLoad']); } public function testShouldAllowRetrievingIndividualDjConfigKeys() { $this->helper->setDjConfigOption('parseOnLoad', 'true'); $this->assertEquals('true', $this->helper->getDjConfigOption('parseOnLoad')); } public function testGetDjConfigShouldReturnEmptyArrayByDefault() { $this->assertSame(array(), $this->helper->getDjConfig()); } public function testGetDjConfigOptionShouldReturnNullWhenKeyDoesNotExist() { $this->assertNull($this->helper->getDjConfigOption('bogus')); } public function testGetDjConfigOptionShouldAllowSpecifyingDefaultValue() { $this->assertEquals('bar', $this->helper->getDjConfigOption('foo', 'bar')); } public function testDjConfigShouldSerializeToJson() { $this->helper->setDjConfigOption('parseOnLoad', true) ->enable(); $html = $this->helper->__toString(); $this->assertContains('var djConfig = ', $html, var_export($html, 1)); $this->assertContains('"parseOnLoad":', $html, $html); } public function testShouldAllowSpecifyingStylesheetByModuleName() { $this->helper->addStylesheetModule('dijit.themes.tundra'); $stylesheets = $this->helper->getStylesheetModules(); $this->assertContains('dijit.themes.tundra', $stylesheets); } public function testDuplicateStylesheetModulesShouldNotBeAllowed() { $this->helper->addStylesheetModule('dijit.themes.tundra'); $stylesheets = $this->helper->getStylesheetModules(); $this->assertContains('dijit.themes.tundra', $stylesheets); $this->helper->addStylesheetModule('dijit.themes.tundra'); $stylesheets = $this->helper->getStylesheetModules(); $this->assertEquals(1, count($stylesheets)); $this->assertContains('dijit.themes.tundra', $stylesheets); } /** * @group ZF-3916 */ public function testAddingStylesheetModuleShouldAllowDashAndUnderscoreCharacters() { $this->helper->addStylesheetModule('dojox._highlight.pygments'); $this->helper->addStylesheetModule('dojo.NodeList-fx.styles'); } public function testInvalidStylesheetModuleNameShouldThrowException() { try { $this->helper->addStylesheetModule('foo/bar/baz'); $this->fail('invalid module designation should throw exception'); } catch (Zend_Dojo_View_Exception $e) { $this->assertContains('Invalid', $e->getMessage()); } } public function testRenderingModuleStylesheetShouldProperlyCreatePaths() { $this->helper->enable() ->addStylesheetModule('dijit.themes.tundra'); $html = $this->helper->__toString(); $this->assertContains('dijit/themes/tundra/tundra.css', $html); } public function testShouldAllowSpecifyingLocalStylesheet() { $this->helper->addStylesheet('/css/foo.css'); $css = $this->helper->getStylesheets(); $this->assertTrue(is_array($css)); $this->assertContains('/css/foo.css', $css); } public function testShouldNotAllowSpecifyingDuplicateLocalStylesheets() { $this->testShouldAllowSpecifyingLocalStylesheet(); $this->helper->addStylesheet('/css/foo.css'); $css = $this->helper->getStylesheets(); $this->assertTrue(is_array($css)); $this->assertEquals(1, count($css)); $this->assertContains('/css/foo.css', $css); } public function testShouldAllowSpecifyingOnLoadFunctionPointer() { $this->helper->addOnLoad('foo'); $onLoad = $this->helper->getOnLoadActions(); $this->assertTrue(is_array($onLoad)); $this->assertEquals(1, count($onLoad)); $action = array_shift($onLoad); $this->assertTrue(is_string($action)); $this->assertEquals('foo', $action); } public function testShouldAllowCapturingOnLoadActions() { $this->helper->onLoadCaptureStart(); ?> function() { bar(); baz(); } helper->onLoadCaptureEnd(); $onLoad = $this->helper->getOnLoadActions(); $this->assertTrue(is_array($onLoad)); $this->assertEquals(1, count($onLoad)); $action = array_shift($onLoad); $this->assertTrue(is_string($action)); $this->assertContains('function() {', $action); $this->assertContains('bar();', $action); $this->assertContains('baz();', $action); } public function testShouldNotAllowSpecifyingDuplicateOnLoadActions() { $this->helper->addOnLoad('foo'); $this->helper->addOnLoad('foo'); $onLoad = $this->helper->getOnLoadActions(); $this->assertTrue(is_array($onLoad)); $this->assertEquals(1, count($onLoad)); $action = array_shift($onLoad); $this->assertEquals('foo', $action); } public function testDojoMethodShouldReturnContainer() { $helper = new Zend_Dojo_View_Helper_Dojo(); $this->assertSame($this->helper, $helper->dojo()); } public function testHelperStorageShouldPersistBetweenViewObjects() { $view1 = $this->getView(); $dojo1 = $view1->getHelper('dojo'); $view2 = $this->getView(); $dojo2 = $view1->getHelper('dojo'); $this->assertSame($dojo1, $dojo2); } public function testSerializingToStringShouldReturnEmptyStringByDefault() { $this->assertEquals('', $this->helper->__toString()); } public function testEnablingHelperShouldCauseStringSerializationToWork() { $this->setupDojo(); $html = $this->helper->__toString(); $doc = new DOMDocument; $doc->loadHTML($html); $xPath = new DOMXPath($doc); $results = $xPath->query('//script'); $this->assertEquals(3, $results->length); for ($i = 0; $i < 3; ++$i) { $script = $doc->saveXML($results->item($i)); switch ($i) { case 0: $this->assertContains('var djConfig = ', $script); $this->assertContains('parseOnLoad', $script); break; case 1: $this->assertRegexp('#src="http://.+/dojo/[0-9.]+/dojo/dojo.xd.js"#', $script); $this->assertContains('/>', $script); break; case 2: $this->assertContains('dojo.registerModulePath("custom", "../custom")', $script, $script); $this->assertContains('dojo.require("dijit.layout.ContentPane")', $script, $script); $this->assertContains('dojo.require("custom.foo")', $script, $script); $this->assertContains('dojo.addOnLoad(foo)', $script, $script); break; } } $results = $xPath->query('//style'); $this->assertEquals(1, $results->length, $html); $style = $doc->saveXML($results->item(0)); $this->assertContains('@import', $style); $this->assertEquals(2, substr_count($style, '@import')); $this->assertEquals(1, substr_count($style, 'http://ajax.googleapis.com/ajax/libs/dojo/'), $style); $this->assertContains('css/custom.css', $style); $this->assertContains('dijit/themes/tundra/tundra.css', $style); } public function testStringSerializationShouldBeDoctypeAware() { $view = $this->getView(); $view->doctype('HTML4_LOOSE'); $this->helper->setView($view); $this->setupDojo(); $html = $this->helper->__toString(); $this->assertRegexp('|