Commit 1fd7dc66 by Paul Klimov

Unit test for AssetController has been fixed.

parent b44d0eb6
<?php <?php
namespace yiiunit\framework\console\controllers;
use yii\helpers\StringHelper;
use yiiunit\TestCase; use yiiunit\TestCase;
use yii\console\controllers\AssetController; use yii\console\controllers\AssetController;
use Yii;
/** /**
* Unit test for [[\yii\console\controllers\AssetController]]. * Unit test for [[\yii\console\controllers\AssetController]].
...@@ -92,11 +96,12 @@ class AssetControllerTest extends TestCase ...@@ -92,11 +96,12 @@ class AssetControllerTest extends TestCase
*/ */
protected function createCompressConfig(array $bundles) protected function createCompressConfig(array $bundles)
{ {
$className = $this->declareAssetBundleClass(['class' => 'AssetBundleAll']);
$baseUrl = '/test'; $baseUrl = '/test';
$config = [ $config = [
'bundles' => $this->createBundleConfig($bundles), 'bundles' => $bundles,
'targets' => [ 'targets' => [
'all' => [ $className => [
'basePath' => $this->testAssetsBasePath, 'basePath' => $this->testAssetsBasePath,
'baseUrl' => $baseUrl, 'baseUrl' => $baseUrl,
'js' => 'all.js', 'js' => 'all.js',
...@@ -112,28 +117,10 @@ class AssetControllerTest extends TestCase ...@@ -112,28 +117,10 @@ class AssetControllerTest extends TestCase
} }
/** /**
* 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. * Creates test compress config file.
* @param string $fileName output file name. * @param string $fileName output file name.
* @param array[] $bundles asset bundles config. * @param array[] $bundles asset bundles config.
* @throws Exception on failure. * @throws \Exception on failure.
*/ */
protected function createCompressConfigFile($fileName, array $bundles) protected function createCompressConfigFile($fileName, array $bundles)
{ {
...@@ -147,7 +134,7 @@ class AssetControllerTest extends TestCase ...@@ -147,7 +134,7 @@ class AssetControllerTest extends TestCase
* Creates test asset file. * Creates test asset file.
* @param string $fileRelativeName file name relative to [[testFilePath]] * @param string $fileRelativeName file name relative to [[testFilePath]]
* @param string $content file content * @param string $content file content
* @throws Exception on failure. * @throws \Exception on failure.
*/ */
protected function createAssetSourceFile($fileRelativeName, $content) protected function createAssetSourceFile($fileRelativeName, $content)
{ {
...@@ -178,7 +165,7 @@ class AssetControllerTest extends TestCase ...@@ -178,7 +165,7 @@ class AssetControllerTest extends TestCase
protected function invokeAssetControllerMethod($methodName, array $args = []) protected function invokeAssetControllerMethod($methodName, array $args = [])
{ {
$controller = $this->createAssetController(); $controller = $this->createAssetController();
$controllerClassReflection = new ReflectionClass(get_class($controller)); $controllerClassReflection = new \ReflectionClass(get_class($controller));
$methodReflection = $controllerClassReflection->getMethod($methodName); $methodReflection = $controllerClassReflection->getMethod($methodName);
$methodReflection->setAccessible(true); $methodReflection->setAccessible(true);
$result = $methodReflection->invokeArgs($controller, $args); $result = $methodReflection->invokeArgs($controller, $args);
...@@ -186,6 +173,60 @@ class AssetControllerTest extends TestCase ...@@ -186,6 +173,60 @@ class AssetControllerTest extends TestCase
return $result; return $result;
} }
/**
* Composes asset bundle class source code.
* @param array $config asset bundle config.
* @return string class source code.
*/
protected function composeAssetBundleClassSource(array &$config)
{
$config = array_merge(
[
'namespace' => StringHelper::dirname(get_class($this)),
'class' => 'AppAsset',
'basePath' => $this->testFilePath,
'baseUrl' => '',
'css' => [],
'js' => [],
'depends' => [],
],
$config
);
foreach ($config as $name => $value) {
if (is_array($value)) {
$config[$name] = var_export($value, true);
}
}
$source = <<<EOL
namespace {$config['namespace']};
use yii\web\AssetBundle;
class {$config['class']} extends AssetBundle
{
public \$basePath = '{$config['basePath']}';
public \$baseUrl = '{$config['baseUrl']}';
public \$css = {$config['css']};
public \$js = {$config['js']};
public \$depends = {$config['depends']};
}
EOL;
return $source;
}
/**
* Declares asset bundle class according to given configuration.
* @param array $config asset bundle config.
* @return string new class full name.
*/
protected function declareAssetBundleClass(array $config)
{
$sourceCode = $this->composeAssetBundleClassSource($config);
eval($sourceCode);
return $config['namespace'] . '\\' . $config['class'];
}
// Tests : // Tests :
public function testActionTemplate() public function testActionTemplate()
...@@ -195,7 +236,7 @@ class AssetControllerTest extends TestCase ...@@ -195,7 +236,7 @@ class AssetControllerTest extends TestCase
$this->assertTrue(file_exists($configFileName), 'Unable to create config file template!'); $this->assertTrue(file_exists($configFileName), 'Unable to create config file template!');
} }
public function atestActionCompress() public function testActionCompress()
{ {
// Given : // Given :
$cssFiles = [ $cssFiles = [
...@@ -219,15 +260,13 @@ class AssetControllerTest extends TestCase ...@@ -219,15 +260,13 @@ class AssetControllerTest extends TestCase
}", }",
]; ];
$this->createAssetSourceFiles($jsFiles); $this->createAssetSourceFiles($jsFiles);
$assetBundleClassName = $this->declareAssetBundleClass([
'css' => array_keys($cssFiles),
'js' => array_keys($jsFiles),
]);
$bundles = [ $bundles = [
'app' => [ $assetBundleClassName
'css' => array_keys($cssFiles),
'js' => array_keys($jsFiles),
'depends' => [
'yii',
],
],
]; ];
$bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php'; $bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php';
......
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