Passed
Push — master ( f68db4...f935c2 )
by Alexander
01:36
created

AssetBundleTest::testSourceSetHashCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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