|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\Config; |
| 7 | +use Simtabi\Laranail\GitCommitChecker\Commands\PreCommitHookCommand; |
| 8 | + |
| 9 | +/** |
| 10 | + * The shipped config named `Simtabi\GitCommitChecker\Commands\PreCommitHookCommand`, a namespace this |
| 11 | + * package never had, so `install` aborted with "Class [...] not found" on every hook. |
| 12 | + */ |
| 13 | +it('names hook commands that exist', function (): void { |
| 14 | + $hooks = Config::get('laranail.git-commit-checker.hooks'); |
| 15 | + |
| 16 | + expect($hooks)->toBeArray()->not->toBeEmpty(); |
| 17 | + |
| 18 | + foreach ($hooks as $hook => $class) { |
| 19 | + expect(class_exists($class))->toBeTrue("hook [$hook] names a class that does not exist: $class") |
| 20 | + ->and(is_subclass_of($class, Command::class))->toBeTrue(); |
| 21 | + } |
| 22 | + |
| 23 | + expect($hooks['pre-commit'])->toBe(PreCommitHookCommand::class); |
| 24 | +}); |
| 25 | + |
| 26 | +/** |
| 27 | + * Registered at `laranail.git-commit-checker`, read at the bare `git-commit-checker` key: unpublished, |
| 28 | + * `enabled` was null, so the hook reported "disabled" and checked nothing, ever. |
| 29 | + */ |
| 30 | +it('runs the hook when enabled at the registered key', function (): void { |
| 31 | + Config::set('laranail.git-commit-checker.enabled', true); |
| 32 | + |
| 33 | + $this->artisan('git-commit-checker:pre-commit-hook') |
| 34 | + ->doesntExpectOutputToContain('is disabled'); |
| 35 | +}); |
| 36 | + |
| 37 | +it('skips the hook when disabled at the registered key', function (): void { |
| 38 | + Config::set('laranail.git-commit-checker.enabled', false); |
| 39 | + |
| 40 | + $this->artisan('git-commit-checker:pre-commit-hook') |
| 41 | + ->expectsOutputToContain('is disabled') |
| 42 | + ->assertSuccessful(); |
| 43 | +}); |
| 44 | + |
| 45 | +/** An application that published the old bare config/git-commit-checker.php keeps its override. */ |
| 46 | +it('still honours a config published to the old bare path', function (): void { |
| 47 | + Config::set('git-commit-checker', array_replace(Config::get('laranail.git-commit-checker'), ['enabled' => false])); |
| 48 | + Config::set('laranail.git-commit-checker.enabled', true); |
| 49 | + |
| 50 | + $this->artisan('git-commit-checker:pre-commit-hook') |
| 51 | + ->expectsOutputToContain('is disabled'); |
| 52 | +}); |
0 commit comments