Passed
Push — master ( f935c2...18bcb3 )
by Alexander
01:28
created

AssetBundleTest::testBaseUrlEmptyString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
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\RootAsset;
15
use Yiisoft\Assets\Tests\stubs\SimpleAsset;
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 testBasePath(): void
30
    {
31
        $this->assertEmpty($this->assetManager->getAssetBundles());
32
33
        $this->assetManager->register([BaseAsset::class]);
34
35
        $this->assertStringContainsString(
36
            '/baseUrl/css/basePath.css',
37
            $this->assetManager->getCssFiles()['/baseUrl/css/basePath.css']['url']
38
        );
39
        $this->assertEquals(
40
            [
41
                'integrity' => 'integrity-hash',
42
                'crossorigin' => 'anonymous'
43
            ],
44
            $this->assetManager->getCssFiles()['/baseUrl/css/basePath.css']['attributes']
45
        );
46
47
        $this->assertStringContainsString(
48
            '/baseUrl/js/basePath.js',
49
            $this->assetManager->getJsFiles()['/baseUrl/js/basePath.js']['url']
50
        );
51
        $this->assertEquals(
52
            [
53
                'integrity' => 'integrity-hash',
54
                'crossorigin' => 'anonymous',
55
                'position' => 3
56
            ],
57
            $this->assetManager->getJsFiles()['/baseUrl/js/basePath.js']['attributes']
58
        );
59
    }
60
61
    public function testBasePathEmptyException(): void
62
    {
63
        $this->assetManager->setBundles(
64
            [
65
                BaseAsset::class => [
66
                    'basePath' => null,
67
                ]
68
            ]
69
        );
70
71
        $this->assertEmpty($this->assetManager->getAssetBundles());
72
73
        $message = 'basePath must be set in AssetPublisher->setBasePath($path) or ' .
74
            'AssetBundle property public ?string $basePath = $path';
75
76
        $this->expectException(InvalidConfigException::class);
77
        $this->expectExceptionMessage($message);
78
79
        $this->assetManager->register([BaseAsset::class]);
80
    }
81
82
    public function testBaseUrlEmptyString(): void
83
    {
84
        $this->assetManager->setBundles(
85
            [
86
                RootAsset::class => [
87
                    'baseUrl' => ''
88
                ],
89
            ]
90
        );
91
92
        $this->assertEmpty($this->assetManager->getAssetBundles());
93
94
        $this->assetManager->register([RootAsset::class]);
95
    }
96
97
    public function testBaseUrlEmptyStringChain(): void
98
    {
99
        $this->assetManager->setBundles(
100
            [
101
                RootAsset::class => [
102
                    'depends' => [BaseAsset::class]
103
                ],
104
                BaseAsset::class => [
105
                    'baseUrl' => null,
106
                ],
107
            ]
108
        );
109
110
        $this->assertEmpty($this->assetManager->getAssetBundles());
111
112
        $this->assetManager->register([RootAsset::class]);
113
    }
114
115
    public function testBaseUrlIsNotSetException(): void
116
    {
117
        $this->assetManager->setBundles(
118
            [
119
                BaseAsset::class => [
120
                    'basePath' => null,
121
                    'baseUrl' => null,
122
                ]
123
            ]
124
        );
125
126
        $this->assetManager->getPublisher()->setBasePath('@basePath');
127
128
        $this->assertEmpty($this->assetManager->getAssetBundles());
129
130
        $message = 'baseUrl must be set in AssetPublisher->setBaseUrl($path) or ' .
131
            'AssetBundle property public ?string $baseUrl = $path';
132
133
        $this->expectException(InvalidConfigException::class);
134
        $this->expectExceptionMessage($message);
135
136
        $this->assetManager->register([BaseAsset::class]);
137
    }
138
139
    public function testBasePathEmptyWithAssetManagerSetBasePath(): void
140
    {
141
        $this->assetManager->getPublisher()->setBasePath('@basePath');
142
143
        $this->assertEmpty($this->assetManager->getAssetBundles());
144
        $this->assertIsObject($this->assetManager->getBundle(BaseAsset::class));
145
    }
146
147
    public function testBasePathEmptyBaseUrlEmptyWithAssetManagerSetBasePathSetBaseUrl(): void
148
    {
149
        $this->assetManager->getPublisher()->setBasePath('@basePath');
150
        $this->assetManager->getPublisher()->setBaseUrl('@baseUrl');
151
152
        $this->assertEmpty($this->assetManager->getAssetBundles());
153
154
        $this->assertIsObject($this->assetManager->getBundle(BaseAsset::class));
155
    }
156
157
    public function testBasePathWrongException(): void
158
    {
159
        $bundle = new BaseWrongAsset();
160
161
        $this->assertEmpty($this->assetManager->getAssetBundles());
162
163
        $file = $bundle->js[0];
164
        $message = "Asset files not found: '$bundle->basePath/$file.'";
165
166
        $this->expectException(InvalidConfigException::class);
167
        $this->expectExceptionMessage($message);
168
169
        $this->assetManager->register([BaseWrongAsset::class]);
170
    }
171
172
    public function testCircularDependency(): void
173
    {
174
        $depends = (new CircleDependsAsset())->depends;
175
176
        $message = "A circular dependency is detected for bundle '$depends[0]'.";
177
178
        $this->expectException(\RuntimeException::class);
179
        $this->expectExceptionMessage($message);
180
181
        $this->assetManager->register([CircleAsset::class]);
182
    }
183
184
    public function testDuplicateAssetFile(): void
185
    {
186
        $this->assertEmpty($this->assetManager->getAssetBundles());
187
188
        $this->assetManager->register([JqueryAsset::class, SimpleAsset::class]);
189
190
        $this->assertCount(3, $this->assetManager->getAssetBundles());
191
        $this->assertArrayHasKey(SimpleAsset::class, $this->assetManager->getAssetBundles());
192
        $this->assertInstanceOf(
193
            AssetBundle::class,
194
            $this->assetManager->getAssetBundles()[SimpleAsset::class]
195
        );
196
197
        $this->assertStringContainsString(
198
            '/js/jquery.js',
199
            $this->assetManager->getJsFiles()['/js/jquery.js']['url']
200
        );
201
        $this->assertEquals(
202
            [
203
                'position' => 3
204
            ],
205
            $this->assetManager->getJsFiles()['/js/jquery.js']['attributes']
206
        );
207
    }
208
209
    public function testFileOptionsAsset(): void
210
    {
211
        $this->assertEmpty($this->assetManager->getAssetBundles());
212
213
        $this->assetManager->register([FileOptionsAsset::class]);
214
215
        $this->assertStringContainsString(
216
            '/baseUrl/css/default_options.css',
217
            $this->assetManager->getCssFiles()['/baseUrl/css/default_options.css']['url']
218
        );
219
        $this->assertEquals(
220
            [
221
                'media' => 'screen',
222
                'hreflang' => 'en'
223
            ],
224
            $this->assetManager->getCssFiles()['/baseUrl/css/default_options.css']['attributes']
225
        );
226
227
        $this->assertStringContainsString(
228
            '/baseUrl/css/tv.css',
229
            $this->assetManager->getCssFiles()['/baseUrl/css/tv.css']['url']
230
        );
231
        $this->assertEquals(
232
            [
233
                'media' => 'tv',
234
                'hreflang' => 'en'
235
            ],
236
            $this->assetManager->getCssFiles()['/baseUrl/css/tv.css']['attributes']
237
        );
238
239
        $this->assertStringContainsString(
240
            '/baseUrl/css/screen_and_print.css',
241
            $this->assetManager->getCssFiles()['/baseUrl/css/screen_and_print.css']['url']
242
        );
243
        $this->assertEquals(
244
            [
245
                'media' => 'screen, print',
246
                'hreflang' => 'en'
247
            ],
248
            $this->assetManager->getCssFiles()['/baseUrl/css/screen_and_print.css']['attributes']
249
        );
250
251
        $this->assertStringContainsString(
252
            '/baseUrl/js/normal.js',
253
            $this->assetManager->getJsFiles()['/baseUrl/js/normal.js']['url']
254
        );
255
        $this->assertEquals(
256
            [
257
                'charset' => 'utf-8',
258
                'position' => 3
259
            ],
260
            $this->assetManager->getJsFiles()['/baseUrl/js/normal.js']['attributes']
261
        );
262
263
        $this->assertStringContainsString(
264
            '/baseUrl/js/defered.js',
265
            $this->assetManager->getJsFiles()['/baseUrl/js/defered.js']['url']
266
        );
267
        $this->assertEquals(
268
            [
269
                'charset' => 'utf-8',
270
                'defer' => true,
271
                'position' => 3
272
            ],
273
            $this->assetManager->getJsFiles()['/baseUrl/js/defered.js']['attributes']
274
        );
275
    }
276
}
277