This commit is contained in:
Nuno Maduro
2020-05-11 18:38:30 +02:00
commit de2929077b
112 changed files with 6211 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Pest\Laravel\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Pest\Exceptions\InvalidConsoleArgument;
/**
* @internal
*/
final class PestDatasetCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'pest:dataset {name : The name of the dataset}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new dataset file';
/**
* Execute the console command.
*/
public function handle(): void
{
/** @var string $name */
$name = $this->argument('name');
$relativePath = sprintf('tests/Datasets/%s.php', ucfirst($name));
/* @phpstan-ignore-next-line */
$target = base_path($relativePath);
if (File::exists($target)) {
throw new InvalidConsoleArgument(sprintf('%s already exist', $target));
}
if (!File::exists(dirname($relativePath))) {
File::makeDirectory(dirname($relativePath));
}
$contents = File::get(implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'stubs',
'Dataset.php',
]));
$name = mb_strtolower($name);
$contents = str_replace('{dataset_name}', $name, $contents);
$element = Str::singular($name);
$contents = str_replace('{dataset_element}', $element, $contents);
File::put($target, str_replace('{dataset_name}', $name, $contents));
$this->output->success(sprintf('`%s` created successfully.', $relativePath));
}
}

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Pest\Laravel\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Pest\Exceptions\InvalidConsoleArgument;
/**
* @internal
*/
final class PestInstallCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'pest:install';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates Pest resources in your current PHPUnit test suite';
/**
* Execute the console command.
*/
public function handle(): void
{
/* @phpstan-ignore-next-line */
$target = base_path('tests/Pest.php');
if (File::exists($target)) {
throw new InvalidConsoleArgument(sprintf('%s already exist', $target));
}
File::copy(implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'stubs',
'Pest.php',
]), $target);
$this->output->success('`tests/Pest.php` created successfully.');
}
}

View File

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Pest\Laravel\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Pest\Exceptions\InvalidConsoleArgument;
use Pest\Support\Str;
/**
* @internal
*/
final class PestTestCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'pest:test {name : The name of the file} {--unit : Create a unit test}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new test file';
/**
* Execute the console command.
*/
public function handle(): void
{
/** @var string $name */
$name = $this->argument('name');
$type = ((bool) $this->option('unit')) ? 'Unit' : 'Feature';
$relativePath = sprintf('tests/%s/%s.php',
$type,
ucfirst($name)
);
/* @phpstan-ignore-next-line */
$target = base_path($relativePath);
if (!File::isDirectory(dirname($target))) {
File::makeDirectory(dirname($target), 0777, true, true);
}
if (File::exists($target)) {
throw new InvalidConsoleArgument(sprintf('%s already exist', $target));
}
$contents = File::get(implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'stubs',
sprintf('%s.php', $type),
]));
$name = mb_strtolower($name);
$name = Str::endsWith($name, 'test') ? mb_substr($name, 0, -4) : $name;
File::put($target, str_replace('{name}', $name, $contents));
$this->output->success(sprintf('`%s` created successfully.', $relativePath));
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Pest\Laravel;
use Illuminate\Support\ServiceProvider;
use Pest\Laravel\Commands\PestDatasetCommand;
use Pest\Laravel\Commands\PestInstallCommand;
use Pest\Laravel\Commands\PestTestCommand;
final class PestServiceProvider extends ServiceProvider
{
/**
* Register artisan commands.
*/
public function register(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
PestInstallCommand::class,
PestTestCommand::class,
PestDatasetCommand::class,
]);
}
}
}