8000 Adding missing test · symfony/symfony@c7df5dc · GitHub
[go: up one dir, main page]

Skip to content

Commit c7df5dc

Browse files
committed
Adding missing test
1 parent 8861512 commit c7df5dc

File tree

5 files changed

+209
-1
lines changed

5 files changed

+209
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"require": {
3636
"php": ">=8.1",
3737
"composer-runtime-api": ">=2.1",
38+
"composer/semver": "^3.0",
3839
"ext-xml": "*",
3940
"friendsofphp/proxy-manager-lts": "^1.0.2",
4041
"doctrine/event-manager": "^1.2|^2",

src/Symfony/Component/AssetMapper/ImportMap/ImportMapVersionChecker.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public function checkVersions(): array
4545
}
4646

4747
$dependencies = $this->packageDownloader->getDependencies($entry->importName);
48+
if (!$dependencies) {
49+
continue;
50+
}
51+
4852
$packageName = $this->extractPackageNameFromImport($entry->importName);
4953

5054
$url = str_replace(

src/Symfony/Component/AssetMapper/ImportMap/PackageVersionProblem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\AssetMapper\ImportMap;
1313

14-
class PackageVersionProblem
14+
final class PackageVersionProblem
1515
{
1616
public function __construct(
1717
public string $packageName,

src/Symfony/Component/AssetMapper/ImportMap/RemotePackageDownloader.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public function getDownloadedPath(string $importName): string
101101
return $this->vendorDir.'/'.$installed[$importName]['path'];
102102
}
103103

104+
/**
105+
* @return string[]
106+
*/
104107
public function getDependencies(string $importName): array
105108
{
106109
$installed = $this->loadInstalled();
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\AssetMapper\Tests\ImportMap;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader;
16+
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntries;
17+
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
18+
use Symfony\Component\AssetMapper\ImportMap\ImportMapVersionChecker;
19+
use Symfony\Component\AssetMapper\ImportMap\PackageVersionProblem;
20+
use Symfony\Component\AssetMapper\ImportMap\RemotePackageDownloader;
21+
use Symfony\Component\HttpClient\MockHttpClient;
22+
use Symfony\Component\HttpClient\Response\MockResponse;
23+
24+
class ImportMapVersionCheckerTest extends TestCase
25+
{
26+
/**
27+
* @dataProvider getCheckVersionsTests
28+
*/
29+
public function testCheckVersions(array $importMapEntries, array $dependencies, array $expectedRequests, array $expectedProblems)
30+
{
31+
$configReader = $this->createMock(ImportMapConfigReader::class);
32+
$configReader->expects($this->once())
33+
->method('getEntries')
34+
->willReturn(new ImportMapEntries($importMapEntries));
35+
36+
$remoteDownloader = $this->createMock(RemotePackageDownloader::class);
37+
$remoteDownloader->expects($this->exactly(\count($importMapEntries)))
38+
->method('getDependencies')
39+
->with($this->callback(function ($importName) use ($importMapEntries) {
40+
foreach ($importMapEntries as $entry) {
41+
if ($entry->importName === $importName) {
42+
return true;
43+
}
44+
}
45+
46+
return false;
47+
}))
48+
->willReturnCallback(function ($importName) use ($dependencies) {
49+
if (!isset($dependencies[$importName])) {
50+
throw new \InvalidArgumentException(sprintf('Missing dependencies in test for "%s"', $importName));
51+
}
52+
53+
return $dependencies[$importName];
54+
});
55+
56+
$responses = [];
57+
foreach ($expectedRequests as $expectedRequest) {
58+
$responses[] = function ($method, $url) use ($expectedRequest) {
59+
$this->assertStringEndsWith($expectedRequest['url'], $url);
60+
61+
return new MockResponse(json_encode($expectedRequest['response']));
62+
};
63+
}
64+
$httpClient = new MockHttpClient($responses);
65+
66+
$versionChecker = new ImportMapVersionChecker($configReader, $remoteDownloader, $httpClient);
67+
$problems = $versionChecker->checkVersions();
68+
$this->assertEquals($expectedProblems, $problems);
69+
$this->assertSame(\count($expectedRequests), $httpClient->getRequestsCount());
70+
}
71+
72+
public static function getCheckVersionsTests()
73+
{
74+
yield 'no dependencies' => [
75+
[
76+
new ImportMapEntry('foo', version: '1.0.0'),
77+
],
78+
[
79+
'foo' => [],
80+
],
81+
[],
82+
[],
83+
];
84+
85+
yield 'single with dependency but no problem' => [
86+
[
87+
new ImportMapEntry('foo', version: '1.0.0'),
88+
new ImportMapEntry('bar', version: '1.5.0'),
89+
],
90+
[
91+
'foo' => ['bar'],
92+
'bar' => [],
93+
],
94+
[
95+
[
96+
'url' => '/foo/1.0.0',
97+
'response' => [
98+
'dependencies' => ['bar' => '^1.0.0'],
99+
],
100+
],
101+
],
102+
[],
103+
];
104+
105+
yield 'single with dependency with problem' => [
106+
[
107+
new ImportMapEntry('foo', version: '1.0.0'),
108+
new ImportMapEntry('bar', version: '1.5.0'),
109+
],
110+
[
111+
'foo' => ['bar'],
112+
'bar' => [],
113+
],
114+
[
115+
[
116+
'url' => '/foo/1.0.0',
117+
'response' => [
118+
'dependencies' => ['bar' => '^2.0.0'],
119+
],
120+
],
121+
],
122+
[
123+
new PackageVersionProblem('foo', 'bar', '^2.0.0', '1.5.0'),
124+
],
125+
];
126+
127+
yield 'single with missing dependency' => [
128+
[
129+
new ImportMapEntry('foo', version: '1.0.0'),
130+
],
131+
[
132+
'foo' => ['bar'],
133+
],
134+
[
135+
[
136 7802 +
'url' => '/foo/1.0.0',
137+
'response' => [
138+
'dependencies' => ['bar' => '^2.0.0'],
139+
],
140+
],
141+
],
142+
[
143+
new PackageVersionProblem('foo', 'bar', '^2.0.0', null),
144+
],
145+
];
146+
147+
yield 'multiple package and problems' => [
148+
[
149+
new ImportMapEntry('foo', version: '1.0.0'),
150+
new ImportMapEntry('bar', version: '1.5.0'),
151+
new ImportMapEntry('baz', version: '2.0.0'),
152+
],
153+
[
154+
'foo' => ['bar'],
155+
'bar' => ['baz'],
156+
'baz' => [],
157+
],
158+
[
159+
[
160+
'url' => '/foo/1.0.0',
161+
'response' => [
162+
'dependencies' => ['bar' => '^2.0.0'],
163+
],
164+
],
165+
[
166+
'url' => '/bar/1.5.0',
167+
'response' => [
168+
'dependencies' => ['baz' => '^1.0.0'],
169+
],
170+
],
171+
],
172+
[
173+
new PackageVersionProblem('foo', 'bar', '^2.0.0', '1.5.0'),
174+
new PackageVersionProblem('bar', 'baz', '^1.0.0', '2.0.0'),
175+
],
176+
];
177+
178+
yield 'single with problem on peerDependency' => [
179+
[
180+
new ImportMapEntry('foo', version: '1.0.0'),
181+
new ImportMapEntry('bar', version: '1.5.0'),
182+
],
183+
[
184+
'foo' => ['bar'],
185+
'bar' => [],
186+
],
187+
[
188+
[
189+
'url' => '/foo/1.0.0',
190+
'response' => [
191+
'peerDependencies' => ['bar' => '^2.0.0'],
192+
],
193+
],
194+
],
195+
[
196+
new PackageVersionProblem('foo', 'bar', '^2.0.0', '1.5.0'),
197+
],
198+
];
199+
}
200+
}

0 commit comments

Comments
 (0)
0