Skip to content

Commit 0331e86

Browse files
committed
LarastanPreCommitHookTest implementation + tests
1 parent 93a3d86 commit 0331e86

10 files changed

+137
-52
lines changed

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ protected function checkAnalyzerInstallation()
186186
*/
187187
protected function suggestAutoFixOrExit()
188188
{
189-
if (Terminal::hasSttyAvailable() && ! empty($this->fixerExecutable) &&
189+
$hasFixerCommand = ! empty($this->fixerCommand());
190+
191+
if (Terminal::hasSttyAvailable() && $hasFixerCommand &&
190192
$this->command->confirm('Would you like to attempt to correct files automagically?')
191193
) {
192194
$errorFilesString = implode(' ', $this->filesBadlyFormattedPaths);

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
use Igorsgm\GitHooks\Console\Commands\Hooks\BladeFormatterPreCommitHook;
4+
use Igorsgm\GitHooks\Console\Commands\Hooks\LarastanPreCommitHook;
5+
use Igorsgm\GitHooks\Console\Commands\Hooks\PHPCodeSnifferPreCommitHook;
6+
use Igorsgm\GitHooks\Console\Commands\Hooks\PintPreCommitHook;
7+
38
dataset('pintConfigurations', [
49
'Config File' => [
510
[
@@ -33,3 +38,41 @@
3338
],
3439
],
3540
]);
41+
42+
dataset('larastanConfiguration', [
43+
'phpstan.neon file & additional params' => [
44+
[
45+
'path' => '../../../bin/phpstan',
46+
'config' => __DIR__.'/../Fixtures/phpstanFixture.neon',
47+
'additional_params' => '--xdebug',
48+
],
49+
],
50+
]);
51+
52+
$nonExistentPath = [
53+
'path' => 'nonexistent/path',
54+
'phpcs_path' => 'nonexistent/path',
55+
];
56+
57+
dataset('codeAnalyzersList', [
58+
'Laravel Pint' => [
59+
'laravel_pint',
60+
$nonExistentPath,
61+
PintPreCommitHook::class,
62+
],
63+
'PHP Code Sniffer' => [
64+
'php_code_sniffer',
65+
$nonExistentPath,
66+
PHPCodeSnifferPreCommitHook::class,
67+
],
68+
'Blade Formatter' => [
69+
'blade_formatter',
70+
$nonExistentPath,
71+
BladeFormatterPreCommitHook::class,
72+
],
73+
'Larastan' => [
74+
'larastan',
75+
$nonExistentPath,
76+
LarastanPreCommitHook::class,
77+
],
78+
]);

tests/Features/Commands/Hooks/BaseCodeAnalyzerPreCommitHookTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,20 @@
3838
$result = $hook->handleCommittedFiles($changedFiles, $next);
3939
expect($result)->toBe('passed');
4040
})->with('modifiedFilesList');
41+
42+
test('Throws HookFailException and notifies when Code Analyzer is not installed',
43+
function ($configName, $nonExistentPathConfig, $preCommitHookClass, $listOfFixableFiles) {
44+
$this->config->set('git-hooks.code_analyzers.'.$configName, $nonExistentPathConfig);
45+
46+
$this->config->set('git-hooks.pre-commit', [
47+
$preCommitHookClass,
48+
]);
49+
50+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
51+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
52+
53+
$preCommitHook = new $preCommitHookClass();
54+
$this->artisan('git-hooks:pre-commit')
55+
->expectsOutputToContain($preCommitHook->getName().' is not installed.')
56+
->assertExitCode(1);
57+
})->with('codeAnalyzersList', 'listOfFixableFiles');

tests/Features/Commands/Hooks/BladeFormatterPreCommitHookTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,6 @@
2626
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
2727
})->with('phpcsConfiguration');
2828

29-
test('Throws HookFailException and notifies when Blade Formatter is not installed', function ($listOfFixableFiles) {
30-
$this->config->set('git-hooks.code_analyzers.blade_formatter', [
31-
'path' => 'inexistent/path/to/blade-formatter',
32-
]);
33-
34-
$this->config->set('git-hooks.pre-commit', [
35-
BladeFormatterPreCommitHook::class,
36-
]);
37-
38-
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
39-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
40-
41-
$this->artisan('git-hooks:pre-commit')
42-
->expectsOutputToContain('Blade Formatter is not installed.')
43-
->assertExitCode(1);
44-
})->with('listOfFixableFiles');
45-
4629
test('Fails commit when Blade Formatter is not passing and user does not autofix the files',
4730
function ($bladeFormatterConfiguration, $listOfFixableFiles) {
4831
$this->config->set('git-hooks.code_analyzers.blade_formatter', $bladeFormatterConfiguration);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Console\Commands\Hooks\LarastanPreCommitHook;
4+
use Igorsgm\GitHooks\Facades\GitHooks;
5+
use Igorsgm\GitHooks\Traits\GitHelper;
6+
7+
uses(GitHelper::class);
8+
beforeEach(function () {
9+
$this->gitInit();
10+
$this->initializeTempDirectory(base_path('temp'));
11+
});
12+
13+
test('Fails commit when Larastan is not passing',
14+
function ($larastanConfiguration) {
15+
$this->config->set('git-hooks.code_analyzers.larastan', $larastanConfiguration);
16+
$this->config->set('git-hooks.pre-commit', [
17+
LarastanPreCommitHook::class,
18+
]);
19+
20+
$this->makeTempFile('ClassWithFixableIssues.php',
21+
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
22+
);
23+
24+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
25+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn(implode(PHP_EOL, [
26+
'AM temp/ClassWithFixableIssues.php',
27+
]));
28+
29+
$this->artisan('git-hooks:pre-commit')
30+
->expectsOutputToContain('Larastan Failed')
31+
->expectsOutputToContain('COMMIT FAILED')
32+
->assertExitCode(1);
33+
})->with('larastanConfiguration');
34+
35+
test('Commit passes when Larastan check is OK', function ($larastanConfiguration) {
36+
$this->config->set('git-hooks.code_analyzers.larastan', $larastanConfiguration);
37+
$this->config->set('git-hooks.pre-commit', [
38+
LarastanPreCommitHook::class,
39+
]);
40+
41+
$tempFileName = 'ClassWithoutFixableIssues.php';
42+
$this->makeTempFile($tempFileName,
43+
file_get_contents(__DIR__.'/../../../Fixtures/'.$tempFileName)
44+
);
45+
46+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
47+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn(implode(PHP_EOL, [
48+
'AM temp/'.$tempFileName,
49+
]));
50+
51+
$this->artisan('git-hooks:pre-commit')
52+
->doesntExpectOutputToContain('Larastan Failed')
53+
->assertSuccessful();
54+
})->with('larastanConfiguration');

tests/Features/Commands/Hooks/PHPCodeSnifferPreCommitHookTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,6 @@
2626
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
2727
})->with('phpcsConfiguration');
2828

29-
test('Throws HookFailException and notifies when PHPCS is not installed', function ($listOfFixableFiles) {
30-
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', [
31-
'phpcs_path' => 'inexistent/path/to/phpcs',
32-
]);
33-
34-
$this->config->set('git-hooks.pre-commit', [
35-
PHPCodeSnifferPreCommitHook::class,
36-
]);
37-
38-
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
39-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
40-
41-
$this->artisan('git-hooks:pre-commit')
42-
->expectsOutputToContain('PHP_CodeSniffer is not installed.')
43-
->assertExitCode(1);
44-
})->with('listOfFixableFiles');
45-
4629
test('Fails commit when PHPCS is not passing and user does not autofix the files',
4730
function ($phpCSConfiguration, $listOfFixableFiles) {
4831
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);

tests/Features/Commands/Hooks/PintPreCommitHookTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,6 @@
2626
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
2727
})->with('pintConfigurations');
2828

29-
test('Throws HookFailException and notifies when Pint is not installed', function ($listOfFixableFiles) {
30-
$this->config->set('git-hooks.code_analyzers.laravel_pint', [
31-
'path' => 'inexistent/path/to/pint',
32-
]);
33-
34-
$this->config->set('git-hooks.pre-commit', [
35-
PintPreCommitHook::class,
36-
]);
37-
38-
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
39-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
40-
41-
$this->artisan('git-hooks:pre-commit')
42-
->expectsOutputToContain('Pint is not installed.')
43-
->assertExitCode(1);
44-
})->with('listOfFixableFiles', 'pintConfigurations');
45-
4629
test('Fails commit when Pint is not passing and user does not autofix the files',
4730
function ($pintConfiguration, $listOfFixableFiles) {
4831
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);

tests/Fixtures/ClassWithFixableIssues.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66

77
class ClassWithFixableIssues
88
{
9+
protected function test(): string
10+
{
11+
return null;
12+
}
913
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Tests\Fixtures;
4+
5+
class ClassWithoutFixableIssues
6+
{
7+
}

tests/Fixtures/phpstanFixture.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- ../../vendor/nunomaduro/larastan/extension.neon
3+
4+
parameters:
5+
paths:
6+
- app/
7+
8+
# Level 9 is the highest level
9+
level: 9

0 commit comments

Comments
 (0)