Skip to content

Commit

Permalink
Create GitCommitterTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 30, 2023
1 parent 9f52f26 commit de20c9d
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tests/Feature/GitCommitterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

use App\Actions\FixCode;
use App\Actions\ElaborateSummary;
use Illuminate\Support\Facades\Process;
use Illuminate\Process\FakeProcessResult;
use Symfony\Component\Console\Output\BufferedOutput;
use function Termwind\renderUsing;

beforeEach(function () {
Process::preventStrayProcesses();
});

it('handles clean working tree', function () {
[$statusCode, $output] = run('default', [
'--commit' => true,
'path' => base_path('tests/Fixtures/without-issues'),
]);

expect($statusCode)->toBe(0)
->and($output)
->toContain('Nothing to commit, working tree clean.');
});

it('prints process error', function () {
// For some reason, the output is not captured by the buffer when a non-zero exit code is returned, so we have to check the renderer directly.
renderUsing($renderer = new BufferedOutput());

$basePath = base_path('tests/Fixtures/with-fixable-issues/file.php');

$fixerOutput = [
$basePath => [
'appliedFixers' => ['psr12'],
'diff' => '',
],
];

$this->swap(FixCode::class, tap(Mockery::mock(FixCode::class), fn($fixer) => $fixer
->shouldReceive('execute')
->once()
->andReturn([1, $fixerOutput])
));

$this->swap(ElaborateSummary::class, tap(Mockery::mock(ElaborateSummary::class), fn($summary) => $summary
->shouldReceive('execute')
->once()
->with(1, $fixerOutput)
->andReturn(0)
));

Process::shouldReceive('run')
->once()
->with(sprintf('git commit -m "Apply style fixes from Laravel Pint" %s', $basePath))
->andReturn(tap(Mockery::mock(FakeProcessResult::class), fn($process) => $process
->shouldReceive('failed')
->once()
->andReturn(true)
->shouldReceive('errorOutput')
->once()
->andReturn('Example Git error')
));

[$statusCode, $output] = run('default', [
'--commit' => true,
'path' => $basePath,
]);

expect($statusCode)->toBe(1)
->and($renderer->fetch())
->toBe(' ERROR: Example Git error ' . PHP_EOL)
->and($output)
->not()->toContain('Apply style fixes from Laravel Pint');
});

it('commits the changes', function () {
renderUsing($renderer = new BufferedOutput());

$basePath = base_path('tests/Fixtures/with-fixable-issues/file.php');

$fixerOutput = [
$basePath => [
'appliedFixers' => ['psr12'],
'diff' => '',
],
];

$this->swap(FixCode::class, tap(Mockery::mock(FixCode::class), fn($fixer) => $fixer
->shouldReceive('execute')
->once()
->andReturn([1, $fixerOutput])
));

$this->swap(ElaborateSummary::class, tap(Mockery::mock(ElaborateSummary::class), fn($summary) => $summary
->shouldReceive('execute')
->once()
->with(1, $fixerOutput)
->andReturn(0)
));

Process::shouldReceive('run')
->once()
->with('git commit -m "Apply style fixes from Laravel Pint" '.base_path('tests/Fixtures/with-fixable-issues/file.php'))
->andReturn(new FakeProcessResult());

[$statusCode, $output] = run('default', [
'--commit' => true,
'path' => base_path('tests/Fixtures/with-fixable-issues/file.php'),
]);

expect($statusCode)->toBe(0)
->and($renderer->fetch())
->toBe(' Changes committed successfully! ' . PHP_EOL);
});

0 comments on commit de20c9d

Please sign in to comment.