Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
a922c41e
Commit
a922c41e
authored
Oct 11, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more asset bundle tests and docs
parent
abc1e0c2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
20 deletions
+82
-20
View.php
framework/yii/base/View.php
+22
-15
AssetBundleTest.php
tests/unit/framework/web/AssetBundleTest.php
+60
-5
No files found.
framework/yii/base/View.php
View file @
a922c41e
...
...
@@ -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
*/
...
...
tests/unit/framework/web/AssetBundleTest.php
View file @
a922c41e
...
...
@@ -42,10 +42,10 @@ class AssetBundleTest extends \yiiunit\TestCase
$view
=
$this
->
getView
();
$this
->
assertEmpty
(
$view
->
assetBundles
);
Test
Jquery
Asset
::
register
(
$view
);
Test
Simple
Asset
::
register
(
$view
);
$this
->
assertEquals
(
1
,
count
(
$view
->
assetBundles
));
$this
->
assertArrayHasKey
(
'yiiunit\\framework\\web\\Test
Jquery
Asset'
,
$view
->
assetBundles
);
$this
->
assertTrue
(
$view
->
assetBundles
[
'yiiunit\\framework\\web\\Test
Jquery
Asset'
]
instanceof
AssetBundle
);
$this
->
assertArrayHasKey
(
'yiiunit\\framework\\web\\Test
Simple
Asset'
,
$view
->
assetBundles
);
$this
->
assertTrue
(
$view
->
assetBundles
[
'yiiunit\\framework\\web\\Test
Simple
Asset'
]
instanceof
AssetBundle
);
$expected
=
<<<EOF
123<script src="/js/jquery.js"></script>
...
...
@@ -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
=
<<<EOF
1<link href="/files/cssFile.css" rel="stylesheet">
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment