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();