|
| 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'); |
0 commit comments