Test Setup Failed
Push — master ( c3e12f...ff2a07 )
by Dimitrios
255:20 queued 252:17
created

test_fill_when_locale_key_unknown()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 5
Ratio 20 %

Importance

Changes 0
Metric Value
dl 5
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 0
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
9
class TranslatableTest 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...
10
{
11
    public function test_it_finds_the_default_translation_class()
12
    {
13
        $country = new Country();
14
        $this->assertEquals(
15
            'Dimsav\Translatable\Test\Model\CountryTranslation',
16
            $country->getTranslationModelNameDefault());
17
    }
18
19
    public function test_it_finds_the_translation_class_with_suffix_set()
20
    {
21
        App::make('config')->set('translatable.translation_suffix', 'Trans');
22
        $country = new Country();
23
        $this->assertEquals(
24
            'Dimsav\Translatable\Test\Model\CountryTrans',
25
            $country->getTranslationModelName());
26
    }
27
28
    public function test_it_returns_custom_TranslationModelName()
29
    {
30
        $country = new Country();
31
32
        $this->assertEquals(
33
            $country->getTranslationModelNameDefault(),
34
            $country->getTranslationModelName()
35
        );
36
37
        $country->translationModel = 'MyAwesomeCountryTranslation';
38
        $this->assertEquals(
39
            'MyAwesomeCountryTranslation',
40
            $country->getTranslationModelName()
41
        );
42
    }
43
44
    public function test_it_returns_relation_key()
45
    {
46
        $country = new Country();
47
        $this->assertEquals('country_id', $country->getRelationKey());
48
49
        $country->translationForeignKey = 'my_awesome_key';
50
        $this->assertEquals('my_awesome_key', $country->getRelationKey());
51
    }
52
53
    public function test_it_returns_the_translation()
54
    {
55
        /** @var Country $country */
56
        $country = Country::whereCode('gr')->first();
57
58
        $englishTranslation = $country->translate('el');
59
        $this->assertEquals('Ελλάδα', $englishTranslation->name);
60
61
        $englishTranslation = $country->translate('en');
62
        $this->assertEquals('Greece', $englishTranslation->name);
63
64
        $this->app->setLocale('el');
65
        $englishTranslation = $country->translate();
66
        $this->assertEquals('Ελλάδα', $englishTranslation->name);
67
68
        $this->app->setLocale('en');
69
        $englishTranslation = $country->translate();
70
        $this->assertEquals('Greece', $englishTranslation->name);
71
    }
72
73
    public function test_it_returns_the_translation_with_accessor()
74
    {
75
        /** @var Country $country */
76
        $country = Country::whereCode('gr')->first();
77
78
        $this->assertEquals('Ελλάδα', $country->{'name:el'});
79
        $this->assertEquals('Greece', $country->{'name:en'});
80
    }
81
82
    public function test_it_returns_null_when_the_locale_doesnt_exist()
83
    {
84
        /** @var Country $country */
85
        $country = Country::whereCode('gr')->first();
86
87
        $this->assertSame(null, $country->{'name:unknown-locale'});
88
    }
89
90 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...
91
    {
92
        $country = Country::whereCode('gr')->first();
93
94
        $country->name = '1234';
95
        $country->save();
96
97
        $country = Country::whereCode('gr')->first();
98
        $this->assertEquals('1234', $country->name);
99
    }
100
101
    public function test_it_saves_translations_with_mutator()
102
    {
103
        $country = Country::whereCode('gr')->first();
104
105
        $country->{'name:en'} = '1234';
106
        $country->{'name:el'} = '5678';
107
        $country->save();
108
109
        $country = Country::whereCode('gr')->first();
110
111
        $this->app->setLocale('en');
112
        $translation = $country->translate();
113
        $this->assertEquals('1234', $translation->name);
114
115
        $this->app->setLocale('el');
116
        $translation = $country->translate();
117
        $this->assertEquals('5678', $translation->name);
118
    }
119
120
    public function test_it_uses_default_locale_to_return_translations()
121
    {
122
        $country = Country::whereCode('gr')->first();
123
124
        $country->translate('el')->name = 'abcd';
125
126
        $this->app->setLocale('el');
127
        $this->assertEquals('abcd', $country->name);
128
        $country->save();
129
130
        $country = Country::whereCode('gr')->first();
131
        $this->assertEquals('abcd', $country->translate('el')->name);
132
    }
133
134
    public function test_it_creates_translations()
135
    {
136
        $country = new Country();
137
        $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...
138
        $country->save();
139
140
        $country = Country::whereCode('be')->first();
141
        $country->name = 'Belgium';
142
        $country->save();
143
144
        $country = Country::whereCode('be')->first();
145
        $this->assertEquals('Belgium', $country->name);
146
    }
147
148 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...
149
    {
150
        $country = new Country();
151
        $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...
152
        $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...
153
        $country->save();
154
155
        $country = Country::whereCode('be')->first();
156
        $this->assertEquals('Belgium', $country->name);
157
    }
158
159
    public function test_it_creates_translations_using_mass_assignment()
160
    {
161
        $data = [
162
            'code' => 'be',
163
            'name' => 'Belgium',
164
        ];
165
        $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...
166
        $this->assertEquals('be', $country->code);
167
        $this->assertEquals('Belgium', $country->name);
168
    }
169
170
    public function test_it_creates_translations_using_mass_assignment_and_locales()
171
    {
172
        $data = [
173
            'code' => 'be',
174
            'en' => ['name' => 'Belgium'],
175
            'fr' => ['name' => 'Belgique'],
176
        ];
177
        $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...
178
        $this->assertEquals('be', $country->code);
179
        $this->assertEquals('Belgium', $country->translate('en')->name);
180
        $this->assertEquals('Belgique', $country->translate('fr')->name);
181
182
        $country = Country::whereCode('be')->first();
183
        $this->assertEquals('Belgium', $country->translate('en')->name);
184
        $this->assertEquals('Belgique', $country->translate('fr')->name);
185
    }
186
187
    /**
188
     * @expectedException Illuminate\Database\Eloquent\MassAssignmentException
189
     */
190
    public function test_it_skips_mass_assignment_if_attributes_non_fillable()
191
    {
192
        $data = [
193
            'code' => 'be',
194
            'en' => ['name' => 'Belgium'],
195
            'fr' => ['name' => 'Belgique'],
196
        ];
197
        $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...
198
        $this->assertEquals('be', $country->code);
199
        $this->assertNull($country->translate('en'));
200
        $this->assertNull($country->translate('fr'));
201
    }
202
203
    public function test_it_returns_if_object_has_translation()
204
    {
205
        $country = Country::find(1);
206
        $this->assertTrue($country->hasTranslation('en'));
207
        $this->assertFalse($country->hasTranslation('abc'));
208
    }
209
210
    public function test_it_returns_default_translation()
211
    {
212
        App::make('config')->set('translatable.fallback_locale', 'de');
213
214
        $country = Country::find(1);
215
        $this->assertSame($country->getTranslation('ch', true)->name, 'Griechenland');
216
        $this->assertSame($country->translateOrDefault('ch')->name, 'Griechenland');
217
        $this->assertSame($country->getTranslation('ch', false), null);
218
    }
219
220
    public function test_fallback_option_in_config_overrides_models_fallback_option()
221
    {
222
        App::make('config')->set('translatable.fallback_locale', 'de');
223
224
        $country = Country::find(1);
225
        $this->assertEquals($country->getTranslation('ch', true)->locale, 'de');
226
227
        $country->useTranslationFallback = false;
228
        $this->assertEquals($country->getTranslation('ch', true)->locale, 'de');
229
230
        $country->useTranslationFallback = true;
231
        $this->assertEquals($country->getTranslation('ch')->locale, 'de');
232
233
        $country->useTranslationFallback = false;
234
        $this->assertSame($country->getTranslation('ch'), null);
235
    }
236
237
    public function test_configuration_defines_if_fallback_is_used()
238
    {
239
        App::make('config')->set('translatable.fallback_locale', 'de');
240
        App::make('config')->set('translatable.use_fallback', true);
241
242
        $country = Country::find(1);
243
        $this->assertEquals($country->getTranslation('ch')->locale, 'de');
244
    }
245
246
    public function test_useTranslationFallback_overrides_configuration()
247
    {
248
        App::make('config')->set('translatable.fallback_locale', 'de');
249
        App::make('config')->set('translatable.use_fallback', true);
250
        $country = Country::find(1);
251
        $country->useTranslationFallback = false;
252
        $this->assertSame($country->getTranslation('ch'), null);
253
    }
254
255
    public function test_it_returns_null_if_fallback_is_not_defined()
256
    {
257
        App::make('config')->set('translatable.fallback_locale', 'ch');
258
259
        $country = Country::find(1);
260
        $this->assertSame($country->getTranslation('pl', true), null);
261
    }
262
263
    public function test_it_fills_a_non_default_language_with_fallback_set()
264
    {
265
        App::make('config')->set('translatable.fallback_locale', 'en');
266
267
        $country = new Country();
268
        $country->fill([
269
            'code' => 'gr',
270
            'en' => ['name' => 'Greece'],
271
            'de' => ['name' => 'Griechenland'],
272
        ]);
273
274
        $this->assertEquals($country->translate('en')->name, 'Greece');
275
    }
276
277
    public function test_it_creates_a_new_translation()
278
    {
279
        App::make('config')->set('translatable.fallback_locale', 'en');
280
281
        $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...
282
        $country->getNewTranslation('en')->name = 'Greece';
283
        $country->save();
284
285
        $this->assertEquals($country->translate('en')->name, 'Greece');
286
    }
287
288
    public function test_the_locale_key_is_locale_by_default()
289
    {
290
        $country = Country::find(1);
291
        $this->assertEquals($country->getLocaleKey(), 'locale');
292
    }
293
294
    public function test_the_locale_key_can_be_overridden_in_configuration()
295
    {
296
        App::make('config')->set('translatable.locale_key', 'language_id');
297
298
        $country = Country::find(1);
299
        $this->assertEquals($country->getLocaleKey(), 'language_id');
300
    }
301
302
    public function test_the_locale_key_can_be_customized_per_model()
303
    {
304
        $country = CountryWithCustomLocaleKey::find(1);
305
        $this->assertEquals($country->getLocaleKey(), 'language_id');
306
    }
307
308
    public function test_it_reads_the_configuration()
309
    {
310
        $this->assertEquals(App::make('config')->get('translatable.translation_suffix'), 'Translation');
311
    }
312
313
    public function test_getting_translation_does_not_create_translation()
314
    {
315
        $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...
316
        $translation = $country->getTranslation('abc', false);
317
        $this->assertSame($translation, null);
318
    }
319
320
    public function test_getting_translated_field_does_not_create_translation()
321
    {
322
        $this->app->setLocale('en');
323
        $country = new Country(['code' => 'pl']);
324
        $country->save();
325
326
        $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...
327
328
        $this->assertSame($country->getTranslation('en'), null);
329
    }
330
331
    /**
332
     * @expectedException Dimsav\Translatable\Exception\LocalesNotDefinedException
333
     */
334
    public function test_if_locales_are_not_defined_throw_exception()
335
    {
336
        $this->app->config->set('translatable.locales', []);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
337
        new Country(['code' => 'pl']);
338
    }
339
340
    public function test_it_has_methods_that_return_always_a_translation()
341
    {
342
        $country = Country::find(1)->first();
343
        $this->assertSame('abc', $country->translateOrNew('abc')->locale);
344
    }
345
346
    public function test_it_returns_if_attribute_is_translated()
347
    {
348
        $country = new Country();
349
350
        $this->assertTrue($country->isTranslationAttribute('name'));
351
        $this->assertFalse($country->isTranslationAttribute('some-field'));
352
    }
353
354
    public function test_config_overrides_apps_locale()
355
    {
356
        $country = Country::find(1);
357
        App::make('config')->set('translatable.locale', 'de');
358
359
        $this->assertSame('Griechenland', $country->name);
360
    }
361
362
    public function test_locales_as_array_keys_are_properly_detected()
363
    {
364
        $this->app->config->set('translatable.locales', ['en' => ['US', 'GB']]);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
365
366
        $data = [
367
            'en' => ['name' => 'French fries'],
368
            'en-US' => ['name' => 'American french fries'],
369
            'en-GB' => ['name' => 'Chips'],
370
        ];
371
        $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...
372
373
        $this->assertSame('French fries', $frenchFries->getTranslation('en')->name);
374
        $this->assertSame('Chips', $frenchFries->getTranslation('en-GB')->name);
375
        $this->assertSame('American french fries', $frenchFries->getTranslation('en-US')->name);
376
    }
377
378
    public function test_locale_separator_can_be_configured()
379
    {
380
        $this->app->config->set('translatable.locales', ['en' => ['GB']]);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
381
        $this->app->config->set('translatable.locale_separator', '_');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
382
        $data = [
383
            'en_GB' => ['name' => 'Chips'],
384
        ];
385
        $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...
386
387
        $this->assertSame('Chips', $frenchFries->getTranslation('en_GB')->name);
388
    }
389
390
    public function test_fallback_for_country_based_locales()
391
    {
392
        $this->app->config->set('translatable.use_fallback', true);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
393
        $this->app->config->set('translatable.fallback_locale', 'fr');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
394
        $this->app->config->set('translatable.locales', ['en' => ['US', 'GB'], 'fr']);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
395
        $this->app->config->set('translatable.locale_separator', '-');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
396
        $data = [
397
            'id' => 1,
398
            'fr' => ['name' => 'frites'],
399
            'en-GB' => ['name' => 'chips'],
400
            'en' => ['name' => 'french fries'],
401
        ];
402
        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...
403
        $fries = Food::find(1);
404
        $this->assertSame('french fries', $fries->getTranslation('en-US')->name);
405
    }
406
407
    public function test_fallback_for_country_based_locales_with_no_base_locale()
408
    {
409
        $this->app->config->set('translatable.use_fallback', true);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
410
        $this->app->config->set('translatable.fallback_locale', 'en');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
411
        $this->app->config->set('translatable.locales', ['pt' => ['PT', 'BR'], 'en']);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
412
        $this->app->config->set('translatable.locale_separator', '-');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
413
        $data = [
414
            'id' => 1,
415
            'en' => ['name' => 'chips'],
416
            'pt-PT' => ['name' => 'batatas fritas'],
417
        ];
418
        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...
419
        $fries = Food::find(1);
420
        $this->assertSame('chips', $fries->getTranslation('pt-BR')->name);
421
    }
422
423
    public function test_to_array_and_fallback_with_country_based_locales_enabled()
424
    {
425
        $this->app->config->set('translatable.use_fallback', true);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
426
        $this->app->config->set('translatable.fallback_locale', 'fr');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
427
        $this->app->config->set('translatable.locales', ['en' => ['GB'], 'fr']);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
428
        $this->app->config->set('translatable.locale_separator', '-');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
429
        $data = [
430
            'id' => 1,
431
            'fr' => ['name' => 'frites'],
432
        ];
433
        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...
434
        $fritesArray = Food::find(1)->toArray();
435
        $this->assertSame('frites', $fritesArray['name']);
436
    }
437
438
    public function test_it_skips_translations_in_to_array_when_config_is_set()
439
    {
440
        $this->app->config->set('translatable.to_array_always_loads_translations', false);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
441
        $greece = Country::whereCode('gr')->first()->toArray();
442
        $this->assertFalse(isset($greece['name']));
443
    }
444
445
    public function test_it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded()
446
    {
447
        $this->app->config->set('translatable.to_array_always_loads_translations', false);
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean monologConfigurator?

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...
448
        $greece = Country::whereCode('gr')->with('translations')->first()->toArray();
449
        $this->assertTrue(isset($greece['name']));
450
    }
451
452
    public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model()
453
    {
454
        $person = new Person(['name' => 'john doe']);
455
        $person->save();
456
        $person = Person::find(1);
457
        $this->assertEquals('John doe', $person->name);
458
    }
459
460
    public function test_it_deletes_all_translations()
461
    {
462
        $country = Country::whereCode('gr')->first();
463
        $this->assertSame(4, count($country->translations));
464
465
        $country->deleteTranslations();
466
467
        $this->assertSame(0, count($country->translations));
468
        $country = Country::whereCode('gr')->first();
469
        $this->assertSame(0, count($country->translations));
470
    }
471
472
    public function test_it_deletes_translations_for_given_locales()
473
    {
474
        $country = Country::whereCode('gr')->with('translations')->first();
475
        $count = count($country->translations);
476
477
        $country->deleteTranslations('fr');
478
479
        $this->assertSame($count - 1, count($country->translations));
480
        $country = Country::whereCode('gr')->with('translations')->first();
481
        $this->assertSame($count - 1, count($country->translations));
482
        $this->assertSame(null, $country->translate('fr'));
483
    }
484
485
    public function test_passing_an_empty_array_should_not_delete_translations()
486
    {
487
        $country = Country::whereCode('gr')->with('translations')->first();
488
        $count = count($country->translations);
489
490
        $country->deleteTranslations([]);
491
492
        $country = Country::whereCode('gr')->with('translations')->first();
493
        $this->assertSame($count, count($country->translations));
494
    }
495
496
    public function test_fill_with_translation_key()
497
    {
498
        $country = new Country();
499
        $country->fill([
500
            'code'    => 'tr',
501
            'name:en' => 'Turkey',
502
            'name:de' => 'Türkei',
503
        ]);
504
        $this->assertEquals($country->translate('en')->name, 'Turkey');
505
        $this->assertEquals($country->translate('de')->name, 'Türkei');
506
507
        $country->save();
508
        $country = Country::whereCode('tr')->first();
509
        $this->assertEquals($country->translate('en')->name, 'Turkey');
510
        $this->assertEquals($country->translate('de')->name, 'Türkei');
511
    }
512
513
    public function test_it_uses_the_default_locale_from_the_model()
514
    {
515
        $country = new Country();
516
        $country->fill([
517
            'code'    => 'tn',
518
            'name:en' => 'Tunisia',
519
            'name:fr' => 'Tunisie',
520
        ]);
521
        $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...
522
        $country->setDefaultLocale('fr');
523
        $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...
524
525
        $country->setDefaultLocale(null);
526
        $country->save();
527
        $country = Country::whereCode('tn')->first();
528
        $this->assertEquals($country->name, 'Tunisia');
529
        $country->setDefaultLocale('fr');
530
        $this->assertEquals($country->name, 'Tunisie');
531
    }
532
533
    public function test_retriving_translatable_array()
534
    {
535
        $country = new Country();
536
        $country->fill([
537
            'code'    => 'tn',
538
            'name:en' => 'Tunisia',
539
            'name:fr' => 'Tunisie',
540
        ]);
541
542
        $testArr = [];
543
544 View Code Duplication
        foreach ($country->translations as $translation) {
0 ignored issues
show
Documentation introduced by
The property translations 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...
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...
545
            foreach ($country->translatedAttributes as $attr) {
546
                $testArr[$translation->locale][$attr] = $translation->{$attr};
547
            }
548
        }
549
550
        $this->assertEquals($testArr, $country->getTranslationsArray());
551
    }
552
553
    public function test_fill_when_locale_key_unknown()
554
    {
555
        config(['translatable.locales' => ['en']]);
556
557
        $country = new Country();
558
        $country->fill([
559
            'code' => 'ua',
560
            'en'   => ['name' => 'Ukraine'],
561
            'ua'   => ['name' => 'Україна'], // "ua" is unknown, so must be ignored
562
        ]);
563
564
        $modelTranslations = [];
565
566 View Code Duplication
        foreach ($country->translations as $translation) {
0 ignored issues
show
Documentation introduced by
The property translations 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...
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...
567
            foreach ($country->translatedAttributes as $attr) {
568
                $modelTranslations[$translation->locale][$attr] = $translation->{$attr};
569
            }
570
        }
571
572
        $expectedTranslations = [
573
            'en' => ['name' => 'Ukraine'],
574
        ];
575
576
        $this->assertEquals($modelTranslations, $expectedTranslations);
577
    }
578
579
    public function test_fill_with_translation_key_when_locale_key_unknown()
580
    {
581
        config(['translatable.locales' => ['en']]);
582
583
        $country = new Country();
584
        $country->fill([
585
            'code'    => 'ua',
586
            'name:en' => 'Ukraine',
587
            'name:ua' => 'Україна', // "ua" is unknown, so must be ignored
588
        ]);
589
590
        $modelTranslations = [];
591
592 View Code Duplication
        foreach ($country->translations as $translation) {
0 ignored issues
show
Documentation introduced by
The property translations 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...
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...
593
            foreach ($country->translatedAttributes as $attr) {
594
                $modelTranslations[$translation->locale][$attr] = $translation->{$attr};
595
            }
596
        }
597
598
        $expectedTranslations = [
599
            'en' => ['name' => 'Ukraine'],
600
        ];
601
602
        $this->assertEquals($modelTranslations, $expectedTranslations);
603
    }
604
605
    public function test_it_uses_fallback_locale_if_default_is_empty()
606
    {
607
        App::make('config')->set('translatable.use_fallback', true);
608
        App::make('config')->set('translatable.use_property_fallback', true);
609
        App::make('config')->set('translatable.fallback_locale', 'en');
610
        $country = new Country();
611
        $country->fill([
612
            'code' => 'tn',
613
            'name:en' => 'Tunisia',
614
            'name:fr' => '',
615
        ]);
616
        $this->app->setLocale('en');
617
        $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...
618
        $this->app->setLocale('fr');
619
        $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...
620
    }
621
}
622