|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Filesystem\Filesystem; |
| 4 | +use Illuminate\Support\Facades\File; |
| 5 | +use Illuminate\Support\Str; |
| 6 | + |
| 7 | +afterEach(function () { |
| 8 | + File::deleteDirectory($this->app->basePath('app/Console/GitHooks')); |
| 9 | + File::deleteDirectory($this->app->basePath('stubs')); |
| 10 | +}); |
| 11 | + |
| 12 | +it('Handles invalid hook types', function () { |
| 13 | + $this->artisan('git-hooks:make', [ |
| 14 | + 'hookType' => 'invalid', |
| 15 | + 'name' => 'InvalidHook', |
| 16 | + ]) |
| 17 | + ->assertFailed() |
| 18 | + ->expectsOutputToContain('ERROR'); |
| 19 | +}); |
| 20 | + |
| 21 | +test('Generates a hook file with a valid hook type', function () { |
| 22 | + $filesystem = mock(Filesystem::class) |
| 23 | + ->expects('put')->withArgs(function ($path, $contents) { |
| 24 | + return Str::contains($path, 'MyCustomPreCommitHook.php'); |
| 25 | + })->andReturns(true) |
| 26 | + ->shouldReceive([ |
| 27 | + 'isDirectory' => true, |
| 28 | + 'exists' => false, |
| 29 | + 'get' => '', |
| 30 | + ])->getMock(); |
| 31 | + |
| 32 | + $this->app->instance(Filesystem::class, $filesystem); |
| 33 | + |
| 34 | + $this->artisan('git-hooks:make', [ |
| 35 | + 'hookType' => 'pre-commit', |
| 36 | + 'name' => 'MyCustomPreCommitHook', |
| 37 | + ])->assertExitCode(0); |
| 38 | +}); |
| 39 | + |
| 40 | +test('Does not overwrite existing hook file', function () { |
| 41 | + $filesystem = mock(Filesystem::class)->allows([ |
| 42 | + 'isDirectory' => true, |
| 43 | + 'exists' => true, |
| 44 | + 'get' => '', |
| 45 | + ]); |
| 46 | + |
| 47 | + $this->app->instance(Filesystem::class, $filesystem); |
| 48 | + |
| 49 | + $this->artisan('git-hooks:make', [ |
| 50 | + 'hookType' => 'pre-commit', |
| 51 | + 'name' => 'MyCustomPreCommitHook', |
| 52 | + ])->expectsOutputToContain('already exists'); |
| 53 | +}); |
| 54 | + |
| 55 | +test('Uses custom stub if available', function () { |
| 56 | + $customStubContent = 'Custom stub content'; |
| 57 | + $customStubPath = $this->app->basePath('stubs/pre-commit-console.stub'); |
| 58 | + File::ensureDirectoryExists(dirname($customStubPath)); |
| 59 | + File::put($customStubPath, $customStubContent); |
| 60 | + |
| 61 | + $hookPath = $this->app->basePath('app/Console/GitHooks/PreCommitHook.php'); |
| 62 | + |
| 63 | + $this->artisan('git-hooks:make', [ |
| 64 | + 'hookType' => 'pre-commit', |
| 65 | + 'name' => 'PreCommitHook', |
| 66 | + ])->assertSuccessful(); |
| 67 | + |
| 68 | + // Check if the custom stub content is used in the generated file |
| 69 | + $generatedFileContent = File::get($hookPath); |
| 70 | + expect($generatedFileContent)->toContain($customStubContent); |
| 71 | +}); |
| 72 | + |
| 73 | +test('prompts for missing arguments and creates hook', function () { |
| 74 | + $possibleValues = implode(', ', array_keys($this->config->get('git-hooks'))); |
| 75 | + $hookPath = $this->app->basePath('app/Console/GitHooks/MyCustomPreCommitHook.php'); |
| 76 | + |
| 77 | + // Run the command without providing required arguments |
| 78 | + $this->artisan('git-hooks:make') |
| 79 | + ->expectsQuestion("What type of the git hook? Possible values: ($possibleValues)", 'pre-commit') |
| 80 | + ->expectsQuestion('What should the git hook be named?', 'MyCustomPreCommitHook') |
| 81 | + ->assertSuccessful(); |
| 82 | + |
| 83 | + expect(File::exists($hookPath))->toBeTrue(); |
| 84 | +}); |
0 commit comments