Skip to content

Commit 7777b83

Browse files
committed
Add tests for docker ci command
1 parent af89390 commit 7777b83

File tree

5 files changed

+159
-9
lines changed

5 files changed

+159
-9
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"twig/twig": "^3.0"
3131
},
3232
"require-dev": {
33+
"brianium/paratest": "^6.10",
3334
"laravel/pint": "^1.4",
35+
"nunomaduro/collision": "^6.2",
3436
"orchestra/testbench": "^7.19"
3537
},
3638
"autoload": {
@@ -59,6 +61,6 @@
5961
}
6062
},
6163
"scripts": {
62-
"coverage": "php -d pcov.enabled=1 -d pcov.directory=src -d pcov.exclude='~vendor~' vendor/bin/phpunit"
64+
"coverage": "XDEBUG_MODE=coverage php -d pcov.enabled=1 -d pcov.directory=src -d pcov.exclude='~vendor~' vendor/bin/testbench package:test --parallel --coverage"
6365
}
6466
}
File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: "CI"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
docker:
13+
name: Docker Build & Deploy
14+
runs-on: ubuntu-latest

src/Commands/DockerCiCommand.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,46 @@ public function handle(): int
2727

2828
$detected = app(CiPlatformDetector::class)->detect();
2929

30-
return self::SUCCESS;
30+
if ($detected) {
31+
return $this->copy($detected);
32+
}
33+
34+
$this->warn('Unfortunately, no CI platform could be detected.');
35+
$this->warn('Please use the [ci-platform] argument to manually define a supported platform.');
36+
37+
return self::FAILURE;
3138
}
3239

3340
protected function copy(string $platform): int
3441
{
35-
if (CiPlatform::GITLAB_CI === $platform) {
42+
if (CiPlatform::GITHUB_ACTIONS === $platform) {
43+
$template = package_path('resources/templates/ci-platforms/github-workflow.yml');
44+
$output = base_path('.github/workflows/ci.yml');
45+
} elseif (CiPlatform::GITLAB_CI === $platform) {
46+
$template = package_path('resources/templates/ci-platforms/.gitlab-ci.yml');
3647
$output = base_path('.gitlab-ci.yml');
48+
} else {
49+
$this->error('Invalid platform passed to '.__METHOD__.' this should never happen.');
3750

38-
if (File::isFile($output)) {
39-
$this->info('Detected GitLab, but [.gitlab-ci.yml] file already exists.');
51+
return self::INVALID;
52+
}
4053

41-
return self::SUCCESS;
42-
}
54+
$fromBasePath = str($output)->after(base_path().'/')->value();
4355

44-
$this->info(sprintf('Detected GitLab, copying [.gitlab-ci.yml] to [%s].', dirname($output)));
56+
if (File::isFile($output)) {
57+
$this->info(sprintf(
58+
'Using [%s], but [%s] file already exists.',
59+
CiPlatform::name($platform),
60+
$fromBasePath,
61+
));
4562

46-
File::copy(package_path('resources/templates/.gitlab-ci.yml'), $output);
63+
return self::SUCCESS;
4764
}
4865

66+
$this->info(sprintf('Using [%s], copying [%s] to [%s].', CiPlatform::name($platform), $fromBasePath, dirname($output)));
67+
68+
File::copy($template, $output);
69+
4970
return self::SUCCESS;
5071
}
5172

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Feature\Commands;
4+
5+
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform;
6+
use BlameButton\LaravelDockerBuilder\Detectors\CiPlatformDetector;
7+
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
8+
use Illuminate\Support\Facades\File;
9+
use Mockery\MockInterface;
10+
use Symfony\Component\Console\Command\Command;
11+
12+
/**
13+
* @uses \BlameButton\LaravelDockerBuilder\Commands\DockerGenerateCommand::getOptions()
14+
* @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform::name()
15+
* @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform::values()
16+
* @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\NodeBuildTool::values()
17+
* @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\NodePackageManager::values()
18+
* @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\PhpExtensions::values()
19+
* @uses \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\PhpVersion::values()
20+
* @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider
21+
* @uses \BlameButton\LaravelDockerBuilder\Integrations\SupportedPhpExtensions
22+
*
23+
* @covers \BlameButton\LaravelDockerBuilder\Commands\DockerCiCommand
24+
*/
25+
class DockerCiCommandTest extends TestCase
26+
{
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
File::delete(base_path('.gitlab-ci.yml'));
32+
File::deleteDirectory(base_path('.github/'));
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
File::delete(base_path('.gitlab-ci.yml'));
38+
File::deleteDirectory(base_path('.github/'));
39+
40+
parent::tearDown();
41+
}
42+
43+
public function testItChecksIfGitlabFileExists(): void
44+
{
45+
touch(base_path('.gitlab-ci.yml'));
46+
47+
$this->artisan('docker:ci gitlab')
48+
->expectsOutput('Using [GitLab CI/CD], but [.gitlab-ci.yml] file already exists.')
49+
->assertSuccessful();
50+
}
51+
52+
public function testItChecksIfGithubFileExists(): void
53+
{
54+
File::ensureDirectoryExists(base_path('.github/workflows'));
55+
touch(base_path('.github/workflows/ci.yml'));
56+
57+
$this->artisan('docker:ci github')
58+
->expectsOutput('Using [GitHub Actions], but [.github/workflows/ci.yml] file already exists.')
59+
->assertSuccessful();
60+
}
61+
62+
public function testItUsesCiPlatformDetector(): void
63+
{
64+
$this->mock(CiPlatformDetector::class, function (MockInterface $mock) {
65+
$mock->expects('detect')
66+
->once()
67+
->withNoArgs()
68+
->andReturn(CiPlatform::GITLAB_CI);
69+
});
70+
71+
$this->artisan('docker:ci')
72+
->expectsOutput(sprintf('Using [GitLab CI/CD], copying [.gitlab-ci.yml] to [%s].', base_path()))
73+
->assertSuccessful();
74+
75+
$this->assertFileExists(base_path('.gitlab-ci.yml'));
76+
}
77+
78+
public function testItTellsUserToManuallyPassAPlatform(): void
79+
{
80+
$this->mock(CiPlatformDetector::class, function (MockInterface $mock) {
81+
$mock->expects('detect')
82+
->once()
83+
->withNoArgs()
84+
->andReturn(false);
85+
});
86+
87+
$this->artisan('docker:ci')
88+
->expectsOutput('Unfortunately, no CI platform could be detected.')
89+
->expectsOutput('Please use the [ci-platform] argument to manually define a supported platform.')
90+
->assertFailed();
91+
}
92+
93+
public function testItThrowsErrorWhenDetectorReturnsInvalid(): void
94+
{
95+
$this->mock(CiPlatformDetector::class, function (MockInterface $mock) {
96+
$mock->expects('detect')
97+
->once()
98+
->withNoArgs()
99+
->andReturn('nonsense');
100+
});
101+
102+
$this->artisan('docker:ci')
103+
->expectsOutput('Invalid platform passed to BlameButton\LaravelDockerBuilder\Commands\DockerCiCommand::copy this should never happen.')
104+
->assertExitCode(Command::INVALID);
105+
}
106+
107+
public function testItErrorsOnInvalidArgument(): void
108+
{
109+
$this->artisan('docker:ci nonsense')
110+
->expectsOutput('Invalid value [nonsense] for argument [ci-platform].')
111+
->assertFailed();
112+
}
113+
}

0 commit comments

Comments
 (0)