1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?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 = '';
public function setUp()
{
$this->testFilePath = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . get_class($this);
$this->createDir($this->testFilePath);
}
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.
* @return array config array.
*/
protected function createCompressConfig()
{
$baseUrl = '/test';
$assetsBasePath = $this->testFilePath.DIRECTORY_SEPARATOR.'assets';
$this->createDir($assetsBasePath);
$config = array(
'bundles' => $this->createBundleConfig(),
'targets' => array(
'all' => array(
'basePath' => $assetsBasePath,
'baseUrl' => $baseUrl,
'js' => 'all-{ts}.js',
'css' => 'all-{ts}.css',
),
),
'assetManager' => array(
'basePath' => $assetsBasePath,
'baseUrl' => $baseUrl,
),
);
return $config;
}
/**
* Creates test bundle configuration.
* @return array bundle config.
*/
protected function createBundleConfig()
{
$baseUrl = '';
//$baseUrl = '/test';
$bundles = array(
'app' => array(
'basePath' => $this->testFilePath,
'baseUrl' => $baseUrl,
'css' => array(
'css/test.css',
),
'js' => array(
'js/test.js',
),
/*'depends' => array(
'yii',
),*/
),
);
return $bundles;
}
/**
* Creates test bundles configuration file.
* @param string $fileName output filename.
* @return boolean success.
*/
protected function createBundleFile($fileName)
{
$content = '<?php return '.var_export($this->createBundleConfig(), true).';';
return (file_put_contents($fileName, $content) > 0);
}
/**
* Creates test compress config file.
* @param string $fileName output file name.
* @return boolean success.
*/
protected function createCompressConfigFile($fileName)
{
$content = '<?php return '.var_export($this->createCompressConfig(), true).';';
return (file_put_contents($fileName, $content) > 0);
}
/**
* Creates test asset file.
* @param string $fileRelativeName file name relative to [[testFilePath]]
* @param string $content file content
* @return boolean success.
*/
protected function createTestAssetFile($fileRelativeName, $content)
{
$fileFullName = $this->testFilePath.DIRECTORY_SEPARATOR.$fileRelativeName;
$this->createDir(dirname($fileFullName));
return (file_put_contents($fileFullName, $content) > 0);
}
// 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()
{
$this->createTestAssetFile(
'css/test.css',
'body {
padding-top: 20px;
padding-bottom: 60px;
}'
);
$this->createTestAssetFile(
'js/test.js',
"function() {
alert('Test message');
}"
);
$configFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'config.php';
$this->createCompressConfigFile($configFile);
$bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php';
$this->createBundleFile($bundleFile);
$this->runAssetControllerAction('compress', array($configFile, $bundleFile));
$this->markTestIncomplete();
}
}