Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Sep 28, 2023
1 parent 46dc048 commit 178f45b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
82 changes: 82 additions & 0 deletions src/Console/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions tests/Feature/Console/FactoryMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;',
Expand Down Expand Up @@ -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;',
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Console/ObserverMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down
6 changes: 4 additions & 2 deletions tests/Feature/Console/PolicyMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;',
Expand All @@ -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)',
Expand Down

0 comments on commit 178f45b

Please sign in to comment.