Test Setup Failed
Push — master ( 476e0e...db2468 )
by Dimitrios
07:25 queued 04:59
created

test_lists_of_translated_fields_disable_autoload_translations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use Dimsav\Translatable\Test\Model\Country;
4
use Dimsav\Translatable\Test\Model\Vegetable;
5
6
class ScopesTest extends TestsBase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public function test_translated_in_scope_returns_only_translated_records_for_this_locale()
9
    {
10
        $translatedCountries = Country::translatedIn('fr')->get();
11
        $this->assertEquals($translatedCountries->count(), 1);
12
    }
13
14
    public function test_translated_in_scope_works_with_default_locale()
15
    {
16
        App::setLocale('de');
17
        $translatedCountries = Country::translatedIn()->get();
18
19
        $this->assertSame($translatedCountries->count(), 1);
20
        $this->assertSame('Griechenland', $translatedCountries->first()->name);
21
    }
22
23 View Code Duplication
    public function test_not_translated_in_scope_returns_only_not_translated_records_for_this_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $notTranslatedCountries = Country::notTranslatedIn('en')->get();
26
        $this->assertCount(2, $notTranslatedCountries);
27
28
        foreach ($notTranslatedCountries as $notTranslatedCountry) {
29
            $this->assertFalse($notTranslatedCountry->hasTranslation('en'));
30
        }
31
    }
32
33 View Code Duplication
    public function test_not_translated_in_scope_works_with_default_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        App::setLocale('en');
36
        $notTranslatedCountries = Country::notTranslatedIn()->get();
37
        $this->assertCount(2, $notTranslatedCountries);
38
39
        foreach ($notTranslatedCountries as $notTranslatedCountry) {
40
            $this->assertFalse($notTranslatedCountry->hasTranslation('en'));
41
        }
42
    }
43
44
    public function test_translated_scope_returns_records_with_at_least_one_translation()
45
    {
46
        $translatedCountries = Country::translated()->get();
47
        $this->assertEquals($translatedCountries->count(), 2);
48
    }
49
50 View Code Duplication
    public function test_lists_of_translated_fields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        App::setLocale('de');
53
        App::make('config')->set('translatable.to_array_always_loads_translations', false);
54
55
        $list = [[
56
            'id'   => '1',
57
            'name' => 'Griechenland',
58
        ]];
59
        $this->assertArraySubset($list, Country::listsTranslations('name')->get()->toArray());
60
    }
61
62
    public function test_lists_of_translated_fields_with_fallback()
63
    {
64
        App::make('config')->set('translatable.fallback_locale', 'en');
65
        App::make('config')->set('translatable.to_array_always_loads_translations', false);
66
        App::setLocale('de');
67
        $country = new Country();
68
        $country->useTranslationFallback = true;
69
        $list = [[
70
            'id'   => 1,
71
            'name' => 'Griechenland',
72
        ], [
73
            'id'   => 2,
74
            'name' => 'France',
75
        ]];
76
        $this->assertArraySubset($list, $country->listsTranslations('name')->get()->toArray());
77
    }
78
79 View Code Duplication
    public function test_lists_of_translated_fields_disable_autoload_translations()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        App::setLocale('de');
82
        App::make('config')->set('translatable.to_array_always_loads_translations', true);
83
84
        $list = [[
85
            'id'   => 1,
86
            'name' => 'Griechenland',
87
        ]];
88
        Country::disableAutoloadTranslations();
89
        $this->assertEquals($list, Country::listsTranslations('name')->get()->toArray());
90
        Country::defaultAutoloadTranslations();
91
    }
92
93
    public function test_scope_withTranslation_without_fallback()
94
    {
95
        $result = Country::withTranslation()->first();
96
        $loadedTranslations = $result->toArray()['translations'];
97
        $this->assertCount(1, $loadedTranslations);
98
        $this->assertSame('Greece', $loadedTranslations[0]['name']);
99
    }
100
101
    public function test_scope_withTranslation_with_fallback()
102
    {
103
        App::make('config')->set('translatable.fallback_locale', 'de');
104
        App::make('config')->set('translatable.use_fallback', true);
105
106
        $result = Country::withTranslation()->first();
107
        $loadedTranslations = $result->toArray()['translations'];
108
        $this->assertCount(2, $loadedTranslations);
109
        $this->assertSame('Greece', $loadedTranslations[0]['name']);
110
        $this->assertSame('Griechenland', $loadedTranslations[1]['name']);
111
    }
112
113
    public function test_scope_withTranslation_with_country_based_fallback()
114
    {
115
        App::make('config')->set('translatable.fallback_locale', 'en');
116
        App::make('config')->set('translatable.use_fallback', true);
117
        App::setLocale('en-GB');
118
        $result = Vegetable::withTranslation()->find(1)->toArray();
119
        $this->assertSame('courgette', $result['name']);
120
121
        App::setLocale('de-CH');
122
        $result = Vegetable::withTranslation()->find(1)->toArray();
123
        $expectedTranslations = [
124
            ['name' => 'zucchini', 'locale' => 'en'],
125
            ['name' => 'Zucchini', 'locale' => 'de'],
126
            ['name' => 'Zucchetti', 'locale' => 'de-CH'],
127
        ];
128
        $this->assertArraySubset($expectedTranslations, $result['translations']);
129
    }
130
131
    public function test_whereTranslation_filters_by_translation()
132
    {
133
        /** @var Country $country */
134
        $country = Country::whereTranslation('name', 'Greece')->first();
135
        $this->assertSame('gr', $country->code);
136
    }
137
138 View Code Duplication
    public function test_orWhereTranslation_filters_by_translation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $result = Country::whereTranslation('name', 'Greece')->orWhereTranslation('name', 'France')->get();
141
        $this->assertCount(2, $result);
142
        $this->assertSame('Greece', $result[0]->name);
143
        $this->assertSame('France', $result[1]->name);
144
    }
145
146 View Code Duplication
    public function test_whereTranslation_filters_by_translation_and_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        Country::create(['code' => 'some-code', 'name' => 'Griechenland']);
149
150
        $this->assertSame(2, Country::whereTranslation('name', 'Griechenland')->count());
151
152
        $result = Country::whereTranslation('name', 'Griechenland', 'de')->get();
153
        $this->assertSame(1, $result->count());
154
        $this->assertSame('gr', $result->first()->code);
155
    }
156
157
    public function test_whereTranslationLike_filters_by_translation()
158
    {
159
        /** @var Country $country */
160
        $country = Country::whereTranslationLike('name', '%Greec%')->first();
161
        $this->assertSame('gr', $country->code);
162
    }
163
164 View Code Duplication
    public function test_orWhereTranslationLike_filters_by_translation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166
        $result = Country::whereTranslationLike('name', '%eece%')->orWhereTranslationLike('name', '%ance%')->get();
167
        $this->assertCount(2, $result);
168
        $this->assertSame('Greece', $result[0]->name);
169
        $this->assertSame('France', $result[1]->name);
170
    }
171
172 View Code Duplication
    public function test_whereTranslationLike_filters_by_translation_and_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        Country::create(['code' => 'some-code', 'name' => 'Griechenland']);
175
176
        $this->assertSame(2, Country::whereTranslationLike('name', 'Griechen%')->count());
177
178
        $result = Country::whereTranslationLike('name', '%riechenlan%', 'de')->get();
179
        $this->assertSame(1, $result->count());
180
        $this->assertSame('gr', $result->first()->code);
181
    }
182
183
    public function test_orderByTranslation_sorts_by_key_asc()
184
    {
185
        $result = Country::orderByTranslation('name')->get();
186
        $this->assertSame(2, $result->first()->id);
187
    }
188
189
    public function test_orderByTranslation_sorts_by_key_desc()
190
    {
191
        $result = Country::orderByTranslation('name', 'desc')->get();
192
        $this->assertSame(1, $result->first()->id);
193
    }
194
}
195