generated from pestphp/pest-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow test stubs to be published and used
- Loading branch information
Michael Rook
committed
May 14, 2024
1 parent
53df511
commit b83cceb
Showing
5 changed files
with
127 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters