Passed
Push — master ( 0d471d...5b5903 )
by Koen
04:29 queued 49s
created

testTranslationsCanBeSavedViaStoreTranslationsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace KoenHoeijmakers\LaravelTranslatable\Tests\Feature;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Foundation\Testing\RefreshDatabase;
7
use KoenHoeijmakers\LaravelTranslatable\HasTranslations;
8
use KoenHoeijmakers\LaravelTranslatable\Tests\TestCase;
9
10
class TranslationTest extends TestCase
11
{
12
    use RefreshDatabase;
0 ignored issues
show
introduced by
The trait Illuminate\Foundation\Testing\RefreshDatabase requires some properties which are not provided by KoenHoeijmakers\LaravelT...Feature\TranslationTest: $connectionsToTransact, $dropViews
Loading history...
13
14
    public function testTranslationsAreBeingSaved()
15
    {
16
        $model = TestModel::query()->create([
17
            'name' => 'Monkey',
18
        ]);
19
20
        $this->assertTrue($model->getAttribute('name') === 'Monkey');
21
        $this->assertDatabaseHas('test_models', ['id' => $model->getKey()]);
22
        $this->assertDatabaseHas('test_model_translations', ['test_model_id' => $model->getKey(), 'name' => 'Monkey']);
23
    }
24
25
    public function testTranslationsCanBeSavedViaStoreTranslation()
26
    {
27
        /** @var \KoenHoeijmakers\LaravelTranslatable\Tests\Feature\TestModel $model */
28
        $model = TestModel::query()->create([
29
            'name' => 'Monkey',
30
        ]);
31
32
        $model->storeTranslation('nl', ['name' => 'Aap']);
33
34
        $this->assertDatabaseHas('test_models', ['id' => $model->getKey()]);
35
        $this->assertDatabaseHas('test_model_translations', ['test_model_id' => $model->getKey(), 'name' => 'Aap']);
36
    }
37
38
    public function testTranslationsCanBeSavedViaStoreTranslationMethod()
39
    {
40
        /** @var \KoenHoeijmakers\LaravelTranslatable\Tests\Feature\TestModel $model */
41
        $model = TestModel::query()->create([
42
            'name' => 'Monkey',
43
        ]);
44
45
        $model->storeTranslation('nl', ['name' => 'Aap']);
46
47
        $this->assertDatabaseHas('test_models', ['id' => $model->getKey()]);
48
        $this->assertTrue($model->translationExists('nl'));
49
    }
50
51
    public function testTranslationsCanBeSavedViaStoreTranslationsMethod()
52
    {
53
        /** @var \KoenHoeijmakers\LaravelTranslatable\Tests\Feature\TestModel $model */
54
        $model = TestModel::query()->create([
55
            'name' => 'Monkey',
56
        ]);
57
58
        $model->storeTranslations([
59
            'nl' => ['name' => 'Aap'],
60
            'de' => ['name' => 'Affe'],
61
        ]);
62
63
        $this->assertDatabaseHas('test_models', ['id' => $model->getKey()]);
64
        $this->assertTrue($model->translationExists('nl'));
65
        $this->assertTrue($model->translationExists('de'));
66
    }
67
68
    public function testCanRetrieveADifferentTranslation()
69
    {
70
        /** @var \KoenHoeijmakers\LaravelTranslatable\Tests\Feature\TestModel $model */
71
        $model = TestModel::query()->create([
72
            'name' => 'Monkey',
73
        ]);
74
75
        $model->storeTranslation('nl', ['name' => 'Aap']);
76
77
        $this->assertTrue($model->translationExists('nl'));
78
79
        /** @var \KoenHoeijmakers\LaravelTranslatable\Tests\Feature\TestModel $model */
80
        $model = TestModel::query()->find(1);
81
82
        $model->translate('nl');
83
84
        $this->assertEquals($model->getLocale(), 'nl');
85
        $this->assertEquals($model->getAttribute('name'), 'Aap');
86
    }
87
88
    public function testCanRetrieveADifferentTranslationAndItsUpdatedInThatLocale()
89
    {
90
        /** @var \KoenHoeijmakers\LaravelTranslatable\Tests\Feature\TestModel $model */
91
        $model = TestModel::query()->create([
92
            'name' => 'Monkey',
93
        ]);
94
95
        $model->storeTranslation('nl', ['name' => 'Aap']);
96
97
        $this->assertTrue($model->translationExists('nl'));
98
99
        $model->translate('nl');
100
101
        $this->assertEquals($model->getLocale(), 'nl');
102
        $this->assertEquals($model->getAttribute('name'), 'Aap');
103
104
        $model->update(['name' => 'Gorilla']);
105
106
        $this->assertEquals($model->getAttribute('name'), 'Gorilla');
107
        $this->assertEquals(TestModel::query()->find(1)->getAttribute('name'), 'Monkey');
108
    }
109
}
110
111
class TestModel extends Model
112
{
113
    use HasTranslations;
0 ignored issues
show
introduced by
The trait KoenHoeijmakers\LaravelT...latable\HasTranslations requires some properties which are not provided by KoenHoeijmakers\LaravelT...Tests\Feature\TestModel: $localeKeyName, $translationModel, $translationForeignKey
Loading history...
114
115
    protected $fillable = ['name'];
116
117
    protected $translatable = ['name'];
118
}
119
120
class TestModelTranslation extends Model
121
{
122
    protected $fillable = ['name'];
123
}
124