From e56e7725f2e3e6648a79ca31c0091fbf238e0915 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 15 Mar 2024 23:13:46 +0800 Subject: [PATCH] [11.x] Use available `getPath()` instead of using `app_path()` to detect if base controller exists. This would be useful for custom packages generator on top of Laravel's default `make:controller` Signed-off-by: Mior Muhammad Zaki --- .../Routing/Console/ControllerMakeCommand.php | 2 +- .../Generators/ControllerMakeCommandTest.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index c4f27fba5ea3..1f0a0b43b3fc 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -127,7 +127,7 @@ protected function buildClass($name) $replace['abort(404);'] = '//'; } - $baseControllerExists = file_exists(app_path('Http/Controllers/Controller.php')); + $baseControllerExists = file_exists($this->getPath("{$rootNamespace}Http\Controllers\Controller")); if ($baseControllerExists) { $replace["use {$controllerNamespace}\Controller;\n"] = ''; diff --git a/tests/Integration/Generators/ControllerMakeCommandTest.php b/tests/Integration/Generators/ControllerMakeCommandTest.php index dad4be7ca887..529c6bf1db83 100644 --- a/tests/Integration/Generators/ControllerMakeCommandTest.php +++ b/tests/Integration/Generators/ControllerMakeCommandTest.php @@ -5,6 +5,7 @@ class ControllerMakeCommandTest extends TestCase { protected $files = [ + 'app/Http/Controllers/Controller.php', 'app/Http/Controllers/FooController.php', 'app/Models/Bar.php', 'app/Models/Foo.php', @@ -23,12 +24,36 @@ public function testItCanGenerateControllerFile() ], 'app/Http/Controllers/FooController.php'); $this->assertFileNotContains([ + 'class FooController extends Controller', 'public function __invoke(Request $request)', ], 'app/Http/Controllers/FooController.php'); $this->assertFilenameNotExists('tests/Feature/Http/Controllers/FooControllerTest.php'); } + public function testItCanGenerateControllerFileWhenBaseControllerExists() + { + $this->artisan('make:controller', ['name' => 'Controller']) + ->assertExitCode(0); + + $this->artisan('make:controller', ['name' => 'FooController']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Http\Controllers;', + 'use Illuminate\Http\Request;', + 'class Controller', + ], 'app/Http/Controllers/Controller.php'); + + $this->assertFileContains([ + 'namespace App\Http\Controllers;', + 'use Illuminate\Http\Request;', + 'class FooController extends Controller', + ], 'app/Http/Controllers/FooController.php'); + + $this->assertFilenameNotExists('tests/Feature/Http/Controllers/FooControllerTest.php'); + } + public function testItCanGenerateControllerFileWithInvokableTypeOption() { $this->artisan('make:controller', ['name' => 'FooController', '--type' => 'invokable'])