Test Setup Failed
Pull Request — master (#527)
by Tom
04:42 queued 02:11
created

TranslatableTest   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 712
Duplicated Lines 4.21 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 60
lcom 1
cbo 5
dl 30
loc 712
c 0
b 0
f 0
rs 3.488

56 Methods

Rating   Name   Duplication   Size   Complexity  
A test_it_finds_the_default_translation_class() 0 7 1
A test_it_finds_the_translation_class_with_namespace_set() 0 8 1
A test_it_finds_the_translation_class_with_suffix_set() 0 8 1
A test_it_returns_custom_TranslationModelName() 0 15 1
A test_it_returns_relation_key() 0 8 1
A test_it_returns_the_translation() 0 19 1
A test_it_returns_the_translation_with_accessor() 0 8 1
A test_it_returns_null_when_the_locale_doesnt_exist() 0 7 1
A test_it_saves_translations() 10 10 1
A test_it_saves_translations_with_mutator() 0 18 1
A test_it_uses_default_locale_to_return_translations() 0 13 1
A test_it_creates_translations() 0 13 1
A test_it_creates_translations_using_the_shortcut() 10 10 1
A test_it_creates_translations_using_mass_assignment() 0 10 1
A test_it_creates_translations_using_mass_assignment_and_locales() 0 16 1
A test_it_skips_mass_assignment_if_attributes_non_fillable() 0 12 1
A test_it_returns_if_object_has_translation() 0 6 1
A test_it_returns_default_translation() 0 12 1
A test_fallback_option_in_config_overrides_models_fallback_option() 0 16 1
A test_configuration_defines_if_fallback_is_used() 0 8 1
A test_useTranslationFallback_overrides_configuration() 0 8 1
A test_it_returns_null_if_fallback_is_not_defined() 0 7 1
A test_it_fills_a_non_default_language_with_fallback_set() 0 13 1
A test_it_creates_a_new_translation() 0 10 1
A test_the_locale_key_is_locale_by_default() 0 5 1
A test_the_locale_key_can_be_overridden_in_configuration() 0 7 1
A test_the_locale_key_can_be_customized_per_model() 0 5 1
A test_the_translation_model_can_be_customized() 0 11 1
A test_it_reads_the_configuration() 0 4 1
A test_getting_translation_does_not_create_translation() 0 6 1
A test_getting_translated_field_does_not_create_translation() 0 10 1
A test_if_locales_are_not_defined_throw_exception() 0 5 1
A test_it_has_methods_that_return_always_a_translation() 0 8 1
A test_it_returns_if_attribute_is_translated() 0 7 1
A test_config_overrides_apps_locale() 0 7 1
A test_locales_as_array_keys_are_properly_detected() 0 15 1
A test_locale_separator_can_be_configured() 0 11 1
A test_fallback_for_country_based_locales() 0 16 1
A test_fallback_for_country_based_locales_with_no_base_locale() 0 15 1
A test_to_array_and_fallback_with_country_based_locales_enabled() 0 15 1
A test_it_skips_translations_in_to_array_when_config_is_set() 0 6 1
A test_it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded() 0 6 1
A test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model() 0 7 1
A test_it_deletes_all_translations() 0 11 1
A test_it_deletes_translations_for_given_locales() 0 12 1
A test_passing_an_empty_array_should_not_delete_translations() 0 10 1
A test_fill_with_translation_key() 0 16 1
A test_it_uses_the_default_locale_from_the_model() 0 19 1
A test_replicate_entity() 0 16 1
A test_getTranslationsArray() 0 18 1
A test_fill_when_locale_key_unknown() 5 25 3
A test_fill_with_translation_key_when_locale_key_unknown() 5 25 3
A test_it_uses_fallback_locale_if_default_is_empty() 0 16 1
A test_it_always_uses_value_when_fallback_not_available() 0 19 1
A test_translation_with_multiconnection() 0 27 1
A test_empty_translated_attribute() 0 6 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TranslatableTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TranslatableTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use Dimsav\Translatable\Test\Model\Food;
4
use Dimsav\Translatable\Test\Model\Person;
5
use Dimsav\Translatable\Test\Model\Country;
6
use Dimsav\Translatable\Test\Model\CountryStrict;
7
use Dimsav\Translatable\Test\Model\CountryWithCustomLocaleKey;
8
use Dimsav\Translatable\Test\Model\CountryWithCustomTranslationModel;
9
10
class TranslatableTest extends TestsBase
11
{
12
    public function test_it_finds_the_default_translation_class()
13
    {
14
        $country = new Country();
15
        $this->assertEquals(
16
            'Dimsav\Translatable\Test\Model\CountryTranslation',
17
            $country->getTranslationModelNameDefault());
18
    }
19
20
    public function test_it_finds_the_translation_class_with_namespace_set()
21
    {
22
        $this->app->make('config')->set('translatable.translation_model_namespace', 'App\Models\Translations');
23
        $country = new Country();
24
        $this->assertEquals(
25
            'App\Models\Translations\CountryTranslation',
26
            $country->getTranslationModelNameDefault());
27
    }
28
29
    public function test_it_finds_the_translation_class_with_suffix_set()
30
    {
31
        App::make('config')->set('translatable.translation_suffix', 'Trans');
32
        $country = new Country();
33
        $this->assertEquals(
34
            'Dimsav\Translatable\Test\Model\CountryTrans',
35
            $country->getTranslationModelName());
36
    }
37
38
    public function test_it_returns_custom_TranslationModelName()
39
    {
40
        $country = new Country();
41
42
        $this->assertEquals(
43
            $country->getTranslationModelNameDefault(),
44
            $country->getTranslationModelName()
45
        );
46
47
        $country->translationModel = 'MyAwesomeCountryTranslation';
48
        $this->assertEquals(
49
            'MyAwesomeCountryTranslation',
50
            $country->getTranslationModelName()
51
        );
52
    }
53
54
    public function test_it_returns_relation_key()
55
    {
56
        $country = new Country();
57
        $this->assertEquals('country_id', $country->getRelationKey());
58
59
        $country->translationForeignKey = 'my_awesome_key';
60
        $this->assertEquals('my_awesome_key', $country->getRelationKey());
61
    }
62
63
    public function test_it_returns_the_translation()
64
    {
65
        /** @var Country $country */
66
        $country = Country::whereCode('gr')->first();
67
68
        $englishTranslation = $country->translate('el');
69
        $this->assertEquals('Ελλάδα', $englishTranslation->name);
70
71
        $englishTranslation = $country->translate('en');
72
        $this->assertEquals('Greece', $englishTranslation->name);
73
74
        $this->app->setLocale('el');
75
        $englishTranslation = $country->translate();
76
        $this->assertEquals('Ελλάδα', $englishTranslation->name);
77
78
        $this->app->setLocale('en');
79
        $englishTranslation = $country->translate();
80
        $this->assertEquals('Greece', $englishTranslation->name);
81
    }
82
83
    public function test_it_returns_the_translation_with_accessor()
84
    {
85
        /** @var Country $country */
86
        $country = Country::whereCode('gr')->first();
87
88
        $this->assertEquals('Ελλάδα', $country->{'name:el'});
89
        $this->assertEquals('Greece', $country->{'name:en'});
90
    }
91
92
    public function test_it_returns_null_when_the_locale_doesnt_exist()
93
    {
94
        /** @var Country $country */
95
        $country = Country::whereCode('gr')->first();
96
97
        $this->assertSame(null, $country->{'name:unknown-locale'});
98
    }
99
100 View Code Duplication
    public function test_it_saves_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...
101
    {
102
        $country = Country::whereCode('gr')->first();
103
104
        $country->name = '1234';
105
        $country->save();
106
107
        $country = Country::whereCode('gr')->first();
108
        $this->assertEquals('1234', $country->name);
109
    }
110
111
    public function test_it_saves_translations_with_mutator()
112
    {
113
        $country = Country::whereCode('gr')->first();
114
115
        $country->{'name:en'} = '1234';
116
        $country->{'name:el'} = '5678';
117
        $country->save();
118
119
        $country = Country::whereCode('gr')->first();
120
121
        $this->app->setLocale('en');
122
        $translation = $country->translate();
123
        $this->assertEquals('1234', $translation->name);
124
125
        $this->app->setLocale('el');
126
        $translation = $country->translate();
127
        $this->assertEquals('5678', $translation->name);
128
    }
129
130
    public function test_it_uses_default_locale_to_return_translations()
131
    {
132
        $country = Country::whereCode('gr')->first();
133
134
        $country->translate('el')->name = 'abcd';
135
136
        $this->app->setLocale('el');
137
        $this->assertEquals('abcd', $country->name);
138
        $country->save();
139
140
        $country = Country::whereCode('gr')->first();
141
        $this->assertEquals('abcd', $country->translate('el')->name);
142
    }
143
144
    public function test_it_creates_translations()
145
    {
146
        $country = new Country();
147
        $country->code = 'be';
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148
        $country->save();
149
150
        $country = Country::whereCode('be')->first();
151
        $country->name = 'Belgium';
152
        $country->save();
153
154
        $country = Country::whereCode('be')->first();
155
        $this->assertEquals('Belgium', $country->name);
156
    }
157
158 View Code Duplication
    public function test_it_creates_translations_using_the_shortcut()
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...
159
    {
160
        $country = new Country();
161
        $country->code = 'be';
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
        $country->name = 'Belgium';
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
163
        $country->save();
164
165
        $country = Country::whereCode('be')->first();
166
        $this->assertEquals('Belgium', $country->name);
167
    }
168
169
    public function test_it_creates_translations_using_mass_assignment()
170
    {
171
        $data = [
172
            'code' => 'be',
173
            'name' => 'Belgium',
174
        ];
175
        $country = Country::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
176
        $this->assertEquals('be', $country->code);
177
        $this->assertEquals('Belgium', $country->name);
178
    }
179
180
    public function test_it_creates_translations_using_mass_assignment_and_locales()
181
    {
182
        $data = [
183
            'code' => 'be',
184
            'en'   => ['name' => 'Belgium'],
185
            'fr'   => ['name' => 'Belgique'],
186
        ];
187
        $country = Country::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
188
        $this->assertEquals('be', $country->code);
189
        $this->assertEquals('Belgium', $country->translate('en')->name);
190
        $this->assertEquals('Belgique', $country->translate('fr')->name);
191
192
        $country = Country::whereCode('be')->first();
193
        $this->assertEquals('Belgium', $country->translate('en')->name);
194
        $this->assertEquals('Belgique', $country->translate('fr')->name);
195
    }
196
197
    /**
198
     * @expectedException Illuminate\Database\Eloquent\MassAssignmentException
199
     */
200
    public function test_it_skips_mass_assignment_if_attributes_non_fillable()
201
    {
202
        $data = [
203
            'code' => 'be',
204
            'en'   => ['name' => 'Belgium'],
205
            'fr'   => ['name' => 'Belgique'],
206
        ];
207
        $country = CountryStrict::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\CountryStrict. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
208
        $this->assertEquals('be', $country->code);
209
        $this->assertNull($country->translate('en'));
210
        $this->assertNull($country->translate('fr'));
211
    }
212
213
    public function test_it_returns_if_object_has_translation()
214
    {
215
        $country = Country::find(1);
216
        $this->assertTrue($country->hasTranslation('en'));
217
        $this->assertFalse($country->hasTranslation('abc'));
218
    }
219
220
    public function test_it_returns_default_translation()
221
    {
222
        App::make('config')->set('translatable.fallback_locale', 'de');
223
224
        $country = Country::find(1);
225
        $this->assertSame($country->getTranslation('ch', true)->name, 'Griechenland');
226
        $this->assertSame($country->translateOrDefault('ch')->name, 'Griechenland');
227
        $this->assertSame($country->getTranslation('ch', false), null);
228
229
        $this->app->setLocale('ch');
230
        $this->assertSame($country->translateOrDefault()->name, 'Griechenland');
231
    }
232
233
    public function test_fallback_option_in_config_overrides_models_fallback_option()
234
    {
235
        App::make('config')->set('translatable.fallback_locale', 'de');
236
237
        $country = Country::find(1);
238
        $this->assertEquals($country->getTranslation('ch', true)->locale, 'de');
239
240
        $country->useTranslationFallback = false;
241
        $this->assertEquals($country->getTranslation('ch', true)->locale, 'de');
242
243
        $country->useTranslationFallback = true;
244
        $this->assertEquals($country->getTranslation('ch')->locale, 'de');
245
246
        $country->useTranslationFallback = false;
247
        $this->assertSame($country->getTranslation('ch'), null);
248
    }
249
250
    public function test_configuration_defines_if_fallback_is_used()
251
    {
252
        App::make('config')->set('translatable.fallback_locale', 'de');
253
        App::make('config')->set('translatable.use_fallback', true);
254
255
        $country = Country::find(1);
256
        $this->assertEquals($country->getTranslation('ch')->locale, 'de');
257
    }
258
259
    public function test_useTranslationFallback_overrides_configuration()
260
    {
261
        App::make('config')->set('translatable.fallback_locale', 'de');
262
        App::make('config')->set('translatable.use_fallback', true);
263
        $country = Country::find(1);
264
        $country->useTranslationFallback = false;
265
        $this->assertSame($country->getTranslation('ch'), null);
266
    }
267
268
    public function test_it_returns_null_if_fallback_is_not_defined()
269
    {
270
        App::make('config')->set('translatable.fallback_locale', 'ch');
271
272
        $country = Country::find(1);
273
        $this->assertSame($country->getTranslation('pl', true), null);
274
    }
275
276
    public function test_it_fills_a_non_default_language_with_fallback_set()
277
    {
278
        App::make('config')->set('translatable.fallback_locale', 'en');
279
280
        $country = new Country();
281
        $country->fill([
282
            'code' => 'gr',
283
            'en'   => ['name' => 'Greece'],
284
            'de'   => ['name' => 'Griechenland'],
285
        ]);
286
287
        $this->assertEquals($country->translate('en')->name, 'Greece');
288
    }
289
290
    public function test_it_creates_a_new_translation()
291
    {
292
        App::make('config')->set('translatable.fallback_locale', 'en');
293
294
        $country = Country::create(['code' => 'gr']);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
295
        $country->getNewTranslation('en')->name = 'Greece';
296
        $country->save();
297
298
        $this->assertEquals($country->translate('en')->name, 'Greece');
299
    }
300
301
    public function test_the_locale_key_is_locale_by_default()
302
    {
303
        $country = Country::find(1);
304
        $this->assertEquals($country->getLocaleKey(), 'locale');
305
    }
306
307
    public function test_the_locale_key_can_be_overridden_in_configuration()
308
    {
309
        App::make('config')->set('translatable.locale_key', 'language_id');
310
311
        $country = Country::find(1);
312
        $this->assertEquals($country->getLocaleKey(), 'language_id');
313
    }
314
315
    public function test_the_locale_key_can_be_customized_per_model()
316
    {
317
        $country = CountryWithCustomLocaleKey::find(1);
318
        $this->assertEquals($country->getLocaleKey(), 'language_id');
319
    }
320
321
    public function test_the_translation_model_can_be_customized()
322
    {
323
        $country = CountryWithCustomTranslationModel::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test...hCustomTranslationModel. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
324
            'code' => 'es',
325
            'name:en' => 'Spain',
326
            'name:de' => 'Spanien',
327
        ]);
328
        $this->assertTrue($country->exists());
329
        $this->assertEquals($country->translate('en')->name, 'Spain');
330
        $this->assertEquals($country->translate('de')->name, 'Spanien');
331
    }
332
333
    public function test_it_reads_the_configuration()
334
    {
335
        $this->assertEquals(App::make('config')->get('translatable.translation_suffix'), 'Translation');
336
    }
337
338
    public function test_getting_translation_does_not_create_translation()
339
    {
340
        $country = Country::with('translations')->find(1);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
341
        $translation = $country->getTranslation('abc', false);
342
        $this->assertSame($translation, null);
343
    }
344
345
    public function test_getting_translated_field_does_not_create_translation()
346
    {
347
        $this->app->setLocale('en');
348
        $country = new Country(['code' => 'pl']);
349
        $country->save();
350
351
        $country->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
352
353
        $this->assertSame($country->getTranslation('en'), null);
354
    }
355
356
    /**
357
     * @expectedException Dimsav\Translatable\Exception\LocalesNotDefinedException
358
     */
359
    public function test_if_locales_are_not_defined_throw_exception()
360
    {
361
        $this->app->config->set('translatable.locales', []);
362
        new Country(['code' => 'pl']);
363
    }
364
365
    public function test_it_has_methods_that_return_always_a_translation()
366
    {
367
        $country = Country::find(1)->first();
368
        $this->assertSame('abc', $country->translateOrNew('abc')->locale);
369
370
        $this->app->setLocale('xyz');
371
        $this->assertSame('xyz', $country->translateOrNew()->locale);
372
    }
373
374
    public function test_it_returns_if_attribute_is_translated()
375
    {
376
        $country = new Country();
377
378
        $this->assertTrue($country->isTranslationAttribute('name'));
379
        $this->assertFalse($country->isTranslationAttribute('some-field'));
380
    }
381
382
    public function test_config_overrides_apps_locale()
383
    {
384
        $country = Country::find(1);
385
        App::make('config')->set('translatable.locale', 'de');
386
387
        $this->assertSame('Griechenland', $country->name);
388
    }
389
390
    public function test_locales_as_array_keys_are_properly_detected()
391
    {
392
        $this->app->config->set('translatable.locales', ['en' => ['US', 'GB']]);
393
394
        $data = [
395
            'en'    => ['name' => 'French fries'],
396
            'en-US' => ['name' => 'American french fries'],
397
            'en-GB' => ['name' => 'Chips'],
398
        ];
399
        $frenchFries = Food::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Food. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
400
401
        $this->assertSame('French fries', $frenchFries->getTranslation('en')->name);
402
        $this->assertSame('Chips', $frenchFries->getTranslation('en-GB')->name);
403
        $this->assertSame('American french fries', $frenchFries->getTranslation('en-US')->name);
404
    }
405
406
    public function test_locale_separator_can_be_configured()
407
    {
408
        $this->app->config->set('translatable.locales', ['en' => ['GB']]);
409
        $this->app->config->set('translatable.locale_separator', '_');
410
        $data = [
411
            'en_GB' => ['name' => 'Chips'],
412
        ];
413
        $frenchFries = Food::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Food. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
414
415
        $this->assertSame('Chips', $frenchFries->getTranslation('en_GB')->name);
416
    }
417
418
    public function test_fallback_for_country_based_locales()
419
    {
420
        $this->app->config->set('translatable.use_fallback', true);
421
        $this->app->config->set('translatable.fallback_locale', 'fr');
422
        $this->app->config->set('translatable.locales', ['en' => ['US', 'GB'], 'fr']);
423
        $this->app->config->set('translatable.locale_separator', '-');
424
        $data = [
425
            'id'    => 1,
426
            'fr'    => ['name' => 'frites'],
427
            'en-GB' => ['name' => 'chips'],
428
            'en'    => ['name' => 'french fries'],
429
        ];
430
        Food::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Food. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
431
        $fries = Food::find(1);
432
        $this->assertSame('french fries', $fries->getTranslation('en-US')->name);
433
    }
434
435
    public function test_fallback_for_country_based_locales_with_no_base_locale()
436
    {
437
        $this->app->config->set('translatable.use_fallback', true);
438
        $this->app->config->set('translatable.fallback_locale', 'en');
439
        $this->app->config->set('translatable.locales', ['pt' => ['PT', 'BR'], 'en']);
440
        $this->app->config->set('translatable.locale_separator', '-');
441
        $data = [
442
            'id'    => 1,
443
            'en'    => ['name' => 'chips'],
444
            'pt-PT' => ['name' => 'batatas fritas'],
445
        ];
446
        Food::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Food. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
447
        $fries = Food::find(1);
448
        $this->assertSame('chips', $fries->getTranslation('pt-BR')->name);
449
    }
450
451
    public function test_to_array_and_fallback_with_country_based_locales_enabled()
452
    {
453
        $this->app->config->set('translatable.locale', 'en-GB');
454
        $this->app->config->set('translatable.use_fallback', true);
455
        $this->app->config->set('translatable.fallback_locale', 'fr');
456
        $this->app->config->set('translatable.locales', ['en' => ['GB'], 'fr']);
457
        $this->app->config->set('translatable.locale_separator', '-');
458
        $data = [
459
            'id' => 1,
460
            'fr' => ['name' => 'frites'],
461
        ];
462
        Food::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Food. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
463
        $fritesArray = Food::find(1)->toArray();
464
        $this->assertSame('frites', $fritesArray['name']);
465
    }
466
467
    public function test_it_skips_translations_in_to_array_when_config_is_set()
468
    {
469
        $this->app->config->set('translatable.to_array_always_loads_translations', false);
470
        $greece = Country::whereCode('gr')->first()->toArray();
471
        $this->assertFalse(isset($greece['name']));
472
    }
473
474
    public function test_it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded()
475
    {
476
        $this->app->config->set('translatable.to_array_always_loads_translations', false);
477
        $greece = Country::whereCode('gr')->with('translations')->first()->toArray();
478
        $this->assertTrue(isset($greece['name']));
479
    }
480
481
    public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model()
482
    {
483
        $person = new Person(['name' => 'john doe']);
484
        $person->save();
485
        $person = Person::find(1);
486
        $this->assertEquals('John doe', $person->name);
487
    }
488
489
    public function test_it_deletes_all_translations()
490
    {
491
        $country = Country::whereCode('gr')->first();
492
        $this->assertSame(4, count($country->translations));
493
494
        $country->deleteTranslations();
495
496
        $this->assertSame(0, count($country->translations));
497
        $country = Country::whereCode('gr')->first();
498
        $this->assertSame(0, count($country->translations));
499
    }
500
501
    public function test_it_deletes_translations_for_given_locales()
502
    {
503
        $country = Country::whereCode('gr')->with('translations')->first();
504
        $count = count($country->translations);
505
506
        $country->deleteTranslations('fr');
507
508
        $this->assertSame($count - 1, count($country->translations));
509
        $country = Country::whereCode('gr')->with('translations')->first();
510
        $this->assertSame($count - 1, count($country->translations));
511
        $this->assertSame(null, $country->translate('fr'));
512
    }
513
514
    public function test_passing_an_empty_array_should_not_delete_translations()
515
    {
516
        $country = Country::whereCode('gr')->with('translations')->first();
517
        $count = count($country->translations);
518
519
        $country->deleteTranslations([]);
520
521
        $country = Country::whereCode('gr')->with('translations')->first();
522
        $this->assertSame($count, count($country->translations));
523
    }
524
525
    public function test_fill_with_translation_key()
526
    {
527
        $country = new Country();
528
        $country->fill([
529
            'code'    => 'tr',
530
            'name:en' => 'Turkey',
531
            'name:de' => 'Türkei',
532
        ]);
533
        $this->assertEquals($country->translate('en')->name, 'Turkey');
534
        $this->assertEquals($country->translate('de')->name, 'Türkei');
535
536
        $country->save();
537
        $country = Country::whereCode('tr')->first();
538
        $this->assertEquals($country->translate('en')->name, 'Turkey');
539
        $this->assertEquals($country->translate('de')->name, 'Türkei');
540
    }
541
542
    public function test_it_uses_the_default_locale_from_the_model()
543
    {
544
        $country = new Country();
545
        $country->fill([
546
            'code'    => 'tn',
547
            'name:en' => 'Tunisia',
548
            'name:fr' => 'Tunisie',
549
        ]);
550
        $this->assertEquals($country->name, 'Tunisia');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
551
        $country->setDefaultLocale('fr');
552
        $this->assertEquals($country->name, 'Tunisie');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
553
554
        $country->setDefaultLocale(null);
555
        $country->save();
556
        $country = Country::whereCode('tn')->first();
557
        $this->assertEquals($country->name, 'Tunisia');
558
        $country->setDefaultLocale('fr');
559
        $this->assertEquals($country->name, 'Tunisie');
560
    }
561
562
    public function test_replicate_entity()
563
    {
564
        $apple = new Food();
565
        $apple->fill([
566
            'name:fr' => 'Pomme',
567
            'name:en' => 'Apple',
568
            'name:de' => 'Apfel',
569
        ]);
570
        $apple->save();
571
572
        $replicatedApple = $apple->replicateWithTranslations();
573
        $this->assertNotSame($replicatedApple->id, $apple->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Dimsav\Translatable\Test\Model\Food>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
574
        $this->assertEquals($replicatedApple->translate('fr')->name, $apple->translate('fr')->name);
575
        $this->assertEquals($replicatedApple->translate('en')->name, $apple->translate('en')->name);
576
        $this->assertEquals($replicatedApple->translate('de')->name, $apple->translate('de')->name);
577
    }
578
579
    public function test_getTranslationsArray()
580
    {
581
        Country::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
582
            'code'    => 'tn',
583
            'name:en' => 'Tunisia',
584
            'name:fr' => 'Tunisie',
585
            'name:de' => 'Tunesien',
586
        ]);
587
588
        /** @var Country $country */
589
        $country = Country::where('code', 'tn')->first();
590
591
        $this->assertSame([
592
            'de' => ['name' => 'Tunesien'],
593
            'en' => ['name' => 'Tunisia'],
594
            'fr' => ['name' => 'Tunisie'],
595
        ], $country->getTranslationsArray());
596
    }
597
598
    public function test_fill_when_locale_key_unknown()
599
    {
600
        config(['translatable.locales' => ['en']]);
601
602
        $country = new Country();
603
        $country->fill([
604
            'code' => 'ua',
605
            'en'   => ['name' => 'Ukraine'],
606
            'ua'   => ['name' => 'Україна'], // "ua" is unknown, so must be ignored
607
        ]);
608
609
        $modelTranslations = [];
610
611 View Code Duplication
        foreach ($country->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
612
            foreach ($country->translatedAttributes as $attr) {
613
                $modelTranslations[$translation->locale][$attr] = $translation->{$attr};
614
            }
615
        }
616
617
        $expectedTranslations = [
618
            'en' => ['name' => 'Ukraine'],
619
        ];
620
621
        $this->assertEquals($modelTranslations, $expectedTranslations);
622
    }
623
624
    public function test_fill_with_translation_key_when_locale_key_unknown()
625
    {
626
        config(['translatable.locales' => ['en']]);
627
628
        $country = new Country();
629
        $country->fill([
630
            'code'    => 'ua',
631
            'name:en' => 'Ukraine',
632
            'name:ua' => 'Україна', // "ua" is unknown, so must be ignored
633
        ]);
634
635
        $modelTranslations = [];
636
637 View Code Duplication
        foreach ($country->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
638
            foreach ($country->translatedAttributes as $attr) {
639
                $modelTranslations[$translation->locale][$attr] = $translation->{$attr};
640
            }
641
        }
642
643
        $expectedTranslations = [
644
            'en' => ['name' => 'Ukraine'],
645
        ];
646
647
        $this->assertEquals($modelTranslations, $expectedTranslations);
648
    }
649
650
    public function test_it_uses_fallback_locale_if_default_is_empty()
651
    {
652
        App::make('config')->set('translatable.use_fallback', true);
653
        App::make('config')->set('translatable.use_property_fallback', true);
654
        App::make('config')->set('translatable.fallback_locale', 'en');
655
        $country = new Country();
656
        $country->fill([
657
            'code'    => 'tn',
658
            'name:en' => 'Tunisia',
659
            'name:fr' => '',
660
        ]);
661
        $this->app->setLocale('en');
662
        $this->assertEquals('Tunisia', $country->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
663
        $this->app->setLocale('fr');
664
        $this->assertEquals('Tunisia', $country->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
665
    }
666
667
    public function test_it_always_uses_value_when_fallback_not_available()
668
    {
669
        App::make('config')->set('translatable.fallback_locale', 'it');
670
        App::make('config')->set('translatable.use_fallback', true);
671
672
        $country = new Country();
673
        $country->fill([
674
            'code' => 'gr',
675
            'en' => ['name' => ''],
676
            'de' => ['name' => 'Griechenland'],
677
        ]);
678
679
        // verify translated attributed is correctly returned when empty (non-existing fallback is ignored)
680
        $this->app->setLocale('en');
681
        $this->assertEquals('', $country->getAttribute('name'));
682
683
        $this->app->setLocale('de');
684
        $this->assertEquals('Griechenland', $country->getAttribute('name'));
685
    }
686
687
    public function test_translation_with_multiconnection()
688
    {
689
        // Add country & translation in second db
690
        $country = new Country();
691
        $country->setConnection('mysql2');
692
        $country->code = 'sg';
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
693
        $country->{'name:sg'} = 'Singapore';
694
        $this->assertTrue($country->save());
695
696
        $countryId = $country->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Dimsav\Translatable\Test\Model\Country>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
697
698
        // Verify added country & translation in second db
699
        $country = new Country();
700
        $country->setConnection('mysql2');
701
        $sgCountry = $country->find($countryId);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Dimsav\Translatable\Test\Model\Country>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
702
        $this->assertEquals('Singapore', $sgCountry->translate('sg')->name);
703
704
        // Verify added country not in default db
705
        $country = new Country();
706
        $sgCountry = $country::where('code', 'sg')->get();
707
        $this->assertEmpty($sgCountry);
708
709
        // Verify added translation not in default db
710
        $country = new Country();
711
        $sgCountry = $country->find($countryId);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Dimsav\Translatable\Test\Model\Country>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
712
        $this->assertEmpty($sgCountry->translate('sg'));
713
    }
714
715
    public function test_empty_translated_attribute()
716
    {
717
        $country = Country::whereCode('gr')->first();
718
        $this->app->setLocale('invalid');
719
        $this->assertSame(null, $country->name);
720
    }
721
}
722