Skip to content

Commit 05bac37

Browse files
committed
- Test optimizations
1 parent 757d083 commit 05bac37

File tree

7 files changed

+52
-77
lines changed

7 files changed

+52
-77
lines changed

tests/Datasets/GitLogsDataset.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Date: Wed Nov 9 04:50:40 2022 -0800
77
88
wip
9-
', mockCommitHash())
9+
', mockCommitHash()),
1010
]);
1111

1212
dataset('mergeLogText', [
@@ -17,9 +17,9 @@
1717
1818
Merge branch 'main' of github.com:igorsgm/laravel-git-hooks
1919
20-
", mockCommitHash())
20+
", mockCommitHash()),
2121
]);
2222

2323
dataset('listOfChangedFiles', [
24-
'list of changed files' => 'AM src/ChangedFiles.php'
24+
'list of changed files' => 'AM src/ChangedFiles.php',
2525
]);

tests/Datasets/HooksDataset.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Contracts\PostCommitHook;
4+
use Igorsgm\GitHooks\Contracts\PreCommitHook;
5+
use Igorsgm\GitHooks\Contracts\PrePushHook;
6+
use Igorsgm\GitHooks\Tests\Fixtures\CommitMessageFixtureHook1;
7+
use Igorsgm\GitHooks\Tests\Fixtures\PrepareCommitMessageFixtureHook1;
8+
9+
dataset('registrableHookTypes', [
10+
'pre-commit' => [
11+
mock(PreCommitHook::class),
12+
'pre-commit',
13+
],
14+
'prepare-commit-msg' => [
15+
PrepareCommitMessageFixtureHook1::class,
16+
'prepare-commit-msg',
17+
],
18+
'commit-msg' => [
19+
CommitMessageFixtureHook1::class,
20+
'commit-msg',
21+
],
22+
'post-commit' => [
23+
mock(PostCommitHook::class),
24+
'post-commit',
25+
],
26+
'pre-push' => [
27+
mock(PrePushHook::class),
28+
'pre-push',
29+
],
30+
]);

tests/Datasets/ModifiedFilesDataset.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
],
4444
]);
4545

46-
4746
dataset('modifiedFilesList', [
4847
'modified files list' => implode(PHP_EOL, [
4948
'M src/Console/Commands/CommitMessage.php',

tests/Features/Commands/PreCommitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Igorsgm\GitHooks\Facades\GitHooks;
66
use Igorsgm\GitHooks\Git\ChangedFiles;
77

8-
it('sends ChangedFiles through HookPipes', function (string $listOfChangedFiles) {
8+
test('Sends ChangedFiles through HookPipes', function (string $listOfChangedFiles) {
99
$preCommitHook1 = mock(PreCommitHook::class)->expect(
1010
handle: function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
1111
$firstChangedFile = (string) $files->getFiles()->first();
@@ -24,7 +24,7 @@
2424
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
2525
})->with('listOfChangedFiles');
2626

27-
it('returns 1 on HookFailException', function () {
27+
it('Returns 1 on HookFailException', function () {
2828
$preCommitHook1 = mock(PreCommitHook::class)->expect(
2929
handle: function (ChangedFiles $files, Closure $closure) {
3030
throw new HookFailException();
Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?php
22

3-
use Igorsgm\GitHooks\Contracts\PostCommitHook;
43
use Igorsgm\GitHooks\Contracts\PreCommitHook;
5-
use Igorsgm\GitHooks\Contracts\PrePushHook;
6-
use Igorsgm\GitHooks\Tests\Fixtures\CommitMessageFixtureHook1;
7-
use Igorsgm\GitHooks\Tests\Fixtures\PrepareCommitMessageFixtureHook1;
84

95
beforeEach(function () {
106
$this->initializeGitAsTempDirectory();
@@ -14,78 +10,30 @@
1410
$this->deleteTempDirectory();
1511
});
1612

17-
it('installs pre-commit file in .git/hooks folder', function () {
13+
test('Throws exception when git was not initialized in project', function () {
14+
// delete .git folder
15+
$this->deleteTempDirectory();
16+
1817
$preCommitHookClass = mock(PreCommitHook::class);
1918

2019
$this->config->set('git-hooks.pre-commit', [
2120
$preCommitHookClass,
2221
]);
2322

24-
$this->artisan('git-hooks:register')->assertSuccessful();
25-
26-
$hookFile = base_path('.git/hooks/pre-commit');
27-
28-
expect($hookFile)
29-
->toBeFile()
30-
->toContainHookArtisanCommand('pre-commit');
23+
expect(fn () => $this->artisan('git-hooks:register'))->toThrow(Exception::class,
24+
'Git not initialized in this project.');
3125
});
3226

33-
it('installs prepare-commit-msg file in .git/hooks folder', function () {
34-
$this->config->set('git-hooks.prepare-commit-msg', [
35-
PrepareCommitMessageFixtureHook1::class,
27+
test('Installs hook file in .git/hooks folder', function ($hookClass, $hookName) {
28+
$this->config->set('git-hooks.'.$hookName, [
29+
$hookClass,
3630
]);
3731

3832
$this->artisan('git-hooks:register')->assertSuccessful();
3933

40-
$hookFile = base_path('.git/hooks/prepare-commit-msg');
34+
$hookFile = base_path('.git/hooks/'.$hookName);
4135

4236
expect($hookFile)
4337
->toBeFile()
44-
->toContainHookArtisanCommand('prepare-commit-msg');
45-
});
46-
47-
it('installs commit-msg file in .git/hooks folder', function () {
48-
$this->config->set('git-hooks.commit-msg', [
49-
CommitMessageFixtureHook1::class,
50-
]);
51-
52-
$this->artisan('git-hooks:register')->assertSuccessful();
53-
54-
$hookFile = base_path('.git/hooks/commit-msg');
55-
56-
expect($hookFile)
57-
->toBeFile()
58-
->toContainHookArtisanCommand('commit-msg');
59-
});
60-
61-
it('installs post-commit file in .git/hooks folder', function () {
62-
$postCommitHookClass = mock(PostCommitHook::class);
63-
64-
$this->config->set('git-hooks.post-commit', [
65-
$postCommitHookClass,
66-
]);
67-
68-
$this->artisan('git-hooks:register')->assertSuccessful();
69-
70-
$hookFile = base_path('.git/hooks/post-commit');
71-
72-
expect($hookFile)
73-
->toBeFile()
74-
->toContainHookArtisanCommand('post-commit');
75-
});
76-
77-
it('installs pre-push file in .git/hooks folder', function () {
78-
$prePushHookClass = mock(PrePushHook::class);
79-
80-
$this->config->set('git-hooks.pre-push', [
81-
$prePushHookClass,
82-
]);
83-
84-
$this->artisan('git-hooks:register')->assertSuccessful();
85-
86-
$hookFile = base_path('.git/hooks/pre-push');
87-
88-
expect($hookFile)
89-
->toBeFile()
90-
->toContainHookArtisanCommand('pre-push');
91-
});
38+
->toContainHookArtisanCommand($hookName);
39+
})->with('registrableHookTypes');

tests/Unit/Git/ChangedFileTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
43
use Igorsgm\GitHooks\Git\ChangedFile;
54

6-
test('Gets file meta', function(string $file, bool $isAdded, bool $isModified, bool $isDeleted, bool $isUntracked, bool $inCommit) {
5+
test('Gets file meta', function (string $file, bool $isAdded, bool $isModified, bool $isDeleted, bool $isUntracked, bool $inCommit) {
76
$file = new ChangedFile($file);
87

98
$this->assertEquals($isAdded, $file->isAdded());
@@ -13,7 +12,7 @@
1312
$this->assertEquals($inCommit, $file->isInCommit());
1413
})->with('modifiedFilesMeta');
1514

16-
test('Gets files', function() {
15+
test('Gets files', function () {
1716
$file = new ChangedFile('AM src/ChangedFiles.php');
1817
$this->assertEquals('src/ChangedFiles.php', $file->getFilePath());
1918

tests/Unit/Git/CommitMessageTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
use Igorsgm\GitHooks\Git\ChangedFiles;
44
use Igorsgm\GitHooks\Git\CommitMessage;
5-
use Igorsgm\GitHooks\Tests2\TestCase;
65

7-
test('Gets commit message', function() {
6+
test('Gets commit message', function () {
87
$messageText = 'Test message';
98
$commitMessage = new CommitMessage($messageText, new ChangedFiles(''));
109

1110
expect($commitMessage->getMessage())->toBe($messageText);
1211
});
1312

14-
test('Sets commit message', function() {
13+
test('Sets commit message', function () {
1514
$messageText = 'Test message';
1615
$commitMessage = new CommitMessage($messageText, new ChangedFiles(''));
1716

@@ -21,7 +20,7 @@
2120
expect($commitMessage->getMessage())->toBe($newMessageText);
2221
});
2322

24-
test('Gets files as ChangedFiles::class', function() {
23+
test('Gets files as ChangedFiles::class', function () {
2524
$commitMessage = new CommitMessage('Test message', new ChangedFiles(''));
2625

2726
expect($commitMessage->getFiles())->toBeInstanceOf(ChangedFiles::class);

0 commit comments

Comments
 (0)