|
12 | 12 | namespace Symfony\Component\AssetMapper\Tests\ImportMap\Resolver;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry; |
| 16 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapType; |
15 | 17 | use Symfony\Component\AssetMapper\ImportMap\PackageRequireOptions;
|
16 | 18 | use Symfony\Component\AssetMapper\ImportMap\Resolver\JsDelivrEsmResolver;
|
17 | 19 | use Symfony\Component\HttpClient\MockHttpClient;
|
@@ -157,6 +1
8000
59,39 @@ public static function provideResolvePackagesTests(): iterable
|
157 | 159 | ],
|
158 | 160 | ];
|
159 | 161 |
|
| 162 | + yield 'require package that imports another' => [ |
| 163 | + 'packages' => [new PackageRequireOptions('@chart/chart.js/auto', '^3')], |
| 164 | + 'expectedRequests' => [ |
| 165 | + [ |
| 166 | + 'url' => '/v1/packages/npm/@chart/chart.js/resolved?specifier=%5E3', |
| 167 | + 'response' => ['body' => ['version' => '3.0.1']], |
| 168 | + ], |
| 169 | + [ |
| 170 | + 'url' => '/@chart/chart.js@3.0.1/auto/+esm', |
| 171 | + 'response' => ['body' => 'import{Color as t}from"/npm/@kurkle/color@0.3.2/+esm";function e(){}const i=(()='] |
| 172 | + ], |
| 173 | + [ |
| 174 | + 'url' => '/v1/packages/npm/@kurkle/color/resolved?specifier=0.3.2', |
| 175 | + 'response' => ['body' => ['version' => '0.3.2']], |
| 176 | + ], |
| 177 | + [ |
| 178 | + 'url' => '/@kurkle/color@0.3.2/+esm', |
| 179 | + ], |
| 180 | + [ |
| 181 | + 'url' => '/v1/packages/npm/@kurkle/color@0.3.2/entrypoints', |
| 182 | + 'response' => ['body' => ['entrypoints' => []]], |
| 183 | + ], |
| 184 | + ], |
| 185 | + 'expectedResolvedPackages' => [ |
| 186 | + '@chart/chart.js/auto' => [ |
| 187 | + 'version' => '3.0.1', |
| 188 | + ], |
| 189 | + '@kurkle/color' => [ |
| 190 | + 'version' => '0.3.2', |
| 191 | + ], |
| 192 | + ], |
| 193 | + ]; |
| 194 | + |
160 | 195 | yield 'require single CSS package' => [
|
161 | 196 | 'packages' => [new PackageRequireOptions('bootstrap/dist/css/bootstrap.min.css')],
|
162 | 197 | 'expectedRequests' => [
|
@@ -230,6 +265,145 @@ public static function provideResolvePackagesTests(): iterable
|
230 | 265 | ];
|
231 | 266 | }
|
232 | 267 |
|
| 268 | + /** |
| 269 | + * @dataProvider provideDownloadPackagesTests |
| 270 | + */ |
| 271 | + public function testDownloadPackages(array $importMapEntries, array $expectedRequests, array $expectedContents) |
| 272 | + { |
| 273 | + $responses = []; |
| 274 | + foreach ($expectedRequests as $expectedRequest) { |
| 275 | + $responses[] = function ($method, $url) use ($expectedRequest) { |
| 276 | + $this->assertSame('GET', $method); |
| 277 | + $this->assertStringEndsWith($expectedRequest['url'], $url); |
| 278 | + |
| 279 | + return new MockResponse($expectedRequest['body']); |
| 280 | + }; |
| 281 | + } |
| 282 | + |
| 283 | + $httpClient = new MockHttpClient($responses); |
| 284 | + |
| 285 | + $provider = new JsDelivrEsmResolver($httpClient); |
| 286 | + $actualContents = $provider->downloadPackages($importMapEntries); |
| 287 | + $this->assertCount(\count($expectedContents), $actualContents); |
| 288 | + $actualContents = array_map('trim', $actualContents); |
| 289 | + $this->assertSame($expectedContents, $actualContents); |
| 290 | + $this->assertSame(count($expectedRequests), $httpClient->getRequestsCount()); |
| 291 | + } |
| 292 | + |
| 293 | + public static function provideDownloadPackagesTests() |
| 294 | + { |
| 295 | + yield 'single package' => [ |
| 296 | + ['lodash' => new ImportMapEntry('lodash', version: '1.2.3')], |
| 297 | + [ |
| 298 | + [ |
| 299 | + 'url' => '/lodash@1.2.3/+esm', |
| 300 | + 'body' => 'lodash contents', |
| 301 | + ], |
| 302 | + ], |
| 303 | + [ |
| 304 | + 'lodash' => 'lodash contents', |
| 305 | + ], |
| 306 | + ]; |
| 307 | + |
| 308 | + yield 'package with path' => [ |
| 309 | + ['lodash' => new ImportMapEntry('chart.js/auto', version: '4.5.6')], |
| 310 | + [ |
| 311 | + [ |
| 312 | + 'url' => '/chart.js@4.5.6/auto/+esm', |
| 313 | + 'body' => 'chart.js contents', |
| 314 | + ], |
| 315 | + ], |
| 316 | + [ |
| 317 | + 'lodash' => 'chart.js contents', |
| 318 | + ], |
| 319 | + ]; |
| 320 | + |
| 321 | + yield 'css file' => [ |
| 322 | + ['lodash' => new ImportMapEntry('bootstrap/dist/bootstrap.css', version: '5.0.6', type: ImportMapType::CSS)], |
| 323 | + [ |
| 324 | + [ |
| 325 | + 'url' => '/bootstrap@5.0.6/dist/bootstrap.css', |
| 326 | + 'body' => 'bootstrap.css contents', |
| 327 | + ], |
| 328 | + ], |
| 329 | + [ |
| 330 | + 'lodash' => 'bootstrap.css contents', |
| 331 | + ], |
| 332 | + ]; |
| 333 | + |
| 334 | + yield 'multiple files' => [ |
| 335 | + [ |
| 336 | + 'lodash' => new ImportMapEntry('lodash', version: '1.2.3'), |
| 337 | + 'chart.js/auto' => new ImportMapEntry('chart.js/auto', version: '4.5.6'), |
| 338 | + 'bootstrap/dist/bootstrap.css' => new ImportMapEntry('bootstrap/dist/bootstrap.css', version: '5.0.6', type: ImportMapType::CSS), |
| 339 | + ], |
| 340 | + [ |
| 341 | + [ |
| 342 | + 'url' => '/lodash@1.2.3/+esm', |
| 343 | + 'body' => 'lodash contents', |
| 344 | + ], |
| 345 | + [ |
| 346 | + 'url' => '/chart.js@4.5.6/auto/+esm', |
| 347 | + 'body' => 'chart.js contents', |
| 348 | + ], |
| 349 | + [ |
| 350 | + 'url' => '/bootstrap@5.0.6/dist/bootstrap.css', |
| 351 | + 'body' => 'bootstrap.css contents', |
| 352 | + ], |
| 353 | + ], |
| 354 | + [ |
| 355 | + 'lodash' => 'lodash contents', |
| 356 | + 'chart.js/auto' => 'chart.js contents', |
| 357 | + 'bootstrap/dist/bootstrap.css' => 'bootstrap.css contents', |
| 358 | + ], |
| 359 | + ]; |
| 360 | + |
| 361 | + yield 'make imports relative' => [ |
| 362 | + [ |
| 363 | + '@chart.js/auto' => new ImportMapEntry('chart.js/auto', version: '1.2.3'), |
| 364 | + ], |
| 365 | + [ |
| 366 | + [ |
| 367 | + 'url' => '/chart.js@1.2.3/auto/+esm', |
| 368 | + 'body' => 'import{Color as t}from"/npm/@kurkle/color@0.3.2/+esm";function e(){}const i=(()=', |
| 369 | + ], |
| 370 | + ], |
| 371 | + [ |
| 372 | + '@chart.js/auto' => 'import{Color as t}from"@kurkle/color";function e(){}const i=(()=', |
| 373 | + ], |
| 374 | + ]; |
| 375 | + |
| 376 | + yield 'js importmap is removed' => [ |
| 377 | + [ |
| 378 | + '@chart.js/auto' => new ImportMapEntry('chart.js/auto', version: '1.2.3'), |
| 379 | + ], |
| 380 | + [ |
| 381 | + [ |
| 382 | + 'url' => '/chart.js@1.2.3/auto/+esm', |
| 383 | + 'body' => 'as Ticks,ta as TimeScale,ia as TimeSeriesScale,oo as Title,wo as Tooltip,Ci as _adapters,us as _detectPlatform,Ye as animator,Si as controllers,tn as default,St as defaults,Pn as elements,qi as layouts,ko as plugins,na as registerables,Ps as registry,sa as scales}; |
| 384 | + //# sourceMappingURL=/sm/bc823a081dbde2b3a5424732858022f831d3f2978d59498cd938e0c2c8cf9ec0.map', |
| 385 | + ], |
| 386 | + ], |
| 387 | + [ |
| 388 | + '@chart.js/auto' => 'as Ticks,ta as TimeScale,ia as TimeSeriesScale,oo as Title,wo as Tooltip,Ci as _adapters,us as _detectPlatform,Ye as animator,Si as controllers,tn as default,St as defaults,Pn as elements,qi as layouts,ko as plugins,na as registerables,Ps as registry,sa as scales};', |
| 389 | + ], |
| 390 | + ]; |
| 391 | + |
| 392 | + yield 'css file removes importmap' => [ |
| 393 | + ['lodash' => new ImportMapEntry('bootstrap/dist/bootstrap.css', version: '5.0.6', type: ImportMapType::CSS)], |
| 394 | + [ |
| 395 | + [ |
| 396 | + 'url' => '/bootstrap@5.0.6/dist/bootstrap.css', |
| 397 | + 'body' => 'print-table-row{display:table-row!important}.d-print-table-cell{display:table-cell!important}.d-print-flex{display:flex!important}.d-print-inline-flex{display:inline-flex!important}.d-print-none{display:none!important}} |
| 398 | + /*# sourceMappingURL=bootstrap.min.css.map */', |
| 399 | + ], |
| 400 | + ], |
| 401 | + [ |
| 402 | + 'lodash' => 'print-table-row{display:table-row!important}.d-print-table-cell{display:table-cell!important}.d-print-flex{display:flex!important}.d-print-inline-flex{display:inline-flex!important}.d-print-none{display:none!important}}', |
| 403 | + ], |
| 404 | + ]; |
| 405 | + } |
| 406 | + |
233 | 407 | /**
|
234 | 408 | * @dataProvider provideImportRegex
|
235 | 409 | */
|
|
0 commit comments