diff --git a/composer.json b/composer.json index fb65b76..ea97748 100644 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ "laravel/prompts": "^0.1.24" }, "require-dev": { + "mockery/mockery": "^1.6", "orchestra/testbench": "^8.23 || ^9.1", "pestphp/pest": "^2.34", "pestphp/pest-plugin-laravel": "^2.4", diff --git a/src/Casts/ColumnCast.php b/src/Casts/ColumnCast.php index 5035348..0e7c91f 100644 --- a/src/Casts/ColumnCast.php +++ b/src/Casts/ColumnCast.php @@ -9,11 +9,14 @@ class ColumnCast implements CastsAttributes { - public function get(Model $model, string $key, mixed $value, array $attributes): ?array + public function get(Model $model, string $key, mixed $value, array $attributes): ?array {} + + public function set(Model $model, string $key, mixed $value, array $attributes): ?string { } - public function set(Model $model, string $key, mixed $value, array $attributes): ?string + protected function locale(): string { + return app()->getLocale(); } } diff --git a/src/Console/ModelMakeCommand.php b/src/Console/ModelMakeCommand.php index 9e8ce28..67e7218 100644 --- a/src/Console/ModelMakeCommand.php +++ b/src/Console/ModelMakeCommand.php @@ -4,13 +4,11 @@ namespace LaravelLang\Models\Console; -use DragonCode\Support\Facades\Filesystem\File; use Illuminate\Console\Command; -use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Console\ModelMakeCommand as BaseMakeCommand; use Illuminate\Support\Str; -use LaravelLang\Config\Facades\Config; -use LaravelLang\Models\Eloquent\Translation; +use LaravelLang\Models\Generators\MigrationGenerator; +use LaravelLang\Models\Generators\ModelGenerator; use LaravelLang\Models\Services\ClassMap; use function Laravel\Prompts\confirm; @@ -28,7 +26,7 @@ class ModelMakeCommand extends Command public function handle(): void { - if (!$model = $this->model()) { + if (! $model = $this->model()) { info('You haven\'t selected a model.'); return; @@ -36,73 +34,19 @@ public function handle(): void $columns = $this->columns(); - $this->generateModel($model, $columns, Config::shared()->models->suffix); - $this->generateMigration($model, $columns, Config::shared()->models->suffix); + $this->generateModel($model, $columns); + $this->generateMigration($model, $columns); $this->generateHelper($model); } - protected function generateModel(string $model, array $columns, string $suffix): void + protected function generateModel(string $model, array $columns): void { - $fillable = array_map( - fn (string $column) => sprintf(" '$column',"), - $columns - ); - - $casts = array_map( - fn (string $column) => sprintf(" '%s' => ColumnCast::class,", $column), - array_filter($columns, fn (string $column) => $column !== 'locale') - ); - - $content = \DragonCode\Support\Facades\Helpers\Str::of( - file_get_contents(__DIR__ . '/../../stubs/model.stub') - )->replaceFormat([ - 'namespace' => Str::of($model)->ltrim('\\')->beforeLast('\\'), - 'model' => Str::afterLast($model, '\\'), - 'suffix' => $suffix, - 'fillable' => implode(PHP_EOL, $fillable), - 'casts' => implode(PHP_EOL, $casts), - ], '{{%s}}') - ->toString(); - - $path = ClassMap::path($model); - $extension = pathinfo($path, PATHINFO_EXTENSION); - - File::store( - Str::beforeLast($path, '.' . $extension) . $suffix . '.' . $extension, - $content - ); + ModelGenerator::of($model, $columns)->generate(); } - protected function generateMigration(string $model, array $columns, string $suffix): void + protected function generateMigration(string $model, array $columns): void { - /** @var Model $base */ - $base = new $model; - - /** @var Translation $translated */ - $translated = new ($model . $suffix); - - $columns = array_map( - fn (string $column) => sprintf(" \$table->string('$column')->nullable();"), - array_filter($columns, fn (string $column) => $column !== 'locale') - ); - - $columnType = $base->getKeyType() === 'uuid' ? 'uuid' : 'bigInteger'; - - $content = \DragonCode\Support\Facades\Helpers\Str::of( - file_get_contents(__DIR__ . '/../../stubs/migration.stub') - )->replaceFormat([ - 'modelNamespace' => $model, - 'model' => class_basename($model), - 'table' => $translated->getTable(), - 'primaryType' => $columnType, - 'columns' => implode(PHP_EOL, $columns), - ], '{{%s}}') - ->toString(); - - File::store( - database_path(sprintf("migrations/%s_create_%s_table.php", date('Y_m_d_His'), $translated->getTable())), - $content - ); + MigrationGenerator::of($model, $columns)->generate(); } protected function generateHelper(string $model): void @@ -116,8 +60,8 @@ protected function model(): ?string $this->askTranslationModel() ); - if (!$model) { - if (!$this->ascToCreate()) { + if (! $model) { + if (! $this->ascToCreate()) { return null; } @@ -130,11 +74,11 @@ protected function model(): ?string protected function columns(): array { if ($columns = $this->option('columns')) { - return collect($columns)->prepend('locale')->all(); + return $columns; } if ($columns = $this->askColumns()) { - return collect($columns)->prepend('locale')->all(); + return $columns; } return $this->columns; @@ -174,7 +118,10 @@ protected function ascToCreate(): bool protected function resolveModelClass(string $model): ?string { - $model = Str::of($model)->replace('/', '\\')->start('\\')->toString(); + $model = Str::of($model) + ->replace('/', '\\') + ->start('\\') + ->toString(); $values = [ $model, @@ -194,10 +141,10 @@ protected function resolveModelClass(string $model): ?string protected function createBaseModel(string $model): void { $this->call(BaseMakeCommand::class, [ - 'name' => Str::after($model, 'App\\Models\\'), + 'name' => Str::after($model, 'App\\Models\\'), '--migration' => true, - '--factory' => true, - '--seed' => true, + '--factory' => true, + '--seed' => true, ]); } } diff --git a/src/Console/ModelsHelperCommand.php b/src/Console/ModelsHelperCommand.php index a1b9581..84aada9 100644 --- a/src/Console/ModelsHelperCommand.php +++ b/src/Console/ModelsHelperCommand.php @@ -7,8 +7,8 @@ use DragonCode\Support\Facades\Filesystem\Directory; use Illuminate\Console\Command; use LaravelLang\Config\Facades\Config; +use LaravelLang\Models\Generators\HelperGenerator; use LaravelLang\Models\Services\ClassMap; -use LaravelLang\Models\Services\HelperGenerator; class ModelsHelperCommand extends Command { diff --git a/src/Eloquent/Translation.php b/src/Eloquent/Translation.php index f2bd10d..5e52c0e 100644 --- a/src/Eloquent/Translation.php +++ b/src/Eloquent/Translation.php @@ -10,6 +10,13 @@ abstract class Translation extends Model { public $timestamps = false; + public function translatable(): array + { + return array_filter($this->getFillable(), function (string $column) { + return $column !== 'locale'; + }); + } + protected function casts(): array { return $this->casts; diff --git a/src/Generators/Generator.php b/src/Generators/Generator.php new file mode 100644 index 0000000..ac20411 --- /dev/null +++ b/src/Generators/Generator.php @@ -0,0 +1,112 @@ +store($this->filename(), $this->make()); + } + + protected function make(): string + { + return Str::of($this->template()) + ->replaceFormat($this->resolveData(), '{{%s}}') + ->toString(); + } + + protected function baseData(): array + { + return [ + 'fqn' => $this->getFqn(), + 'namespace' => $this->getNamespace(), + 'model' => $this->getModel(), + ]; + } + + protected function resolveData(): array + { + return collect($this->data()) + ->map(fn (mixed $value) => is_array($value) ? implode(PHP_EOL, $value) : $value) + ->merge($this->baseData()) + ->all(); + } + + protected function store(string $path, string $content): void + { + File::store($path, $content); + } + + protected function getFqn(): string + { + return ltrim($this->model, '\\'); + } + + protected function getNamespace(): string + { + return IS::of($this->model) + ->ltrim('\\') + ->beforeLast('\\') + ->toString(); + } + + protected function getModel(): string + { + return IS::afterLast($this->model, '\\'); + } + + protected function baseModel(): Model + { + return $this->registry[$this->model] ??= new $this->model(); + } + + protected function translationModel(): Translation + { + $model = $this->translationModelNamespace(); + + return $this->registry[$model] ??= new $model(); + } + + protected function translationModelNamespace(): string + { + return $this->model . $this->modelSuffix(); + } + + protected function modelSuffix(): string + { + return Config::shared()->models->suffix; + } + + protected function template(): string + { + return file_get_contents($this->stub); + } +} diff --git a/src/Generators/HelperGenerator.php b/src/Generators/HelperGenerator.php new file mode 100644 index 0000000..b9ac288 --- /dev/null +++ b/src/Generators/HelperGenerator.php @@ -0,0 +1,51 @@ + $this->getHash(), + 'properties' => $this->getProperties(), + + 'translationNamespace' => $this->translationModelNamespace(), + 'translationModel' => $this->getTranslationModel(), + ]; + } + + protected function filename(): string + { + return Config::shared()->models->helpers . '/_ide_helper_models_' . $this->getHash() . '.php'; + } + + protected function getProperties(): array + { + return array_map(fn (string $attribute) => sprintf($this->properties, $attribute), $this->getTranslatable()); + } + + protected function getHash(): string + { + return md5($this->model); + } + + protected function getTranslatable(): array + { + return $this->translationModel()->translatable(); + } + + protected function getTranslationModel(): string + { + return Str::afterLast($this->translationModelNamespace(), '\\'); + } +} diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php new file mode 100644 index 0000000..6d14ad3 --- /dev/null +++ b/src/Generators/MigrationGenerator.php @@ -0,0 +1,35 @@ +string(\'%s\')->nullable();'; + + protected function data(): array + { + return [ + 'table' => $this->getTable(), + 'columns' => $this->getColumns(), + ]; + } + + protected function filename(): string + { + return database_path(sprintf('migrations/%s_create_%s_table.php', date('Y_m_d_His'), $this->getTable())); + } + + protected function getTable(): string + { + return $this->translationModel()->getTable(); + } + + protected function getColumns(): array + { + return array_map(fn (string $attribute) => sprintf($this->column, $attribute), $this->columns); + } +} diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php new file mode 100644 index 0000000..9dfa560 --- /dev/null +++ b/src/Generators/ModelGenerator.php @@ -0,0 +1,49 @@ + $this->modelSuffix(), + 'fillable' => $this->getFillable(), + ]; + } + + protected function filename(): string + { + $directory = dirname($path = $this->path()); + $filename = $this->getModel() . $this->modelSuffix(); + $extension = $this->extension($path); + + return $directory . '/' . $filename . '.' . $extension; + } + + protected function getFillable(): array + { + return array_map(function (string $attribute) { + return sprintf($this->fillables, $attribute); + }, $this->columns); + } + + protected function path(): string + { + return ClassMap::path($this->model); + } + + protected function extension(string $path): string + { + return Path::extension($path); + } +} diff --git a/src/HasTranslations.php b/src/HasTranslations.php index 186b3b6..e3100ae 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -4,17 +4,8 @@ namespace LaravelLang\Models; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; -use Illuminate\Support\Arr; -use LaravelLang\LocaleList\Locale; -use LaravelLang\Locales\Facades\Locales; -use LaravelLang\Models\Data\ContentData; -use LaravelLang\Models\Events\AllTranslationsHasBeenForgetEvent; -use LaravelLang\Models\Events\TranslationHasBeenForgetEvent; -use LaravelLang\Models\Events\TranslationHasBeenSetEvent; -use LaravelLang\Models\Exceptions\AttributeIsNotTranslatableException; -use LaravelLang\Models\Exceptions\UnavailableLocaleException; +use LaravelLang\Config\Facades\Config; /** * @mixin \Illuminate\Database\Eloquent\Concerns\HasAttributes @@ -22,143 +13,143 @@ */ trait HasTranslations { - public static function bootHasTranslations(): void - { - static::saved(function (Model $model) { - // @var \LaravelLang\Models\HasTranslations $model - $model->translation?->setAttribute('model_id', $model->getKey()); - $model->translation?->save(); - - if (! $model->translation) { - $model->setRelation('translation', $model->translation()->make()); - } - }); - - static::deleting(function (Model $model) { - // @var \LaravelLang\Models\HasTranslations $model - return $model->translation?->delete() ?? $model->translation()->delete(); - }); - - if (method_exists(static::class, 'forceDeleted')) { - static::forceDeleted(function (Model $model) { - // @var \LaravelLang\Models\HasTranslations $model - return $model->translation?->forceDelete() ?? $model->translation()->forceDelete(); - }); - } - - if (method_exists(static::class, 'restored')) { - static::restored(function (Model $model) { - // @var \LaravelLang\Models\HasTranslations $model - $model->translation()->onlyTrashed()?->restore(); - }); - } - } - + //public static function bootHasTranslations(): void + //{ + // static::saved(function (Model $model) { + // // @var \LaravelLang\Models\HasTranslations $model + // $model->translation?->setAttribute('model_id', $model->getKey()); + // $model->translation?->save(); + // + // if (! $model->translation) { + // $model->setRelation('translation', $model->translation()->make()); + // } + // }); + // + // static::deleting(function (Model $model) { + // // @var \LaravelLang\Models\HasTranslations $model + // return $model->translation?->delete() ?? $model->translation()->delete(); + // }); + // + // if (method_exists(static::class, 'forceDeleted')) { + // static::forceDeleted(function (Model $model) { + // // @var \LaravelLang\Models\HasTranslations $model + // return $model->translation?->forceDelete() ?? $model->translation()->forceDelete(); + // }); + // } + // + // if (method_exists(static::class, 'restored')) { + // static::restored(function (Model $model) { + // // @var \LaravelLang\Models\HasTranslations $model + // $model->translation()->onlyTrashed()?->restore(); + // }); + // } + //} + // public function translation(): HasOne { - $suffix = config('aaa.models.suffix'); - - return $this->hasOne(static::class . $suffix, 'parent_id'); - } - - public function setTranslation( - string $column, - array|ContentData|float|int|string|null $value, - Locale|string|null $locale = null - ): static { - $this->validateTranslationColumn($column, $locale, true); - - if (is_null($this->translation)) { - $this->setRelation('translation', $this->translation()->make()); - } - - TranslationHasBeenSetEvent::dispatch( - $this, - $column, - $locale?->value ?? $locale, - $this->getTranslation($column, $locale), - $value - ); - - $this->translation->content->set($column, $value, $locale); - - return $this; - } - - public function getTranslation(string $column, Locale|string|null $locale = null): float|int|string|null - { - $this->validateTranslationColumn($column, $locale); - - return $this->translation?->content?->get($column, $locale); - } - - public function hasTranslated(string $column, Locale|string|null $locale = null): bool - { - $this->validateTranslationColumn($column, $locale); - - return $this->translation->content?->has($column, $locale) ?? false; - } - - public function isTranslatable(string $column): bool - { - return in_array($column, $this->translatable(), true); - } - - public function forgetTranslation(string $column, Locale|string|null $locale = null): void - { - $this->validateTranslationColumn($column, $locale); - - $this->translation->content?->forget($column, $locale); - - TranslationHasBeenForgetEvent::dispatch($this, $column, $locale?->value ?? $locale); - } - - public function forgetAllTranslations(): void - { - $this->translation?->setAttribute('content', new ContentData([])); - - AllTranslationsHasBeenForgetEvent::dispatch($this); - } - - public function translatable(): array - { - return []; - } - - public function getAttribute($key): mixed - { - if ($this->isTranslatable($key)) { - return $this->getTranslation($key); - } - - return parent::getAttribute($key); - } - - public function newInstance($attributes = [], $exists = false): static - { - $basic = Arr::except($attributes, $this->translatable()); - $translatable = Arr::only($attributes, $this->translatable()); - - $model = parent::newInstance($basic, $exists); - - foreach ($translatable as $key => $value) { - $model->setTranslation($key, $value); - } - - return $model; - } - - protected function validateTranslationColumn( - string $column, - Locale|string|null $locale, - bool $withInstalled = false - ): void { - if (! $this->isTranslatable($column)) { - throw new AttributeIsNotTranslatableException($column, $this); - } + $suffix = Config::shared()->models->suffix; - if ($locale && ! $withInstalled && ! Locales::isInstalled($locale)) { - throw new UnavailableLocaleException($locale); - } + return $this->hasOne(static::class . $suffix, 'item_id'); } + // + //public function setTranslation( + // string $column, + // array|ContentData|float|int|string|null $value, + // Locale|string|null $locale = null + //): static { + // $this->validateTranslationColumn($column, $locale, true); + // + // if (is_null($this->translation)) { + // $this->setRelation('translation', $this->translation()->make()); + // } + // + // TranslationHasBeenSetEvent::dispatch( + // $this, + // $column, + // $locale?->value ?? $locale, + // $this->getTranslation($column, $locale), + // $value + // ); + // + // $this->translation->content->set($column, $value, $locale); + // + // return $this; + //} + // + //public function getTranslation(string $column, Locale|string|null $locale = null): float|int|string|null + //{ + // $this->validateTranslationColumn($column, $locale); + // + // return $this->translation?->content?->get($column, $locale); + //} + // + //public function hasTranslated(string $column, Locale|string|null $locale = null): bool + //{ + // $this->validateTranslationColumn($column, $locale); + // + // return $this->translation->content?->has($column, $locale) ?? false; + //} + // + //public function isTranslatable(string $column): bool + //{ + // return in_array($column, $this->translatable(), true); + //} + // + //public function forgetTranslation(string $column, Locale|string|null $locale = null): void + //{ + // $this->validateTranslationColumn($column, $locale); + // + // $this->translation->content?->forget($column, $locale); + // + // TranslationHasBeenForgetEvent::dispatch($this, $column, $locale?->value ?? $locale); + //} + // + //public function forgetAllTranslations(): void + //{ + // $this->translation?->setAttribute('content', new ContentData([])); + // + // AllTranslationsHasBeenForgetEvent::dispatch($this); + //} + // + //public function translatable(): array + //{ + // return []; + //} + // + //public function getAttribute($key): mixed + //{ + // if ($this->isTranslatable($key)) { + // return $this->getTranslation($key); + // } + // + // return parent::getAttribute($key); + //} + // + //public function newInstance($attributes = [], $exists = false): static + //{ + // $basic = Arr::except($attributes, $this->translatable()); + // $translatable = Arr::only($attributes, $this->translatable()); + // + // $model = parent::newInstance($basic, $exists); + // + // foreach ($translatable as $key => $value) { + // $model->setTranslation($key, $value); + // } + // + // return $model; + //} + // + //protected function validateTranslationColumn( + // string $column, + // Locale|string|null $locale, + // bool $withInstalled = false + //): void { + // if (! $this->isTranslatable($column)) { + // throw new AttributeIsNotTranslatableException($column, $this); + // } + // + // if ($locale && ! $withInstalled && ! Locales::isInstalled($locale)) { + // throw new UnavailableLocaleException($locale); + // } + //} } diff --git a/src/Services/ClassMap.php b/src/Services/ClassMap.php index 6cd33cf..f9105cb 100644 --- a/src/Services/ClassMap.php +++ b/src/Services/ClassMap.php @@ -6,6 +6,7 @@ use Composer\ClassMapGenerator\ClassMapGenerator; use DragonCode\Support\Facades\Instances\Instance; +use Illuminate\Support\Arr; use Illuminate\Support\Str; use LaravelLang\Config\Facades\Config; use LaravelLang\Models\HasTranslations; @@ -37,13 +38,23 @@ public static function path(string $class): ?string protected static function map(): array { - return ClassMapGenerator::createMap(static::modelsPath()); + $generator = static::generator(); + + foreach (static::modelsPath() as $path) { + $generator->scanPaths($path); + } + + return $generator->getClassMap()->getMap(); + } + + protected static function generator(): ClassMapGenerator + { + return new ClassMapGenerator(); } - protected static function modelsPath(): string + protected static function modelsPath(): array { - return base_path('app'); - return Config::hidden()->models->directory; + return Arr::wrap(Config::hidden()->models->directory); } protected static function isTranslatable(string $class): bool diff --git a/src/Services/HelperGenerator.php b/src/Services/HelperGenerator.php deleted file mode 100644 index 52c92dd..0000000 --- a/src/Services/HelperGenerator.php +++ /dev/null @@ -1,98 +0,0 @@ -store( - $this->make() - ); - } - - protected function make(): string - { - return Str::of($this->stub())->replaceFormat([ - 'namespace' => $this->getNamespace(), - 'model' => $this->getName(), - 'hash' => $this->getHash(), - 'properties' => $this->getProperties(), - ], '{{%s}}')->toString(); - } - - protected function getProperties(): string - { - return $this->getTranslatable($this->class) - ->map(fn (string $attribute) => sprintf($this->template, $attribute)) - ->implode(PHP_EOL); - } - - protected function getNamespace(): string - { - return IS::beforeLast($this->class, '\\'); - } - - protected function getName(): string - { - return class_basename($this->class); - } - - protected function getHash(): string - { - return md5($this->class); - } - - protected function getTranslatable(string $class): Collection - { - return collect($this->initializeModel($class)->getFillable()) - ->reject(fn (string $value) => strtolower($value) === 'locale'); - } - - protected function initializeModel(string $class): Translation - { - return new ($class . $this->modelSuffix())(); - } - - protected function modelSuffix(): string - { - return IS::ucfirst(Config::shared()->models->suffix); - } - - protected function stub(): string - { - return file_get_contents(__DIR__ . '/../../stubs/helper.stub'); - } - - protected function store(string $content): void - { - File::store($this->filename(), $content); - } - - protected function filename(): string - { - return Config::shared()->models->helpers . '/' . $this->filenamePrefix . $this->getHash() . '.php'; - } -} diff --git a/stubs/helper.stub b/stubs/helper.stub index 3a47a13..c97612e 100644 --- a/stubs/helper.stub +++ b/stubs/helper.stub @@ -3,10 +3,12 @@ namespace {{namespace}} { + use {{translationNamespace}}; use Illuminate\Database\Eloquent\Model; /** {{properties}} + * @property-read {{translationModel}} $translation */ class {{model}} extends Model {} } diff --git a/stubs/migration.stub b/stubs/migration.stub index 5a0974f..d955e03 100644 --- a/stubs/migration.stub +++ b/stubs/migration.stub @@ -2,10 +2,10 @@ declare(strict_types=1); +use {{fqn}}; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -use {{modelNamespace}}; return new class extends Migration { public function up(): void @@ -13,15 +13,13 @@ return new class extends Migration { Schema::create('{{table}}', function (Blueprint $table) { $table->id(); - $table->{{primaryType}}('item_id')->index(); + $table->foreignIdFor({{model}}::class, 'item_id')->constrained()->cascadeOnDelete(); $table->string('locale'); {{columns}} $table->unique(['item_id', 'locale']); - - $table->foreignIdFor({{model}}::class, 'item_id')->constrained()->cascadeOnDelete(); }); } diff --git a/stubs/model.stub b/stubs/model.stub index f0ea157..bfccd23 100644 --- a/stubs/model.stub +++ b/stubs/model.stub @@ -4,16 +4,12 @@ declare(strict_types=1); namespace {{namespace}}; -use LaravelLang\Models\Casts\ColumnCast; use LaravelLang\Models\Eloquent\Translation; class {{model}}{{suffix}} extends Translation { protected $fillable = [ + 'locale', {{fillable}} ]; - - protected $casts = [ -{{casts}} - ]; } diff --git a/tests/Fixtures/Models/TestModel.php b/tests/Fixtures/Models/TestModel.php index 1f53583..c38cc62 100644 --- a/tests/Fixtures/Models/TestModel.php +++ b/tests/Fixtures/Models/TestModel.php @@ -4,14 +4,16 @@ namespace Tests\Fixtures\Models; +use App\Models\TestTranslation; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use LaravelLang\Models\HasTranslations; /** * @property string $key - * @property array|string|null $title - * @property array|string|null $description + * @property string|null $title + * @property string|null $description + * @property-read TestTranslation $translation */ class TestModel extends Model { diff --git a/tests/TestCase.php b/tests/TestCase.php index 32b0946..08b6b9d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -32,7 +32,10 @@ protected function defineEnvironment($app): void $config->set('app.locale', FakeValue::LocaleMain); $config->set('app.fallback_locale', FakeValue::LocaleFallback); - $config->set(Name::Hidden() . '.models.directory', __DIR__ . '/Fixtures/Models'); + $config->set(Name::Hidden() . '.models.directory', [ + __DIR__ . '/Fixtures/Models', + base_path('app'), + ]); } protected function defineDatabaseMigrations(): void diff --git a/tests/Unit/Console/HelperTest.php b/tests/Unit/Console/HelperTest.php index fd1db63..050bfd3 100644 --- a/tests/Unit/Console/HelperTest.php +++ b/tests/Unit/Console/HelperTest.php @@ -6,6 +6,7 @@ use LaravelLang\Config\Facades\Config; use LaravelLang\Models\Console\ModelsHelperCommand; use Tests\Fixtures\Models\TestModel; +use Tests\Fixtures\Models\TestModelTranslation; use function Pest\Laravel\artisan; @@ -28,9 +29,11 @@ expect(file_get_contents($path)) ->toContain('namespace Tests\Fixtures\Models {') + ->toContain('use ' . TestModelTranslation::class . ';') ->toContain('class TestModel extends Model {}') - ->toContain('@property array|string|null $title') - ->toContain('@property array|string|null $description'); + ->toContain('@property string|null $title') + ->toContain('@property string|null $description') + ->toContain('@property-read Collection $translation'); }); test('generate one model', function () { @@ -50,7 +53,9 @@ expect(file_get_contents($path)) ->toContain('namespace Tests\Fixtures\Models {') + ->toContain('use ' . TestModelTranslation::class . ';') ->toContain('class TestModel extends Model {}') - ->toContain('@property array|string|null $title') - ->toContain('@property array|string|null $description'); + ->toContain('@property string|null $title') + ->toContain('@property string|null $description') + ->toContain('@property-read Collection $translation'); }); diff --git a/tests/Unit/Console/ModelTest.php b/tests/Unit/Console/ModelTest.php index 734f2ae..6f6eb6e 100644 --- a/tests/Unit/Console/ModelTest.php +++ b/tests/Unit/Console/ModelTest.php @@ -3,6 +3,8 @@ declare(strict_types=1); use DragonCode\Support\Facades\Filesystem\Directory; +use DragonCode\Support\Facades\Filesystem\File; +use DragonCode\Support\Facades\Filesystem\Path; use Illuminate\Foundation\Console\ModelMakeCommand as LaravelMakeModel; use LaravelLang\Config\Facades\Config; use LaravelLang\Models\Console\ModelMakeCommand as PackageMakeModel; @@ -14,6 +16,14 @@ base_path('app/Models/TestTranslation.php'), ])); +afterEach(function () { + $migrations = File::allPaths(database_path('migrations'), function (string $path) { + return Path::extension($path) === 'php'; + }); + + File::ensureDelete($migrations); +}); + test('with exists model', function () { artisan(LaravelMakeModel::class, [ 'name' => 'Test', @@ -39,21 +49,11 @@ 'description', ]; TEXT - ) - ->toContain( - << ColumnCast::class, - 'description' => ColumnCast::class, - ]; -TEXT - ); expect(file_get_contents($migration)) ->toContain('Schema::create(\'test_translations\'') ->toContain('Schema::dropIfExists(\'test_translations\')') - ->toContain('$table->bigInteger(\'item_id\')->index();') ->toContain('$table->string(\'title\')->nullable()') ->toContain('$table->string(\'description\')->nullable()');