Skip to content

Commit 03878f7

Browse files
committed
Update tests to use filesystem
1 parent 8f51217 commit 03878f7

11 files changed

+78
-57
lines changed

src/Commands/DockerCiCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform;
66
use BlameButton\LaravelDockerBuilder\Detectors\CiPlatformDetector;
7+
use Illuminate\Support\Facades\File;
78
use Symfony\Component\Console\Input\InputArgument;
89

910
class DockerCiCommand extends BaseCommand
@@ -34,15 +35,15 @@ protected function copy(string $platform): int
3435
if (CiPlatform::GITLAB_CI === $platform) {
3536
$output = base_path('.gitlab-ci.yml');
3637

37-
if (file_exists($output)) {
38+
if (File::isFile($output)) {
3839
$this->info('Detected GitLab, but [.gitlab-ci.yml] file already exists.');
3940

4041
return self::SUCCESS;
4142
}
4243

4344
$this->info(sprintf('Detected GitLab, copying [.gitlab-ci.yml] to [%s].', dirname($output)));
4445

45-
copy(package_path('resources/templates/.gitlab-ci.yml'), $output);
46+
File::copy(package_path('resources/templates/.gitlab-ci.yml'), $output);
4647
}
4748

4849
return self::SUCCESS;

src/Commands/DockerGenerateCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BlameButton\LaravelDockerBuilder\Exceptions\InvalidOptionValueException;
1616
use BlameButton\LaravelDockerBuilder\Objects\Configuration;
1717
use BlameButton\LaravelDockerBuilder\Traits\InteractsWithTwig;
18+
use Illuminate\Support\Facades\File;
1819
use Symfony\Component\Console\Input\InputOption;
1920

2021
class DockerGenerateCommand extends BaseCommand
@@ -115,7 +116,10 @@ private function saveDockerfiles(Configuration $config): void
115116
$dockerfile = sprintf('%s/%s', $dir, $file);
116117

117118
// Save Dockerfile contents
118-
file_put_contents($dockerfile, $content);
119+
File::put(
120+
path: $dockerfile,
121+
contents: $content,
122+
);
119123

120124
// Output saved Dockerfile location
121125
$filename = str($dockerfile)->after(base_path())->trim('/');

src/Detectors/CiPlatformDetector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BlameButton\LaravelDockerBuilder\Detectors;
44

55
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform;
6+
use Illuminate\Support\Facades\File;
67
use Illuminate\Support\Str;
78

89
class CiPlatformDetector implements DetectorContract
@@ -11,11 +12,11 @@ public function detect(): string|false
1112
{
1213
$config = base_path('.git/config');
1314

14-
if (! file_exists($config)) {
15+
if (File::missing($config)) {
1516
return false;
1617
}
1718

18-
$config = file_get_contents($config);
19+
$config = File::get($config);
1920

2021
if (Str::contains($config, 'gitlab')) {
2122
return CiPlatform::GITLAB_CI;

src/Detectors/FileDetector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace BlameButton\LaravelDockerBuilder\Detectors;
44

5+
use Illuminate\Support\Facades\File;
6+
57
abstract class FileDetector implements DetectorContract
68
{
79
public function detect(): string|false
810
{
911
foreach ($this->getPathMapping() as $file => $tool) {
10-
if (file_exists($file)) {
12+
if (File::isFile($file)) {
1113
return $tool;
1214
}
1315
}

src/Detectors/PhpVersionDetector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\PhpVersion;
66
use Composer\Semver\VersionParser;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Facades\File;
89
use Illuminate\Support\Str;
910

1011
class PhpVersionDetector implements DetectorContract
@@ -36,6 +37,10 @@ public function detect(): string|false
3637

3738
public function getComposerFileContents(): string|false
3839
{
39-
return file_get_contents(base_path('composer.json'));
40+
if (File::missing($path = base_path('composer.json'))) {
41+
return false;
42+
}
43+
44+
return File::get($path);
4045
}
4146
}

src/Integrations/SupportedPhpExtensions.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace BlameButton\LaravelDockerBuilder\Integrations;
44

55
use Illuminate\Support\Facades\Http;
6-
use function Symfony\Component\String\b;
76

87
class SupportedPhpExtensions
98
{
@@ -13,7 +12,7 @@ class SupportedPhpExtensions
1312

1413
public function get(string $phpVersion = null): array
1514
{
16-
if (!is_null($this->cache)) {
15+
if (! is_null($this->cache)) {
1716
return $this->cache;
1817
}
1918

@@ -24,8 +23,8 @@ public function get(string $phpVersion = null): array
2423
}
2524

2625
return $this->cache = collect($contents)
27-
->filter(fn(string $extension): bool => is_null($phpVersion) || str($extension)->contains($phpVersion))
28-
->map(fn(string $extension): string => str($extension)->trim()->before(' '))
26+
->filter(fn (string $extension): bool => is_null($phpVersion) || str($extension)->contains($phpVersion))
27+
->map(fn (string $extension): string => str($extension)->trim()->before(' '))
2928
->filter()
3029
->values()
3130
->toArray();
@@ -34,7 +33,7 @@ public function get(string $phpVersion = null): array
3433
public function fetch(): array|false
3534
{
3635
$response = rescue(
37-
callback: fn() => Http::get(self::URL),
36+
callback: fn () => Http::get(self::URL),
3837
rescue: false,
3938
);
4039

tests/Feature/Commands/DockerGenerateCommandTest.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Integrations\SupportedPhpExtensions;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Illuminate\Support\Facades\File;
78
use Mockery\MockInterface;
89

910
/**
@@ -71,6 +72,8 @@ public function provideCommands(): array
7172
/** @dataProvider provideCommands */
7273
public function testItGeneratesConfigurations(array $expected, string $command): void
7374
{
75+
File::deleteDirectory(base_path('.docker'));
76+
7477
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
7578
$mock->shouldReceive('get')->withAnyArgs()->andReturn([
7679
'bcmath', 'pdo_mysql', 'pdo_pgsql', 'redis', 'apcu',
@@ -86,7 +89,16 @@ public function testItGeneratesConfigurations(array $expected, string $command):
8689
}
8790
}
8891

89-
public function testItAsksQuestions(): void
92+
public function provideIsInformationCorrectAnswer(): array
93+
{
94+
return [
95+
['yes'],
96+
['no'],
97+
];
98+
}
99+
100+
/** @dataProvider provideIsInformationCorrectAnswer */
101+
public function testItAsksQuestions(string $isCorrect): void
90102
{
91103
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
92104
$mock->shouldReceive('get')->with('8.2')->once()->andReturn(['bcmath', 'redis']);
@@ -100,18 +112,23 @@ public function testItAsksQuestions(): void
100112
$command->expectsConfirmation('Do you want to use "Alpine Linux" based images?', 'yes');
101113
$command->expectsChoice('Which Node package manager do you use?', 'npm', ['npm', 'yarn', 'none']);
102114
$command->expectsChoice('Which Node build tool do you use?', 'vite', ['vite', 'mix']);
103-
$command->expectsConfirmation('Does this look correct?', 'yes');
104-
$command->expectsOutput('Configuration:');
105-
$command->expectsTable(['Key', 'Value'], [
106-
['PHP version', '8.2'],
107-
['PHP extensions', 'bcmath, redis'],
108-
['Artisan Optimize', 'true'],
109-
['Alpine images', 'true'],
110-
['Node package manager', 'NPM'],
111-
['Node build tool', 'Vite.js'],
112-
]);
113-
$command->expectsOutput('Command to generate above configuration:');
114-
$command->expectsOutput(' php artisan docker:generate -n -p 8.2 -e bcmath,redis -o -a -m npm -b vite');
115+
$command->expectsConfirmation('Does this look correct?', $isCorrect);
116+
if ($isCorrect == 'yes') {
117+
$command->expectsOutput('Configuration:');
118+
$command->expectsTable(['Key', 'Value'], [
119+
['PHP version', '8.2'],
120+
['PHP extensions', 'bcmath, redis'],
121+
['Artisan Optimize', 'true'],
122+
['Alpine images', 'true'],
123+
['Node package manager', 'NPM'],
124+
['Node build tool', 'Vite.js'],
125+
]);
126+
$command->expectsOutput('Command to generate above configuration:');
127+
$command->expectsOutput(' php artisan docker:generate -n -p 8.2 -e bcmath,redis -o -a -m npm -b vite');
128+
} else {
129+
$command->expectsOutput('Exiting.');
130+
}
131+
$command->assertSuccessful();
115132
}
116133

117134
public function provideInvalidOptions(): array

tests/Unit/Detectors/CiPlatformDetectorTest.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Detectors\CiPlatformDetector;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Illuminate\Support\Facades\File;
78

89
/**
910
* @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider
@@ -16,33 +17,22 @@ protected function setUp(): void
1617
{
1718
parent::setUp();
1819

19-
if (! is_dir($path = base_path('.git'))) {
20-
mkdir($path);
21-
}
22-
23-
if (file_exists($path = base_path('.git/config'))) {
24-
unlink($path);
25-
}
20+
File::ensureDirectoryExists(base_path('.git'));
21+
File::delete(base_path('.git/config'));
2622
}
2723

2824
protected function tearDown(): void
2925
{
3026
parent::tearDown();
3127

32-
if (file_exists($path = base_path('.git/config'))) {
33-
unlink($path);
34-
}
35-
36-
if (is_dir($path = base_path('.git'))) {
37-
rmdir($path);
38-
}
28+
File::deleteDirectory(base_path('.git'));
3929
}
4030

4131
public function testItDetectsGitHub(): void
4232
{
43-
file_put_contents(
44-
filename: base_path('.git/config'),
45-
data: "[remote \"origin\"]\n\turl = git@github.com:blamebutton/laravel-docker-builder.git",
33+
File::put(
34+
path: base_path('.git/config'),
35+
contents: "[remote \"origin\"]\n\turl = git@github.com:blamebutton/laravel-docker-builder.git",
4636
);
4737

4838
$detected = app(CiPlatformDetector::class)->detect();
@@ -52,9 +42,9 @@ public function testItDetectsGitHub(): void
5242

5343
public function testItDetectsGitLab(): void
5444
{
55-
file_put_contents(
56-
filename: base_path('.git/config'),
57-
data: "[remote \"origin\"]\n\turl = git@gitlab.com:blamebutton/laravel-docker-builder.git",
45+
File::put(
46+
path: base_path('.git/config'),
47+
contents: "[remote \"origin\"]\n\turl = git@gitlab.com:blamebutton/laravel-docker-builder.git",
5848
);
5949

6050
$detected = app(CiPlatformDetector::class)->detect();
@@ -64,9 +54,9 @@ public function testItDetectsGitLab(): void
6454

6555
public function testItReturnsFalseWithNoMatches(): void
6656
{
67-
file_put_contents(
68-
filename: base_path('.git/config'),
69-
data: "[remote \"origin\"]\n\turl = git@bitbucket.com:blamebutton/laravel-docker-builder.git",
57+
File::put(
58+
path: base_path('.git/config'),
59+
contents: "[remote \"origin\"]\n\turl = git@bitbucket.com:blamebutton/laravel-docker-builder.git",
7060
);
7161

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

tests/Unit/Detectors/NodeBuildToolDetectorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Detectors\NodeBuildToolDetector;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Illuminate\Support\Facades\File;
78

89
/**
910
* @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider
@@ -17,10 +18,11 @@ protected function setUp(): void
1718
{
1819
parent::setUp();
1920

20-
collect(['vite.config.js', 'vite.config.ts', 'webpack.mix.js'])
21-
->map(fn ($file) => base_path($file))
22-
->filter(fn ($file) => file_exists($file))
23-
->each(fn ($file) => unlink($file));
21+
File::delete([
22+
base_path('vite.config.js'),
23+
base_path('vite.config.ts'),
24+
base_path('webpack.mix.js'),
25+
]);
2426
}
2527

2628
public function providePathMappings(): array

tests/Unit/Detectors/NodePackageManagerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Detectors\NodePackageManagerDetector;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Illuminate\Support\Facades\File;
78

89
/**
910
* @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider
@@ -17,10 +18,10 @@ protected function setUp(): void
1718
{
1819
parent::setUp();
1920

20-
collect(['package-lock.json', 'yarn.lock'])
21-
->map(fn ($file) => base_path($file))
22-
->filter(fn ($file) => file_exists($file))
23-
->each(fn ($file) => unlink($file));
21+
File::delete([
22+
base_path('package-lock.json'),
23+
base_path('yarn.lock'),
24+
]);
2425
}
2526

2627
public function providePathMappings(): array

tests/Unit/Detectors/PhpExtensionsDetectorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public function testItDetectsExtensionsWithoutDuplicates(string $expected, array
133133
$detected = app(PhpExtensionsDetector::class)->detect();
134134

135135
self::assertEquals($expected, $detected);
136-
137136
}
138137

139138
public function testItReturnsDefaultExtensions(): void

0 commit comments

Comments
 (0)