Commit a922c41e by Carsten Brandt

more asset bundle tests and docs

parent abc1e0c2
...@@ -543,6 +543,24 @@ class View extends Component ...@@ -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. * Marks the beginning of an HTML body section.
*/ */
public function beginBody() public function beginBody()
...@@ -568,25 +586,14 @@ class View extends Component ...@@ -568,25 +586,14 @@ class View extends Component
echo self::PH_HEAD; 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. * Registers the named asset bundle.
* All dependent asset bundles will be registered. * All dependent asset bundles will be registered.
* @param string $name the name of the asset bundle. * @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 * @param integer|null $position if set, this forces a minimum position for javascript files.
* Null means to register on default position. * 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 * @return AssetBundle the registered asset bundle instance
* @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected
*/ */
......
...@@ -42,10 +42,10 @@ class AssetBundleTest extends \yiiunit\TestCase ...@@ -42,10 +42,10 @@ class AssetBundleTest extends \yiiunit\TestCase
$view = $this->getView(); $view = $this->getView();
$this->assertEmpty($view->assetBundles); $this->assertEmpty($view->assetBundles);
TestJqueryAsset::register($view); TestSimpleAsset::register($view);
$this->assertEquals(1, count($view->assetBundles)); $this->assertEquals(1, count($view->assetBundles));
$this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles);
$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle);
$expected = <<<EOF $expected = <<<EOF
123<script src="/js/jquery.js"></script> 123<script src="/js/jquery.js"></script>
...@@ -60,11 +60,13 @@ EOF; ...@@ -60,11 +60,13 @@ EOF;
$this->assertEmpty($view->assetBundles); $this->assertEmpty($view->assetBundles);
TestAssetBundle::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\\TestAssetBundle', $view->assetBundles);
$this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $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\\TestAssetBundle'] instanceof AssetBundle);
$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle);
$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle);
$expected = <<<EOF $expected = <<<EOF
1<link href="/files/cssFile.css" rel="stylesheet"> 1<link href="/files/cssFile.css" rel="stylesheet">
...@@ -105,17 +107,21 @@ EOF; ...@@ -105,17 +107,21 @@ EOF;
TestJqueryAsset::register($view); TestJqueryAsset::register($view);
} }
TestAssetBundle::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\\TestAssetBundle', $view->assetBundles);
$this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $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\\TestAssetBundle'] instanceof AssetBundle);
$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] 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->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions);
$this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions['position']); $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions['position']);
$this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions); $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions);
$this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions['position']); $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) switch($pos)
{ {
...@@ -183,6 +189,21 @@ EOF; ...@@ -183,6 +189,21 @@ EOF;
$this->setExpectedException('yii\\base\\InvalidConfigException'); $this->setExpectedException('yii\\base\\InvalidConfigException');
TestAssetBundle::register($view); 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 class TestAssetBundle extends AssetBundle
...@@ -207,4 +228,37 @@ class TestJqueryAsset extends AssetBundle ...@@ -207,4 +228,37 @@ class TestJqueryAsset extends AssetBundle
public $js = array( public $js = array(
'jquery.js', '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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment