Skip to content

Commit 805c843

Browse files
committed
- BladeFormatterPreCommitHook tests implemented
- blade-formatter using config path
1 parent 0995e71 commit 805c843

11 files changed

+236
-81
lines changed

config/git-hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
],
181181
'blade_formatter' => [
182182
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
183-
// Create your own .bladeformatterrc.json file in the root of your project
183+
'config' => env('BLADE_FORMATTER_CONFIG', '.bladeformatterrc.json'),
184184
],
185185
],
186186

src/Console/Commands/Hooks/BladeFormatterPreCommitHook.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class BladeFormatterPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
1010
{
11+
/**
12+
* @var string
13+
*/
14+
protected $analyzerConfigParam;
15+
1116
/**
1217
* Get the name of the hook.
1318
*/
@@ -25,6 +30,8 @@ public function getName(): ?string
2530
*/
2631
public function handle(ChangedFiles $files, Closure $next)
2732
{
33+
$this->analyzerConfigParam = $this->analyzerConfigParam();
34+
2835
return $this->setFileExtensions('/\.blade\.php$/')
2936
->setAnalyzerExecutable(config('git-hooks.code_analyzers.blade_formatter.path'), true)
3037
->handleCommittedFiles($files, $next);
@@ -35,14 +42,28 @@ public function handle(ChangedFiles $files, Closure $next)
3542
*/
3643
public function analyzerCommand(): string
3744
{
38-
return trim(sprintf('%s -c', $this->getAnalyzerExecutable()));
45+
return trim(sprintf('%s -c %s', $this->getAnalyzerExecutable(), $this->analyzerConfigParam));
3946
}
4047

4148
/**
4249
* Returns the command to run Blade Formatter fixer
4350
*/
4451
public function fixerCommand(): string
4552
{
46-
return trim(sprintf('%s --write', $this->getFixerExecutable()));
53+
return trim(sprintf('%s --write %s', $this->getFixerExecutable(), $this->analyzerConfigParam));
54+
}
55+
56+
/**
57+
* Returns the configuration parameter for the analyzer.
58+
* This method retrieves the Blade Formatter configuration path (usually .bladeformatterrc.json or .bladeformatterrc)
59+
* from the Git hooks configuration file and returns it as a string in the format '--config=<configFile>'.
60+
*
61+
* @return string The configuration parameter for the analyzer.
62+
*/
63+
public function analyzerConfigParam(): string
64+
{
65+
$bladeFormatterConfig = rtrim(config('git-hooks.code_analyzers.blade_formatter.config'), '/');
66+
67+
return empty($bladeFormatterConfig) ? '' : '--config='.$bladeFormatterConfig;
4768
}
4869
}

tests/Datasets/GitLogsDataset.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@
2525
]);
2626

2727
dataset('listOfFixableFiles', [
28-
'List of Fixable Files' => 'AM temp/ClassWithFixableIssues.php',
28+
'List of Fixable Files' => implode(PHP_EOL, [
29+
'AM temp/ClassWithFixableIssues.php',
30+
'AM temp/fixable-blade-file.blade.php',
31+
]),
2932
]);

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@
2424
],
2525
],
2626
]);
27+
28+
dataset('bladeFormatterConfiguration', [
29+
'.bladeformatterrc.json file' => [
30+
[
31+
'path' => '../../../../node_modules/.bin/blade-formatter',
32+
'config' => __DIR__.'/../Fixtures/bladeFormatterFixture.json',
33+
],
34+
],
35+
]);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Facades\GitHooks;
4+
use Igorsgm\GitHooks\Git\ChangedFiles;
5+
use Igorsgm\GitHooks\Tests\Fixtures\ConcreteBaseCodeAnalyzerFixture;
6+
use Igorsgm\GitHooks\Traits\GitHelper;
7+
8+
uses(GitHelper::class);
9+
beforeEach(function () {
10+
$this->gitInit();
11+
$this->initializeTempDirectory(base_path('temp'));
12+
});
13+
14+
test('Skips check if there are no files added to commit', function () {
15+
$changedFiles = mock(ChangedFiles::class)
16+
->shouldReceive('getAddedToCommit')
17+
->andReturn(collect())
18+
->getMock();
19+
20+
$next = function ($files) {
21+
return 'passed';
22+
};
23+
24+
$hook = new ConcreteBaseCodeAnalyzerFixture();
25+
$result = $hook->handleCommittedFiles($changedFiles, $next);
26+
expect($result)->toBe('passed');
27+
});
28+
29+
test('Skips check during a Merge process', function ($modifiedFilesList) {
30+
$changedFiles = new ChangedFiles($modifiedFilesList);
31+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(true);
32+
33+
$next = function ($files) {
34+
return 'passed';
35+
};
36+
37+
$hook = new ConcreteBaseCodeAnalyzerFixture();
38+
$result = $hook->handleCommittedFiles($changedFiles, $next);
39+
expect($result)->toBe('passed');
40+
})->with('modifiedFilesList');
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Console\Commands\Hooks\BladeFormatterPreCommitHook;
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('Skips Blade Formatter check when there is none .blade.php files added to commit', function ($phpCSConfiguration) {
14+
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);
15+
$this->config->set('git-hooks.pre-commit', [
16+
BladeFormatterPreCommitHook::class,
17+
]);
18+
19+
$this->makeTempFile('CommitMessageFixtureHook1.php',
20+
file_get_contents(__DIR__.'/../../../Fixtures/CommitMessageFixtureHook1.php')
21+
);
22+
23+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
24+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn('AM src/CommitMessageFixtureHook1.php');
25+
26+
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
27+
})->with('phpcsConfiguration');
28+
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+
46+
test('Fails commit when Blade Formatter is not passing and user does not autofix the files',
47+
function ($bladeFormatterConfiguration, $listOfFixableFiles) {
48+
$this->config->set('git-hooks.code_analyzers.blade_formatter', $bladeFormatterConfiguration);
49+
$this->config->set('git-hooks.pre-commit', [
50+
BladeFormatterPreCommitHook::class,
51+
]);
52+
53+
$this->makeTempFile('fixable-blade-file.blade.php',
54+
file_get_contents(__DIR__.'/../../../Fixtures/fixable-blade-file.blade.php')
55+
);
56+
57+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
58+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
59+
60+
$this->artisan('git-hooks:pre-commit')
61+
->expectsOutputToContain('Blade Formatter Failed')
62+
->expectsOutputToContain('COMMIT FAILED')
63+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'no')
64+
->assertExitCode(1);
65+
})->with('bladeFormatterConfiguration', 'listOfFixableFiles');
66+
67+
test('Commit passes when Blade Formatter fixes the files', function ($bladeFormatterConfiguration, $listOfFixableFiles) {
68+
$this->config->set('git-hooks.code_analyzers.blade_formatter', $bladeFormatterConfiguration);
69+
$this->config->set('git-hooks.pre-commit', [
70+
BladeFormatterPreCommitHook::class,
71+
]);
72+
73+
$this->makeTempFile('fixable-blade-file.blade.php',
74+
file_get_contents(__DIR__.'/../../../Fixtures/fixable-blade-file.blade.php')
75+
);
76+
77+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
78+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
79+
80+
$this->artisan('git-hooks:pre-commit')
81+
->expectsOutputToContain('Blade Formatter Failed')
82+
->expectsOutputToContain('COMMIT FAILED')
83+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'yes');
84+
85+
$this->artisan('git-hooks:pre-commit')
86+
->doesntExpectOutputToContain('Blade Formatter Failed')
87+
->assertSuccessful();
88+
})->with('bladeFormatterConfiguration', 'listOfFixableFiles');

tests/Features/Commands/Hooks/PHPCodeSnifferPreCommitHookTest.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use Igorsgm\GitHooks\Console\Commands\Hooks\PHPCodeSnifferPreCommitHook;
44
use Igorsgm\GitHooks\Facades\GitHooks;
5-
use Igorsgm\GitHooks\Git\ChangedFiles;
65
use Igorsgm\GitHooks\Traits\GitHelper;
76

87
uses(GitHelper::class);
@@ -11,34 +10,6 @@
1110
$this->initializeTempDirectory(base_path('temp'));
1211
});
1312

14-
test('Skips PHPCS check if there are no files added to commit', function () {
15-
$changedFiles = mock(ChangedFiles::class)
16-
->shouldReceive('getAddedToCommit')
17-
->andReturn(collect())
18-
->getMock();
19-
20-
$next = function ($files) {
21-
return 'passed';
22-
};
23-
24-
$hook = new PHPCodeSnifferPreCommitHook();
25-
$result = $hook->handle($changedFiles, $next);
26-
expect($result)->toBe('passed');
27-
});
28-
29-
test('Skips PHPCS check during a Merge process', function ($modifiedFilesList) {
30-
$changedFiles = new ChangedFiles($modifiedFilesList);
31-
GitHooks::shouldReceive('isMergeInProgress')->andReturn(true);
32-
33-
$next = function ($files) {
34-
return 'passed';
35-
};
36-
37-
$hook = new PHPCodeSnifferPreCommitHook();
38-
$result = $hook->handle($changedFiles, $next);
39-
expect($result)->toBe('passed');
40-
})->with('modifiedFilesList');
41-
4213
test('Skips PHPCS check when there is none php files added to commit', function ($phpCSConfiguration) {
4314
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);
4415
$this->config->set('git-hooks.pre-commit', [

tests/Features/Commands/Hooks/PintPreCommitHookTest.php

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use Igorsgm\GitHooks\Console\Commands\Hooks\PintPreCommitHook;
44
use Igorsgm\GitHooks\Facades\GitHooks;
5-
use Igorsgm\GitHooks\Git\ChangedFiles;
65
use Igorsgm\GitHooks\Traits\GitHelper;
76

87
uses(GitHelper::class);
@@ -11,34 +10,6 @@
1110
$this->initializeTempDirectory(base_path('temp'));
1211
});
1312

14-
test('Skips Pint check if there are no files added to commit', function () {
15-
$changedFiles = mock(ChangedFiles::class)
16-
->shouldReceive('getAddedToCommit')
17-
->andReturn(collect())
18-
->getMock();
19-
20-
$next = function ($files) {
21-
return 'passed';
22-
};
23-
24-
$hook = new PintPreCommitHook();
25-
$result = $hook->handle($changedFiles, $next);
26-
expect($result)->toBe('passed');
27-
});
28-
29-
test('Skips Pint check during a Merge process', function ($modifiedFilesList) {
30-
$changedFiles = new ChangedFiles($modifiedFilesList);
31-
GitHooks::shouldReceive('isMergeInProgress')->andReturn(true);
32-
33-
$next = function ($files) {
34-
return 'passed';
35-
};
36-
37-
$hook = new PintPreCommitHook();
38-
$result = $hook->handle($changedFiles, $next);
39-
expect($result)->toBe('passed');
40-
})->with('modifiedFilesList');
41-
4213
test('Skips Pint check when there is none php files added to commit', function ($pintConfiguration) {
4314
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
4415
$this->config->set('git-hooks.pre-commit', [
@@ -72,25 +43,26 @@
7243
->assertExitCode(1);
7344
})->with('listOfFixableFiles', 'pintConfigurations');
7445

75-
test('Fails commit when Pint is not passing and user does not autofix the files', function ($pintConfiguration, $listOfFixableFiles) {
76-
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
77-
$this->config->set('git-hooks.pre-commit', [
78-
PintPreCommitHook::class,
79-
]);
80-
81-
$this->makeTempFile('ClassWithFixableIssues.php',
82-
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
83-
);
84-
85-
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
86-
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
87-
88-
$this->artisan('git-hooks:pre-commit')
89-
->expectsOutputToContain('Pint Failed')
90-
->expectsOutputToContain('COMMIT FAILED')
91-
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'no')
92-
->assertExitCode(1);
93-
})->with('pintConfigurations', 'listOfFixableFiles');
46+
test('Fails commit when Pint is not passing and user does not autofix the files',
47+
function ($pintConfiguration, $listOfFixableFiles) {
48+
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
49+
$this->config->set('git-hooks.pre-commit', [
50+
PintPreCommitHook::class,
51+
]);
52+
53+
$this->makeTempFile('ClassWithFixableIssues.php',
54+
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
55+
);
56+
57+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
58+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
59+
60+
$this->artisan('git-hooks:pre-commit')
61+
->expectsOutputToContain('Pint Failed')
62+
->expectsOutputToContain('COMMIT FAILED')
63+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'no')
64+
->assertExitCode(1);
65+
})->with('pintConfigurations', 'listOfFixableFiles');
9466

9567
test('Commit passes when Pint fixes the files', function ($pintConfiguration, $listOfFixableFiles) {
9668
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Tests\Fixtures;
4+
5+
use Igorsgm\GitHooks\Console\Commands\Hooks\BaseCodeAnalyzerPreCommitHook;
6+
7+
class ConcreteBaseCodeAnalyzerFixture extends BaseCodeAnalyzerPreCommitHook
8+
{
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"indentSize": 6,
3+
"wrapAttributes": "auto",
4+
"wrapLineLength": 120,
5+
"endWithNewLine": true,
6+
"endOfLine": "LF",
7+
"useTabs": false,
8+
"sortTailwindcssClasses": true,
9+
"sortHtmlAttributes": "none",
10+
"noMultipleEmptyLines": false,
11+
"noPhpSyntaxCheck": false
12+
}

0 commit comments

Comments
 (0)