Skip to content

Commit

Permalink
Stub command tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Nov 10, 2024
1 parent eb86f75 commit 59eb892
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 93 deletions.
7 changes: 3 additions & 4 deletions src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function getOptions()
return [
['config', 'c', InputOption::VALUE_NONE, 'Publish the config file'],
['stubs', 's', InputOption::VALUE_NONE, 'Publish the stubs'],
['all', 'a', InputOption::VALUE_NONE, 'Publish both the config file and stubs'],
];
}

Expand All @@ -40,6 +41,7 @@ public function handle(): int
$thingsToPublish = [
...$this->option('config') ? ['config'] : [],
...$this->option('stubs') ? ['stubs'] : [],
...$this->option('all') ? ['config', 'stubs'] : [],
] ?: $this->askForThingsToPublish();

if (in_array('config', $thingsToPublish)) {
Expand All @@ -51,10 +53,7 @@ public function handle(): int

if (in_array('stubs', $thingsToPublish)) {
$this->comment('Publishing stubs...');

$this->callSilently('vendor:publish', [
'--tag' => 'ddd-stubs',
]);
$this->call('ddd:stub --all');
}

return self::SUCCESS;
Expand Down
106 changes: 17 additions & 89 deletions src/Commands/StubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\StubPublishCommand;
use Illuminate\Foundation\Events\PublishingStubs;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use ReflectionClass;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand Down Expand Up @@ -36,81 +34,17 @@ protected function getOptions()
];
}

protected function getNativeLaravelStubs()
protected function getStubChoices()
{
$laravelStubCommand = new ReflectionClass(new StubPublishCommand);

$dir = dirname($laravelStubCommand->getFileName());

return [
$dir.'/stubs/cast.inbound.stub' => 'cast.inbound.stub',
$dir.'/stubs/cast.stub' => 'cast.stub',
$dir.'/stubs/class.stub' => 'class.stub',
$dir.'/stubs/class.invokable.stub' => 'class.invokable.stub',
$dir.'/stubs/console.stub' => 'console.stub',
$dir.'/stubs/enum.stub' => 'enum.stub',
$dir.'/stubs/enum.backed.stub' => 'enum.backed.stub',
$dir.'/stubs/event.stub' => 'event.stub',
$dir.'/stubs/job.queued.stub' => 'job.queued.stub',
$dir.'/stubs/job.stub' => 'job.stub',
$dir.'/stubs/listener.typed.queued.stub' => 'listener.typed.queued.stub',
$dir.'/stubs/listener.queued.stub' => 'listener.queued.stub',
$dir.'/stubs/listener.typed.stub' => 'listener.typed.stub',
$dir.'/stubs/listener.stub' => 'listener.stub',
$dir.'/stubs/mail.stub' => 'mail.stub',
$dir.'/stubs/markdown-mail.stub' => 'markdown-mail.stub',
$dir.'/stubs/markdown-notification.stub' => 'markdown-notification.stub',
$dir.'/stubs/model.pivot.stub' => 'model.pivot.stub',
$dir.'/stubs/model.stub' => 'model.stub',
$dir.'/stubs/notification.stub' => 'notification.stub',
$dir.'/stubs/observer.plain.stub' => 'observer.plain.stub',
$dir.'/stubs/observer.stub' => 'observer.stub',
$dir.'/stubs/pest.stub' => 'pest.stub',
$dir.'/stubs/pest.unit.stub' => 'pest.unit.stub',
$dir.'/stubs/policy.plain.stub' => 'policy.plain.stub',
$dir.'/stubs/policy.stub' => 'policy.stub',
$dir.'/stubs/provider.stub' => 'provider.stub',
$dir.'/stubs/request.stub' => 'request.stub',
$dir.'/stubs/resource.stub' => 'resource.stub',
$dir.'/stubs/resource-collection.stub' => 'resource-collection.stub',
$dir.'/stubs/rule.stub' => 'rule.stub',
$dir.'/stubs/scope.stub' => 'scope.stub',
$dir.'/stubs/test.stub' => 'test.stub',
$dir.'/stubs/test.unit.stub' => 'test.unit.stub',
$dir.'/stubs/trait.stub' => 'trait.stub',
$dir.'/stubs/view-component.stub' => 'view-component.stub',
// realpath($dir . '/../../Database/Console/Factories/stubs/factory.stub') => 'factory.stub',
realpath($dir.'/../../Database/Console/Seeds/stubs/seeder.stub') => 'seeder.stub',
realpath($dir.'/../../Database/Migrations/stubs/migration.create.stub') => 'migration.create.stub',
realpath($dir.'/../../Database/Migrations/stubs/migration.stub') => 'migration.stub',
realpath($dir.'/../../Database/Migrations/stubs/migration.update.stub') => 'migration.update.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.api.stub') => 'controller.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.invokable.stub') => 'controller.invokable.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.model.api.stub') => 'controller.model.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.model.stub') => 'controller.model.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.api.stub') => 'controller.nested.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.singleton.api.stub') => 'controller.nested.singleton.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.singleton.stub') => 'controller.nested.singleton.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.stub') => 'controller.nested.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.plain.stub') => 'controller.plain.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.singleton.api.stub') => 'controller.singleton.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.singleton.stub') => 'controller.singleton.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.stub') => 'controller.stub',
realpath($dir.'/../../Routing/Console/stubs/middleware.stub') => 'middleware.stub',
...app('ddd')->stubs()->dddStubs(),
...app('ddd')->stubs()->frameworkStubs(),
];
}

protected function resolveSelectedStubs(array $names = [])
{
$stubs = [
realpath(__DIR__.'/../../stubs/action.stub') => 'action.stub',
realpath(__DIR__.'/../../stubs/dto.stub') => 'dto.stub',
realpath(__DIR__.'/../../stubs/value-object.stub') => 'value-object.stub',
realpath(__DIR__.'/../../stubs/view-model.stub') => 'view-model.stub',
realpath(__DIR__.'/../../stubs/base-view-model.stub') => 'base-view-model.stub',
realpath(__DIR__.'/../../stubs/factory.stub') => 'factory.stub',
...$this->getNativeLaravelStubs(),
];
$stubs = $this->getStubChoices();

if ($names) {
return collect($stubs)
Expand All @@ -121,14 +55,18 @@ protected function resolveSelectedStubs(array $names = [])
->all();
}

return multisearch(
$selected = multisearch(
label: 'Which stub should be published?',
placeholder: 'Search for a stub...',
options: fn (string $value) => strlen($value) > 0
? collect($stubs)->filter(fn ($stub, $path) => str($stub)->contains($value))->all()
: $stubs,
required: true
);

return collect($stubs)
->filter(fn ($stub, $path) => in_array($stub, $selected))
->all();
}

public function handle(): int
Expand All @@ -139,36 +77,26 @@ public function handle(): int
default => select(
label: 'What do you want to do?',
options: [
'all' => 'Publish all stubs',
'some' => 'Choose stubs to publish',
'all' => 'Publish all stubs',
],
required: true,
default: 'all'
default: 'some'
)
};

if ($option === 'all') {
$this->comment('Publishing all stubs...');

$this->call('vendor:publish', [
'--tag' => 'ddd-stubs',
]);

return self::SUCCESS;
}

$stubs = $this->resolveSelectedStubs($this->argument('name'));

if (! is_dir($stubsPath = $this->laravel->basePath('stubs/ddd'))) {
(new Filesystem)->makeDirectory($stubsPath, recursive: true);
}
$stubs = $option === 'all'
? $this->getStubChoices()
: $this->resolveSelectedStubs($this->argument('name'));

if (empty($stubs)) {
$this->warn('No matching stubs found.');

return self::INVALID;
}

File::ensureDirectoryExists($stubsPath = $this->laravel->basePath('stubs/ddd'));

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

foreach ($event->stubs as $from => $to) {
Expand Down
8 changes: 8 additions & 0 deletions src/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ class DomainManager

protected ?DomainCommandContext $commandContext;

protected StubManager $stubs;

public function __construct()
{
$this->autoloadFilter = null;
$this->applicationLayerFilter = null;
$this->namespaceResolver = null;
$this->commandContext = null;
$this->stubs = new StubManager;
}

public function filterAutoloadPathsUsing(callable $filter): void
Expand Down Expand Up @@ -84,4 +87,9 @@ public function packagePath($path = ''): string
{
return Path::normalize(__DIR__.'/../'.$path);
}

public function stubs(): StubManager
{
return $this->stubs;
}
}
2 changes: 2 additions & 0 deletions src/Facades/DDD.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Lunarstorm\LaravelDDD\Facades;

use Illuminate\Support\Facades\Facade;
use Lunarstorm\LaravelDDD\StubManager;

/**
* @see \Lunarstorm\LaravelDDD\DomainManager
*
* @method static void filterAutoloadPathsUsing(callable $filter)
* @method static void resolveNamespaceUsing(callable $resolver)
* @method static string packagePath(string $path = '')
* @method static StubManager stubs()
*/
class DDD extends Facade
{
Expand Down
96 changes: 96 additions & 0 deletions src/StubManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Lunarstorm\LaravelDDD;

use Illuminate\Foundation\Console\StubPublishCommand;
use ReflectionClass;

class StubManager
{
public function __construct() {}

public function allStubs()
{
return [
...$this->dddStubs(),
...$this->frameworkStubs(),
];
}

public function dddStubs()
{
return [
realpath(__DIR__.'/../stubs/action.stub') => 'action.stub',
realpath(__DIR__.'/../stubs/dto.stub') => 'dto.stub',
realpath(__DIR__.'/../stubs/value-object.stub') => 'value-object.stub',
realpath(__DIR__.'/../stubs/view-model.stub') => 'view-model.stub',
realpath(__DIR__.'/../stubs/base-view-model.stub') => 'base-view-model.stub',
realpath(__DIR__.'/../stubs/factory.stub') => 'factory.stub',
];
}

public function frameworkStubs()
{
$laravelStubCommand = new ReflectionClass(new StubPublishCommand);

$dir = dirname($laravelStubCommand->getFileName());

return [
$dir.'/stubs/cast.inbound.stub' => 'cast.inbound.stub',
$dir.'/stubs/cast.stub' => 'cast.stub',
$dir.'/stubs/class.stub' => 'class.stub',
$dir.'/stubs/class.invokable.stub' => 'class.invokable.stub',
$dir.'/stubs/console.stub' => 'console.stub',
$dir.'/stubs/enum.stub' => 'enum.stub',
$dir.'/stubs/enum.backed.stub' => 'enum.backed.stub',
$dir.'/stubs/event.stub' => 'event.stub',
$dir.'/stubs/job.queued.stub' => 'job.queued.stub',
$dir.'/stubs/job.stub' => 'job.stub',
$dir.'/stubs/listener.typed.queued.stub' => 'listener.typed.queued.stub',
$dir.'/stubs/listener.queued.stub' => 'listener.queued.stub',
$dir.'/stubs/listener.typed.stub' => 'listener.typed.stub',
$dir.'/stubs/listener.stub' => 'listener.stub',
$dir.'/stubs/mail.stub' => 'mail.stub',
$dir.'/stubs/markdown-mail.stub' => 'markdown-mail.stub',
$dir.'/stubs/markdown-notification.stub' => 'markdown-notification.stub',
$dir.'/stubs/model.pivot.stub' => 'model.pivot.stub',
$dir.'/stubs/model.stub' => 'model.stub',
$dir.'/stubs/notification.stub' => 'notification.stub',
$dir.'/stubs/observer.plain.stub' => 'observer.plain.stub',
$dir.'/stubs/observer.stub' => 'observer.stub',
// $dir . '/stubs/pest.stub' => 'pest.stub',
// $dir . '/stubs/pest.unit.stub' => 'pest.unit.stub',
$dir.'/stubs/policy.plain.stub' => 'policy.plain.stub',
$dir.'/stubs/policy.stub' => 'policy.stub',
$dir.'/stubs/provider.stub' => 'provider.stub',
$dir.'/stubs/request.stub' => 'request.stub',
$dir.'/stubs/resource.stub' => 'resource.stub',
$dir.'/stubs/resource-collection.stub' => 'resource-collection.stub',
$dir.'/stubs/rule.stub' => 'rule.stub',
$dir.'/stubs/scope.stub' => 'scope.stub',
$dir.'/stubs/test.stub' => 'test.stub',
$dir.'/stubs/test.unit.stub' => 'test.unit.stub',
$dir.'/stubs/trait.stub' => 'trait.stub',
$dir.'/stubs/view-component.stub' => 'view-component.stub',
// Factories will use a ddd-specific stub
// realpath($dir . '/../../Database/Console/Factories/stubs/factory.stub') => 'factory.stub',
realpath($dir.'/../../Database/Console/Seeds/stubs/seeder.stub') => 'seeder.stub',
realpath($dir.'/../../Database/Migrations/stubs/migration.create.stub') => 'migration.create.stub',
realpath($dir.'/../../Database/Migrations/stubs/migration.stub') => 'migration.stub',
realpath($dir.'/../../Database/Migrations/stubs/migration.update.stub') => 'migration.update.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.api.stub') => 'controller.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.invokable.stub') => 'controller.invokable.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.model.api.stub') => 'controller.model.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.model.stub') => 'controller.model.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.api.stub') => 'controller.nested.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.singleton.api.stub') => 'controller.nested.singleton.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.singleton.stub') => 'controller.nested.singleton.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.nested.stub') => 'controller.nested.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.plain.stub') => 'controller.plain.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.singleton.api.stub') => 'controller.singleton.api.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.singleton.stub') => 'controller.singleton.stub',
realpath($dir.'/../../Routing/Console/stubs/controller.stub') => 'controller.stub',
realpath($dir.'/../../Routing/Console/stubs/middleware.stub') => 'middleware.stub',
];
}
}
Loading

0 comments on commit 59eb892

Please sign in to comment.