|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Internal\DLoad\Tests\Unit\Module\Repository; |
| 6 | + |
| 7 | +use Internal\DLoad\Module\Common\Architecture; |
| 8 | +use Internal\DLoad\Module\Common\OperatingSystem; |
| 9 | +use Internal\DLoad\Module\Common\Stability; |
| 10 | +use Internal\DLoad\Module\Repository\Collection\AssetsCollection; |
| 11 | +use Internal\DLoad\Tests\Unit\Module\Repository\Stub\AssetStub; |
| 12 | +use Internal\DLoad\Tests\Unit\Module\Repository\Stub\ReleaseStub; |
| 13 | +use Internal\DLoad\Tests\Unit\Module\Repository\Stub\RepositoryStub; |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +#[CoversClass(AssetsCollection::class)] |
| 19 | +final class AssetsCollectionTest extends TestCase |
| 20 | +{ |
| 21 | + private RepositoryStub $repository; |
| 22 | + private ReleaseStub $release; |
| 23 | + private array $assets; |
| 24 | + private AssetsCollection $collection; |
| 25 | + |
| 26 | + public static function provideNamePatterns(): \Generator |
| 27 | + { |
| 28 | + yield 'exact linux x64 asset' => [ |
| 29 | + '/^package-1\.2\.3-linux-x64\.tar\.gz$/', |
| 30 | + ['package-1.2.3-linux-x64.tar.gz'], |
| 31 | + ]; |
| 32 | + |
| 33 | + yield 'all linux assets' => [ |
| 34 | + '/linux/', |
| 35 | + [ |
| 36 | + 'package-1.2.3-linux-x64.tar.gz', |
| 37 | + 'package-1.2.3-linux-arm64.tar.gz', |
| 38 | + ], |
| 39 | + ]; |
| 40 | + |
| 41 | + yield 'all tar.gz assets' => [ |
| 42 | + '/\.tar\.gz$/', |
| 43 | + [ |
| 44 | + 'package-1.2.3-linux-x64.tar.gz', |
| 45 | + 'package-1.2.3-linux-arm64.tar.gz', |
| 46 | + 'package-1.2.3-darwin-x64.tar.gz', |
| 47 | + ], |
| 48 | + ]; |
| 49 | + |
| 50 | + yield 'all zip assets' => [ |
| 51 | + '/\.zip$/', |
| 52 | + [ |
| 53 | + 'package-1.2.3-windows-x64.zip', |
| 54 | + 'package-1.2.3-source.zip', |
| 55 | + ], |
| 56 | + ]; |
| 57 | + |
| 58 | + yield 'no matches' => [ |
| 59 | + '/nonexistent/', |
| 60 | + [], |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + public function testWhereOperatingSystemFiltersAssetsByOs(): void |
| 65 | + { |
| 66 | + // Act |
| 67 | + $result = $this->collection->whereOperatingSystem(OperatingSystem::Linux); |
| 68 | + |
| 69 | + // Assert |
| 70 | + self::assertCount(2, $result); |
| 71 | + |
| 72 | + foreach ($result as $asset) { |
| 73 | + self::assertSame(OperatingSystem::Linux, $asset->getOperatingSystem()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function testWhereArchitectureFiltersAssetsByArchitecture(): void |
| 78 | + { |
| 79 | + // Act |
| 80 | + $result = $this->collection->whereArchitecture(Architecture::ARM_64); |
| 81 | + |
| 82 | + // Assert |
| 83 | + self::assertCount(3, $result); |
| 84 | + |
| 85 | + foreach ($result as $asset) { |
| 86 | + self::assertSame(Architecture::ARM_64, $asset->getArchitecture()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + #[DataProvider('provideNamePatterns')] |
| 91 | + public function testWhereNameMatchesFiltersAssetsByNamePattern( |
| 92 | + string $pattern, |
| 93 | + array $expectedMatches, |
| 94 | + ): void { |
| 95 | + // Act |
| 96 | + $result = $this->collection->whereNameMatches($pattern); |
| 97 | + |
| 98 | + // Assert |
| 99 | + self::assertCount(\count($expectedMatches), $result); |
| 100 | + |
| 101 | + $actualNames = \array_map( |
| 102 | + static fn($asset) => $asset->getName(), |
| 103 | + \iterator_to_array($result), |
| 104 | + ); |
| 105 | + |
| 106 | + foreach ($expectedMatches as $expectedName) { |
| 107 | + self::assertContains($expectedName, $actualNames); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public function testWhereNameMatchesWithInvalidPattern(): void |
| 112 | + { |
| 113 | + // Act |
| 114 | + $new = $this->collection->whereNameMatches('/invalid[pattern/'); |
| 115 | + |
| 116 | + // Assert |
| 117 | + self::assertCount(0, $new); |
| 118 | + } |
| 119 | + |
| 120 | + public function testChainedFiltersWorkCorrectly(): void |
| 121 | + { |
| 122 | + // Act |
| 123 | + $result = $this->collection |
| 124 | + ->whereOperatingSystem(OperatingSystem::Linux) |
| 125 | + ->whereArchitecture(Architecture::ARM_64); |
| 126 | + |
| 127 | + // Assert |
| 128 | + self::assertCount(1, $result); |
| 129 | + $asset = $result->first(); |
| 130 | + self::assertSame('package-1.2.3-linux-arm64.tar.gz', $asset->getName()); |
| 131 | + } |
| 132 | + |
| 133 | + public function testFirstReturnsFirstAssetOrNull(): void |
| 134 | + { |
| 135 | + // Act with non-empty collection |
| 136 | + $first = $this->collection->first(); |
| 137 | + |
| 138 | + // Assert |
| 139 | + self::assertNotNull($first); |
| 140 | + self::assertSame('package-1.2.3-linux-x64.tar.gz', $first->getName()); |
| 141 | + |
| 142 | + // Act with empty collection |
| 143 | + $empty = new AssetsCollection([]); |
| 144 | + $result = $empty->first(); |
| 145 | + |
| 146 | + // Assert |
| 147 | + self::assertNull($result); |
| 148 | + } |
| 149 | + |
| 150 | + public function testEmptyReturnsTrueForEmptyCollection(): void |
| 151 | + { |
| 152 | + // Act with non-empty collection |
| 153 | + $resultNonEmpty = $this->collection->empty(); |
| 154 | + |
| 155 | + // Assert |
| 156 | + self::assertFalse($resultNonEmpty); |
| 157 | + |
| 158 | + // Act with empty collection |
| 159 | + $empty = new AssetsCollection([]); |
| 160 | + $resultEmpty = $empty->empty(); |
| 161 | + |
| 162 | + // Assert |
| 163 | + self::assertTrue($resultEmpty); |
| 164 | + } |
| 165 | + |
| 166 | + protected function setUp(): void |
| 167 | + { |
| 168 | + // Arrange |
| 169 | + $this->repository = new RepositoryStub('vendor/package', []); |
| 170 | + $this->release = new ReleaseStub( |
| 171 | + $this->repository, |
| 172 | + '1.2.3', |
| 173 | + 'v1.2.3', |
| 174 | + Stability::Stable, |
| 175 | + [], |
| 176 | + ); |
| 177 | + |
| 178 | + // Create a variety of assets with different characteristics |
| 179 | + $this->assets = [ |
| 180 | + new AssetStub( |
| 181 | + $this->release, |
| 182 | + 'package-1.2.3-linux-x64.tar.gz', |
| 183 | + 'https://example.com/downloads/package-1.2.3-linux-x64.tar.gz', |
| 184 | + OperatingSystem::Linux, |
| 185 | + Architecture::X86_64, |
| 186 | + ), |
| 187 | + new AssetStub( |
| 188 | + $this->release, |
| 189 | + 'package-1.2.3-linux-arm64.tar.gz', |
| 190 | + 'https://example.com/downloads/package-1.2.3-linux-arm64.tar.gz', |
| 191 | + OperatingSystem::Linux, |
| 192 | + Architecture::ARM_64, |
| 193 | + ), |
| 194 | + new AssetStub( |
| 195 | + $this->release, |
| 196 | + 'package-1.2.3-windows-x64.zip', |
| 197 | + 'https://example.com/downloads/package-1.2.3-windows-x64.zip', |
| 198 | + OperatingSystem::Windows, |
| 199 | + Architecture::ARM_64, |
| 200 | + ), |
| 201 | + new AssetStub( |
| 202 | + $this->release, |
| 203 | + 'package-1.2.3-darwin-x64.tar.gz', |
| 204 | + 'https://example.com/downloads/package-1.2.3-darwin-x64.tar.gz', |
| 205 | + OperatingSystem::Darwin, |
| 206 | + Architecture::ARM_64, |
| 207 | + ), |
| 208 | + new AssetStub( |
| 209 | + $this->release, |
| 210 | + 'package-1.2.3-source.zip', |
| 211 | + 'https://example.com/downloads/package-1.2.3-source.zip', |
| 212 | + null, |
| 213 | + null, |
| 214 | + ), |
| 215 | + ]; |
| 216 | + |
| 217 | + $this->collection = new AssetsCollection($this->assets); |
| 218 | + } |
| 219 | +} |
0 commit comments