|
| 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