diff --git a/src/Commands/DomainModelMakeCommand.php b/src/Commands/DomainModelMakeCommand.php index ed44a90..1bc72b3 100644 --- a/src/Commands/DomainModelMakeCommand.php +++ b/src/Commands/DomainModelMakeCommand.php @@ -56,20 +56,24 @@ protected function buildClass($name) { $stub = parent::buildClass($name); - $replacements = []; + if ($this->isUsingPublishedStub()) { + return $stub; + } + + $replace = []; if ($baseModel = $this->getBaseModel()) { $baseModelClass = class_basename($baseModel); - $replacements = array_merge($replacements, [ + $replace = array_merge($replace, [ 'extends Model' => "extends {$baseModelClass}", 'use Illuminate\Database\Eloquent\Model;' => "use {$baseModel};", ]); } $stub = str_replace( - array_keys($replacements), - array_values($replacements), + array_keys($replace), + array_values($replace), $stub ); diff --git a/tests/Generator/ControllerMakeTest.php b/tests/Generator/ControllerMakeTest.php index c6889e1..b2d5c25 100644 --- a/tests/Generator/ControllerMakeTest.php +++ b/tests/Generator/ControllerMakeTest.php @@ -219,8 +219,6 @@ ]); it('does not attempt to extend base controller when using custom stubs', function ($domainName, $controllerName, $relativePath, $expectedNamespace, $stubFolder) { - $this->setupTestApplication(); - $expectedPath = base_path($relativePath); if (file_exists($expectedPath)) { diff --git a/tests/Generator/Model/MakeTest.php b/tests/Generator/Model/MakeTest.php index 4b841a5..5367043 100644 --- a/tests/Generator/Model/MakeTest.php +++ b/tests/Generator/Model/MakeTest.php @@ -2,9 +2,15 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\File; use Lunarstorm\LaravelDDD\Support\Domain; use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature; +beforeEach(function () { + $this->cleanSlate(); + $this->setupTestApplication(); +}); + it('can generate domain models', function ($domainPath, $domainRoot) { Config::set('ddd.domain_path', $domainPath); Config::set('ddd.domain_namespace', $domainRoot); @@ -211,3 +217,53 @@ ['Illuminate\Database\Eloquent\NonExistentModel', 'NonExistentModel'], ['OtherVendor\OtherPackage\Models\NonExistentModel', 'NonExistentModel'], ]); + +it('does not attempt to extend custom base models when using custom stubs', function ($baseModelClass, $baseModelName, $stubFolder) { + Config::set('ddd.base_model', $baseModelClass); + + $domain = 'Fruits'; + $modelName = 'Lemon'; + + $expectedModelPath = base_path(implode('/', [ + config('ddd.domain_path'), + $domain, + config('ddd.namespaces.model'), + "{$modelName}.php", + ])); + + if (file_exists($expectedModelPath)) { + unlink($expectedModelPath); + } + + // Publish a custom stub + $customStub = <<<'STUB' +basePath($stubFolder)); + file_put_contents(app()->basePath($stubFolder.'/model.stub'), $customStub); + expect(file_exists(app()->basePath($stubFolder.'/model.stub')))->toBeTrue(); + + Artisan::call("ddd:model {$domain}:{$modelName}"); + + expect(file_exists($expectedModelPath))->toBeTrue(); + + expect(file_get_contents($expectedModelPath)) + ->toContain('use CustomModelTrait;') + ->not->toContain("use {$baseModelClass};") + ->not->toContain("extends {$baseModelName}"); + + $this->cleanStubs(); +})->with([ + ['Domain\Shared\Models\BaseModel', 'BaseModel'], +])->with([ + 'stubs', + 'stubs/ddd', +]);