Skip to content

Commit 746e63b

Browse files
authored
Merge pull request #39 from im-ryan/chore/full-test-coverage
chore: 100% test coverage
2 parents 0b60a38 + 3f8146d commit 746e63b

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

src/Commands/EssentialsPintCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace NunoMaduro\Essentials\Commands;
66

77
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\File;
89

910
final class EssentialsPintCommand extends Command
1011
{
@@ -36,20 +37,20 @@ public function handle(): int
3637
$stub_path = __DIR__.'/../../stubs/pint.stub';
3738
$destination_path = base_path('pint.json');
3839

39-
if (! file_exists($stub_path)) {
40+
if (! File::exists($stub_path)) {
4041
$this->components->error('Pint configuration stub file not found.');
4142

4243
return 1;
4344
}
4445

45-
if (file_exists($destination_path) && $this->option('backup')) {
46-
copy($destination_path, $destination_path.'.backup');
46+
if (File::exists($destination_path) && $this->option('backup')) {
47+
File::copy($destination_path, $destination_path.'.backup');
4748
$this->components->info('Backup created at: '.$destination_path.'.backup');
4849
}
4950

5051
$this->components->info('Publishing Pint configuration file...');
5152

52-
if (! copy($stub_path, $destination_path)) {
53+
if (! File::copy($stub_path, $destination_path)) {
5354
$this->components->error('Failed to publish the Pint configuration file.');
5455

5556
return 1;

src/Commands/EssentialsRectorCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace NunoMaduro\Essentials\Commands;
66

77
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\File;
89

910
final class EssentialsRectorCommand extends Command
1011
{
@@ -36,20 +37,20 @@ public function handle(): int
3637
$stub_path = __DIR__.'/../../stubs/rector.stub';
3738
$destination_path = base_path('rector.php');
3839

39-
if (! file_exists($stub_path)) {
40+
if (! File::exists($stub_path)) {
4041
$this->components->error('Rector configuration stub file not found.');
4142

4243
return 1;
4344
}
4445

45-
if (file_exists($destination_path) && $this->option('backup')) {
46-
copy($destination_path, $destination_path.'.backup');
46+
if (File::exists($destination_path) && $this->option('backup')) {
47+
File::copy($destination_path, $destination_path.'.backup');
4748
$this->components->info('Backup created at: '.$destination_path.'.backup');
4849
}
4950

5051
$this->components->info('Publishing Rector configuration file...');
5152

52-
if (! copy($stub_path, $destination_path)) {
53+
if (! File::copy($stub_path, $destination_path)) {
5354
$this->components->error('Failed to publish the Rector configuration file.');
5455

5556
return 1;

tests/Commands/EssentialsPintCommandTest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
use Illuminate\Support\Facades\File;
6-
use NunoMaduro\Essentials\Commands\EssentialsPintCommand;
76

87
beforeEach(function (): void {
98
// Clean up any existing pint.json files
@@ -16,13 +15,33 @@
1615
}
1716
});
1817

19-
it('publishes pint configuration file', function (): void {
20-
$command = new EssentialsPintCommand;
21-
18+
it('publishes pint configuration file without a backup by default', function (): void {
2219
$this->artisan('essentials:pint', ['--force' => true])
2320
->assertExitCode(0);
2421

2522
expect(file_exists(base_path('pint.json')))->toBeTrue();
23+
expect(file_exists(base_path('pint.json.backup')))->toBeFalse();
24+
});
25+
26+
it('returns error when pint configuration file does not exist', function (): void {
27+
// The stub file should not exist
28+
File::shouldReceive('exists')
29+
->once()
30+
->andReturnFalse();
31+
32+
$this->artisan('essentials:pint', ['--force' => true])
33+
->assertExitCode(1);
34+
});
35+
36+
it('returns error when copy operation fails', function (): void {
37+
// The file should exist but not be copyable
38+
File::shouldReceive('exists')->andReturnTrue();
39+
File::shouldReceive('copy')
40+
->once()
41+
->andReturnFalse();
42+
43+
$this->artisan('essentials:pint', ['--force' => true])
44+
->assertExitCode(1);
2645
});
2746

2847
it('creates a backup when requested', function (): void {
@@ -47,6 +66,19 @@
4766
expect(file_get_contents(base_path('pint.json')))->toBe('{"test": "original"}');
4867
});
4968

69+
it('publishes pint configuration file when user confirms', function (): void {
70+
// Create a dummy pint.json file first
71+
File::put(base_path('pint.json'), '{"test": "original"}');
72+
73+
$this->artisan('essentials:pint')
74+
->expectsConfirmation('Do you wish to publish the Pint configuration file? This will override the existing [pint.json] file.', 'yes')
75+
->assertExitCode(0);
76+
77+
expect(file_exists(base_path('pint.json')))
78+
->not()
79+
->toBe('{"test": "original"}');
80+
});
81+
5082
afterEach(function (): void {
5183
// Clean up any created files
5284
if (file_exists(base_path('pint.json'))) {

tests/Commands/EssentialsRectorCommandTest.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
use Illuminate\Support\Facades\File;
6-
use NunoMaduro\Essentials\Commands\EssentialsRectorCommand;
76

87
beforeEach(function (): void {
98
if (file_exists(base_path('rector.php'))) {
@@ -15,13 +14,32 @@
1514
}
1615
});
1716

18-
it('publishes rector configuration file', function (): void {
19-
$command = new EssentialsRectorCommand();
20-
17+
it('publishes rector configuration file without a backup by default', function (): void {
2118
$this->artisan('essentials:rector', ['--force' => true])
2219
->assertExitCode(0);
2320

2421
expect(file_exists(base_path('rector.php')))->toBeTrue();
22+
expect(file_exists(base_path('rector.json.backup')))->toBeFalse();
23+
});
24+
25+
it('returns error when rector configuration file does not exist', function (): void {
26+
File::shouldReceive('exists')
27+
->once()
28+
->andReturnFalse();
29+
30+
$this->artisan('essentials:rector', ['--force' => true])
31+
->expectsOutputToContain('Rector configuration stub file not found.')
32+
->assertExitCode(1);
33+
});
34+
35+
it('returns error when copy operation fails', function (): void {
36+
File::shouldReceive('exists')->andReturnTrue();
37+
File::shouldReceive('copy')
38+
->once()
39+
->andReturnFalse();
40+
41+
$this->artisan('essentials:rector', ['--force' => true])
42+
->assertExitCode(1);
2543
});
2644

2745
it('creates a backup when requested', function (): void {
@@ -43,6 +61,18 @@
4361
expect(file_get_contents(base_path('rector.php')))->toBe('<?php return [];');
4462
});
4563

64+
it('publishes rector configuration file when user confirms', function (): void {
65+
File::put(base_path('rector.php'), '<?php return [];');
66+
67+
$this->artisan('essentials:rector')
68+
->expectsConfirmation('Do you wish to publish the Rector configuration file? This will override the existing [rector.php] file.', 'yes')
69+
->assertExitCode(0);
70+
71+
expect(file_get_contents(base_path('rector.php')))
72+
->not()
73+
->toBe('<?php return [];');
74+
});
75+
4676
afterEach(function (): void {
4777
if (file_exists(base_path('rector.php'))) {
4878
unlink(base_path('rector.php'));

0 commit comments

Comments
 (0)