1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Assets\Tests; |
5
|
|
|
|
6
|
|
|
use Yiisoft\Assets\AssetBundle; |
7
|
|
|
use Yiisoft\Assets\Exception\InvalidConfigException; |
8
|
|
|
use Yiisoft\Assets\Tests\stubs\BaseAsset; |
9
|
|
|
use Yiisoft\Assets\Tests\stubs\BaseWrongAsset; |
10
|
|
|
use Yiisoft\Assets\Tests\stubs\CircleAsset; |
11
|
|
|
use Yiisoft\Assets\Tests\stubs\CircleDependsAsset; |
12
|
|
|
use Yiisoft\Assets\Tests\stubs\FileOptionsAsset; |
13
|
|
|
use Yiisoft\Assets\Tests\stubs\JqueryAsset; |
14
|
|
|
use Yiisoft\Assets\Tests\stubs\SimpleAsset; |
15
|
|
|
use Yiisoft\Assets\Tests\stubs\SourceAsset; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* AssetBundleTest. |
19
|
|
|
*/ |
20
|
|
|
final class AssetBundleTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
protected function tearDown(): void |
23
|
|
|
{ |
24
|
|
|
parent::tearDown(); |
25
|
|
|
|
26
|
|
|
$this->removeAssets('@basePath'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testBaseAppendtimestamp(): void |
30
|
|
|
{ |
31
|
|
|
$bundle = new BaseAsset(); |
32
|
|
|
|
33
|
|
|
$timestampCss = @filemtime($this->aliases->get($bundle->basePath) . '/' . $bundle->css[0]); |
34
|
|
|
$urlCss = "/baseUrl/css/basePath.css?v=$timestampCss"; |
35
|
|
|
|
36
|
|
|
$timestampJs = @filemtime($this->aliases->get($bundle->basePath) . '/' . $bundle->js[0]); |
37
|
|
|
$urlJs = "/baseUrl/js/basePath.js?v=$timestampJs"; |
38
|
|
|
|
39
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
40
|
|
|
|
41
|
|
|
$this->assetManager->setAppendTimestamp(true); |
42
|
|
|
$this->assetManager->register([BaseAsset::class]); |
43
|
|
|
|
44
|
|
|
$this->assertStringContainsString( |
45
|
|
|
$urlCss, |
46
|
|
|
$this->assetManager->getCssFiles()[$urlCss]['url'] |
47
|
|
|
); |
48
|
|
|
$this->assertEquals( |
49
|
|
|
[ |
50
|
|
|
'integrity' => 'integrity-hash', |
51
|
|
|
'crossorigin' => 'anonymous' |
52
|
|
|
], |
53
|
|
|
$this->assetManager->getCssFiles()[$urlCss]['attributes'] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->assertStringContainsString( |
57
|
|
|
$urlJs, |
58
|
|
|
$this->assetManager->getJsFiles()[$urlJs]['url'] |
59
|
|
|
); |
60
|
|
|
$this->assertEquals( |
61
|
|
|
[ |
62
|
|
|
'integrity' => 'integrity-hash', |
63
|
|
|
'crossorigin' => 'anonymous', |
64
|
|
|
'position' => 3 |
65
|
|
|
], |
66
|
|
|
$this->assetManager->getJsFiles()[$urlJs]['attributes'] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testBasePath(): void |
71
|
|
|
{ |
72
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
73
|
|
|
|
74
|
|
|
$this->assetManager->register([BaseAsset::class]); |
75
|
|
|
|
76
|
|
|
$this->assertStringContainsString( |
77
|
|
|
'/baseUrl/css/basePath.css', |
78
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/basePath.css']['url'] |
79
|
|
|
); |
80
|
|
|
$this->assertEquals( |
81
|
|
|
[ |
82
|
|
|
'integrity' => 'integrity-hash', |
83
|
|
|
'crossorigin' => 'anonymous' |
84
|
|
|
], |
85
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/basePath.css']['attributes'] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->assertStringContainsString( |
89
|
|
|
'/baseUrl/js/basePath.js', |
90
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/js/basePath.js']['url'] |
91
|
|
|
); |
92
|
|
|
$this->assertEquals( |
93
|
|
|
[ |
94
|
|
|
'integrity' => 'integrity-hash', |
95
|
|
|
'crossorigin' => 'anonymous', |
96
|
|
|
'position' => 3 |
97
|
|
|
], |
98
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/js/basePath.js']['attributes'] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testBasePathEmptyException(): void |
103
|
|
|
{ |
104
|
|
|
$this->assetManager->setBundles( |
105
|
|
|
[ |
106
|
|
|
BaseAsset::class => [ |
107
|
|
|
'basePath' => null, |
108
|
|
|
] |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
113
|
|
|
|
114
|
|
|
$message = 'basePath must be set in AssetManager->setBasePath($path) or ' . |
115
|
|
|
'AssetBundle property public ?string $basePath = $path'; |
116
|
|
|
|
117
|
|
|
$this->expectException(InvalidConfigException::class); |
118
|
|
|
$this->expectExceptionMessage($message); |
119
|
|
|
|
120
|
|
|
$this->assetManager->register([BaseAsset::class]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testBaseUrlEmptyException(): void |
124
|
|
|
{ |
125
|
|
|
$this->assetManager->setBundles( |
126
|
|
|
[ |
127
|
|
|
BaseAsset::class => [ |
128
|
|
|
'basePath' => null, |
129
|
|
|
'baseUrl' => null, |
130
|
|
|
] |
131
|
|
|
] |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$this->assetManager->setBasePath('@basePath'); |
135
|
|
|
|
136
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
137
|
|
|
|
138
|
|
|
$message = 'baseUrl must be set in AssetManager->setBaseUrl($path) or ' . |
139
|
|
|
'AssetBundle property public ?string $baseUrl = $path'; |
140
|
|
|
|
141
|
|
|
$this->expectException(InvalidConfigException::class); |
142
|
|
|
$this->expectExceptionMessage($message); |
143
|
|
|
|
144
|
|
|
$this->assetManager->register([BaseAsset::class]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testBasePathEmptyWithAssetManagerSetBasePath(): void |
148
|
|
|
{ |
149
|
|
|
$config = [ |
|
|
|
|
150
|
|
|
'basePath' => null, |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
$this->assetManager->setBasePath('@basePath'); |
154
|
|
|
|
155
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
156
|
|
|
$this->assertIsObject($this->assetManager->getBundle(BaseAsset::class)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testBasePathEmptyBaseUrlEmptyWithAssetManagerSetBasePathSetBaseUrl(): void |
160
|
|
|
{ |
161
|
|
|
$config = [ |
|
|
|
|
162
|
|
|
'basePath' => null, |
163
|
|
|
'baseUrl' => null, |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
$this->assetManager->setBasePath('@basePath'); |
167
|
|
|
$this->assetManager->setBaseUrl('@baseUrl'); |
168
|
|
|
|
169
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
170
|
|
|
|
171
|
|
|
$this->assertIsObject($this->assetManager->getBundle(BaseAsset::class)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testBasePathWrongException(): void |
175
|
|
|
{ |
176
|
|
|
$bundle = new BaseWrongAsset(); |
177
|
|
|
|
178
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
179
|
|
|
|
180
|
|
|
$file = $bundle->js[0]; |
181
|
|
|
$message = "Asset files not found: '$bundle->basePath/$file.'"; |
182
|
|
|
|
183
|
|
|
$this->expectException(InvalidConfigException::class); |
184
|
|
|
$this->expectExceptionMessage($message); |
185
|
|
|
|
186
|
|
|
$this->assetManager->register([BaseWrongAsset::class]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testCircularDependency(): void |
190
|
|
|
{ |
191
|
|
|
$depends = (new CircleDependsAsset())->depends; |
192
|
|
|
|
193
|
|
|
$message = "A circular dependency is detected for bundle '$depends[0]'."; |
194
|
|
|
|
195
|
|
|
$this->expectException(\RuntimeException::class); |
196
|
|
|
$this->expectExceptionMessage($message); |
197
|
|
|
|
198
|
|
|
$this->assetManager->register([CircleAsset::class]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testDuplicateAssetFile(): void |
202
|
|
|
{ |
203
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
204
|
|
|
|
205
|
|
|
$this->assetManager->register([JqueryAsset::class, SimpleAsset::class]); |
206
|
|
|
|
207
|
|
|
$this->assertCount(3, $this->assetManager->getAssetBundles()); |
208
|
|
|
$this->assertArrayHasKey(SimpleAsset::class, $this->assetManager->getAssetBundles()); |
209
|
|
|
$this->assertInstanceOf( |
210
|
|
|
AssetBundle::class, |
211
|
|
|
$this->assetManager->getAssetBundles()[SimpleAsset::class] |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$this->assertStringContainsString( |
215
|
|
|
'/js/jquery.js', |
216
|
|
|
$this->assetManager->getJsFiles()['/js/jquery.js']['url'] |
217
|
|
|
); |
218
|
|
|
$this->assertEquals( |
219
|
|
|
[ |
220
|
|
|
'position' => 3 |
221
|
|
|
], |
222
|
|
|
$this->assetManager->getJsFiles()['/js/jquery.js']['attributes'] |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function testFileOptionsAsset(): void |
227
|
|
|
{ |
228
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
229
|
|
|
|
230
|
|
|
$this->assetManager->register([FileOptionsAsset::class]); |
231
|
|
|
|
232
|
|
|
$this->assertStringContainsString( |
233
|
|
|
'/baseUrl/css/default_options.css', |
234
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/default_options.css']['url'] |
235
|
|
|
); |
236
|
|
|
$this->assertEquals( |
237
|
|
|
[ |
238
|
|
|
'media' => 'screen', |
239
|
|
|
'hreflang' => 'en' |
240
|
|
|
], |
241
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/default_options.css']['attributes'] |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
$this->assertStringContainsString( |
245
|
|
|
'/baseUrl/css/tv.css', |
246
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/tv.css']['url'] |
247
|
|
|
); |
248
|
|
|
$this->assertEquals( |
249
|
|
|
[ |
250
|
|
|
'media' => 'tv', |
251
|
|
|
'hreflang' => 'en' |
252
|
|
|
], |
253
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/tv.css']['attributes'] |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$this->assertStringContainsString( |
257
|
|
|
'/baseUrl/css/screen_and_print.css', |
258
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/screen_and_print.css']['url'] |
259
|
|
|
); |
260
|
|
|
$this->assertEquals( |
261
|
|
|
[ |
262
|
|
|
'media' => 'screen, print', |
263
|
|
|
'hreflang' => 'en' |
264
|
|
|
], |
265
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/css/screen_and_print.css']['attributes'] |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
$this->assertStringContainsString( |
269
|
|
|
'/baseUrl/js/normal.js', |
270
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/js/normal.js']['url'] |
271
|
|
|
); |
272
|
|
|
$this->assertEquals( |
273
|
|
|
[ |
274
|
|
|
'charset' => 'utf-8', |
275
|
|
|
'position' => 3 |
276
|
|
|
], |
277
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/js/normal.js']['attributes'] |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
$this->assertStringContainsString( |
281
|
|
|
'/baseUrl/js/defered.js', |
282
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/js/defered.js']['url'] |
283
|
|
|
); |
284
|
|
|
$this->assertEquals( |
285
|
|
|
[ |
286
|
|
|
'charset' => 'utf-8', |
287
|
|
|
'defer' => true, |
288
|
|
|
'position' => 3 |
289
|
|
|
], |
290
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/js/defered.js']['attributes'] |
291
|
|
|
); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
public function testSourceSetHashCallback(): void |
296
|
|
|
{ |
297
|
|
|
$this->assetManager->setHashCallback(function () { |
298
|
|
|
return 'HashCallback'; |
299
|
|
|
}); |
300
|
|
|
|
301
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
302
|
|
|
|
303
|
|
|
$this->assetManager->register([SourceAsset::class]); |
304
|
|
|
|
305
|
|
|
$this->assertStringContainsString( |
306
|
|
|
'/baseUrl/HashCallback/css/stub.css', |
307
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/HashCallback/css/stub.css']['url'] |
308
|
|
|
); |
309
|
|
|
$this->assertEquals( |
310
|
|
|
[], |
311
|
|
|
$this->assetManager->getCssFiles()['/baseUrl/HashCallback/css/stub.css']['attributes'] |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
$this->assertStringContainsString( |
315
|
|
|
'/js/jquery.js', |
316
|
|
|
$this->assetManager->getJsFiles()['/js/jquery.js']['url'] |
317
|
|
|
); |
318
|
|
|
$this->assertEquals( |
319
|
|
|
[ |
320
|
|
|
'position' => 3 |
321
|
|
|
], |
322
|
|
|
$this->assetManager->getJsFiles()['/js/jquery.js']['attributes'] |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
$this->assertStringContainsString( |
326
|
|
|
'/baseUrl/HashCallback/js/stub.js', |
327
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/HashCallback/js/stub.js']['url'] |
328
|
|
|
); |
329
|
|
|
$this->assertEquals( |
330
|
|
|
[ |
331
|
|
|
'position' => 3 |
332
|
|
|
], |
333
|
|
|
$this->assetManager->getJsFiles()['/baseUrl/HashCallback/js/stub.js']['attributes'] |
334
|
|
|
); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testSourcesPublishOptionsOnlyRegex(): void |
338
|
|
|
{ |
339
|
|
|
$bundle = new SourceAsset(); |
340
|
|
|
|
341
|
|
|
$bundle->publishOptions = [ |
342
|
|
|
'only' => [ |
343
|
|
|
'js/*' |
344
|
|
|
], |
345
|
|
|
]; |
346
|
|
|
|
347
|
|
|
[$bundle->basePath, $bundle->baseUrl] = $this->assetManager->getPublish()->publish($bundle); |
348
|
|
|
|
349
|
|
|
$notNeededFilesDir = dirname($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->css[0]); |
350
|
|
|
|
351
|
|
|
$this->assertFileNotExists($notNeededFilesDir); |
352
|
|
|
|
353
|
|
|
foreach ($bundle->js as $filename) { |
354
|
|
|
$publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename; |
355
|
|
|
|
356
|
|
|
$this->assertFileExists($publishedFile); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$this->assertDirectoryExists(dirname($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->js[0])); |
360
|
|
|
$this->assertDirectoryExists($bundle->basePath); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function testSourcesPathException(): void |
364
|
|
|
{ |
365
|
|
|
$bundle = new SourceAsset(); |
366
|
|
|
|
367
|
|
|
$bundle->sourcePath = '/wrong'; |
368
|
|
|
|
369
|
|
|
$message = "The sourcePath to be published does not exist: $bundle->sourcePath"; |
370
|
|
|
|
371
|
|
|
$this->expectException(InvalidConfigException::class); |
372
|
|
|
$this->expectExceptionMessage($message); |
373
|
|
|
|
374
|
|
|
$this->assetManager->getPublish()->publish($bundle); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|