From 178f45b2aa3a2719fe7a445c682b792ebc014861 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 28 Sep 2023 12:02:34 +0800 Subject: [PATCH] wip Signed-off-by: Mior Muhammad Zaki --- src/Console/FactoryMakeCommand.php | 82 +++++++++++++++++++ .../Console/FactoryMakeCommandTest.php | 4 +- .../Console/ObserverMakeCommandTest.php | 2 +- .../Feature/Console/PolicyMakeCommandTest.php | 6 +- 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/Console/FactoryMakeCommand.php b/src/Console/FactoryMakeCommand.php index 16fa3e27..5177a039 100644 --- a/src/Console/FactoryMakeCommand.php +++ b/src/Console/FactoryMakeCommand.php @@ -2,11 +2,28 @@ namespace Orchestra\Canvas\Console; +use Orchestra\Canvas\Core\Concerns\CodeGenerator; +use Orchestra\Canvas\Core\Concerns\UsesGeneratorOverrides; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'make:factory', description: 'Create a new model factory')] class FactoryMakeCommand extends \Illuminate\Database\Console\Factories\FactoryMakeCommand { + use CodeGenerator; + use UsesGeneratorOverrides; + + /** + * Execute the console command. + * + * @return bool|null + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public function handle() + { + return $this->generateCode() ? self::SUCCESS : self::FAILURE; + } + /** * Resolve the default fully-qualified path to the stub. * @@ -17,4 +34,69 @@ protected function resolveDefaultStubPath($stub) { return __DIR__.$stub; } + + /** + * Get the full namespace for a given class, without the class name. + * + * @param string $name + * @return string + */ + protected function getNamespace($name) + { + return rtrim($this->generatorPreset()->factoryNamespace(), '\\'); + } + + /** + * Get the generator preset source path. + */ + protected function getGeneratorSourcePath(): string + { + return $this->generatorPreset()->factoryPath(); + } + + /** + * Guess the model name from the Factory name or return a default model name. + * + * @param string $name + * @return string + */ + protected function guessModelName($name) + { + if (str_ends_with($name, 'Factory')) { + $name = substr($name, 0, -7); + } + + return $this->qualifyModelUsingCanvas($name); + } + + /** + * Get the destination class path. + * + * @param string $name + * @return string + */ + protected function getPath($name) + { + return $this->getPathUsingCanvas($name); + } + + /** + * Get the root namespace for the class. + * + * @return string + */ + protected function rootNamespace() + { + return $this->rootNamespaceUsingCanvas(); + } + + /** + * Get the model for the default guard's user provider. + * + * @return string|null + */ + protected function userProviderModel() + { + return $this->userProviderModelUsingCanvas(); + } } diff --git a/tests/Feature/Console/FactoryMakeCommandTest.php b/tests/Feature/Console/FactoryMakeCommandTest.php index 25f8f4bd..124670ee 100644 --- a/tests/Feature/Console/FactoryMakeCommandTest.php +++ b/tests/Feature/Console/FactoryMakeCommandTest.php @@ -19,7 +19,7 @@ public function it_can_generate_factory_file() $this->assertFileContains([ 'namespace Database\Factories;', - 'use App\Models\Foo;', + 'use App\Foo;', 'use Illuminate\Database\Eloquent\Factories\Factory;', 'class FooFactory extends Factory', 'protected $model = Foo::class;', @@ -55,7 +55,7 @@ public function it_can_generate_factory_file_with_custom_preset() $this->assertFileContains([ 'namespace Acme\Database\Factory;', - 'use Acme\Models\Foo;', + 'use Acme\Foo;', 'use Illuminate\Database\Eloquent\Factories\Factory;', 'class FooFactory extends Factory', 'protected $model = Foo::class;', diff --git a/tests/Feature/Console/ObserverMakeCommandTest.php b/tests/Feature/Console/ObserverMakeCommandTest.php index abe29f1f..2dee8d90 100644 --- a/tests/Feature/Console/ObserverMakeCommandTest.php +++ b/tests/Feature/Console/ObserverMakeCommandTest.php @@ -50,7 +50,7 @@ public function testItCanGenerateObserverFileWithCustomNamespacedModel() $this->assertFileContains([ 'namespace App\Observers;', - 'use Acme\Model\Foo;', + 'use App\Model\Foo;', 'class FooObserver', 'public function created(Foo $foo)', 'public function updated(Foo $foo)', diff --git a/tests/Feature/Console/PolicyMakeCommandTest.php b/tests/Feature/Console/PolicyMakeCommandTest.php index 2fb38bca..41884e35 100644 --- a/tests/Feature/Console/PolicyMakeCommandTest.php +++ b/tests/Feature/Console/PolicyMakeCommandTest.php @@ -28,6 +28,8 @@ public function testItCanGeneratePolicyFileWithModelOption() $this->artisan('make:policy', ['name' => 'FooPolicy', '--model' => 'Post', '--preset' => 'canvas']) ->assertSuccessful(); + $this->app['files']->ensureDirectoryExists($this->app->basePath('Models')); + $this->assertFileContains([ 'namespace App\Policies;', 'use App\Models\Post;', @@ -54,8 +56,8 @@ public function testItCanGeneratePolicyFileWithModelOptionWithCustomNamespace() $this->assertFileContains([ 'namespace App\Policies;', - 'use Acme\Model\Post;', - 'use Illuminate\Foundation\Auth\User;', + 'use App\Model\Post;', + 'use App\Models\User;', 'class FooPolicy', 'public function viewAny(User $user)', 'public function view(User $user, Post $post)',