|
| 1 | +<?php |
| 2 | + |
| 3 | +use Igorsgm\GitHooks\Console\Commands\Hooks\LarastanPreCommitHook; |
| 4 | +use Igorsgm\GitHooks\Facades\GitHooks; |
| 5 | +use Igorsgm\GitHooks\Traits\GitHelper; |
| 6 | + |
| 7 | +uses(GitHelper::class); |
| 8 | +beforeEach(function () { |
| 9 | + $this->gitInit(); |
| 10 | + $this->initializeTempDirectory(base_path('temp')); |
| 11 | +}); |
| 12 | + |
| 13 | +test('Fails commit when Larastan is not passing', |
| 14 | + function ($larastanConfiguration) { |
| 15 | + $this->config->set('git-hooks.code_analyzers.larastan', $larastanConfiguration); |
| 16 | + $this->config->set('git-hooks.pre-commit', [ |
| 17 | + LarastanPreCommitHook::class, |
| 18 | + ]); |
| 19 | + |
| 20 | + $this->makeTempFile('ClassWithFixableIssues.php', |
| 21 | + file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php') |
| 22 | + ); |
| 23 | + |
| 24 | + GitHooks::shouldReceive('isMergeInProgress')->andReturn(false); |
| 25 | + GitHooks::shouldReceive('getListOfChangedFiles')->andReturn(implode(PHP_EOL, [ |
| 26 | + 'AM temp/ClassWithFixableIssues.php', |
| 27 | + ])); |
| 28 | + |
| 29 | + $this->artisan('git-hooks:pre-commit') |
| 30 | + ->expectsOutputToContain('Larastan Failed') |
| 31 | + ->expectsOutputToContain('COMMIT FAILED') |
| 32 | + ->assertExitCode(1); |
| 33 | + })->with('larastanConfiguration'); |
| 34 | + |
| 35 | +test('Commit passes when Larastan check is OK', function ($larastanConfiguration) { |
| 36 | + $this->config->set('git-hooks.code_analyzers.larastan', $larastanConfiguration); |
| 37 | + $this->config->set('git-hooks.pre-commit', [ |
| 38 | + LarastanPreCommitHook::class, |
| 39 | + ]); |
| 40 | + |
| 41 | + $tempFileName = 'ClassWithoutFixableIssues.php'; |
| 42 | + $this->makeTempFile($tempFileName, |
| 43 | + file_get_contents(__DIR__.'/../../../Fixtures/'.$tempFileName) |
| 44 | + ); |
| 45 | + |
| 46 | + GitHooks::shouldReceive('isMergeInProgress')->andReturn(false); |
| 47 | + GitHooks::shouldReceive('getListOfChangedFiles')->andReturn(implode(PHP_EOL, [ |
| 48 | + 'AM temp/'.$tempFileName, |
| 49 | + ])); |
| 50 | + |
| 51 | + $this->artisan('git-hooks:pre-commit') |
| 52 | + ->doesntExpectOutputToContain('Larastan Failed') |
| 53 | + ->assertSuccessful(); |
| 54 | +})->with('larastanConfiguration'); |
0 commit comments