Skip to content

Commit

Permalink
Allow test stubs to be published and used
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Rook committed May 14, 2024
1 parent 53df511 commit b83cceb
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 12 deletions.
10 changes: 4 additions & 6 deletions src/Commands/PestDatasetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Pest\Laravel\Commands\Traits\PublishedStubs;
use Pest\TestSuite;

use function Pest\testDirectory;
Expand All @@ -16,6 +17,8 @@
*/
final class PestDatasetCommand extends Command
{
use PublishedStubs;

/**
* The Console Command name.
*
Expand Down Expand Up @@ -58,12 +61,7 @@ public function handle(): int
File::makeDirectory(dirname($relativePath));
}

$contents = File::get(implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'pest',
'stubs',
'Dataset.php',
]));
$contents = File::get($this->resolveStubPath('Dataset.php'));

$name = mb_strtolower($name);
$contents = str_replace('{dataset_name}', $name, $contents);
Expand Down
84 changes: 84 additions & 0 deletions src/Commands/PestPublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Pest\Laravel\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Events\PublishingStubs;
use Illuminate\Support\Str;

/**
* @internal
*/
final class PestPublishCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pest:publish
{--existing : Publish and overwrite only the files that have already been published}
{--force : Overwrite any existing files}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Publish Pest test stubs for customization';

/**
* Execute the console command.
*/
public function handle(): void
{
$pestStubsPath = implode(DIRECTORY_SEPARATOR, [
$this->laravel->basePath('vendor'),
'pestphp',
'pest',
'stubs',
]);

$stubsPath = $this->laravel->basePath('stubs');

if (! is_dir($stubsPath)) {
(new Filesystem)->makeDirectory($stubsPath);
}

$stubs = [
'Browser.php',
'Dataset.php',
'Feature.php',
'Unit.php',
];

$this->laravel['events']->dispatch($event = new PublishingStubs($stubs));

foreach ($event->stubs as $stub) {
$pestStub = implode(DIRECTORY_SEPARATOR, [
$pestStubsPath,
$stub,
]);

$customStubName = (string) Str::of($stub)
->replace('.php', '.stub')
->lower()
->prepend('pest.');

$customStub = implode(DIRECTORY_SEPARATOR, [
$stubsPath,
$customStubName,
]);

if ((! $this->option('existing') && (! file_exists($customStub) || $this->option('force')))
|| ($this->option('existing') && file_exists($customStub))) {
file_put_contents($customStub, file_get_contents($pestStub));
}
}

$this->components->info('Pest test stubs published successfully.');
}
}
10 changes: 4 additions & 6 deletions src/Commands/PestTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Support\Facades\File;
use Pest\Laravel\Commands\Traits\PublishedStubs;
use Pest\Support\Str;
use Pest\TestSuite;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -20,6 +21,8 @@
*/
final class PestTestCommand extends Command implements PromptsForMissingInput
{
use PublishedStubs;

/**
* The console command name.
*
Expand Down Expand Up @@ -70,12 +73,7 @@ public function handle(): int
return 1;
}

$contents = File::get(implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'pest',
'stubs',
sprintf('%s.php', $type),
]));
$contents = File::get($this->resolveStubPath(sprintf('%s.php', $type)));

$name = mb_strtolower($name);
$name = Str::endsWith($name, 'test') ? mb_substr($name, 0, -4) : $name;
Expand Down
33 changes: 33 additions & 0 deletions src/Commands/Traits/PublishedStubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Pest\Laravel\Commands\Traits;

use Illuminate\Support\Str;

trait PublishedStubs
{
protected function resolveStubPath($stub): string
{
$pestStub = implode(DIRECTORY_SEPARATOR, [
$this->laravel->basePath('vendor'),
'pestphp',
'pest',
'stubs',
$stub,
]);

$customStubName = (string) Str::of($stub)
->replace('.php', '.stub')
->lower()
->prepend('pest.');

$customStub = implode(DIRECTORY_SEPARATOR, [
$this->laravel->basePath('stubs'),
$customStubName,
]);

return file_exists($customStub) ? $customStub : $pestStub;
}
}
2 changes: 2 additions & 0 deletions src/PestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laravel\Dusk\Console\DuskCommand;
use Pest\Laravel\Commands\PestDatasetCommand;
use Pest\Laravel\Commands\PestDuskCommand;
use Pest\Laravel\Commands\PestPublishCommand;
use Pest\Laravel\Commands\PestTestCommand;

final class PestServiceProvider extends ServiceProvider
Expand All @@ -21,6 +22,7 @@ public function register(): void
$this->commands([
PestTestCommand::class,
PestDatasetCommand::class,
PestPublishCommand::class,
]);

if (class_exists(DuskCommand::class)) {
Expand Down

0 comments on commit b83cceb

Please sign in to comment.