1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Assets\Tests; |
5
|
|
|
|
6
|
|
|
use Yiisoft\Assets\AssetBundle; |
7
|
|
|
use Yiisoft\Assets\Tests\stubs\JqueryAsset; |
8
|
|
|
use Yiisoft\Assets\Tests\stubs\Level3Asset; |
9
|
|
|
use Yiisoft\Assets\Tests\stubs\PositionAsset; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* AssetPositionTest. |
13
|
|
|
*/ |
14
|
|
|
final class AssetPositionTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
public function positionProvider(): array |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
|
|
[1, true], |
23
|
|
|
[1, false], |
24
|
|
|
[2, true], |
25
|
|
|
[2, false], |
26
|
|
|
[3, true], |
27
|
|
|
[3, false], |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider positionProvider |
33
|
|
|
* |
34
|
|
|
* @param int $pos |
35
|
|
|
* @param bool $jqAlreadyRegistered |
36
|
|
|
*/ |
37
|
|
|
public function testPositionDependency(int $pos, bool $jqAlreadyRegistered): void |
38
|
|
|
{ |
39
|
|
|
$this->assetManager->setBundles([ |
40
|
|
|
PositionAsset::class => [ |
41
|
|
|
'jsOptions' => [ |
42
|
|
|
'position' => $pos, |
43
|
|
|
], |
44
|
|
|
] |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$this->assertEmpty($this->assetManager->getAssetBundles()); |
48
|
|
|
|
49
|
|
|
if ($jqAlreadyRegistered) { |
50
|
|
|
$this->assetManager->register([JqueryAsset::class, PositionAsset::class]); |
51
|
|
|
} else { |
52
|
|
|
$this->assetManager->register([PositionAsset::class]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->assertCount(3, $this->assetManager->getAssetBundles()); |
56
|
|
|
$this->assertArrayHasKey(PositionAsset::class, $this->assetManager->getAssetBundles()); |
57
|
|
|
$this->assertArrayHasKey(JqueryAsset::class, $this->assetManager->getAssetBundles()); |
58
|
|
|
$this->assertArrayHasKey(Level3Asset::class, $this->assetManager->getAssetBundles()); |
59
|
|
|
|
60
|
|
|
$this->assertInstanceOf( |
61
|
|
|
AssetBundle::class, |
62
|
|
|
$this->assetManager->getAssetBundles()[PositionAsset::class] |
63
|
|
|
); |
64
|
|
|
$this->assertInstanceOf( |
65
|
|
|
AssetBundle::class, |
66
|
|
|
$this->assetManager->getAssetBundles()[JqueryAsset::class] |
67
|
|
|
); |
68
|
|
|
$this->assertInstanceOf( |
69
|
|
|
AssetBundle::class, |
70
|
|
|
$this->assetManager->getAssetBundles()[Level3Asset::class] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$this->assertArrayHasKey( |
74
|
|
|
'position', |
75
|
|
|
$this->assetManager->getAssetBundles()[PositionAsset::class]->jsOptions |
76
|
|
|
); |
77
|
|
|
$this->assertEquals( |
78
|
|
|
$pos, |
79
|
|
|
$this->assetManager->getAssetBundles()[PositionAsset::class]->jsOptions['position'] |
80
|
|
|
); |
81
|
|
|
$this->assertArrayHasKey( |
82
|
|
|
'position', |
83
|
|
|
$this->assetManager->getAssetBundles()[JqueryAsset::class]->jsOptions |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->assertEquals( |
87
|
|
|
$pos, |
88
|
|
|
$this->assetManager->getAssetBundles()[JqueryAsset::class]->jsOptions['position'] |
89
|
|
|
); |
90
|
|
|
$this->assertArrayHasKey( |
91
|
|
|
'position', |
92
|
|
|
$this->assetManager->getAssetBundles()[Level3Asset::class]->jsOptions |
93
|
|
|
); |
94
|
|
|
$this->assertEquals( |
95
|
|
|
$pos, |
96
|
|
|
$this->assetManager->getAssetBundles()[Level3Asset::class]->jsOptions['position'] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->assertEquals( |
100
|
|
|
[ |
101
|
|
|
'position' => $pos |
102
|
|
|
], |
103
|
|
|
$this->assetManager->getJsFiles()['/js/jquery.js']['attributes'] |
104
|
|
|
); |
105
|
|
|
$this->assertEquals( |
106
|
|
|
[ |
107
|
|
|
'position' => $pos |
108
|
|
|
], |
109
|
|
|
$this->assetManager->getJsFiles()['/files/jsFile.js']['attributes'] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function positionProvider2(): array |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
[1, true], |
121
|
|
|
[1, false], |
122
|
|
|
[2, true], |
123
|
|
|
[2, false], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @dataProvider positionProvider2 |
129
|
|
|
* |
130
|
|
|
* @param int $pos |
131
|
|
|
* @param bool $jqAlreadyRegistered |
132
|
|
|
*/ |
133
|
|
|
public function testPositionDependencyConflict(int $pos, bool $jqAlreadyRegistered): void |
134
|
|
|
{ |
135
|
|
|
$jqAsset = JqueryAsset::class; |
136
|
|
|
|
137
|
|
|
$this->assetManager->setBundles([ |
138
|
|
|
PositionAsset::class => [ |
139
|
|
|
'jsOptions' => [ |
140
|
|
|
'position' => $pos - 1, |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
JqueryAsset::class => [ |
144
|
|
|
'jsOptions' => [ |
145
|
|
|
'position' => $pos, |
146
|
|
|
], |
147
|
|
|
] |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
if ($jqAlreadyRegistered) { |
151
|
|
|
$message = "An asset bundle that depends on '$jqAsset' has a higher javascript file " . |
152
|
|
|
"position configured than '$jqAsset'."; |
153
|
|
|
|
154
|
|
|
$this->expectException(\RuntimeException::class); |
155
|
|
|
$this->expectExceptionMessage($message); |
156
|
|
|
|
157
|
|
|
$this->assetManager->register([JqueryAsset::class, PositionAsset::class]); |
158
|
|
|
} else { |
159
|
|
|
$message = "An asset bundle that depends on '$jqAsset' has a higher javascript file " . |
160
|
|
|
"position configured than '$jqAsset'."; |
161
|
|
|
|
162
|
|
|
$this->expectException(\RuntimeException::class); |
163
|
|
|
$this->expectExceptionMessage($message); |
164
|
|
|
|
165
|
|
|
$this->assetManager->register([PositionAsset::class]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|