Skip to content

Commit 8492f7e

Browse files
committed
- PrePushTest implemented
1 parent 51d0fa9 commit 8492f7e

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

tests/Feature/CommitMessageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
->andReturn('Test commit');
2020

2121
GitHooks::shouldReceive('getListOfChangedFiles')
22-
->andReturn('AM src/ChangedFiles.php');
22+
->andReturn(mockListOfChangedFiles());
2323

2424
GitHooks::shouldReceive('updateCommitMessageContentInFile')
2525
->with(base_path($file), 'Test commit hook1 hook2');
@@ -48,7 +48,7 @@
4848
->andReturn('Test commit');
4949

5050
GitHooks::shouldReceive('getListOfChangedFiles')
51-
->andReturn('AM src/ChangedFiles.php');
51+
->andReturn(mockListOfChangedFiles());
5252

5353
GitHooks::shouldReceive('updateCommitMessageContentInFile')
5454
->with(base_path($file), 'Test commit hello world');

tests/Feature/PostCommitTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
use Igorsgm\GitHooks\Git\Log;
66

77
test('Git Log is sent through HookPipes', function () {
8-
$commitHash = 'b636c88159e121d0c8276c417576d57ebb380dc3';
9-
108
$postCommitHook1 = mock(PostCommitHook::class)->expect(
11-
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe($commitHash)
9+
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
1210
);
1311
$postCommitHook2 = clone $postCommitHook1;
1412

@@ -17,13 +15,7 @@
1715
$postCommitHook2,
1816
]);
1917

20-
GitHooks::shouldReceive('getLastCommitFromLog')
21-
->andReturn("commit $commitHash
22-
Author: Igor Moraes <[email protected]>
23-
Date: Wed Nov 9 04:50:40 2022 -0800
24-
25-
wip
26-
");
18+
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn(mockLastCommitLog());
2719

2820
$this->artisan('git-hooks:post-commit')->assertSuccessful();
2921
});

tests/Feature/PreCommitTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
use Igorsgm\GitHooks\Git\ChangedFiles;
77

88
it('sends ChangedFiles through HookPipes', function () {
9-
$changedFilesString = 'AM src/ChangedFiles.php';
10-
119
$preCommitHook1 = mock(PreCommitHook::class)->expect(
12-
handle: function (ChangedFiles $files, Closure $closure) use ($changedFilesString) {
10+
handle: function (ChangedFiles $files, Closure $closure) {
1311
$firstChangedFile = (string) $files->getFiles()->first();
14-
expect($firstChangedFile)->toBe($changedFilesString);
12+
expect($firstChangedFile)->toBe(mockListOfChangedFiles());
1513
}
1614
);
1715
$preCommitHook2 = clone $preCommitHook1;
@@ -21,7 +19,7 @@
2119
$preCommitHook2,
2220
]);
2321

24-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($changedFilesString);
22+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn(mockListOfChangedFiles());
2523

2624
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
2725
});

tests/Feature/PrePushTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Contracts\PostCommitHook;
4+
use Igorsgm\GitHooks\Facades\GitHooks;
5+
use Igorsgm\GitHooks\Git\Log;
6+
7+
test('Git Log is sent through HookPipes', function () {
8+
$prePushHook1 = mock(PostCommitHook::class)->expect(
9+
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
10+
);
11+
$prePushHook2 = clone $prePushHook1;
12+
13+
$this->config->set('git-hooks.pre-push', [
14+
$prePushHook1,
15+
$prePushHook2,
16+
]);
17+
18+
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn(mockLastCommitLog());
19+
20+
$this->artisan('git-hooks:pre-push')->assertSuccessful();
21+
});

tests/Feature/PrepareCommitMessageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
->andReturn('Test commit');
1919

2020
GitHooks::shouldReceive('getListOfChangedFiles')
21-
->andReturn('AM src/ChangedFiles.php');
21+
->andReturn(mockListOfChangedFiles());
2222

2323
GitHooks::shouldReceive('updateCommitMessageContentInFile')
2424
->with(base_path($file), 'Test commit hook1 hook2');

tests/Pest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,22 @@
4141
| global functions to help you to reduce the number of lines of code in your test files.
4242
|
4343
*/
44+
function mockCommitHash()
45+
{
46+
return 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
47+
}
48+
49+
function mockListOfChangedFiles()
50+
{
51+
return 'AM src/ChangedFiles.php';
52+
}
53+
54+
function mockLastCommitLog()
55+
{
56+
return sprintf('commit %s
57+
Author: Igor Moraes <[email protected]>
58+
Date: Wed Nov 9 04:50:40 2022 -0800
59+
60+
wip
61+
', mockCommitHash());
62+
}

0 commit comments

Comments
 (0)