Passed
Push — master ( 59ab14...8278b6 )
by Alexander
06:48
created

AssetBundleTest::testFileOptionsAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 38
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 65
rs 9.312

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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