From a922c41ee63ac4e182042a201c442257f6e35788 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Fri, 11 Oct 2013 02:14:59 +0200 Subject: [PATCH] more asset bundle tests and docs --- framework/yii/base/View.php | 37 +++++++++------- tests/unit/framework/web/AssetBundleTest.php | 64 +++++++++++++++++++++++++--- 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/framework/yii/base/View.php b/framework/yii/base/View.php index 1b656d2..27ede61 100644 --- a/framework/yii/base/View.php +++ b/framework/yii/base/View.php @@ -543,6 +543,24 @@ class View extends Component } /** + * Registers all files provided by an asset bundle including depending bundles files. + * Removes a bundle from [[assetBundles]] once registered. + * @param string $name name of the bundle to register + */ + private function registerAssetFiles($name) + { + if (!isset($this->assetBundles[$name])) { + return; + } + $bundle = $this->assetBundles[$name]; + foreach($bundle->depends as $dep) { + $this->registerAssetFiles($dep); + } + $bundle->registerAssets($this); + unset($this->assetBundles[$name]); + } + + /** * Marks the beginning of an HTML body section. */ public function beginBody() @@ -568,25 +586,14 @@ class View extends Component echo self::PH_HEAD; } - protected function registerAssetFiles($name) - { - if (!isset($this->assetBundles[$name])) { - return; - } - $bundle = $this->assetBundles[$name]; - foreach($bundle->depends as $depName) { - $this->registerAssetFiles($depName); - } - $bundle->registerAssets($this); - unset($this->assetBundles[$name]); - } - /** * Registers the named asset bundle. * All dependent asset bundles will be registered. * @param string $name the name of the asset bundle. - * @param integer|null $position optional parameter to force a minimum Javascript position TODO link to relevant method - * Null means to register on default position. + * @param integer|null $position if set, this forces a minimum position for javascript files. + * This will adjust depending assets javascript file position or fail if requirement can not be met. + * If this is null, asset bundles position settings will not be changed. + * See [[registerJsFile]] for more details on javascript position. * @return AssetBundle the registered asset bundle instance * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected */ diff --git a/tests/unit/framework/web/AssetBundleTest.php b/tests/unit/framework/web/AssetBundleTest.php index 02cb5ed..ce33ce7 100644 --- a/tests/unit/framework/web/AssetBundleTest.php +++ b/tests/unit/framework/web/AssetBundleTest.php @@ -42,10 +42,10 @@ class AssetBundleTest extends \yiiunit\TestCase $view = $this->getView(); $this->assertEmpty($view->assetBundles); - TestJqueryAsset::register($view); + TestSimpleAsset::register($view); $this->assertEquals(1, count($view->assetBundles)); - $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); + $this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles); + $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle); $expected = << @@ -60,11 +60,13 @@ EOF; $this->assertEmpty($view->assetBundles); TestAssetBundle::register($view); - $this->assertEquals(2, count($view->assetBundles)); + $this->assertEquals(3, count($view->assetBundles)); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles); + $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles); $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle); $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); + $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle); $expected = << @@ -105,17 +107,21 @@ EOF; TestJqueryAsset::register($view); } TestAssetBundle::register($view); - $this->assertEquals(2, count($view->assetBundles)); + $this->assertEquals(3, count($view->assetBundles)); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles); + $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles); $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle); $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); + $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle); $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions); $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions['position']); $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions); $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions['position']); + $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']->jsOptions); + $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']->jsOptions['position']); switch($pos) { @@ -183,6 +189,21 @@ EOF; $this->setExpectedException('yii\\base\\InvalidConfigException'); TestAssetBundle::register($view); } + + public function testCircularDependency() + { + $this->setExpectedException('yii\\base\\InvalidConfigException'); + TestAssetCircleA::register($this->getView()); + } +} + +class TestSimpleAsset extends AssetBundle +{ + public $basePath = '@testWebRoot/js'; + public $baseUrl = '@testWeb/js'; + public $js = array( + 'jquery.js', + ); } class TestAssetBundle extends AssetBundle @@ -207,4 +228,37 @@ class TestJqueryAsset extends AssetBundle public $js = array( 'jquery.js', ); + public $depends = array( + 'yiiunit\\framework\\web\\TestAssetLevel3' + ); +} + +class TestAssetLevel3 extends AssetBundle +{ + public $basePath = '@testWebRoot/js'; + public $baseUrl = '@testWeb/js'; +} + +class TestAssetCircleA extends AssetBundle +{ + public $basePath = '@testWebRoot/js'; + public $baseUrl = '@testWeb/js'; + public $js = array( + 'jquery.js', + ); + public $depends = array( + 'yiiunit\\framework\\web\\TestAssetCircleB' + ); +} + +class TestAssetCircleB extends AssetBundle +{ + public $basePath = '@testWebRoot/js'; + public $baseUrl = '@testWeb/js'; + public $js = array( + 'jquery.js', + ); + public $depends = array( + 'yiiunit\\framework\\web\\TestAssetCircleA' + ); } \ No newline at end of file