Skip to content

Commit 488d3e7

Browse files
committed
Fix broken tests by updating mock implementations
- Update PostCommitTest.php, PreCommitTest.php, PrepareCommitMessageTest.php, and PrePushTest.php to use alternative mock implementation due to issues with the current version of Mockery. - Add @todo comments to revisit these tests once Pest or Mockery versions are updated.
1 parent b24c7c2 commit 488d3e7

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

tests/Features/Commands/PostCommitTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
use Igorsgm\GitHooks\Git\Log;
77

88
test('Git Log is sent through HookPipes', function (string $logText) {
9-
$postCommitHook1 = mock(PostCommitHook::class)->expect(
10-
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
11-
);
9+
// This approach is broken in the current version of Mockery
10+
// @TODO: Update this test once Pest or Mockery versions are updated
11+
// $postCommitHook1 = mock(PostCommitHook::class)->expect(
12+
// handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
13+
// );
14+
15+
$postCommitHook1 = mock(PostCommitHook::class);
16+
$postCommitHook1->expects('handle')
17+
->withArgs(fn($log, $closure) => $log->getHash() === mockCommitHash());
18+
1219
$postCommitHook2 = clone $postCommitHook1;
1320

1421
$this->config->set('git-hooks.post-commit', [
@@ -22,11 +29,19 @@
2229
})->with('lastCommitLogText');
2330

2431
it('Returns 1 on HookFailException', function ($logText) {
25-
$postCommitHook1 = mock(PostCommitHook::class)->expect(
26-
handle: function (Log $log, Closure $closure) {
32+
// This approach is broken in the current version of Mockery
33+
// @TODO: Update this test once Pest or Mockery versions are updated
34+
// $postCommitHook1 = mock(PostCommitHook::class)->expect(
35+
// handle: function (Log $log, Closure $closure) {
36+
// throw new HookFailException();
37+
// }
38+
// );
39+
40+
$postCommitHook1 = mock(PostCommitHook::class);
41+
$postCommitHook1->expects('handle')
42+
->andReturnUsing(function (Log $log, Closure $closure) {
2743
throw new HookFailException();
28-
}
29-
);
44+
});
3045

3146
$this->config->set('git-hooks.post-commit', [
3247
$postCommitHook1,

tests/Features/Commands/PreCommitTest.php

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

88
test('Sends ChangedFiles through HookPipes', function (string $listOfChangedFiles) {
9-
$preCommitHook1 = mock(PreCommitHook::class)->expect(
10-
handle: function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
9+
// This approach is broken in the current version of Mockery
10+
// @TODO: Update this test once Pest or Mockery versions are updated
11+
// $preCommitHook1 = mock(PreCommitHook::class)->expect(
12+
// handle: function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
13+
// $firstChangedFile = (string) $files->getFiles()->first();
14+
// expect($firstChangedFile)->toBe($listOfChangedFiles);
15+
// }
16+
// );
17+
$preCommitHook1 = mock(PreCommitHook::class);
18+
$preCommitHook1->expects('handle')
19+
->andReturnUsing(function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
1120
$firstChangedFile = (string) $files->getFiles()->first();
1221
expect($firstChangedFile)->toBe($listOfChangedFiles);
13-
}
14-
);
22+
});
23+
1524
$preCommitHook2 = clone $preCommitHook1;
1625

1726
$this->config->set('git-hooks.pre-commit', [
@@ -25,11 +34,19 @@
2534
})->with('listOfChangedFiles');
2635

2736
it('Returns 1 on HookFailException', function ($listOfChangedFiles) {
28-
$preCommitHook1 = mock(PreCommitHook::class)->expect(
29-
handle: function (ChangedFiles $files, Closure $closure) {
37+
// This approach is broken in the current version of Mockery
38+
// @TODO: Update this test once Pest or Mockery versions are updated
39+
// $preCommitHook1 = mock(PreCommitHook::class)->expect(
40+
// handle: function (ChangedFiles $files, Closure $closure) {
41+
// throw new HookFailException();
42+
// }
43+
// );
44+
$preCommitHook1 = mock(PreCommitHook::class);
45+
$preCommitHook1->expects('handle')
46+
->andReturnUsing(function (ChangedFiles $files, Closure $closure) {
3047
throw new HookFailException();
31-
}
32-
);
48+
});
49+
3350

3451
$this->config->set('git-hooks.pre-commit', [
3552
$preCommitHook1,

tests/Features/Commands/PrePushTest.php

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

77
test('Git Log is sent through HookPipes', function (string $logText) {
8-
$prePushHook1 = mock(PostCommitHook::class)->expect(
9-
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
10-
);
8+
// This approach is broken in the current version of Mockery
9+
// @TODO: Update this test once Pest or Mockery versions are updated
10+
// $prePushHook1 = mock(PostCommitHook::class)->expect(
11+
// handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
12+
// );
13+
$prePushHook1 = mock(PostCommitHook::class);
14+
$prePushHook1->expects('handle')
15+
->withArgs(fn(Log $log, Closure $closure) => $log->getHash() === mockCommitHash())
16+
->once();
17+
1118
$prePushHook2 = clone $prePushHook1;
1219

1320
$this->config->set('git-hooks.pre-push', [

tests/Features/Commands/PrepareCommitMessageTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@
3535
})->with('listOfChangedFiles');
3636

3737
it('Returns 1 on HookFailException', function ($listOfChangedFiles) {
38-
$postCommitHook1 = mock(MessageHook::class)->expect(
39-
handle: function (CommitMessage $commitMessage, Closure $closure) {
38+
// This approach is broken in the current version of Mockery
39+
// @TODO: Update this test once Pest or Mockery versions are updated
40+
// $postCommitHook1 = mock(MessageHook::class)->expect(
41+
// handle: function (CommitMessage $commitMessage, Closure $closure) {
42+
// throw new HookFailException();
43+
// }
44+
// );
45+
$postCommitHook1 = mock(MessageHook::class);
46+
$postCommitHook1->expects('handle')
47+
->andReturnUsing(function (CommitMessage $commitMessage, Closure $closure) {
4048
throw new HookFailException();
41-
}
42-
);
49+
});
50+
4351

4452
$this->config->set('git-hooks.prepare-commit-msg', [
4553
$postCommitHook1,

0 commit comments

Comments
 (0)