Skip to content

Commit f45e7ab

Browse files
committed
GitHelperTest implemented
1 parent f939ef2 commit f45e7ab

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/Unit/Traits/GitHelperTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Traits\GitHelper;
4+
use Symfony\Component\Process\Exception\ProcessFailedException;
5+
6+
uses(GitHelper::class);
7+
beforeEach(function () {
8+
$this->initializeGitAsTempDirectory();
9+
});
10+
11+
test('Gets list of changed files', function () {
12+
chdir(__DIR__);
13+
14+
expect($this->getListOfChangedFiles())->toBe(
15+
shell_exec('git status --short')
16+
);
17+
});
18+
19+
test('Gets last commit text from git log', function () {
20+
chdir(__DIR__);
21+
22+
expect($this->getLastCommitFromLog())->toBe(
23+
shell_exec('git log -1 HEAD')
24+
);
25+
});
26+
27+
test('Throws ProcessFailedException on errored git command', function () {
28+
expect(fn () => $this->runCommandAndGetOutput('git --wrong'))
29+
->toThrow(ProcessFailedException::class);
30+
});
31+
32+
test('Gets Commit message content from .git file', function () {
33+
$gitFileName = 'COMMIT_MESSAGE';
34+
$messageText = 'test message';
35+
36+
$this->makeTempFile($gitFileName, $messageText);
37+
$gitFilePath = $this->getTempFilePath($gitFileName);
38+
39+
expect($this->getCommitMessageContentFromFile($gitFilePath))
40+
->toBe($messageText);
41+
});
42+
43+
test('Updates Commit message content in .git file', function () {
44+
$gitFileName = 'COMMIT_MESSAGE';
45+
$messageText = 'test message';
46+
47+
$this->makeTempFile($gitFileName, $messageText);
48+
$gitFilePath = $this->getTempFilePath($gitFileName);
49+
50+
$newMessage = 'new message 1';
51+
$this->updateCommitMessageContentInFile($gitFilePath, $newMessage);
52+
expect(file_get_contents($gitFilePath))->toBe($newMessage);
53+
});

0 commit comments

Comments
 (0)