Skip to content

Commit 757d083

Browse files
committed
- Git folder tests
- Data sets implemented
1 parent fc7f1ee commit 757d083

13 files changed

+255
-35
lines changed

tests/Datasets/GitLogsDataset.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
dataset('lastCommitLogText', [
4+
'default git log' => sprintf('commit %s
5+
Author: Igor Moraes <[email protected]>
6+
Date: Wed Nov 9 04:50:40 2022 -0800
7+
8+
wip
9+
', mockCommitHash())
10+
]);
11+
12+
dataset('mergeLogText', [
13+
'merge git log' => sprintf("commit %s
14+
Merge: 123abc 456def
15+
Author: Igor Moraes <[email protected]>
16+
Date: Wed Nov 9 04:50:40 2022 -0800
17+
18+
Merge branch 'main' of github.com:igorsgm/laravel-git-hooks
19+
20+
", mockCommitHash())
21+
]);
22+
23+
dataset('listOfChangedFiles', [
24+
'list of changed files' => 'AM src/ChangedFiles.php'
25+
]);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
dataset('modifiedFilesMeta', [
4+
'empty log' => [
5+
'',
6+
false,
7+
false,
8+
false,
9+
false,
10+
false,
11+
],
12+
'added and modified files' => [
13+
'AM src/ChangedFiles.php',
14+
true,
15+
true,
16+
false,
17+
false,
18+
true,
19+
],
20+
'modified file' => [
21+
' M src/Console/Commands/CommitMessage.php',
22+
false,
23+
true,
24+
false,
25+
false,
26+
false,
27+
],
28+
'deleted file' => [
29+
' D LICENSE',
30+
false,
31+
false,
32+
true,
33+
false,
34+
false,
35+
],
36+
'untracked file' => [
37+
'?? LICENSE',
38+
false,
39+
false,
40+
false,
41+
true,
42+
false,
43+
],
44+
]);
45+
46+
47+
dataset('modifiedFilesList', [
48+
'modified files list' => implode(PHP_EOL, [
49+
'M src/Console/Commands/CommitMessage.php',
50+
'M src/Console/Commands/PrepareCommitMessage.php',
51+
'A src/Traits/WithCommitMessage.php',
52+
'M src/Contracts/MessageHook.php',
53+
'AM src/Git/ChangedFile.php',
54+
'AM src/Git/ChangedFiles.php',
55+
'A src/Git/CommitMessage.php',
56+
'M tests/Console/Commands/CommitMessageTest.php',
57+
'M tests/Console/Commands/PrepareCommitMessageTest.php',
58+
'AM tests/Git/ChangedFileTest.php',
59+
'AM tests/Git/ChangedFilesTest.php',
60+
'D tests/Git/CommitMessageTest.php',
61+
'?? tests/Git/UntrackedFile.php',
62+
]),
63+
]);

tests/Feature/CommitMessageTest.php renamed to tests/Features/Commands/CommitMessageTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Igorsgm\GitHooks\Tests\Fixtures\CommitMessageFixtureHook2;
66
use Igorsgm\GitHooks\Tests\Fixtures\CommitMessageFixtureHook4;
77

8-
test('Commit Message is sent through HookPipes', function () {
8+
test('Commit Message is sent through HookPipes', function (string $listOfChangedFiles) {
99
$commitMessageHooks = [
1010
CommitMessageFixtureHook1::class,
1111
CommitMessageFixtureHook2::class,
@@ -19,7 +19,7 @@
1919
->andReturn('Test commit');
2020

2121
GitHooks::shouldReceive('getListOfChangedFiles')
22-
->andReturn(mockListOfChangedFiles());
22+
->andReturn($listOfChangedFiles);
2323

2424
GitHooks::shouldReceive('updateCommitMessageContentInFile')
2525
->with(base_path($file), 'Test commit hook1 hook2');
@@ -30,9 +30,9 @@
3030
foreach ($commitMessageHooks as $hook) {
3131
$command->expectsOutputToContain(sprintf('Hook: %s...', resolve($hook)->getName()));
3232
}
33-
});
33+
})->with('listOfChangedFiles');
3434

35-
test('Pass parameters into Commit Hook class', function () {
35+
test('Pass parameters into Commit Hook class', function (string $listOfChangedFiles) {
3636
$commitMessageHooks = [
3737
CommitMessageFixtureHook4::class => [
3838
'param1' => 'hello',
@@ -48,7 +48,7 @@
4848
->andReturn('Test commit');
4949

5050
GitHooks::shouldReceive('getListOfChangedFiles')
51-
->andReturn(mockListOfChangedFiles());
51+
->andReturn($listOfChangedFiles);
5252

5353
GitHooks::shouldReceive('updateCommitMessageContentInFile')
5454
->with(base_path($file), 'Test commit hello world');
@@ -60,4 +60,4 @@
6060
$hook = resolve($hook, compact('parameters'));
6161
$command->expectsOutputToContain(sprintf('Hook: %s...', $hook->getName()));
6262
}
63-
});
63+
})->with('listOfChangedFiles');

tests/Feature/PostCommitTest.php renamed to tests/Features/Commands/PostCommitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Igorsgm\GitHooks\Facades\GitHooks;
55
use Igorsgm\GitHooks\Git\Log;
66

7-
test('Git Log is sent through HookPipes', function () {
7+
test('Git Log is sent through HookPipes', function (string $logText) {
88
$postCommitHook1 = mock(PostCommitHook::class)->expect(
99
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
1010
);
@@ -15,7 +15,7 @@
1515
$postCommitHook2,
1616
]);
1717

18-
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn(mockLastCommitLog());
18+
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn($logText);
1919

2020
$this->artisan('git-hooks:post-commit')->assertSuccessful();
21-
});
21+
})->with('lastCommitLogText');

tests/Feature/PreCommitTest.php renamed to tests/Features/Commands/PreCommitTest.php

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

8-
it('sends ChangedFiles through HookPipes', function () {
8+
it('sends ChangedFiles through HookPipes', function (string $listOfChangedFiles) {
99
$preCommitHook1 = mock(PreCommitHook::class)->expect(
10-
handle: function (ChangedFiles $files, Closure $closure) {
10+
handle: function (ChangedFiles $files, Closure $closure) use ($listOfChangedFiles) {
1111
$firstChangedFile = (string) $files->getFiles()->first();
12-
expect($firstChangedFile)->toBe(mockListOfChangedFiles());
12+
expect($firstChangedFile)->toBe($listOfChangedFiles);
1313
}
1414
);
1515
$preCommitHook2 = clone $preCommitHook1;
@@ -19,10 +19,10 @@
1919
$preCommitHook2,
2020
]);
2121

22-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn(mockListOfChangedFiles());
22+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfChangedFiles);
2323

2424
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
25-
});
25+
})->with('listOfChangedFiles');
2626

2727
it('returns 1 on HookFailException', function () {
2828
$preCommitHook1 = mock(PreCommitHook::class)->expect(

tests/Feature/PrePushTest.php renamed to tests/Features/Commands/PrePushTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Igorsgm\GitHooks\Facades\GitHooks;
55
use Igorsgm\GitHooks\Git\Log;
66

7-
test('Git Log is sent through HookPipes', function () {
7+
test('Git Log is sent through HookPipes', function (string $logText) {
88
$prePushHook1 = mock(PostCommitHook::class)->expect(
99
handle: fn (Log $log, Closure $closure) => expect($log->getHash())->toBe(mockCommitHash())
1010
);
@@ -15,7 +15,7 @@
1515
$prePushHook2,
1616
]);
1717

18-
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn(mockLastCommitLog());
18+
GitHooks::shouldReceive('getLastCommitFromLog')->andReturn($logText);
1919

2020
$this->artisan('git-hooks:pre-push')->assertSuccessful();
21-
});
21+
})->with('lastCommitLogText');

tests/Feature/PrepareCommitMessageTest.php renamed to tests/Features/Commands/PrepareCommitMessageTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Igorsgm\GitHooks\Tests\Fixtures\PrepareCommitMessageFixtureHook1;
55
use Igorsgm\GitHooks\Tests\Fixtures\PrepareCommitMessageFixtureHook2;
66

7-
test('Commit Message is sent through HookPipes', function () {
7+
test('Commit Message is sent through HookPipes', function (string $listOfChangedFiles) {
88
$prepareCommitMessageHooks = [
99
PrepareCommitMessageFixtureHook1::class,
1010
PrepareCommitMessageFixtureHook2::class,
@@ -18,7 +18,7 @@
1818
->andReturn('Test commit');
1919

2020
GitHooks::shouldReceive('getListOfChangedFiles')
21-
->andReturn(mockListOfChangedFiles());
21+
->andReturn($listOfChangedFiles);
2222

2323
GitHooks::shouldReceive('updateCommitMessageContentInFile')
2424
->with(base_path($file), 'Test commit hook1 hook2');
@@ -29,4 +29,4 @@
2929
foreach ($prepareCommitMessageHooks as $hook) {
3030
$command->expectsOutputToContain(sprintf('Hook: %s...', resolve($hook)->getName()));
3131
}
32-
});
32+
})->with('listOfChangedFiles');

tests/Pest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,3 @@ function mockCommitHash()
5252
{
5353
return 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
5454
}
55-
56-
function mockListOfChangedFiles()
57-
{
58-
return 'AM src/ChangedFiles.php';
59-
}
60-
61-
function mockLastCommitLog()
62-
{
63-
return sprintf('commit %s
64-
Author: Igor Moraes <[email protected]>
65-
Date: Wed Nov 9 04:50:40 2022 -0800
66-
67-
wip
68-
', mockCommitHash());
69-
}

tests/Unit/Git/ChangedFileTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
4+
use Igorsgm\GitHooks\Git\ChangedFile;
5+
6+
test('Gets file meta', function(string $file, bool $isAdded, bool $isModified, bool $isDeleted, bool $isUntracked, bool $inCommit) {
7+
$file = new ChangedFile($file);
8+
9+
$this->assertEquals($isAdded, $file->isAdded());
10+
$this->assertEquals($isModified, $file->isModified());
11+
$this->assertEquals($isDeleted, $file->isDeleted());
12+
$this->assertEquals($isUntracked, $file->isUntracked());
13+
$this->assertEquals($inCommit, $file->isInCommit());
14+
})->with('modifiedFilesMeta');
15+
16+
test('Gets files', function() {
17+
$file = new ChangedFile('AM src/ChangedFiles.php');
18+
$this->assertEquals('src/ChangedFiles.php', $file->getFilePath());
19+
20+
$file = new ChangedFile('?? LICENSE');
21+
$this->assertEquals('LICENSE', $file->getFilePath());
22+
});

0 commit comments

Comments
 (0)