Skip to content

Commit

Permalink
Started working with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 11, 2024
1 parent 837cbdf commit c679d4b
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
/** @mixin \Illuminate\Database\Eloquent\Model */
trait HasTranslations
{
/*
* Translatable columns
*/
protected array $translatable = [];

public static function bootHasTranslations(): void
{
static::saved(function (Model $model) {
Expand All @@ -40,13 +35,22 @@ public function setTranslation(
int|float|string|null $value,
Locale|string|null $locale = null
): static {
if (is_null($this->translation)) {
$this->setRelation('translation', $this->translation()->make());
}

$this->translation->content->set($column, $value, $locale);

return $this;
}

public function getTranslation(string $column, Locale|string|null $locale = null): int|float|string|null
{
return $this->translation->content->get($column, $locale);
return $this->translation?->content?->get($column, $locale);
}

protected function translatable(): array
{
return [];
}
}
8 changes: 0 additions & 8 deletions tests/Feature/ExampleTest.php

This file was deleted.

20 changes: 20 additions & 0 deletions tests/Fixtures/Models/TestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Models;

use Illuminate\Database\Eloquent\Model;
use LaravelLang\Models\HasTranslations;

class TestModel extends Model
{
use HasTranslations;

protected function translatable(): array
{
return [
'title',
];
}
}
8 changes: 8 additions & 0 deletions tests/Helpers/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

function fakeText(?string $locale = null): string
{
return fake($locale)->words(5, true);
}
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

uses(Tests\TestCase::class)->in('Feature');
uses(Tests\TestCase::class)->in('Unit');

// expect()->extend('toBeOne', function () {
// return $this->toBe(1);
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Models/EmptyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

use Tests\Fixtures\Models\TestModel;

test('get', function () {
$model = TestModel::create();

$locale = config('app.locale');
$fallback = config('app.fallback_locale');

expect($model->title)->toBeNull();

expect($model->translation->content->get('title', $locale))->toBeNull();
expect($model->translation->content->get('title', $fallback))->toBeNull();

expect($model->getTranslation('title'))->toBeNull();
expect($model->getTranslation('title', $locale))->toBeNull();
expect($model->getTranslation('title', $fallback))->toBeNull();
});

test('set', function () {
$model = TestModel::create();

$locale = config('app.locale');
$fallback = config('app.fallback_locale');

$text = fakeText();

$model->title = $text;

expect($model->title)->toBe($text);

expect($model->translation->content->get('title', $locale))->toBe($text);
expect($model->translation->content->get('title', $fallback))->toBe($text);

expect($model->getTranslation('title'))->toBe($text);
expect($model->getTranslation('title', $locale))->toBe($text);
expect($model->getTranslation('title', $fallback))->toBe($text);
});
5 changes: 5 additions & 0 deletions tests/Unit/Models/FallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

test('', function () {});
5 changes: 5 additions & 0 deletions tests/Unit/Models/MainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

test('', function () {});
26 changes: 26 additions & 0 deletions tests/database/migrations/2024_06_11_031722_create_test_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::create('test_models', function (Blueprint $table) {
$table->id();

$table->string('key', 255);

$table->timestamps();
$table->softDeletes();
});
}

public function down(): void
{
Schema::dropIfExists('test_models');
}
};

0 comments on commit c679d4b

Please sign in to comment.