Skip to content

Commit

Permalink
Add --api command to make:model based on laravel/framework#31197
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Jan 24, 2020
1 parent 1cf99c6 commit dc4fd9f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Commands/Database/Eloquent.php
Expand Up @@ -64,7 +64,7 @@ public function codeHasBeenGenerated(string $className): int
$this->createSeeder($className);
}

if ($this->option('controller') || $this->option('resource')) {
if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
$this->createController($className);
}

Expand Down Expand Up @@ -120,10 +120,11 @@ protected function createController(string $eloquentClassName): void
{
$controller = Str::studly(\class_basename($this->argument('name')));

$this->call('make:controller', [
$this->call('make:controller', \array_filter([
'name' => "{$controller}Controller",
'--model' => $this->option('resource') ? $eloquentClassName : null,
]);
'--model' => $this->option('resource') || $this->option('api') ? $eloquentClassName : null,
'--api' => $this->option('api'),
]));
}

/**
Expand Down Expand Up @@ -156,6 +157,7 @@ protected function getOptions()
['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder file for the model'],
['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'],
['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an api controller'],
];
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Generators/Database/EloquentTest.php
Expand Up @@ -216,6 +216,34 @@ public function it_can_generate_nested_eloquent_with_resource_controller_options
$this->assertFilenameNotExists('database/seeds/FooSeeder.php');
}

/** @test */
public function it_can_generate_nested_eloquent_with_api_controller_options_file()
{
$this->artisan('make:model', ['name' => 'Foo/Bar', '--api' => true, '--no-interaction' => true])
->assertExitCode(0);

$this->assertFileContains([
'namespace App\Foo;',
'use Illuminate\Database\Eloquent\Model;',
'class Bar extends Model',
], 'app/Foo/Bar.php');

$this->assertFileContains([
'namespace App\Http\Controllers;',
'use App\Foo\Bar;',
'use Illuminate\Http\Request;',
'class BarController extends Controller',
'public function index()',
'public function store(Request $request)',
'public function show(Bar $bar)',
'public function update(Request $request, Bar $bar)',
'public function destroy(Bar $bar)',
], 'app/Http/Controllers/BarController.php');

$this->assertFilenameNotExists('database/factories/FooFactory.php');
$this->assertFilenameNotExists('database/seeds/FooSeeder.php');
}

/** @test */
public function it_can_generate_eloquent_with_all_options_file()
{
Expand Down

0 comments on commit dc4fd9f

Please sign in to comment.