8000 improving tests · symfony/symfony@42781ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 42781ed

Browse files
committed
improving tests
1 parent 947f9c0 commit 42781ed

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\AssetMapper\ImportMap\Resolver;
1313

1414
use Symfony\Component\AssetMapper\Exception\RuntimeException;
15+
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
1516
use Symfony\Component\AssetMapper\ImportMap\ImportMapType;
1617
use Symfony\Component\AssetMapper\ImportMap\PackageRequireOptions;
1718
use Symfony\Component\HttpClient\HttpClient;
@@ -151,6 +152,9 @@ public function resolvePackages(array $packagesToRequire): array
151152
return array_values($resolvedPackages);
152153
}
153154

155+
/**
156+
* @param ImportMapEntry[] $importMapEntries
157+
*/
154158
public function downloadPackages(array $importMapEntries, callable $progressCallback = null): array
155159
{
156160
$responses = [];

src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php

Lines changed: 174 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\AssetMapper\Tests\ImportMap\Resolver;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
16+
use Symfony\Component\AssetMapper\ImportMap\ImportMapType;
1517
use Symfony\Component\AssetMapper\ImportMap\PackageRequireOptions;
1618
use Symfony\Component\AssetMapper\ImportMap\Resolver\JsDelivrEsmResolver;
1719
use Symfony\Component\HttpClient\MockHttpClient;
@@ -157,6 +159,39 @@ public static function provideResolvePackagesTests(): iterable
157159
],
158160
];
159161

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+
160195
yield 'require single CSS package' => [
161196
'packages' => [new PackageRequireOptions('bootstrap/dist/css/bootstrap.min.css')],
162197
'expectedRequests' => [
@@ -230,6 +265,145 @@ public static function provideResolvePackagesTests(): iterable
230265
];
231266
}
232267

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+
233407
/**
234408
* @dataProvider provideImportRegex
235409
*/

0 commit comments

Comments
 (0)
0