Skip to content

Commit a8ae18a

Browse files
committed
PHPCodeSnifferPreCommitHookTest implemented
1 parent ae76abc commit a8ae18a

File tree

6 files changed

+154
-3
lines changed

6 files changed

+154
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"orchestra/testbench": "^7.0",
3636
"pestphp/pest": "^1.22",
3737
"pestphp/pest-plugin-mock": "^1.0",
38-
"phpunit/phpunit": "^9.0"
38+
"phpunit/phpunit": "^9.0",
39+
"squizlabs/php_codesniffer": "^3.7"
3940
},
4041
"autoload": {
4142
"psr-4": {

src/Console/Commands/Hooks/PHPCodeSnifferPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function fixerCommand(): string
6363
*/
6464
public function analyzerConfigParam(): string
6565
{
66-
$phpCSStandard = trim(config('git-hooks.code_analyzers.php_code_sniffer.standard'), '/');
66+
$phpCSStandard = rtrim(config('git-hooks.code_analyzers.php_code_sniffer.standard'), '/');
6767

6868
return empty($phpCSStandard) ? '' : '--standard='.$phpCSStandard;
6969
}

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@
1414
],
1515
],
1616
]);
17+
18+
dataset('phpcsConfiguration', [
19+
'phpcs.xml file' => [
20+
[
21+
'phpcs_path' => '../../../bin/phpcs',
22+
'phpcbf_path' => '../../../bin/phpcbf',
23+
'standard' => __DIR__.'/../Fixtures/phpcsFixture.xml',
24+
],
25+
],
26+
]);
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Console\Commands\Hooks\PHPCodeSnifferPreCommitHook;
4+
use Igorsgm\GitHooks\Facades\GitHooks;
5+
use Igorsgm\GitHooks\Git\ChangedFiles;
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 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+
42+
test('Skips PHPCS check when there is none php files added to commit', function ($phpCSConfiguration) {
43+
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);
44+
$this->config->set('git-hooks.pre-commit', [
45+
PHPCodeSnifferPreCommitHook::class,
46+
]);
47+
48+
$this->makeTempFile('sample.js',
49+
file_get_contents(__DIR__.'/../../../Fixtures/sample.js')
50+
);
51+
52+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
53+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn('AM src/sample.js');
54+
55+
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
56+
})->with('phpcsConfiguration');
57+
58+
test('Throws HookFailException and notifies when PHPCS is not installed', function ($listOfFixableFiles) {
59+
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', [
60+
'phpcs_path' => 'inexistent/path/to/phpcs',
61+
]);
62+
63+
$this->config->set('git-hooks.pre-commit', [
64+
PHPCodeSnifferPreCommitHook::class,
65+
]);
66+
67+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
68+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
69+
70+
$this->artisan('git-hooks:pre-commit')
71+
->expectsOutputToContain('PHP_CodeSniffer is not installed.')
72+
->assertExitCode(1);
73+
})->with('listOfFixableFiles');
74+
75+
test('Fails commit when PHPCS is not passing and user does not autofix the files',
76+
function ($phpCSConfiguration, $listOfFixableFiles) {
77+
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);
78+
$this->config->set('git-hooks.pre-commit', [
79+
PHPCodeSnifferPreCommitHook::class,
80+
]);
81+
82+
$this->makeTempFile('ClassWithFixableIssues.php',
83+
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
84+
);
85+
86+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
87+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
88+
89+
$this->artisan('git-hooks:pre-commit')
90+
->expectsOutputToContain('PHP_CodeSniffer Failed')
91+
->expectsOutputToContain('COMMIT FAILED')
92+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'no')
93+
->assertExitCode(1);
94+
})->with('phpcsConfiguration', 'listOfFixableFiles');
95+
96+
test('Commit passes when PHPCBF fixes the files', function ($phpCSConfiguration, $listOfFixableFiles) {
97+
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);
98+
$this->config->set('git-hooks.pre-commit', [
99+
PHPCodeSnifferPreCommitHook::class,
100+
]);
101+
102+
$this->makeTempFile('ClassWithFixableIssues.php',
103+
file_get_contents(__DIR__.'/../../../Fixtures/ClassWithFixableIssues.php')
104+
);
105+
106+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
107+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixableFiles);
108+
109+
$this->artisan('git-hooks:pre-commit')
110+
->expectsOutputToContain('PHP_CodeSniffer Failed')
111+
->expectsOutputToContain('COMMIT FAILED')
112+
->expectsConfirmation('Would you like to attempt to correct files automagically?', 'yes');
113+
114+
$this->artisan('git-hooks:pre-commit')
115+
->doesntExpectOutputToContain('PHP_CodeSniffer Failed')
116+
->assertSuccessful();
117+
})->with('phpcsConfiguration', 'listOfFixableFiles');

tests/Features/Commands/Hooks/PintPreCommitHookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
->assertExitCode(1);
9393
})->with('pintConfigurations', 'listOfFixableFiles');
9494

95-
test('Commit passes when Pint fixes fix the files', function ($pintConfiguration, $listOfFixableFiles) {
95+
test('Commit passes when Pint fixes the files', function ($pintConfiguration, $listOfFixableFiles) {
9696
$this->config->set('git-hooks.code_analyzers.laravel_pint', $pintConfiguration);
9797
$this->config->set('git-hooks.pre-commit', [
9898
PintPreCommitHook::class,

tests/Fixtures/phpcsFixture.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<description>The coding standard for our project.</description>
4+
<rule ref="PSR12"/>
5+
6+
<file>app</file>
7+
<file>bootstrap</file>
8+
<file>config</file>
9+
<file>database</file>
10+
<file>resources</file>
11+
<file>routes</file>
12+
<file>tests</file>
13+
14+
<exclude-pattern>bootstrap/cache/*</exclude-pattern>
15+
<exclude-pattern>bootstrap/autoload.php</exclude-pattern>
16+
<exclude-pattern>*/migrations/*</exclude-pattern>
17+
<exclude-pattern>*/seeds/*</exclude-pattern>
18+
<exclude-pattern>*.blade.php</exclude-pattern>
19+
<exclude-pattern>*.js</exclude-pattern>
20+
21+
<!-- Show progression -->
22+
<!-- <arg value="p"/>-->
23+
</ruleset>

0 commit comments

Comments
 (0)