Skip to content

Commit e281a1c

Browse files
committed
Add feature tests for DockerGenerateCommand
1 parent 6008c1d commit e281a1c

16 files changed

+111
-18
lines changed

src/Commands/DockerGenerateCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public function printConfigurationTable(Configuration $config): void
7777
['Alpine images',
7878
'<comment>'.json_encode($config->isAlpine()).'</comment>',
7979
],
80-
['Node Package Manager',
80+
['Node package manager',
8181
NodePackageManager::name($config->getNodePackageManager()),
8282
],
83-
['Node Build Tool',
83+
['Node build tool',
8484
$config->getNodePackageManager()
8585
? NodeBuildTool::name($config->getNodeBuildTool())
8686
: 'None',
@@ -161,7 +161,6 @@ protected function getOptions(): array
161161
shortcut: 'a',
162162
mode: InputOption::VALUE_NEGATABLE,
163163
description: 'Use Alpine Linux based images',
164-
default: true,
165164
),
166165
new InputOption(
167166
name: 'node-package-manager',

src/Commands/GenerateQuestions/ArtisanOptimizeQuestion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class ArtisanOptimizeQuestion extends BaseQuestion
88
{
99
public function getAnswer(BaseCommand $command): bool
1010
{
11+
if ($command->option('optimize') === false) {
12+
return false;
13+
}
14+
1115
if ($command->option('optimize') || $command->option('detect')) {
1216
return true;
1317
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Feature\Commands;
4+
5+
use BlameButton\LaravelDockerBuilder\Integrations\SupportedPhpExtensions;
6+
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Mockery\MockInterface;
8+
9+
/**
10+
* @covers \BlameButton\LaravelDockerBuilder\Commands\DockerGenerateCommand
11+
*/
12+
class DockerGenerateCommandTest extends TestCase
13+
{
14+
public function provideCommands(): array
15+
{
16+
return [
17+
[
18+
[
19+
"FROM php:8.2-fpm-alpine AS composer\n",
20+
"FROM node:lts-alpine AS node\n",
21+
"COPY /package.json /package-lock.json /app/",
22+
"COPY /vite.config.js /app/\n",
23+
"RUN npm run build\n",
24+
"RUN install-php-extensions bcmath pdo_pgsql redis\n",
25+
"COPY --from=node /app/public/build/ /app/public/build/\n",
26+
"RUN echo \"php artisan optimize --no-ansi && php-fpm\" >> /usr/bin/entrypoint.sh",
27+
"CMD [\"/usr/bin/entrypoint.sh\"]\n"
28+
],
29+
'docker:generate -n -p 8.2 -e bcmath,pdo_pgsql,redis -o -a -m npm -b vite',
30+
],
31+
[
32+
[
33+
"FROM php:8.1-fpm AS composer\n",
34+
"FROM node:lts AS node\n",
35+
"COPY /package.json /yarn.lock /app/",
36+
"COPY /webpack.mix.js /app/\n",
37+
"RUN yarn run production\n",
38+
"RUN install-php-extensions bcmath pdo_mysql apcu\n",
39+
"COPY --from=node /app/public/css/ /app/public/css/\n",
40+
"COPY --from=node /app/public/js/ /app/public/js/\n",
41+
"COPY --from=node /app/public/fonts/ /app/public/fonts/\n",
42+
"RUN echo \"php-fpm\" >> /usr/bin/entrypoint.sh",
43+
"CMD [\"/usr/bin/entrypoint.sh\"]\n"
44+
],
45+
'docker:generate -n -p 8.1 -e bcmath,pdo_mysql,apcu --no-optimize --no-alpine -m yarn -b mix',
46+
],
47+
];
48+
}
49+
50+
/** @dataProvider provideCommands */
51+
public function testItGeneratesConfigurations(array $expected, string $command): void
52+
{
53+
$this->artisan($command);
54+
55+
$contents = file_get_contents(base_path('.docker/php.dockerfile'));
56+
57+
foreach ($expected as $assertion) {
58+
self::assertStringContainsString($assertion, $contents);
59+
}
60+
}
61+
62+
public function testItAsksQuestions(): void
63+
{
64+
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
65+
$mock->shouldReceive('fetch')->with('8.2')->once()->andReturn(['bcmath', 'redis']);
66+
$mock->shouldReceive('fetch')->with(null)->andReturn(['not the same as with 8.2']);
67+
});
68+
69+
$command = $this->artisan('docker:generate');
70+
$command->expectsChoice('PHP version', '8.2', ['8.2', '8.1', '8.0']);
71+
$command->expectsChoice('PHP extensions', ['bcmath', 'redis'], ['bcmath', 'redis']);
72+
$command->expectsConfirmation('Do you want to run "php artisan optimize" when the image boots?', 'yes');
73+
$command->expectsConfirmation('Do you want to use "Alpine Linux" based images?', 'yes');
74+
$command->expectsChoice('Which Node package manager do you use?', 'npm', ['npm', 'yarn', 'none']);
75+
$command->expectsChoice('Which Node build tool do you use?', 'vite', ['vite', 'mix']);
76+
$command->expectsConfirmation('Does this look correct?', 'yes');
77+
$command->expectsOutput('Configuration:');
78+
$command->expectsTable(['Key', 'Value'], [
79+
['PHP version', '8.2'],
80+
['PHP extensions', 'bcmath, redis'],
81+
['Artisan Optimize', 'true'],
82+
['Alpine images', 'true'],
83+
['Node package manager', 'NPM'],
84+
['Node build tool', 'Vite.js'],
85+
]);
86+
$command->expectsOutput('Command to generate above configuration:');
87+
$command->expectsOutput(' php artisan docker:generate -n -p 8.2 -e bcmath,redis -o -a -m npm -b vite');
88+
}
89+
}

tests/Commands/BaseDockerCommandTest.php renamed to tests/Unit/Commands/BaseDockerCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands;
44

55
use BlameButton\LaravelDockerBuilder\Commands\BaseDockerCommand;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;

tests/Commands/DockerBuildCommandTest.php renamed to tests/Unit/Commands/DockerBuildCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands;
44

55
use BlameButton\LaravelDockerBuilder\Commands\DockerBuildCommand;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;

tests/Commands/DockerPushCommandTest.php renamed to tests/Unit/Commands/DockerPushCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands;
44

55
use BlameButton\LaravelDockerBuilder\Commands\DockerPushCommand;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;

tests/Commands/GenerateQuestions/AlpineQuestionTest.php renamed to tests/Unit/Commands/GenerateQuestions/AlpineQuestionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands\GenerateQuestions;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands\GenerateQuestions;
44

55
use BlameButton\LaravelDockerBuilder\Commands\BaseCommand;
66
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\AlpineQuestion;

tests/Commands/GenerateQuestions/ArtisanOptimizeQuestionTest.php renamed to tests/Unit/Commands/GenerateQuestions/ArtisanOptimizeQuestionTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands\GenerateQuestions;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands\GenerateQuestions;
44

55
use BlameButton\LaravelDockerBuilder\Commands\BaseCommand;
66
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\ArtisanOptimizeQuestion;
@@ -18,6 +18,7 @@ private function provideOptions(): array
1818
return [
1919
'it returns true when detect is true' => [true, null, true],
2020
'it returns true when optimize is true' => [true, true, null],
21+
'it returns false when optimize is false and detect is true' => [false, false, true],
2122
'it returns true when optimize and detect are true' => [true, true, true],
2223
];
2324
}
@@ -26,7 +27,7 @@ private function provideOptions(): array
2627
public function testItHandlesOptionsCorrectly($expected, $optimize, $detect): void
2728
{
2829
$mock = $this->createMock(BaseCommand::class);
29-
$mock->expects($this->atMost(2))
30+
$mock->expects($this->atMost(3))
3031
->method('option')
3132
->willReturnMap([
3233
['optimize', $optimize],
@@ -53,7 +54,7 @@ public function testItHandlesAnswersCorrectly($expected, $input): void
5354
{
5455
$stub = $this->createStub(BaseCommand::class);
5556
$stub->method('option')
56-
->willReturn(false);
57+
->willReturn(null);
5758
$stub->method('confirm')
5859
->willReturn($input);
5960

tests/Commands/GenerateQuestions/Choices/NodeBuildToolTest.php renamed to tests/Unit/Commands/GenerateQuestions/Choices/NodeBuildToolTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands\GenerateQuestions\Choices;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands\GenerateQuestions\Choices;
44

55
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\NodeBuildTool;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;

tests/Commands/GenerateQuestions/Choices/NodePackageManagerTest.php renamed to tests/Unit/Commands/GenerateQuestions/Choices/NodePackageManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BlameButton\LaravelDockerBuilder\Tests\Commands\GenerateQuestions\Choices;
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Commands\GenerateQuestions\Choices;
44

55
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\NodePackageManager;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;

0 commit comments

Comments
 (0)