14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \AssetMapper \Exception \RuntimeException ;
16
16
use Symfony \Component \AssetMapper \ImportMap \ImportMapAuditor ;
17
+ use Symfony \Component \AssetMapper \ImportMap \ImportMapConfigReader ;
18
+ use Symfony \Component \AssetMapper \ImportMap \ImportMapEntries ;
17
19
use Symfony \Component \AssetMapper \ImportMap \ImportMapEntry ;
18
20
use Symfony \Component \AssetMapper \ImportMap \ImportMapManager ;
19
21
use Symfony \Component \AssetMapper \ImportMap \ImportMapPackageAudit ;
20
22
use Symfony \Component \AssetMapper \ImportMap \ImportMapPackageAuditVulnerability ;
23
+ use Symfony \Component \AssetMapper \ImportMap \Resolver \PackageResolver ;
24
+ use Symfony \Component \AssetMapper \ImportMap \Resolver \PackageResolverInterface ;
21
25
use Symfony \Component \HttpClient \MockHttpClient ;
22
26
use Symfony \Component \HttpClient \Response \MockResponse ;
23
27
use Symfony \Contracts \HttpClient \HttpClientInterface ;
24
28
25
29
class ImportMapAuditorTest extends TestCase
26
30
{
27
- private ImportMapManager $ importMapManager ;
31
+ private ImportMapConfigReader $ importMapConfigReader ;
32
+ private PackageResolverInterface $ packageResolver ;
28
33
private HttpClientInterface $ httpClient ;
29
34
private ImportMapAuditor $ importMapAuditor ;
30
35
31
36
protected function setUp (): void
32
37
{
33
- $ this ->importMapManager = $ this ->createMock (ImportMapManager::class);
38
+ $ this ->importMapConfigReader = $ this ->createMock (ImportMapConfigReader::class);
39
+ $ this ->packageResolver = $ this ->createMock (PackageResolverInterface::class);
34
40
$ this ->httpClient = new MockHttpClient ();
35
- $ this ->importMapAuditor = new ImportMapAuditor ($ this ->importMapManager , $ this ->httpClient );
41
+ $ this ->importMapAuditor = new ImportMapAuditor ($ this ->importMapConfigReader , $ this -> packageResolver , $ this ->httpClient );
36
42
}
37
43
38
44
public function testAudit ()
@@ -63,7 +69,7 @@ public function testAudit()
63
69
],
64
70
],
65
71
])));
66
- $ this ->importMapManager ->method ('loadImportMapEntries ' )->willReturn ([
72
+ $ this ->importMapConfigReader ->method ('getEntries ' )->willReturn ( new ImportMapEntries ([
67
73
'@hotwired/stimulus ' => new ImportMapEntry (
68
74
importName: '@hotwired/stimulus ' ,
69
75
url: 'https://unpkg.com/@hotwired/stimulus@3.2.1/dist/stimulus.js ' ,
@@ -79,7 +85,7 @@ public function testAudit()
79
85
url: 'https://ga.jspm.io/npm:lodash@4.17.21/lodash.js ' ,
80
86
version: '4.17.21 ' ,
81
87
),
82
- ]);
88
+ ])) ;
83
89
84
90
$ audit = $ this ->importMapAuditor ->audit ();
85
91
@@ -119,13 +125,13 @@ public function testAuditWithVersionRange(bool $expectMatch, string $version, ?s
119
125
],
120
126
],
121
127
])));
122
- $ this ->importMapManager ->method ('loadImportMapEntries ' )->willReturn ([
128
+ $ this ->importMapConfigReader ->method ('getEntries ' )->willReturn ( new ImportMapEntries ([
123
129
'json5 ' => new ImportMapEntry (
124
130
importName: 'json5 ' ,
125
131
url: "https://cdn.jsdelivr.net/npm/json5@ $ version/+esm " ,
126
132
version: $ version ,
127
133
),
128
- ]);
134
+ ])) ;
129
135
130
136
$ audit = $ this ->importMapAuditor ->audit ();
131
137
@@ -145,17 +151,43 @@ public function provideAuditWithVersionRange(): iterable
145
151
yield [false , '1.2.0 ' , '> 1.0.0, < 1.2.0 ' ];
146
152
}
147
153
148
- public function testAuditError ()
154
+ public function testAuditWithVersionResolving ()
149
155
{
156
+ $ this ->httpClient ->setResponseFactory (new MockResponse ('[] ' ));
157
+ $ this ->importMapConfigReader ->method ('getEntries ' )->willReturn (new ImportMapEntries ([
158
+ '@hotwired/stimulus ' => new ImportMapEntry (
159
+ importName: '@hotwired/stimulus ' ,
160
+ url: 'https://unpkg.com/@hotwired/stimulus/dist/stimulus.js ' ,
161
+ version: '3.2.1 ' ,
162
+ ),
163
+ 'json5 ' => new ImportMapEntry (
164
+ importName: 'json5 ' ,
165
+ url: 'https://cdn.jsdelivr.net/npm/json5/+esm ' ,
166
+ ),
167
+ 'lodash ' => new ImportMapEntry (
168
+ importName: 'lodash ' ,
169
+ url: 'https://ga.jspm.io/npm:lodash@4.17.21/lodash.js ' ,
170
+ ),
171
+ ]));
172
+ $ this ->packageResolver ->method ('getPackageVersion ' )->willReturn ('1.2.3 ' );
150
173
174
+ $ audit = $ this ->importMapAuditor ->audit ();
175
+
176
+ $ this ->assertSame ('3.2.1 ' , $ audit [0 ]->version );
177
+ $ this ->assertSame ('1.2.3 ' , $ audit [1 ]->version );
178
+ $ this ->assertSame ('1.2.3 ' , $ audit [2 ]->version );
179
+ }
180
+
181
+ public function testAuditError ()
182
+ {
151
183
$ this ->httpClient ->setResponseFactory (new MockResponse ('Server error ' , ['http_code ' => 500 ]));
152
- $ this ->importMapManager ->method ('loadImportMapEntries ' )->willReturn ([
184
+ $ this ->importMapConfigReader ->method ('getEntries ' )->willReturn ( new ImportMapEntries ([
153
185
'json5 ' => new ImportMapEntry (
154
186
importName: 'json5 ' ,
155
- url: " https://cdn.jsdelivr.net/npm/json5@1.0.0/+esm " ,
187
+ url: ' https://cdn.jsdelivr.net/npm/json5@1.0.0/+esm ' ,
156
188
version: '1.0.0 ' ,
157
189
),
158
- ]);
190
+ ])) ;
159
191
160
192
$ this ->expectException (RuntimeException::class);
161
193
$ this ->expectExceptionMessage ('Error 500 auditing packages. Response: Server error ' );
0 commit comments