Skip to content

Commit

Permalink
[11.x] Use available getPath() instead of using app_path() to detect
Browse files Browse the repository at this point in the history
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 <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 15, 2024
1 parent 08acc92 commit e56e772
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = '';
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Generators/ControllerMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'])
Expand Down

0 comments on commit e56e772

Please sign in to comment.