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