Skip to content

Commit

Permalink
test stencil file processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley19280 committed Jan 14, 2024
1 parent fae0668 commit 76ae918
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/CodeStencilHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
12 changes: 3 additions & 9 deletions src/Laravel/LaravelCodeStencilServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +14,8 @@

class LaravelCodeStencilServiceProvider extends ServiceProvider

Check failure on line 15 in src/Laravel/LaravelCodeStencilServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Class CodeStencil\Laravel\LaravelCodeStencilServiceProvider extends unknown class Illuminate\Support\ServiceProvider.
{
use RegistersOverrideStubLocationMacro;

/**
* Register services.
*/
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Laravel/RegistersOverrideStubLocationMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CodeStencil\Laravel;

use CodeStencil\Stencil;

trait RegistersOverrideStubLocationMacro
{
private function registerOverrideStubLocationMacro(): void
{
Stencil::macro('overrideStubLocation', function(string $path) {
$newPath = $this->substituteVariables($path);
$newPath = $this->applyFunctions($newPath);

$this->variable('overrideStubLocation', $newPath);

return $this;
});
}
}
3 changes: 1 addition & 2 deletions src/Stencil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions tests/.pest/snapshots/StencilFileProcessorTest/process_file.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class test extends Model
{
use HasFactory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class test extends Model
{
use HasFactory;
}
12 changes: 12 additions & 0 deletions tests/Fixtures/LaravelStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use CodeStencil\Stencil;

return Stencil::make()
->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;')
);
13 changes: 13 additions & 0 deletions tests/Fixtures/LaravelStubCustomLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use CodeStencil\Stencil;

return Stencil::make()
->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');
35 changes: 35 additions & 0 deletions tests/StencilFileProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use CodeStencil\Laravel\RegistersOverrideStubLocationMacro;
use CodeStencil\Laravel\StencilFileProcessor;

uses(RegistersOverrideStubLocationMacro::class);

test('process file', function() {
$realStubPath = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'LaravelStub.php';
$tmpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'LaravelStub.php';
copy($realStubPath, $tmpPath);

(new StencilFileProcessor($tmpPath, ['i_name' => '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();
});

0 comments on commit 76ae918

Please sign in to comment.