Skip to content

Commit fc7f1ee

Browse files
committed
- RegisterHooksTest implemented
1 parent 8492f7e commit fc7f1ee

File tree

6 files changed

+199
-5
lines changed

6 files changed

+199
-5
lines changed

config/git-hooks.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
| Pre-Rebase Hooks
7979
|-----------------------------------------------------------------
8080
|
81+
| @TODO Implement support to pre-rebase hooks
82+
|
8183
| The pre-rebase hook runs before you rebase anything and can halt the process by exiting non-zero.
8284
| You can use this hook to disallow rebasing any commits that have already been pushed.
8385
| The example pre-rebase hook that Git installs does this, although it makes some assumptions that may not
@@ -93,6 +95,8 @@
9395
| Post-Rewrite Hooks
9496
|-----------------------------------------------------------------
9597
|
98+
| @TODO Implement support to post-rewrite hook
99+
|
96100
| The post-rewrite hook is run by commands that replace commits, such as git commit --amend and git rebase
97101
| (though not by git filter-branch). Its single argument is which command triggered the rewrite, and it receives
98102
| a list of rewrites on stdin. This hook has many of the same uses as the post-checkout and post-merge hooks.
@@ -107,6 +111,8 @@
107111
| Post-Checkout Hooks
108112
|-----------------------------------------------------------------
109113
|
114+
| @TODO Implement support to post-checkout hook
115+
|
110116
| After you run a successful git checkout, the post-checkout hook runs; you can use it to set up your working
111117
| directory properly for your project environment. This may mean moving in large binary files that you don't want
112118
| source controlled, auto-generating documentation, or something along those lines.
@@ -121,10 +127,13 @@
121127
| Post-Merge Hooks
122128
|-----------------------------------------------------------------
123129
|
130+
| @TODO Implement support to post-merge hook
131+
|
124132
| The post-merge hook runs after a successful merge command. You can use it to restore data in the working tree
125133
| that Git can't track, such as permissions data. This hook can likewise validate the presence of files external
126134
| to Git control that you may want copied in when the working tree changes.
127135
|
136+
|
128137
*/
129138
'post-merge' => [
130139

src/Console/Commands/RegisterHooks.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class RegisterHooks extends Command
1212
*
1313
* @var string
1414
*/
15-
protected $signature = 'git-hooks:register-hooks';
15+
protected $signature = 'git-hooks:register';
1616

1717
/**
1818
* The console command description.
1919
*
2020
* @var string
2121
*/
22-
protected $description = 'Register git hooks for application';
22+
protected $description = 'Register or re-register git hooks for application';
2323

2424
/**
2525
* Execute the console command.
@@ -37,6 +37,6 @@ public function handle(GitHooks $gitHooks)
3737
$gitHooks->install($hook);
3838
}
3939

40-
$this->info('Git hooks have been successfully created');
40+
$this->info('Git hooks have been successfully installed.');
4141
}
4242
}

tests/Feature/RegisterHooksTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
beforeEach(function () {
10+
$this->initializeGitAsTempDirectory();
11+
});
12+
13+
afterEach(function () {
14+
$this->deleteTempDirectory();
15+
});
16+
17+
it('installs pre-commit file in .git/hooks folder', function () {
18+
$preCommitHookClass = mock(PreCommitHook::class);
19+
20+
$this->config->set('git-hooks.pre-commit', [
21+
$preCommitHookClass,
22+
]);
23+
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');
31+
});
32+
33+
it('installs prepare-commit-msg file in .git/hooks folder', function () {
34+
$this->config->set('git-hooks.prepare-commit-msg', [
35+
PrepareCommitMessageFixtureHook1::class,
36+
]);
37+
38+
$this->artisan('git-hooks:register')->assertSuccessful();
39+
40+
$hookFile = base_path('.git/hooks/prepare-commit-msg');
41+
42+
expect($hookFile)
43+
->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+
});

tests/Pest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
use Igorsgm\GitHooks\Tests\TestCase;
1515

16-
uses(TestCase::class)->in('Feature');
17-
uses(TestCase::class)->in('Unit');
16+
uses(TestCase::class)->in(__DIR__);
1817

1918
/*
2019
|--------------------------------------------------------------------------
@@ -31,6 +30,13 @@
3130
return $this->toBe(1);
3231
});
3332

33+
expect()->extend('toContainHookArtisanCommand', function ($hookName) {
34+
$this->value = file_get_contents($this->value);
35+
$artisanCommand = sprintf('php %s git-hooks:%s $@ >&2', base_path('artisan'), $hookName);
36+
37+
return $this->toContain($artisanCommand);
38+
});
39+
3440
/*
3541
|--------------------------------------------------------------------------
3642
| Functions
@@ -41,6 +47,7 @@
4147
| global functions to help you to reduce the number of lines of code in your test files.
4248
|
4349
*/
50+
4451
function mockCommitHash()
4552
{
4653
return 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Igorsgm\GitHooks\Facades\GitHooks;
66
use Igorsgm\GitHooks\GitHooksServiceProvider;
7+
use Igorsgm\GitHooks\Tests\Traits\WithTmpFiles;
78

89
class TestCase extends \Orchestra\Testbench\TestCase
910
{
11+
use WithTmpFiles;
12+
1013
/**
1114
* @var \Illuminate\Config\Repository
1215
*/
@@ -67,4 +70,14 @@ protected function getPackageAliases($app)
6770
'GitHooks' => GitHooks::class,
6871
];
6972
}
73+
74+
/**
75+
* @return void
76+
*/
77+
public function initializeGitAsTempDirectory()
78+
{
79+
chdir(base_path());
80+
shell_exec('git init --quiet');
81+
$this->initializeTempDirectory(base_path('.git'));
82+
}
7083
}

tests/Traits/WithTmpFiles.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Tests\Traits;
4+
5+
use Illuminate\Support\Facades\File;
6+
7+
trait WithTmpFiles
8+
{
9+
private $tempDirectoryPath;
10+
11+
public function initializeTempDirectory(?string $path = '', bool $force = false)
12+
{
13+
if ($path) {
14+
$this->setTempDirectoryPath($path);
15+
}
16+
17+
$tempDirectoryPath = $this->getTempDirectoryPath();
18+
19+
if ($force) {
20+
$this->deleteTempDirectory();
21+
}
22+
23+
if (! is_dir($tempDirectoryPath)) {
24+
File::makeDirectory($tempDirectoryPath, 0755, true);
25+
}
26+
}
27+
28+
protected function deleteTempDirectory()
29+
{
30+
File::deleteDirectory(
31+
$this->getTempDirectoryPath()
32+
);
33+
}
34+
35+
/**
36+
* @param string $filename
37+
* @return string
38+
*/
39+
protected function getTempFilePath(string $filename): string
40+
{
41+
return $this->getTempDirectoryPath().DIRECTORY_SEPARATOR.$filename;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getTempDirectoryPath(): string
48+
{
49+
return $this->tempDirectoryPath ?: __DIR__.DIRECTORY_SEPARATOR.'temp';
50+
}
51+
52+
/**
53+
* @param string $directoryName
54+
* @return void
55+
*/
56+
public function setTempDirectoryPath(string $directoryName): void
57+
{
58+
$this->tempDirectoryPath = $directoryName;
59+
}
60+
61+
/**
62+
* @param string $filename
63+
* @param string $content
64+
* @return string
65+
*/
66+
protected function makeTempFile(string $filename, string $content): string
67+
{
68+
$path = $this->getTempFilePath($filename);
69+
70+
file_put_contents($path, $content);
71+
72+
return $path;
73+
}
74+
}

0 commit comments

Comments
 (0)