|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Detectors; |
| 4 | + |
| 5 | +use BlameButton\LaravelDockerBuilder\Detectors\PhpVersionDetector; |
| 6 | +use BlameButton\LaravelDockerBuilder\Tests\TestCase; |
| 7 | +use Composer\Semver\VersionParser; |
| 8 | +use Mockery\MockInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider |
| 12 | + * @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\PhpVersion |
| 13 | + * |
| 14 | + * @covers \BlameButton\LaravelDockerBuilder\Detectors\PhpVersionDetector |
| 15 | + */ |
| 16 | +class PhpVersionDetectorTest extends TestCase |
| 17 | +{ |
| 18 | + public function testItReturnsFalseWhenNoComposerFileWasFound(): void |
| 19 | + { |
| 20 | + $this->partialMock(PhpVersionDetector::class, function (MockInterface $mock) { |
| 21 | + $mock->shouldReceive('getComposerFileContents') |
| 22 | + ->once() |
| 23 | + ->andReturn(false); |
| 24 | + }); |
| 25 | + |
| 26 | + $detected = app(PhpVersionDetector::class)->detect(); |
| 27 | + |
| 28 | + self::assertFalse($detected); |
| 29 | + } |
| 30 | + |
| 31 | + public function testItReturnsFalseWhenNoPhpVersionWasFound(): void |
| 32 | + { |
| 33 | + $this->partialMock(PhpVersionDetector::class, function (MockInterface $mock) { |
| 34 | + $mock->shouldReceive('getComposerFileContents') |
| 35 | + ->once() |
| 36 | + ->andReturn('{ "require": {} }'); |
| 37 | + }); |
| 38 | + |
| 39 | + $detected = app(PhpVersionDetector::class)->detect(); |
| 40 | + |
| 41 | + self::assertFalse($detected); |
| 42 | + } |
| 43 | + |
| 44 | + public function provideVersions(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + ['8.2', '^8.2'], |
| 48 | + ['8.2', '>=8.2'], |
| 49 | + ['8.1', '~8.1'], |
| 50 | + ['8.1', '8.1.*'], |
| 51 | + ['8.0', '^8.0.2'], |
| 52 | + ['8.0', '8.0.24'], |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + /** @dataProvider provideVersions */ |
| 57 | + public function testItParsesJsonVersion($expected, string $version): void |
| 58 | + { |
| 59 | + $this->partialMock(PhpVersionDetector::class, function (MockInterface $mock) use ($version) { |
| 60 | + $mock->shouldReceive('getComposerFileContents') |
| 61 | + ->once() |
| 62 | + ->andReturn(sprintf('{ "require": { "php": "%s" } }', $version)); |
| 63 | + }); |
| 64 | + |
| 65 | + $this->partialMock(VersionParser::class, function (MockInterface $mock) use ($version) { |
| 66 | + $mock->shouldReceive('parseConstraints') |
| 67 | + ->with($version) |
| 68 | + ->once() |
| 69 | + ->passthru(); |
| 70 | + }); |
| 71 | + |
| 72 | + $detected = app(PhpVersionDetector::class)->detect(); |
| 73 | + |
| 74 | + self::assertEquals($expected, $detected); |
| 75 | + } |
| 76 | +} |
0 commit comments