From a3fdbde07fc7b00fa44821c8e5a9f969e6576343 Mon Sep 17 00:00:00 2001 From: Eder Soares Date: Thu, 26 Oct 2023 13:41:22 -0300 Subject: [PATCH 1/3] Adds test to `--requests` option --- .../Console/ControllerMakeCommandTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Feature/Console/ControllerMakeCommandTest.php b/tests/Feature/Console/ControllerMakeCommandTest.php index 032e3ec2..de7cc8c0 100644 --- a/tests/Feature/Console/ControllerMakeCommandTest.php +++ b/tests/Feature/Console/ControllerMakeCommandTest.php @@ -9,6 +9,8 @@ class ControllerMakeCommandTest extends TestCase { protected $files = [ 'app/Http/Controllers/FooController.php', + 'app/Http/Requests/StoreFooRequest.php', + 'app/Http/Requests/UpdateFooRequest.php', 'tests/Feature/Http/Controllers/FooControllerTest.php', ]; @@ -226,4 +228,30 @@ public function it_can_generate_controller_file_with_tests() $this->assertFilenameExists('app/Http/Controllers/FooController.php'); $this->assertFilenameExists('tests/Feature/Http/Controllers/FooControllerTest.php'); } + + /** @test */ + public function it_can_generate_controller_with_model_and_requests() + { + $this->artisan('make:controller', ['name' => 'FooController', '--model' => 'Foo', '--requests' => true, '--preset' => 'canvas']) + ->expectsQuestion('A App\Models\Foo model does not exist. Do you want to generate it?', false) + ->assertSuccessful(); + + $this->assertFileContains([ + 'namespace App\Http\Controllers;', + 'use App\Models\Foo;', + 'use App\Http\Requests\StoreFooRequest;', + 'use App\Http\Requests\UpdateFooRequest;', + 'public function index()', + 'public function create()', + 'public function store(StoreFooRequest $request)', + 'public function show(Foo $foo)', + 'public function edit(Foo $foo)', + 'public function update(UpdateFooRequest $request, Foo $foo)', + 'public function destroy(Foo $foo)', + ], 'app/Http/Controllers/FooController.php'); + + $this->assertFilenameExists('app/Http/Controllers/FooController.php'); + $this->assertFilenameExists('app/Http/Requests/StoreFooRequest.php'); + $this->assertFilenameExists('app/Http/Requests/UpdateFooRequest.php'); + } } From b7327ad2e00c3e2ae8deb34183c7b8f95f1e5acd Mon Sep 17 00:00:00 2001 From: Eder Soares Date: Thu, 26 Oct 2023 13:41:54 -0300 Subject: [PATCH 2/3] Adds test to `--requests` option with custom preset --- .../Console/ControllerMakeCommandTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Feature/Console/ControllerMakeCommandTest.php b/tests/Feature/Console/ControllerMakeCommandTest.php index de7cc8c0..6364425d 100644 --- a/tests/Feature/Console/ControllerMakeCommandTest.php +++ b/tests/Feature/Console/ControllerMakeCommandTest.php @@ -254,4 +254,34 @@ public function it_can_generate_controller_with_model_and_requests() $this->assertFilenameExists('app/Http/Requests/StoreFooRequest.php'); $this->assertFilenameExists('app/Http/Requests/UpdateFooRequest.php'); } + + /** @test */ + public function it_can_generate_controller_with_model_and_requests_with_custom_preset() + { + $this->instance('orchestra.canvas', new Laravel( + ['namespace' => 'Acme', 'model' => ['namespace' => 'Acme\Models']], $this->app->basePath() + )); + + $this->artisan('make:controller', ['name' => 'FooController', '--model' => 'Foo', '--requests' => true, '--preset' => 'canvas']) + ->expectsQuestion('A Acme\Models\Foo model does not exist. Do you want to generate it?', false) + ->assertSuccessful(); + + $this->assertFileContains([ + 'namespace Acme\Http\Controllers;', + 'use Acme\Models\Foo;', + 'use Acme\Http\Requests\StoreFooRequest;', + 'use Acme\Http\Requests\UpdateFooRequest;', + 'public function index()', + 'public function create()', + 'public function store(StoreFooRequest $request)', + 'public function show(Foo $foo)', + 'public function edit(Foo $foo)', + 'public function update(UpdateFooRequest $request, Foo $foo)', + 'public function destroy(Foo $foo)', + ], 'app/Http/Controllers/FooController.php'); + + $this->assertFilenameExists('app/Http/Controllers/FooController.php'); + $this->assertFilenameExists('app/Http/Requests/StoreFooRequest.php'); + $this->assertFilenameExists('app/Http/Requests/UpdateFooRequest.php'); + } } From 13e6e5be6a08bcef905506edc4610a5ef4024b78 Mon Sep 17 00:00:00 2001 From: Eder Soares Date: Thu, 26 Oct 2023 13:42:40 -0300 Subject: [PATCH 3/3] Replaces requests namespace correctly --- src/Console/ControllerMakeCommand.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Console/ControllerMakeCommand.php b/src/Console/ControllerMakeCommand.php index f5b4790e..d580369a 100644 --- a/src/Console/ControllerMakeCommand.php +++ b/src/Console/ControllerMakeCommand.php @@ -96,6 +96,12 @@ protected function buildModelReplacements(array $replace) $replace = $this->buildFormRequestReplacements($replace, $modelClass); + if ($this->option('requests')) { + $namespace = $this->rootNamespace(); + $replace['{{ namespacedRequests }}'] = str_replace('App\\', $namespace, $replace['{{ namespacedRequests }}']); + $replace['{{namespacedRequests}}'] = str_replace('App\\', $namespace, $replace['{{namespacedRequests}}']); + } + return array_merge($replace, [ 'DummyFullModelClass' => $modelClass, '{{ namespacedModel }}' => $modelClass,