Skip to content

Commit afb7a5e

Browse files
committed
Add PhpVersionQuestion tests
1 parent f6d11b0 commit afb7a5e

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed

src/Commands/GenerateQuestions/NodeBuildToolQuestion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getAnswer(BaseCommand $command): string
2828

2929
$detected = app(NodeBuildToolDetector::class)->detect();
3030

31-
if ($command->option('detect')) {
31+
if ($detected && $command->option('detect')) {
3232
return $detected;
3333
}
3434

src/Commands/GenerateQuestions/NodePackageManagerQuestion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getAnswer(BaseCommand $command): string|false
2828

2929
$detected = app(NodePackageManagerDetector::class)->detect();
3030

31-
if ($command->option('detect')) {
31+
if ($detected && $command->option('detect')) {
3232
return $detected;
3333
}
3434

src/Integrations/SupportedPhpExtensions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function get(string $phpVersion = null): array
2828
->toArray();
2929
}
3030

31+
/**
32+
* @codeCoverageIgnore this fetches an actual file from GitHub
33+
*/
3134
protected function fetch(): array|false
3235
{
3336
return file(self::URL);

tests/Unit/Commands/GenerateQuestions/NodeBuildToolQuestionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function testItHandlesOptions($expected, $input): void
5151
]);
5252

5353
$answer = app(NodeBuildToolQuestion::class)->getAnswer($mock);
54+
55+
self::assertEquals($expected, $answer);
5456
}
5557

5658
public function provideDetectedBuildTools(): array
@@ -93,7 +95,7 @@ public function provideQuestionInput(): array
9395
public function testItAsksQuestion($expected, $input): void
9496
{
9597
$mock = $this->createMock(BaseCommand::class);
96-
$mock->expects($this->exactly(2))
98+
$mock->expects($this->once())
9799
->method('option')
98100
->willReturnMap([
99101
['node-build-tool', null],

tests/Unit/Commands/GenerateQuestions/NodePackageManagerQuestionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function testItHandlesOptions($expected, $input): void
5151
]);
5252

5353
$answer = app(NodePackageManagerQuestion::class)->getAnswer($mock);
54+
55+
self::assertEquals($expected, $answer);
5456
}
5557

5658
public function provideDetectedPackageManagers(): array
@@ -93,7 +95,7 @@ public function provideQuestionInput(): array
9395
public function testItAsksQuestion($expected, $input): void
9496
{
9597
$mock = $this->createMock(BaseCommand::class);
96-
$mock->expects($this->exactly(2))
98+
$mock->expects($this->once())
9799
->method('option')
98100
->willReturnMap([
99101
['node-package-manager', null],
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)