From 76ae91864fc9aef93fd0067dd1d16d006788fb5e Mon Sep 17 00:00:00 2001 From: Riley Schoppa Date: Sun, 14 Jan 2024 15:41:55 -0500 Subject: [PATCH] test stencil file processor --- src/CodeStencilHelpers.php | 2 +- .../LaravelCodeStencilServiceProvider.php | 12 ++----- .../RegistersOverrideStubLocationMacro.php | 20 +++++++++++ src/Stencil.php | 3 +- .../process_file.snap | 11 ++++++ .../process_file_custom_location.snap | 11 ++++++ tests/Fixtures/LaravelStub.php | 12 +++++++ tests/Fixtures/LaravelStubCustomLocation.php | 13 +++++++ tests/StencilFileProcessorTest.php | 35 +++++++++++++++++++ 9 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 src/Laravel/RegistersOverrideStubLocationMacro.php create mode 100644 tests/.pest/snapshots/StencilFileProcessorTest/process_file.snap create mode 100644 tests/.pest/snapshots/StencilFileProcessorTest/process_file_custom_location.snap create mode 100644 tests/Fixtures/LaravelStub.php create mode 100644 tests/Fixtures/LaravelStubCustomLocation.php create mode 100644 tests/StencilFileProcessorTest.php diff --git a/src/CodeStencilHelpers.php b/src/CodeStencilHelpers.php index 2d747da..7bf867b 100644 --- a/src/CodeStencilHelpers.php +++ b/src/CodeStencilHelpers.php @@ -26,7 +26,7 @@ public function multilineComment(string|array $comment): static return $this->foreach($lines, fn(self $s, string $line) => $s->line($line)); } - public function phpdoc(string|array|null $summary = null, string|array|null $description = null, ?array $tags = null): static + public function phpdoc(string|array $summary = null, string|array $description = null, array $tags = null): static { if ($summary === null) { $summary = []; diff --git a/src/Laravel/LaravelCodeStencilServiceProvider.php b/src/Laravel/LaravelCodeStencilServiceProvider.php index 452f7a1..2d0fef0 100644 --- a/src/Laravel/LaravelCodeStencilServiceProvider.php +++ b/src/Laravel/LaravelCodeStencilServiceProvider.php @@ -2,7 +2,6 @@ namespace CodeStencil\Laravel; -use CodeStencil\Stencil; use Illuminate\Console\Events\CommandFinished; use Illuminate\Console\Events\CommandStarting; use Illuminate\Support\Facades\App; @@ -15,6 +14,8 @@ class LaravelCodeStencilServiceProvider extends ServiceProvider { + use RegistersOverrideStubLocationMacro; + /** * Register services. */ @@ -63,14 +64,7 @@ public function boot(): void }); } - Stencil::macro('overrideStubLocation', function(string $path) { - $newPath = $this->substituteVariables($path); - $newPath = $this->applyFunctions($newPath); - - $this->variable('overrideStubLocation', $newPath); - - return $this; - }); + $this->registerOverrideStubLocationMacro(); } private function getFiles(): array diff --git a/src/Laravel/RegistersOverrideStubLocationMacro.php b/src/Laravel/RegistersOverrideStubLocationMacro.php new file mode 100644 index 0000000..9730d8c --- /dev/null +++ b/src/Laravel/RegistersOverrideStubLocationMacro.php @@ -0,0 +1,20 @@ +substituteVariables($path); + $newPath = $this->applyFunctions($newPath); + + $this->variable('overrideStubLocation', $newPath); + + return $this; + }); + } +} diff --git a/src/Stencil.php b/src/Stencil.php index 91d7db4..ecc4da7 100644 --- a/src/Stencil.php +++ b/src/Stencil.php @@ -3,11 +3,10 @@ namespace CodeStencil; use Closure; +use function CodeStencil\Utility\array_flatten; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; -use function CodeStencil\Utility\array_flatten; - class Stencil { use CodeStencilHelpers; diff --git a/tests/.pest/snapshots/StencilFileProcessorTest/process_file.snap b/tests/.pest/snapshots/StencilFileProcessorTest/process_file.snap new file mode 100644 index 0000000..761ed9a --- /dev/null +++ b/tests/.pest/snapshots/StencilFileProcessorTest/process_file.snap @@ -0,0 +1,11 @@ +php() + ->namespace('App\Models') + ->use('Illuminate\Database\Eloquent\Factories\HasFactory') + ->use('Illuminate\Database\Eloquent\Model') + ->curlyStatement('class i_name extends Model', fn(Stencil $s) => $s + ->line('use HasFactory;') + ); diff --git a/tests/Fixtures/LaravelStubCustomLocation.php b/tests/Fixtures/LaravelStubCustomLocation.php new file mode 100644 index 0000000..cab5745 --- /dev/null +++ b/tests/Fixtures/LaravelStubCustomLocation.php @@ -0,0 +1,13 @@ +php() + ->namespace('App\Models') + ->use('Illuminate\Database\Eloquent\Factories\HasFactory') + ->use('Illuminate\Database\Eloquent\Model') + ->curlyStatement('class i_name extends Model', fn(Stencil $s) => $s + ->line('use HasFactory;') + ) + ->overrideStubLocation(__DIR__ . DIRECTORY_SEPARATOR . 'Output.php'); diff --git a/tests/StencilFileProcessorTest.php b/tests/StencilFileProcessorTest.php new file mode 100644 index 0000000..fd3ff4a --- /dev/null +++ b/tests/StencilFileProcessorTest.php @@ -0,0 +1,35 @@ + 'test']))(); + + $contents = file_get_contents($tmpPath); + + expect($contents)->toMatchSnapshot(); +}); + +test('process file custom location', function() { + $realStubPath = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'LaravelStubCustomLocation.php'; + $tmpDir = sys_get_temp_dir(); + $tmpPath = $tmpDir . DIRECTORY_SEPARATOR . 'LaravelStubCustomLocation.php'; + copy($realStubPath, $tmpPath); + + $this->registerOverrideStubLocationMacro(); + + (new StencilFileProcessor($tmpPath, ['i_name' => 'test']))(); + + $contents = file_get_contents($tmpDir . DIRECTORY_SEPARATOR . 'Output.php'); + + expect($contents)->toMatchSnapshot(); + + expect(file_exists($tmpPath))->toBeFalse(); +});