|
| 1 | +<?php |
| 2 | + |
| 3 | +use Igorsgm\GitHooks\Contracts\PreCommitHook; |
| 4 | +use Igorsgm\GitHooks\Exceptions\HookFailException; |
| 5 | +use Igorsgm\GitHooks\Facades\GitHooks; |
| 6 | +use Igorsgm\GitHooks\Git\ChangedFiles; |
| 7 | + |
| 8 | +it('sends ChangedFiles through HookPipes', function () { |
| 9 | + $changedFilesString = 'AM src/ChangedFiles.php'; |
| 10 | + |
| 11 | + $preCommitHook1 = mock(PreCommitHook::class)->expect( |
| 12 | + handle: function (ChangedFiles $files, Closure $closure) use ($changedFilesString) { |
| 13 | + $firstChangedFile = (string) $files->getFiles()->first(); |
| 14 | + expect($firstChangedFile)->toBe($changedFilesString); |
| 15 | + } |
| 16 | + ); |
| 17 | + $preCommitHook2 = clone $preCommitHook1; |
| 18 | + |
| 19 | + $this->config->set('git-hooks.pre-commit', [ |
| 20 | + $preCommitHook1, |
| 21 | + $preCommitHook2, |
| 22 | + ]); |
| 23 | + |
| 24 | + GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($changedFilesString); |
| 25 | + |
| 26 | + $this->artisan('git-hooks:pre-commit')->assertSuccessful(); |
| 27 | +}); |
| 28 | + |
| 29 | +it('returns 1 on HookFailException', function () { |
| 30 | + $changedFilesString = 'AM src/ChangedFiles.php'; |
| 31 | + |
| 32 | + $preCommitHook1 = mock(PreCommitHook::class)->expect( |
| 33 | + handle: function (ChangedFiles $files, Closure $closure) { |
| 34 | + throw new HookFailException(); |
| 35 | + } |
| 36 | + ); |
| 37 | + |
| 38 | + $this->config->set('git-hooks.pre-commit', [ |
| 39 | + $preCommitHook1, |
| 40 | + ]); |
| 41 | + |
| 42 | + GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($changedFilesString); |
| 43 | + |
| 44 | + $this->artisan('git-hooks:pre-commit')->assertExitCode(1); |
| 45 | +}); |
0 commit comments