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

AssetManagerTest::testPositionDependencyConflict()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
nc 2
nop 2
dl 0
loc 33
c 0
b 0
f 0
cc 2
rs 9.6
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Assets\Tests;
5
6
use Yiisoft\Assets\AssetBundle;
7
use Yiisoft\Assets\AssetConverterInterface;
8
use Yiisoft\Assets\Tests\stubs\JqueryAsset;
9
use Yiisoft\Assets\Tests\stubs\Level3Asset;
10
use Yiisoft\Assets\Tests\stubs\PositionAsset;
11
use Yiisoft\Assets\Tests\stubs\SourceAsset;
12
13
final class AssetManagerTest extends TestCase
14
{
15
    public function testGetConverter(): void
16
    {
17
        $this->assertInstanceOf(
18
            AssetConverterInterface::class,
19
            $this->assetManager->getConverter()
20
        );
21
    }
22
23
    public function testGetPublishedPathLinkAssetsFalse(): void
24
    {
25
        $bundle = new SourceAsset();
26
27
        $sourcePath = $this->aliases->get($bundle->sourcePath);
28
29
        $path = $sourcePath . @filemtime($sourcePath);
30
        $path = sprintf('%x', crc32($path . '|' . $this->assetManager->getPublisher()->getLinkAssets()));
31
32
        $this->assertEmpty($this->assetManager->getAssetBundles());
33
        $this->assetManager->register([SourceAsset::class]);
34
35
        $this->assertEquals(
36
            $this->assetManager->getPublisher()->getPublishedPath($bundle->sourcePath),
0 ignored issues
show
Bug introduced by
It seems like $bundle->sourcePath can also be of type null; however, parameter $sourcePath of Yiisoft\Assets\AssetPublisher::getPublishedPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            $this->assetManager->getPublisher()->getPublishedPath(/** @scrutinizer ignore-type */ $bundle->sourcePath),
Loading history...
37
            $this->aliases->get("@public/assets/$path")
38
        );
39
    }
40
41
    public function testGetPublishedPathWrong(): void
42
    {
43
        $this->assertEmpty($this->assetManager->getAssetBundles());
44
45
        $this->assetManager->register([SourceAsset::class]);
46
47
        $this->assertNull($this->assetManager->getPublisher()->getPublishedPath('/wrong'));
48
    }
49
50
    public function testGetPublishedUrl(): void
51
    {
52
        $bundle = new SourceAsset();
53
54
        $sourcePath = $this->aliases->get($bundle->sourcePath);
55
56
        $path = $sourcePath . @filemtime($sourcePath);
57
        $path = sprintf('%x', crc32($path . '|' . $this->assetManager->getPublisher()->getLinkAssets()));
58
59
        $this->assertEmpty($this->assetManager->getAssetBundles());
60
61
        $this->assetManager->register([SourceAsset::class]);
62
63
        $this->assertEquals(
64
            $this->assetManager->getPublisher()->getPublishedUrl($bundle->sourcePath),
0 ignored issues
show
Bug introduced by
It seems like $bundle->sourcePath can also be of type null; however, parameter $sourcePath of Yiisoft\Assets\AssetPublisher::getPublishedUrl() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $this->assetManager->getPublisher()->getPublishedUrl(/** @scrutinizer ignore-type */ $bundle->sourcePath),
Loading history...
65
            "/baseUrl/$path"
66
        );
67
    }
68
69
    public function testGetPublishedUrlWrong(): void
70
    {
71
        $this->assertEmpty($this->assetManager->getAssetBundles());
72
73
        $this->assetManager->register([SourceAsset::class]);
74
75
        $this->assertNull($this->assetManager->getPublisher()->getPublishedUrl('/wrong'));
76
    }
77
78
    public function testAssetManagerSetBundles(): void
79
    {
80
        $urlJs = 'https://code.jquery.com/jquery-3.4.1.js';
81
82
        $this->assetManager->setBundles(
83
            [
84
                JqueryAsset::class => [
85
                    'sourcePath' => null, //no publish asset bundle
86
                    'js' => [
87
                        [
88
                            $urlJs,
89
                            'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
90
                            'crossorigin' => 'anonymous'
91
                        ]
92
                    ]
93
                ]
94
            ]
95
        );
96
97
        $this->assertEmpty($this->assetManager->getAssetBundles());
98
99
        $this->assetManager->register([JqueryAsset::class]);
100
101
        $this->assertStringContainsString(
102
            $urlJs,
103
            $this->assetManager->getJsFiles()[$urlJs]['url']
104
        );
105
        $this->assertEquals(
106
            [
107
                'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
108
                'crossorigin' => 'anonymous',
109
                'position' => 3
110
            ],
111
            $this->assetManager->getJsFiles()[$urlJs]['attributes']
112
        );
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function positionProvider(): array
119
    {
120
        return [
121
            [1, true],
122
            [1, false],
123
            [2, true],
124
            [2, false],
125
            [3, true],
126
            [3, false],
127
        ];
128
    }
129
130
    /**
131
     * @dataProvider positionProvider
132
     *
133
     * @param int $pos
134
     * @param bool $jqAlreadyRegistered
135
     */
136
    public function testPositionDependency(int $pos, bool $jqAlreadyRegistered): void
137
    {
138
        $this->assetManager->setBundles([
139
            PositionAsset::class => [
140
                'jsOptions' =>  [
141
                    'position' => $pos,
142
                ],
143
            ]
144
        ]);
145
146
        $this->assertEmpty($this->assetManager->getAssetBundles());
147
148
        if ($jqAlreadyRegistered) {
149
            $this->assetManager->register([JqueryAsset::class, PositionAsset::class]);
150
        } else {
151
            $this->assetManager->register([PositionAsset::class]);
152
        }
153
154
        $this->assertCount(3, $this->assetManager->getAssetBundles());
155
        $this->assertArrayHasKey(PositionAsset::class, $this->assetManager->getAssetBundles());
156
        $this->assertArrayHasKey(JqueryAsset::class, $this->assetManager->getAssetBundles());
157
        $this->assertArrayHasKey(Level3Asset::class, $this->assetManager->getAssetBundles());
158
159
        $this->assertInstanceOf(
160
            AssetBundle::class,
161
            $this->assetManager->getAssetBundles()[PositionAsset::class]
162
        );
163
        $this->assertInstanceOf(
164
            AssetBundle::class,
165
            $this->assetManager->getAssetBundles()[JqueryAsset::class]
166
        );
167
        $this->assertInstanceOf(
168
            AssetBundle::class,
169
            $this->assetManager->getAssetBundles()[Level3Asset::class]
170
        );
171
172
        $this->assertArrayHasKey(
173
            'position',
174
            $this->assetManager->getAssetBundles()[PositionAsset::class]->jsOptions
175
        );
176
        $this->assertEquals(
177
            $pos,
178
            $this->assetManager->getAssetBundles()[PositionAsset::class]->jsOptions['position']
179
        );
180
        $this->assertArrayHasKey(
181
            'position',
182
            $this->assetManager->getAssetBundles()[JqueryAsset::class]->jsOptions
183
        );
184
185
        $this->assertEquals(
186
            $pos,
187
            $this->assetManager->getAssetBundles()[JqueryAsset::class]->jsOptions['position']
188
        );
189
        $this->assertArrayHasKey(
190
            'position',
191
            $this->assetManager->getAssetBundles()[Level3Asset::class]->jsOptions
192
        );
193
        $this->assertEquals(
194
            $pos,
195
            $this->assetManager->getAssetBundles()[Level3Asset::class]->jsOptions['position']
196
        );
197
198
        $this->assertEquals(
199
            [
200
                'position' => $pos
201
            ],
202
            $this->assetManager->getJsFiles()['/js/jquery.js']['attributes']
203
        );
204
        $this->assertEquals(
205
            [
206
                'position' => $pos
207
            ],
208
            $this->assetManager->getJsFiles()['/files/jsFile.js']['attributes']
209
        );
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