Skip to content

Commit

Permalink
Rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 17, 2024
1 parent 01be0ac commit 526a503
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 124 deletions.
10 changes: 10 additions & 0 deletions src/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Models\Console;

class ModelMakeCommand
{

}
2 changes: 1 addition & 1 deletion src/Console/ModelsHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class ModelsHelperCommand extends Command
{
protected $signature = 'lang:models';
protected $signature = 'lang:models {model?}';

protected $description = 'Generating autocomplete translatable properties for models';

Expand Down
3 changes: 3 additions & 0 deletions src/Data/ContentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use LaravelLang\Models\Concerns\HasStrings;
use LaravelLang\Models\Exceptions\UnavailableLocaleException;

/**
* @deprecated
*/
class ContentData implements Arrayable, Jsonable
{
use HasStrings;
Expand Down
5 changes: 1 addition & 4 deletions src/Translation.php → src/Eloquent/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

declare(strict_types=1);

namespace LaravelLang\Models;
namespace LaravelLang\Models\Eloquent;

use Illuminate\Database\Eloquent\Model;

abstract class Translation extends Model
{
protected $fillable = [
'aa'
];
}
6 changes: 3 additions & 3 deletions stubs/migration_create.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ return new class extends Migration {
Schema::create('{{table}}', function (Blueprint $table) {
$table->id();

$table->{{primaryType}}('{{primaryName}}')->index();

$table->string('locale');

$table->{{primaryType}}('item_id')->index();

{{columns}}

$table->unique(['{{primaryName}}', 'locale']);
$table->unique(['item_id', 'locale']);
});
}

Expand Down
23 changes: 0 additions & 23 deletions stubs/migration_update.stub

This file was deleted.

16 changes: 2 additions & 14 deletions tests/Fixtures/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use LaravelLang\Models\Data\ContentData;
use LaravelLang\Models\HasTranslations;

/**
* @property string $key
* @property array|string|ContentData $title
* @property array|string|ContentData $description
* @property array|string|null $title
* @property array|string|null $description
*/
class TestModel extends Model
{
Expand All @@ -21,16 +20,5 @@ class TestModel extends Model

protected $fillable = [
'key',
//'title',
//'description',
];

/** @deprecated */
public function translatable(): array
{
return [
'title',
'description',
];
}
}
3 changes: 2 additions & 1 deletion tests/Fixtures/Models/TestModelTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace Tests\Fixtures\Models;

use LaravelLang\Models\Translation;
use LaravelLang\Models\Eloquent\Translation;

class TestModelTranslation extends Translation
{
protected $fillable = [
'locale',
'title',
'description',
];
Expand Down
27 changes: 7 additions & 20 deletions tests/Helpers/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use LaravelLang\Models\Data\ContentData;
use Illuminate\Support\Collection;
use Tests\Constants\FakeValue;
use Tests\Fixtures\Models\TestModel;

Expand Down Expand Up @@ -31,26 +31,13 @@ function fakeTranslation(
?string $custom = null,
?string $uninstalled = null
): void {
$data = [];

if ($text) {
$data[FakeValue::ColumnTitle][FakeValue::LocaleMain] = $text;
}

if ($fallback) {
$data[FakeValue::ColumnTitle][FakeValue::LocaleFallback] = $fallback;
}

if ($custom) {
$data[FakeValue::ColumnTitle][FakeValue::LocaleCustom] = $custom;
}

if ($uninstalled) {
$data[FakeValue::ColumnTitle][FakeValue::LocaleUninstalled] = $uninstalled;
}

$model->translation->fill([
'content' => new ContentData($data),
FakeValue::ColumnTitle => collect()
->when($text, fn (Collection $values) => $values->put(FakeValue::LocaleMain, $text))
->when($fallback, fn (Collection $values) => $values->put(FakeValue::LocaleFallback, $fallback))
->when($custom, fn (Collection $values) => $values->put(FakeValue::LocaleCustom, $custom))
->when($uninstalled, fn (Collection $values) => $values->put(FakeValue::LocaleUninstalled, $uninstalled))
->all()
])->save();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

use Illuminate\Foundation\Testing\RefreshDatabase;

uses(Tests\TestCase::class, RefreshDatabase::class)->in('Unit');
uses(Tests\TestCase::class, RefreshDatabase::class)->in(__DIR__);
8 changes: 3 additions & 5 deletions tests/Unit/Console/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
expect($path)->toBeReadableFile();

expect(file_get_contents($path))
->toContain('Tests\Fixtures\Models')
->toContain('namespace Tests\Fixtures\Models;')
->toContain('TestModel')
->toContain('@property string $title')
->toContain('@property string $description')
->not->toContain('@property string $key')
->not->toContain('Translation');
->toContain('@property array|string|null $title')
->toContain('@property array|string|null $description');
});
32 changes: 20 additions & 12 deletions tests/Unit/Console/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
declare(strict_types=1);

use DragonCode\Support\Facades\Filesystem\Directory;
use LaravelLang\Models\Console\ModelsHelperCommand;
use Illuminate\Foundation\Console\ModelMakeCommand as LaravelMakeModel;
use LaravelLang\Config\Facades\Config;
use LaravelLang\Models\Console\ModelMakeCommand as PackageMakeModel;

use function Pest\Laravel\artisan;

Expand All @@ -13,24 +15,21 @@
]));

test('generation', function () {
$path = base_path('app/Models/TestTranslation.php');

expect($path)->not->toBeReadableFile();

artisan(ModelsHelperCommand::class, [
artisan(LaravelMakeModel::class, [
'name' => 'Test',
])->run();

artisan(ModelGenerator::class, [
'model' => 'App\Models\Test',
'columns' => ['test', 'description'],
artisan(PackageMakeModel::class, [
'model' => 'App\Models\Test',
'columns' => ['title', 'description'],
])->run();

expect($path)->toBeReadableFile();
$model = base_path('app/Models/TestTranslation.php');
$migration = database_path('migrations/' . date('Y_m_d_His') . '_create_test_translations_table.php');
$helper = sprintf('%s/_ide_helper_models_%s.php', Config::shared()->models->helpers, md5('App\Models\Test'));

expect(file_get_contents($path))
expect(file_get_contents($model))
->toContain('App\Models')
->toContain('TestTranslation')
->toContain('class TestTranslation extends Translation')
->toContain(
<<<TEXT
Expand All @@ -41,4 +40,13 @@
];
TEXT
);

expect(file_get_contents($migration))
->toContain('Schema::create(\'test_translations\'')
->toContain('Schema::dropIfExists(\'test_translations\')')
->toContain('$table->bigInteger(\'item_id\')->index();')
->toContain('$table->json(\'title\')')
->toContain('$table->json(\'description\')');

expect($helper)->toBeReadableFile();
});
10 changes: 3 additions & 7 deletions tests/Unit/Events/AllTranslationsHasBeenForgetEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
'key' => fake()->word,

FakeValue::ColumnTitle => [
FakeValue::LocaleMain => 'qwerty 10',
FakeValue::LocaleMain => 'qwerty 10',
FakeValue::LocaleFallback => 'qwerty 11',
],

FakeValue::ColumnDescription => [
FakeValue::LocaleMain => 'qwerty 20',
FakeValue::LocaleMain => 'qwerty 20',
FakeValue::LocaleFallback => 'qwerty 21',
],
]);
Expand Down Expand Up @@ -50,9 +50,5 @@
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleMain))->toBeNull();
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleFallback))->toBeNull();

expect($model->translation->content->getRaw())->toBeEmpty();

Event::assertDispatched(function (AllTranslationsHasBeenForgetEvent $event) use ($model) {
return $event->model->getKey() === $model->getKey();
});
Event::assertDispatched(AllTranslationsHasBeenForgetEvent::class);
});
16 changes: 8 additions & 8 deletions tests/Unit/Events/TranslationHasBeenForgetEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
'key' => fake()->word,

FakeValue::ColumnTitle => [
FakeValue::LocaleMain => 'qwerty 10',
FakeValue::LocaleMain => 'qwerty 10',
FakeValue::LocaleFallback => 'qwerty 11',
],

FakeValue::ColumnDescription => [
FakeValue::LocaleMain => 'qwerty 20',
FakeValue::LocaleMain => 'qwerty 20',
FakeValue::LocaleFallback => 'qwerty 21',
],
]);

Event::fake(TranslationHasBeenForgetEvent::class);
});

test('column', function () {
test('forget column', function () {
$model = findFakeModel();

expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleMain))->toBeTrue();
Expand All @@ -52,12 +52,12 @@

Event::assertDispatched(function (TranslationHasBeenForgetEvent $event) use ($model) {
return $event->model->getKey() === $model->getKey()
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === null;
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === null;
});
});

test('locale', function () {
test('forget locale', function () {
$model = findFakeModel();

expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleMain))->toBeTrue();
Expand All @@ -84,7 +84,7 @@

Event::assertDispatched(function (TranslationHasBeenForgetEvent $event) use ($model) {
return $event->model->getKey() === $model->getKey()
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === FakeValue::LocaleMain;
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === FakeValue::LocaleMain;
});
});
Loading

0 comments on commit 526a503

Please sign in to comment.