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

AssetManagerTest::testAssetManagerSetBundles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 34
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Assets\Tests;
6
7
use Yiisoft\Assets\AssetBundle;
8
use Yiisoft\Assets\AssetConverterInterface;
9
use Yiisoft\Assets\Tests\stubs\JqueryAsset;
10
use Yiisoft\Assets\Tests\stubs\Level3Asset;
11
use Yiisoft\Assets\Tests\stubs\PositionAsset;
12
use Yiisoft\Assets\Tests\stubs\SourceAsset;
13
14
final class AssetManagerTest extends TestCase
15
{
16
    public function testGetConverter(): void
17
    {
18
        $this->assertInstanceOf(
19
            AssetConverterInterface::class,
20
            $this->assetManager->getConverter()
21
        );
22
    }
23
24
    public function testGetPublishedPathLinkAssetsFalse(): void
25
    {
26
        $bundle = new SourceAsset();
27
28
        $sourcePath = $this->aliases->get($bundle->sourcePath);
29
30
        $path = $sourcePath . @filemtime($sourcePath);
31
        $path = sprintf('%x', crc32($path . '|' . $this->assetManager->getPublisher()->getLinkAssets()));
32
33
        $this->assertEmpty($this->assetManager->getAssetBundles());
34
        $this->assetManager->register([SourceAsset::class]);
35
36
        $this->assertEquals(
37
            $this->assetManager->getPublisher()->getPublishedPath($bundle->sourcePath),
38
            $this->aliases->get("@public/assets/$path")
39
        );
40
    }
41
42
    public function testGetPublishedPathWrong(): void
43
    {
44
        $this->assertEmpty($this->assetManager->getAssetBundles());
45
46
        $this->assetManager->register([SourceAsset::class]);
47
48
        $this->assertNull($this->assetManager->getPublisher()->getPublishedPath('/wrong'));
49
    }
50
51
    public function testGetPublishedUrl(): void
52
    {
53
        $bundle = new SourceAsset();
54
55
        $sourcePath = $this->aliases->get($bundle->sourcePath);
56
57
        $path = $sourcePath . @filemtime($sourcePath);
58
        $path = sprintf('%x', crc32($path . '|' . $this->assetManager->getPublisher()->getLinkAssets()));
59
60
        $this->assertEmpty($this->assetManager->getAssetBundles());
61
62
        $this->assetManager->register([SourceAsset::class]);
63
64
        $this->assertEquals(
65
            $this->assetManager->getPublisher()->getPublishedUrl($bundle->sourcePath),
66
            "/baseUrl/$path"
67
        );
68
    }
69
70
    public function testGetPublishedUrlWrong(): void
71
    {
72
        $this->assertEmpty($this->assetManager->getAssetBundles());
73
74
        $this->assetManager->register([SourceAsset::class]);
75
76
        $this->assertNull($this->assetManager->getPublisher()->getPublishedUrl('/wrong'));
77
    }
78
79
    public function testAssetManagerSetBundles(): void
80
    {
81
        $urlJs = 'https://code.jquery.com/jquery-3.4.1.js';
82
83
        $this->assetManager->setBundles(
84
            [
85
                JqueryAsset::class => [
86
                    'sourcePath' => null, //no publish asset bundle
87
                    'js' => [
88
                        [
89
                            $urlJs,
90
                            'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
91
                            'crossorigin' => 'anonymous'
92
                        ]
93
                    ]
94
                ]
95
            ]
96
        );
97
98
        $this->assertEmpty($this->assetManager->getAssetBundles());
99
100
        $this->assetManager->register([JqueryAsset::class]);
101
102
        $this->assertStringContainsString(
103
            $urlJs,
104
            $this->assetManager->getJsFiles()[$urlJs]['url']
105
        );
106
        $this->assertEquals(
107
            [
108
                'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
109
                'crossorigin' => 'anonymous',
110
                'position' => 3
111
            ],
112
            $this->assetManager->getJsFiles()[$urlJs]['attributes']
113
        );
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function positionProvider(): array
120
    {
121
        return [
122
            [1, true],
123
            [1, false],
124
            [2, true],
125
            [2, false],
126
            [3, true],
127
            [3, false],
128
        ];
129
    }
130
131
    /**
132
     * @dataProvider positionProvider
133
     *
134
     * @param int $pos
135
     * @param bool $jqAlreadyRegistered
136
     */
137
    public function testPositionDependency(int $pos, bool $jqAlreadyRegistered): void
138
    {
139
        $this->assetManager->setBundles([
140
            PositionAsset::class => [
141
                'jsOptions' =>  [
142
                    'position' => $pos,
143
                ],
144
            ]
145
        ]);
146
147
        $this->assertEmpty($this->assetManager->getAssetBundles());
148
149
        if ($jqAlreadyRegistered) {
150
            $this->assetManager->register([JqueryAsset::class, PositionAsset::class]);
151
        } else {
152
            $this->assetManager->register([PositionAsset::class]);
153
        }
154
155
        $this->assertCount(3, $this->assetManager->getAssetBundles());
156
        $this->assertArrayHasKey(PositionAsset::class, $this->assetManager->getAssetBundles());
157
        $this->assertArrayHasKey(JqueryAsset::class, $this->assetManager->getAssetBundles());
158
        $this->assertArrayHasKey(Level3Asset::class, $this->assetManager->getAssetBundles());
159
160
        $this->assertInstanceOf(
161
            AssetBundle::class,
162
            $this->assetManager->getAssetBundles()[PositionAsset::class]
163
        );
164
        $this->assertInstanceOf(
165
            AssetBundle::class,
166
            $this->assetManager->getAssetBundles()[JqueryAsset::class]
167
        );
168
        $this->assertInstanceOf(
169
            AssetBundle::class,
170
            $this->assetManager->getAssetBundles()[Level3Asset::class]
171
        );
172
173
        $this->assertArrayHasKey(
174
            'position',
175
            $this->assetManager->getAssetBundles()[PositionAsset::class]->jsOptions
176
        );
177
        $this->assertEquals(
178
            $pos,
179
            $this->assetManager->getAssetBundles()[PositionAsset::class]->jsOptions['position']
180
        );
181
        $this->assertArrayHasKey(
182
            'position',
183
            $this->assetManager->getAssetBundles()[JqueryAsset::class]->jsOptions
184
        );
185
186
        $this->assertEquals(
187
            $pos,
188
            $this->assetManager->getAssetBundles()[JqueryAsset::class]->jsOptions['position']
189
        );
190
        $this->assertArrayHasKey(
191
            'position',
192
            $this->assetManager->getAssetBundles()[Level3Asset::class]->jsOptions
193
        );
194
        $this->assertEquals(
195
            $pos,
196
            $this->assetManager->getAssetBundles()[Level3Asset::class]->jsOptions['position']
197
        );
198
199
        $this->assertEquals(
200
            [
201
                'position' => $pos
202
            ],
203
            $this->assetManager->getJsFiles()['/js/jquery.js']['attributes']
204
        );
205
        $this->assertEquals(
206
            [
207
                'position' => $pos
208
            ],
209
            $this->assetManager->getJsFiles()['/files/jsFile.js']['attributes']
210
        );
211
    }
212
213
    /**
214
     * @return array
215
     */
216
    public function positionProvider2(): array
217
    {
218
        return [
219
            [1, true],
220
            [1, false],
221
            [2, true],
222
            [2, false],
223
        ];
224
    }
225
226
    /**
227
     * @dataProvider positionProvider2
228
     *
229
     * @param int $pos
230
     * @param bool $jqAlreadyRegistered
231
     */
232
    public function testPositionDependencyConflict(int $pos, bool $jqAlreadyRegistered): void
233
    {
234
        $jqAsset = JqueryAsset::class;
235
236
        $this->assetManager->setBundles([
237
            PositionAsset::class => [
238
                'jsOptions' =>  [
239
                    'position' => $pos - 1,
240
                ],
241
            ],
242
            JqueryAsset::class => [
243
                'jsOptions' =>  [
244
                    'position' => $pos,
245
                ],
246
            ]
247
        ]);
248
249
        if ($jqAlreadyRegistered) {
250
            $message = "An asset bundle that depends on '$jqAsset' has a higher javascript file " .
251
            "position configured than '$jqAsset'.";
252
253
            $this->expectException(\RuntimeException::class);
254
            $this->expectExceptionMessage($message);
255
256
            $this->assetManager->register([JqueryAsset::class, PositionAsset::class]);
257
        } else {
258
            $message = "An asset bundle that depends on '$jqAsset' has a higher javascript file " .
259
                "position configured than '$jqAsset'.";
260
261
            $this->expectException(\RuntimeException::class);
262
            $this->expectExceptionMessage($message);
263
264
            $this->assetManager->register([PositionAsset::class]);
265
        }
266
    }
267
}
268