From b404fb62d530e815a09d97e9e2e366b1f65504d1 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 20:28:19 -0500 Subject: [PATCH 01/13] Refine the controller make command. --- src/Commands/DomainControllerMakeCommand.php | 34 ++----------- tests/Generator/ControllerMakeTest.php | 53 ++++++++++++++------ 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 1cba0c9..cab4763 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -7,8 +7,6 @@ use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs; use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; -use function Laravel\Prompts\confirm; - class DomainControllerMakeCommand extends ControllerMakeCommand { use ForwardsToDomainCommands, @@ -17,33 +15,6 @@ class DomainControllerMakeCommand extends ControllerMakeCommand protected $name = 'ddd:controller'; - protected function buildModelReplacements(array $replace) - { - $modelClass = $this->parseModel($this->option('model')); - - if ( - ! app()->runningUnitTests() - && ! class_exists($modelClass) - && confirm("A {$modelClass} model does not exist. Do you want to generate it?", default: true) - ) { - $this->call('make:model', ['name' => $modelClass]); - } - - $replace = $this->buildFormRequestReplacements($replace, $modelClass); - - return array_merge($replace, [ - 'DummyFullModelClass' => $modelClass, - '{{ namespacedModel }}' => $modelClass, - '{{namespacedModel}}' => $modelClass, - 'DummyModelClass' => class_basename($modelClass), - '{{ model }}' => class_basename($modelClass), - '{{model}}' => class_basename($modelClass), - 'DummyModelVariable' => lcfirst(class_basename($modelClass)), - '{{ modelVariable }}' => lcfirst(class_basename($modelClass)), - '{{modelVariable}}' => lcfirst(class_basename($modelClass)), - ]); - } - protected function buildFormRequestReplacements(array $replace, $modelClass) { [$namespace, $storeRequestClass, $updateRequestClass] = [ @@ -99,7 +70,10 @@ protected function buildClass($name) if ($baseControllerExists) { $controllerClass = class_basename($name); - $replace["\nclass {$controllerClass}\n"] = "\nuse {$appRootNamespace}Http\Controllers\Controller;\n\nclass {$controllerClass} extends Controller\n"; + $fullyQualifiedBaseController = "{$appRootNamespace}Http\Controllers\Controller"; + $namespaceLine = "namespace {$this->getNamespace($name)};"; + $replace[$namespaceLine.PHP_EOL] = $namespaceLine.PHP_EOL.PHP_EOL."use {$fullyQualifiedBaseController};"; + $replace["class {$controllerClass}".PHP_EOL] = "class {$controllerClass} extends Controller".PHP_EOL; } $stub = str_replace( diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 8f41808..3bf0448 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -3,6 +3,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; +use Lunarstorm\LaravelDDD\Support\Path; use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature; beforeEach(function () { @@ -36,15 +37,19 @@ expect(file_exists($expectedPath))->toBeTrue(); - expect($contents = file_get_contents($expectedPath)) - ->toContain("namespace {$expectedNamespace};"); + $contents = file_get_contents($expectedPath); - if (Feature::Laravel11->exists()) { - // These assertions don't seem to pass on Laravel 10 - expect($contents) - ->toContain("use App\Http\Controllers\Controller;") - ->toContain('extends Controller'); - } + expect($contents) + ->toContain("namespace {$expectedNamespace};") + ->toContain("use App\Http\Controllers\Controller;") + ->toContain('extends Controller'); + + // if (Feature::Laravel11->exists()) { + // // These assertions don't seem to pass on Laravel 10 + // expect($contents) + // ->toContain("use App\Http\Controllers\Controller;") + // ->toContain('extends Controller'); + // } })->with([ 'Invoicing:InvoiceController' => [ 'Invoicing', @@ -70,12 +75,20 @@ expect(file_exists($expectedPath))->toBeFalse(); - Artisan::call('ddd:controller', [ + $modelExists = class_exists($modelClass); + + $command = $this->artisan('ddd:controller', [ 'name' => $controllerName, '--domain' => $domainName, '--model' => $modelName, ]); + if (! $modelExists) { + $command->expectsQuestion("A {$modelClass} model does not exist. Do you want to generate it?", false); + } + + $command->assertSuccessful()->execute(); + expect(file_exists($expectedPath))->toBeTrue(); $modelVariable = lcfirst(class_basename($modelClass)); @@ -123,20 +136,28 @@ } } - Artisan::call('ddd:controller', [ + $modelExists = class_exists($modelClass); + + $command = $this->artisan('ddd:controller', [ 'name' => $controllerName, '--domain' => $domainName, '--model' => $modelName, '--requests' => true, ]); - $output = Artisan::output(); + if (! $modelExists) { + $command->expectsQuestion("A {$modelClass} model does not exist. Do you want to generate it?", false); + } foreach ($generatedPaths as $path) { if (Feature::IncludeFilepathInGeneratorCommandOutput->exists()) { - expect($output)->toContainFilepath($path); + $command->expectsOutputToContain(Path::normalize($path)); } + } + $command->assertSuccessful()->execute(); + + foreach ($generatedPaths as $path) { expect(file_exists(base_path($path)))->toBeTrue("Expecting {$path} to exist"); } @@ -202,7 +223,9 @@ expect(file_exists($baseControllerPath))->toBeFalse(); - Artisan::call("ddd:controller {$domainName}:{$controllerName}"); + $this->artisan("ddd:controller {$domainName}:{$controllerName}") + ->assertSuccessful() + ->execute(); expect(file_exists($expectedPath))->toBeTrue(); @@ -253,7 +276,9 @@ class {{ class }} file_put_contents(app()->basePath($stubFolder.'/controller.plain.stub'), $customStub); expect(file_exists(app()->basePath($stubFolder.'/controller.plain.stub')))->toBeTrue(); - Artisan::call("ddd:controller {$domainName}:{$controllerName}"); + $this->artisan("ddd:controller {$domainName}:{$controllerName}") + ->assertSuccessful() + ->execute(); expect(file_exists($expectedPath))->toBeTrue(); From 29a328b9cb8d3100417abc1030b22c6713e63ba2 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 20:42:30 -0500 Subject: [PATCH 02/13] Fix for laravel 10. --- src/Commands/DomainControllerMakeCommand.php | 2 +- tests/Generator/ControllerMakeTest.php | 18 ++++-------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index cab4763..2e50e0f 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -64,7 +64,7 @@ protected function buildClass($name) $replace = []; $appRootNamespace = $this->laravel->getNamespace(); - $pathToAppBaseController = parent::getPath("Http\Controllers\Controller"); + $pathToAppBaseController = app_path('Http/Controllers/Controller.php'); $baseControllerExists = $this->files->exists($pathToAppBaseController); diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 3bf0448..1fb74cf 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -41,15 +41,8 @@ expect($contents) ->toContain("namespace {$expectedNamespace};") - ->toContain("use App\Http\Controllers\Controller;") + ->toContain("use App\Http\Controllers\Controller;".PHP_EOL."use Illuminate\Http\Request;") ->toContain('extends Controller'); - - // if (Feature::Laravel11->exists()) { - // // These assertions don't seem to pass on Laravel 10 - // expect($contents) - // ->toContain("use App\Http\Controllers\Controller;") - // ->toContain('extends Controller'); - // } })->with([ 'Invoicing:InvoiceController' => [ 'Invoicing', @@ -232,12 +225,9 @@ expect($contents = file_get_contents($expectedPath)) ->toContain("namespace {$expectedNamespace};"); - if (Feature::Laravel11->exists()) { - // These assertions don't seem to pass on Laravel 10 - expect($contents) - ->not->toContain("use App\Http\Controllers\Controller;") - ->not->toContain('extends Controller'); - } + expect($contents) + ->not->toContain("use App\Http\Controllers\Controller;") + ->not->toContain('extends Controller'); })->with([ 'Invoicing:InvoiceController' => [ 'Invoicing', From dba8a7cec8c1509db1186dbcc66875cb327fbf87 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 20:58:42 -0500 Subject: [PATCH 03/13] Handle laravel 10 side effect --- src/Commands/DomainControllerMakeCommand.php | 3 +++ tests/Generator/ControllerMakeTest.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 2e50e0f..729d7c4 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -76,6 +76,9 @@ protected function buildClass($name) $replace["class {$controllerClass}".PHP_EOL] = "class {$controllerClass} extends Controller".PHP_EOL; } + // Remove Laravel 10 side effect + $replace["use {$this->getNamespace($name)}\Http\Controllers\Controller;".PHP_EOL] = ''; + $stub = str_replace( array_keys($replace), array_values($replace), diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 1fb74cf..86f41e8 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -208,7 +208,7 @@ expect(file_exists($expectedPath))->toBeFalse(); // Remove the base controller - $baseControllerPath = base_path('app/Http/Controllers/Controller.php'); + $baseControllerPath = app_path('Http/Controllers/Controller.php'); if (file_exists($baseControllerPath)) { unlink($baseControllerPath); From 1537bbb21d8ee4adcc7316cb4eee3b5f205afc7a Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:07:28 -0500 Subject: [PATCH 04/13] Laravel 10 fix cont'd --- src/Commands/DomainControllerMakeCommand.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 729d7c4..2afec56 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -61,6 +61,21 @@ protected function buildClass($name) return $stub; } + // Handle Laravel 10 side effect + $invalidUse = "use {$this->getNamespace($name)}\Http\Controllers\Controller;"; + if (strpos($stub, $invalidUse) !== false) { + $laravel10Replacements = [ + $invalidUse.PHP_EOL => '', + 'extends Controller' => '', + ]; + + $stub = str_replace( + array_keys($laravel10Replacements), + array_values($laravel10Replacements), + $stub + ); + } + $replace = []; $appRootNamespace = $this->laravel->getNamespace(); @@ -76,9 +91,6 @@ protected function buildClass($name) $replace["class {$controllerClass}".PHP_EOL] = "class {$controllerClass} extends Controller".PHP_EOL; } - // Remove Laravel 10 side effect - $replace["use {$this->getNamespace($name)}\Http\Controllers\Controller;".PHP_EOL] = ''; - $stub = str_replace( array_keys($replace), array_values($replace), From 3a1d5418301200ee259cde9861080d9a70ef7360 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:10:35 -0500 Subject: [PATCH 05/13] Fix --- src/Commands/DomainControllerMakeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 2afec56..3e872b3 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -66,7 +66,7 @@ protected function buildClass($name) if (strpos($stub, $invalidUse) !== false) { $laravel10Replacements = [ $invalidUse.PHP_EOL => '', - 'extends Controller' => '', + ' extends Controller' => '', ]; $stub = str_replace( From 1e68e1de555b0d217349ffc568fae662073286f7 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:17:23 -0500 Subject: [PATCH 06/13] Try restoring base controller after test. --- tests/Generator/ControllerMakeTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 86f41e8..77b57fc 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -209,8 +209,10 @@ // Remove the base controller $baseControllerPath = app_path('Http/Controllers/Controller.php'); + $originalBaseControllerContent = null; if (file_exists($baseControllerPath)) { + $originalBaseControllerContent = file_get_contents($baseControllerPath); unlink($baseControllerPath); } @@ -228,6 +230,11 @@ expect($contents) ->not->toContain("use App\Http\Controllers\Controller;") ->not->toContain('extends Controller'); + + // Restore the base controller + if ($originalBaseControllerContent) { + file_put_contents($baseControllerPath, $originalBaseControllerContent); + } })->with([ 'Invoicing:InvoiceController' => [ 'Invoicing', From ae70e5d943a7b2781f2f435c59e17f418da9a4f8 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:22:59 -0500 Subject: [PATCH 07/13] Update syntax --- src/Commands/DomainControllerMakeCommand.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 3e872b3..80f4797 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -62,11 +62,10 @@ protected function buildClass($name) } // Handle Laravel 10 side effect - $invalidUse = "use {$this->getNamespace($name)}\Http\Controllers\Controller;"; - if (strpos($stub, $invalidUse) !== false) { + if (str($stub)->contains($invalidUse = "use {$this->getNamespace($name)}\Http\Controllers\Controller;\n")) { $laravel10Replacements = [ - $invalidUse.PHP_EOL => '', ' extends Controller' => '', + $invalidUse => '', ]; $stub = str_replace( From 6b1582ebb23ac096665b7b077cc70fc264fefcea Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:37:28 -0500 Subject: [PATCH 08/13] Expect app base controller file to exist. --- tests/Generator/ControllerMakeTest.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 77b57fc..958e46f 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -27,6 +27,7 @@ } expect(file_exists($expectedPath))->toBeFalse(); + expect(file_exists(app_path('Http/Controllers/Controller.php')))->toBeTrue(); Artisan::call("ddd:controller {$domainName}:{$controllerName}"); @@ -209,10 +210,8 @@ // Remove the base controller $baseControllerPath = app_path('Http/Controllers/Controller.php'); - $originalBaseControllerContent = null; if (file_exists($baseControllerPath)) { - $originalBaseControllerContent = file_get_contents($baseControllerPath); unlink($baseControllerPath); } @@ -230,11 +229,6 @@ expect($contents) ->not->toContain("use App\Http\Controllers\Controller;") ->not->toContain('extends Controller'); - - // Restore the base controller - if ($originalBaseControllerContent) { - file_put_contents($baseControllerPath, $originalBaseControllerContent); - } })->with([ 'Invoicing:InvoiceController' => [ 'Invoicing', From 00f9eeb224fb59af1475912c195cbacab7aa2bef Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:47:38 -0500 Subject: [PATCH 09/13] Ensure app base controller is always scaffolded. --- tests/TestCase.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index a7ba0a9..2c723c9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -259,6 +259,9 @@ protected function setupTestApplication() File::copyDirectory($folder, app_path(basename($folder))); } + File::ensureDirectoryExists(app_path('Http/Controllers')); + File::copy(__DIR__.'/.skeleton/app/Http/Controllers/Controller.php', app_path('Http/Controllers/Controller.php')); + File::copyDirectory(__DIR__.'/.skeleton/database', base_path('database')); File::copyDirectory(__DIR__.'/.skeleton/src', base_path('src')); File::copy(__DIR__.'/.skeleton/bootstrap/providers.php', base_path('bootstrap/providers.php')); From 033c2bcfd11f8041e8b8b73b67d256e83d8499a9 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 21:58:41 -0500 Subject: [PATCH 10/13] wip --- tests/Generator/ControllerMakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 958e46f..600fc9c 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -27,7 +27,6 @@ } expect(file_exists($expectedPath))->toBeFalse(); - expect(file_exists(app_path('Http/Controllers/Controller.php')))->toBeTrue(); Artisan::call("ddd:controller {$domainName}:{$controllerName}"); @@ -37,6 +36,7 @@ ); expect(file_exists($expectedPath))->toBeTrue(); + expect(file_exists(app_path('Http/Controllers/Controller.php')))->toBeTrue(); $contents = file_get_contents($expectedPath); From a6110319951a7bee66ead8674528d912792aa968 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 22:01:52 -0500 Subject: [PATCH 11/13] Attempt normalizing path. --- src/Commands/DomainControllerMakeCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 80f4797..e91e67f 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -6,6 +6,7 @@ use Lunarstorm\LaravelDDD\Commands\Concerns\ForwardsToDomainCommands; use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs; use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; +use Lunarstorm\LaravelDDD\Support\Path; class DomainControllerMakeCommand extends ControllerMakeCommand { @@ -78,7 +79,7 @@ protected function buildClass($name) $replace = []; $appRootNamespace = $this->laravel->getNamespace(); - $pathToAppBaseController = app_path('Http/Controllers/Controller.php'); + $pathToAppBaseController = Path::normalize(app_path('Http/Controllers/Controller.php')); $baseControllerExists = $this->files->exists($pathToAppBaseController); From a6c184abe018a5bfaa9fe34fef818d947a54756c Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 22:17:29 -0500 Subject: [PATCH 12/13] Try replacing PHP_EOLs --- src/Commands/DomainControllerMakeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index e91e67f..4271a2d 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -79,7 +79,7 @@ protected function buildClass($name) $replace = []; $appRootNamespace = $this->laravel->getNamespace(); - $pathToAppBaseController = Path::normalize(app_path('Http/Controllers/Controller.php')); + $pathToAppBaseController = Path::normalize(app()->path('Http/Controllers/Controller.php')); $baseControllerExists = $this->files->exists($pathToAppBaseController); @@ -87,8 +87,8 @@ protected function buildClass($name) $controllerClass = class_basename($name); $fullyQualifiedBaseController = "{$appRootNamespace}Http\Controllers\Controller"; $namespaceLine = "namespace {$this->getNamespace($name)};"; - $replace[$namespaceLine.PHP_EOL] = $namespaceLine.PHP_EOL.PHP_EOL."use {$fullyQualifiedBaseController};"; - $replace["class {$controllerClass}".PHP_EOL] = "class {$controllerClass} extends Controller".PHP_EOL; + $replace["{$namespaceLine}\n"] = "{$namespaceLine}\n\nuse {$fullyQualifiedBaseController};"; + $replace["class {$controllerClass}\n"] = "class {$controllerClass} extends Controller\n"; } $stub = str_replace( From b2c6f88091a76272e72c4f744888be09dcf057c9 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 22 Nov 2024 22:22:49 -0500 Subject: [PATCH 13/13] Remove one more PHP_EOL --- tests/Generator/ControllerMakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index 600fc9c..c447f0b 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -42,7 +42,7 @@ expect($contents) ->toContain("namespace {$expectedNamespace};") - ->toContain("use App\Http\Controllers\Controller;".PHP_EOL."use Illuminate\Http\Request;") + ->toContain("use App\Http\Controllers\Controller;\nuse Illuminate\Http\Request;") ->toContain('extends Controller'); })->with([ 'Invoicing:InvoiceController' => [