Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Add the ability to publish and use test stubs #55

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
83 changes: 83 additions & 0 deletions src/Commands/PestPublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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, [
dirname(__DIR__, 3),
'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
32 changes: 32 additions & 0 deletions src/Commands/Traits/PublishedStubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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, [
dirname(__DIR__, 3),
'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