|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Service; |
| 4 | + |
| 5 | +use App\DTO\Repository; |
| 6 | +use App\Service\LockProcessor; |
| 7 | +use App\Service\RepositoryManager; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +final class LockProcessorTest extends TestCase |
| 11 | +{ |
| 12 | + public function testProcessFileAddsRepositories(): void |
| 13 | + { |
| 14 | + $json = <<<JSON |
| 15 | +{ |
| 16 | + "packages": [ |
| 17 | + { |
| 18 | + "name": "vendor/package1", |
| 19 | + "source": { "url": "https://example.com/repo1.git", "type": "git" } |
| 20 | + } |
| 21 | + ], |
| 22 | + "packages-dev": [ |
| 23 | + { |
| 24 | + "name": "vendor/package2", |
| 25 | + "source": { "url": "https://example.com/repo2.git", "type": "git" } |
| 26 | + } |
| 27 | + ] |
| 28 | +} |
| 29 | +JSON; |
| 30 | + |
| 31 | + $tmpFile = \tmpfile(); |
| 32 | + \fwrite($tmpFile, $json); |
| 33 | + $path = \stream_get_meta_data($tmpFile)['uri']; |
| 34 | + $file = new \SplFileObject($path); |
| 35 | + |
| 36 | + $manager = $this->createMock(RepositoryManager::class); |
| 37 | + $manager->expects($this->once()) |
| 38 | + ->method('addAll') |
| 39 | + ->with($this->callback(function ($repositories) { |
| 40 | + return 2 === \count($repositories) |
| 41 | + && $repositories[0] instanceof Repository |
| 42 | + && 'https://example.com/repo1.git' === $repositories[0]->getUrl() |
| 43 | + && 'git' === $repositories[0]->getType() |
| 44 | + && $repositories[1] instanceof Repository |
| 45 | + && 'https://example.com/repo2.git' === $repositories[1]->getUrl() |
| 46 | + && 'git' === $repositories[1]->getType(); |
| 47 | + })); |
| 48 | + |
| 49 | + $processor = new LockProcessor($manager); |
| 50 | + $processor->processFile($file); |
| 51 | + |
| 52 | + \fclose($tmpFile); |
| 53 | + } |
| 54 | + |
| 55 | + public function testGetRepositoriesFiltersInvalidPackages(): void |
| 56 | + { |
| 57 | + $manager = $this->createMock(RepositoryManager::class); |
| 58 | + $processor = new LockProcessor($manager); |
| 59 | + |
| 60 | + $packages = [ |
| 61 | + (object) [ |
| 62 | + 'name' => 'vendor/valid', |
| 63 | + 'source' => (object) ['url' => 'https://example.com/repo.git', 'type' => 'git'], |
| 64 | + ], |
| 65 | + (object) [ |
| 66 | + 'name' => 'vendor/invalid', |
| 67 | + 'source' => (object) ['url' => '', 'type' => ''], |
| 68 | + ], |
| 69 | + (object) [ |
| 70 | + 'name' => 'vendor/nosource', |
| 71 | + ], |
| 72 | + ]; |
| 73 | + |
| 74 | + $method = new \ReflectionMethod(LockProcessor::class, 'getRepositories'); |
| 75 | + $repositories = $method->invoke($processor, $packages); |
| 76 | + |
| 77 | + $this->assertCount(1, $repositories); |
| 78 | + $this->assertSame('https://example.com/repo.git', $repositories[0]->getUrl()); |
| 79 | + $this->assertSame('git', $repositories[0]->getType()); |
| 80 | + } |
| 81 | +} |
0 commit comments