|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands\GenerateQuestions; |
| 4 | + |
| 5 | +use BlameButton\LaravelDockerBuilder\Commands\BaseCommand; |
| 6 | +use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\PhpVersion; |
| 7 | +use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\PhpVersionQuestion; |
| 8 | +use BlameButton\LaravelDockerBuilder\Detectors\PhpVersionDetector; |
| 9 | +use BlameButton\LaravelDockerBuilder\Exceptions\InvalidOptionValueException; |
| 10 | +use BlameButton\LaravelDockerBuilder\Tests\TestCase; |
| 11 | +use Mockery\MockInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider |
| 15 | + * @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\PhpVersion |
| 16 | + * |
| 17 | + * @covers \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\PhpVersionQuestion |
| 18 | + */ |
| 19 | +class PhpVersionQuestionTest extends TestCase |
| 20 | +{ |
| 21 | + public function testItThrowsErrorOnInvalidInput(): void |
| 22 | + { |
| 23 | + $mock = $this->createMock(BaseCommand::class); |
| 24 | + $mock->expects($this->once()) |
| 25 | + ->method('option') |
| 26 | + ->willReturnMap([ |
| 27 | + ['php-version', 'invalid-value'], |
| 28 | + ]); |
| 29 | + |
| 30 | + $this->expectException(InvalidOptionValueException::class); |
| 31 | + |
| 32 | + app(PhpVersionQuestion::class)->getAnswer($mock); |
| 33 | + } |
| 34 | + |
| 35 | + private function provideOptions(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + '8.2' => [PhpVersion::v8_2, '8.2'], |
| 39 | + '8.1' => [PhpVersion::v8_1, '8.1'], |
| 40 | + '8.0' => [PhpVersion::v8_0, '8.0'], |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** @dataProvider provideOptions */ |
| 45 | + public function testItHandlesOptions($expected, $input): void |
| 46 | + { |
| 47 | + $mock = $this->createMock(BaseCommand::class); |
| 48 | + $mock->expects($this->once()) |
| 49 | + ->method('option') |
| 50 | + ->willReturnMap([ |
| 51 | + ['php-version', $input], |
| 52 | + ]); |
| 53 | + |
| 54 | + $answer = app(PhpVersionQuestion::class)->getAnswer($mock); |
| 55 | + |
| 56 | + self::assertEquals($expected, $answer); |
| 57 | + } |
| 58 | + |
| 59 | + public function provideDetected(): array |
| 60 | + { |
| 61 | + return [ |
| 62 | + '8.2' => [PhpVersion::v8_2, '8.2'], |
| 63 | + '8.1' => [PhpVersion::v8_1, '8.1'], |
| 64 | + '8.0' => [PhpVersion::v8_0, '8.0'], |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + /** @dataProvider provideDetected */ |
| 69 | + public function testItDetectsPackageManagers($expected, $detected): void |
| 70 | + { |
| 71 | + $mock = $this->createMock(BaseCommand::class); |
| 72 | + $mock->expects($this->exactly(2)) |
| 73 | + ->method('option') |
| 74 | + ->willReturnMap([ |
| 75 | + ['php-version', null], |
| 76 | + ['detect', true], |
| 77 | + ]); |
| 78 | + |
| 79 | + $this->mock(PhpVersionDetector::class, function (MockInterface $mock) use ($detected) { |
| 80 | + $mock->shouldReceive('detect')->once()->andReturn($detected); |
| 81 | + }); |
| 82 | + |
| 83 | + $answer = app(PhpVersionQuestion::class)->getAnswer($mock); |
| 84 | + |
| 85 | + self::assertEquals($expected, $answer); |
| 86 | + } |
| 87 | + |
| 88 | + public function provideQuestionInput(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + '8.2' => ['8.2', '8.2'], |
| 92 | + '8.1' => ['8.1', '8.1'], |
| 93 | + '8.0' => ['8.0', '8.0'], |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + /** @dataProvider provideQuestionInput */ |
| 98 | + public function testItAsksQuestion($expected, $input): void |
| 99 | + { |
| 100 | + $mock = $this->createMock(BaseCommand::class); |
| 101 | + $mock->expects($this->exactly(1)) |
| 102 | + ->method('option') |
| 103 | + ->willReturnMap([ |
| 104 | + ['php-version', null], |
| 105 | + ['detect', false], |
| 106 | + ]); |
| 107 | + $mock->expects($this->once()) |
| 108 | + ->method('choice') |
| 109 | + ->willReturn($input); |
| 110 | + |
| 111 | + $this->mock(PhpVersionDetector::class, function (MockInterface $mock) { |
| 112 | + $mock->shouldReceive('detect')->once()->andReturn(false); |
| 113 | + }); |
| 114 | + |
| 115 | + $answer = app(PhpVersionQuestion::class)->getAnswer($mock); |
| 116 | + |
| 117 | + self::assertEquals($expected, $answer); |
| 118 | + } |
| 119 | +} |
0 commit comments