Skip to content

Commit d393a78

Browse files
committed
- PreCommitTest implemented
1 parent 93ea2e4 commit d393a78

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tests/Feature/PostCommitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$commitHash = 'b636c88159e121d0c8276c417576d57ebb380dc3';
99

1010
$postCommitHook1 = mock(PostCommitHook::class)->expect(
11-
handle: fn(Log $log, Closure $closure) => expect($log->getHash())->toBe($commitHash)
11+
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe($commitHash)
1212
);
1313
$postCommitHook2 = clone $postCommitHook1;
1414

tests/Feature/PreCommitTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)