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
14228f86
Commit
14228f86
authored
May 17, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'asset-command'
parents
9e948ccd
1c786edb
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
237 additions
and
0 deletions
+237
-0
AssetControllerTest.php
...nit/framework/console/controllers/AssetControllerTest.php
+237
-0
AssetController.php
yii/console/controllers/AssetController.php
+0
-0
No files found.
tests/unit/framework/console/controllers/AssetControllerTest.php
0 → 100644
View file @
14228f86
<?php
use
yiiunit\TestCase
;
use
yii\console\controllers\AssetController
;
/**
* Unit test for [[\yii\console\controllers\AssetController]].
* @see AssetController
*/
class
AssetControllerTest
extends
TestCase
{
/**
* @var string path for the test files.
*/
protected
$testFilePath
=
''
;
/**
* @var string test assets path.
*/
protected
$testAssetsBasePath
=
''
;
public
function
setUp
()
{
$this
->
testFilePath
=
Yii
::
getAlias
(
'@yiiunit/runtime'
)
.
DIRECTORY_SEPARATOR
.
get_class
(
$this
);
$this
->
createDir
(
$this
->
testFilePath
);
$this
->
testAssetsBasePath
=
$this
->
testFilePath
.
DIRECTORY_SEPARATOR
.
'assets'
;
$this
->
createDir
(
$this
->
testAssetsBasePath
);
}
public
function
tearDown
()
{
$this
->
removeDir
(
$this
->
testFilePath
);
}
/**
* Creates directory.
* @param $dirName directory full name.
*/
protected
function
createDir
(
$dirName
)
{
if
(
!
file_exists
(
$dirName
))
{
mkdir
(
$dirName
,
0777
,
true
);
}
}
/**
* Removes directory.
* @param $dirName directory full name
*/
protected
function
removeDir
(
$dirName
)
{
if
(
!
empty
(
$dirName
)
&&
file_exists
(
$dirName
))
{
exec
(
"rm -rf
{
$dirName
}
"
);
}
}
/**
* Creates test asset controller instance.
* @return AssetController
*/
protected
function
createAssetController
()
{
$module
=
$this
->
getMock
(
'yii\\base\\Module'
,
array
(
'fake'
),
array
(
'console'
));
$assetController
=
new
AssetController
(
'asset'
,
$module
);
$assetController
->
interactive
=
false
;
$assetController
->
jsCompressor
=
'cp {from} {to}'
;
$assetController
->
cssCompressor
=
'cp {from} {to}'
;
return
$assetController
;
}
/**
* Emulates running of the asset controller action.
* @param string $actionId id of action to be run.
* @param array $args action arguments.
* @return string command output.
*/
protected
function
runAssetControllerAction
(
$actionId
,
array
$args
=
array
())
{
$controller
=
$this
->
createAssetController
();
ob_start
();
ob_implicit_flush
(
false
);
$params
=
array
(
\yii\console\Request
::
ANONYMOUS_PARAMS
=>
$args
);
$controller
->
run
(
$actionId
,
$params
);
return
ob_get_clean
();
}
/**
* Creates test compress config.
* @param array[] $bundles asset bundles config.
* @return array config array.
*/
protected
function
createCompressConfig
(
array
$bundles
)
{
$baseUrl
=
'/test'
;
$config
=
array
(
'bundles'
=>
$this
->
createBundleConfig
(
$bundles
),
'targets'
=>
array
(
'all'
=>
array
(
'basePath'
=>
$this
->
testAssetsBasePath
,
'baseUrl'
=>
$baseUrl
,
'js'
=>
'all.js'
,
'css'
=>
'all.css'
,
),
),
'assetManager'
=>
array
(
'basePath'
=>
$this
->
testAssetsBasePath
,
'baseUrl'
=>
$baseUrl
,
),
);
return
$config
;
}
/**
* Creates test bundle configuration.
* @param array[] $bundles asset bundles config.
* @return array bundle config.
*/
protected
function
createBundleConfig
(
array
$bundles
)
{
foreach
(
$bundles
as
$name
=>
$config
)
{
if
(
!
array_key_exists
(
'basePath'
,
$config
))
{
$bundles
[
$name
][
'basePath'
]
=
$this
->
testFilePath
;
}
if
(
!
array_key_exists
(
'baseUrl'
,
$config
))
{
$bundles
[
$name
][
'baseUrl'
]
=
''
;
}
}
return
$bundles
;
}
/**
* Creates test compress config file.
* @param string $fileName output file name.
* @param array[] $bundles asset bundles config.
* @throws Exception on failure.
*/
protected
function
createCompressConfigFile
(
$fileName
,
array
$bundles
)
{
$content
=
'<?php return '
.
var_export
(
$this
->
createCompressConfig
(
$bundles
),
true
)
.
';'
;
if
(
file_put_contents
(
$fileName
,
$content
)
<=
0
)
{
throw
new
\Exception
(
"Unable to create file '
{
$fileName
}
'!"
);
}
}
/**
* Creates test asset file.
* @param string $fileRelativeName file name relative to [[testFilePath]]
* @param string $content file content
* @throws Exception on failure.
*/
protected
function
createAssetSourceFile
(
$fileRelativeName
,
$content
)
{
$fileFullName
=
$this
->
testFilePath
.
DIRECTORY_SEPARATOR
.
$fileRelativeName
;
$this
->
createDir
(
dirname
(
$fileFullName
));
if
(
file_put_contents
(
$fileFullName
,
$content
)
<=
0
)
{
throw
new
\Exception
(
"Unable to create file '
{
$fileFullName
}
'!"
);
}
}
/**
* Creates a list of asset source files.
* @param array $files assert source files in format: file/relative/name => fileContent
*/
protected
function
createAssertSourceFiles
(
array
$files
)
{
foreach
(
$files
as
$name
=>
$content
)
{
$this
->
createAssetSourceFile
(
$name
,
$content
);
}
}
// Tests :
public
function
testActionTemplate
()
{
$configFileName
=
$this
->
testFilePath
.
DIRECTORY_SEPARATOR
.
'config.php'
;
$this
->
runAssetControllerAction
(
'template'
,
array
(
$configFileName
));
$this
->
assertTrue
(
file_exists
(
$configFileName
),
'Unable to create config file template!'
);
}
public
function
testActionCompress
()
{
// Given :
$cssFiles
=
array
(
'css/test_body.css'
=>
'body {
padding-top: 20px;
padding-bottom: 60px;
}'
,
'css/test_footer.css'
=>
'.footer {
margin: 20px;
display: block;
}'
,
);
$this
->
createAssertSourceFiles
(
$cssFiles
);
$jsFiles
=
array
(
'js/test_alert.js'
=>
"function test() {
alert('Test message');
}"
,
'js/test_sum_ab.js'
=>
"function sumAB(a, b) {
return a + b;
}"
,
);
$this
->
createAssertSourceFiles
(
$jsFiles
);
$bundles
=
array
(
'app'
=>
array
(
'css'
=>
array_keys
(
$cssFiles
),
'js'
=>
array_keys
(
$jsFiles
),
),
);;
$bundleFile
=
$this
->
testFilePath
.
DIRECTORY_SEPARATOR
.
'bundle.php'
;
$configFile
=
$this
->
testFilePath
.
DIRECTORY_SEPARATOR
.
'config.php'
;
$this
->
createCompressConfigFile
(
$configFile
,
$bundles
);
// When :
$this
->
runAssetControllerAction
(
'compress'
,
array
(
$configFile
,
$bundleFile
));
// Then :
$this
->
assertTrue
(
file_exists
(
$bundleFile
),
'Unable to create output bundle file!'
);
$compressedCssFileName
=
$this
->
testAssetsBasePath
.
DIRECTORY_SEPARATOR
.
'all.css'
;
$this
->
assertTrue
(
file_exists
(
$compressedCssFileName
),
'Unable to compress CSS files!'
);
$compressedJsFileName
=
$this
->
testAssetsBasePath
.
DIRECTORY_SEPARATOR
.
'all.js'
;
$this
->
assertTrue
(
file_exists
(
$compressedJsFileName
),
'Unable to compress JS files!'
);
$compressedCssFileContent
=
file_get_contents
(
$compressedCssFileName
);
foreach
(
$cssFiles
as
$name
=>
$content
)
{
$this
->
assertContains
(
$content
,
$compressedCssFileContent
,
"Source of '
{
$name
}
' is missing in combined file!"
);
}
$compressedJsFileContent
=
file_get_contents
(
$compressedJsFileName
);
foreach
(
$jsFiles
as
$name
=>
$content
)
{
$this
->
assertContains
(
$content
,
$compressedJsFileContent
,
"Source of '
{
$name
}
' is missing in combined file!"
);
}
}
}
yii/console/controllers/AssetController.php
View file @
14228f86
This diff is collapsed.
Click to expand it.
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