Passed
Pull Request — master (#417)
by
unknown
62:59
created

ScopesTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 53
loc 159
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A test_translated_in_scope_returns_only_translated_records_for_this_locale() 0 5 1
A test_translated_in_scope_works_with_default_locale() 0 8 1
A test_not_translated_in_scope_returns_only_not_translated_records_for_this_locale() 9 9 2
A test_not_translated_in_scope_works_with_default_locale() 10 10 2
A test_translated_scope_returns_records_with_at_least_one_translation() 0 5 1
A test_lists_of_translated_fields() 0 9 1
A test_lists_of_translated_fields_with_fallback() 0 15 1
A test_scope_withTranslation_without_fallback() 0 7 1
A test_scope_withTranslation_with_fallback() 0 11 1
A test_scope_withTranslation_with_country_based_fallback() 0 16 1
A test_whereTranslation_filters_by_translation() 0 6 1
A test_orWhereTranslation_filters_by_translation() 7 7 1
A test_whereTranslation_filters_by_translation_and_locale() 10 10 1
A test_whereTranslationLike_filters_by_translation() 0 6 1
A test_orWhereTranslationLike_filters_by_translation() 7 7 1
A test_whereTranslationLike_filters_by_translation_and_locale() 10 10 1

How to fix   Duplicated Code   

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:

1
<?php
2
3
use Dimsav\Translatable\Test\Model\Vegetable;
4
use Dimsav\Translatable\Test\Model\Country;
5
6
class ScopesTest extends TestsBase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

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

namespace YourVendor;

class YourClass { }

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

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

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

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

Loading history...
24
    {
25
        $notTranslatedCountries = Country::notTranslatedIn('en')->get();
0 ignored issues
show
Bug introduced by
The method notTranslatedIn() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean translate()?

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...
26
        $this->assertCount(2, $notTranslatedCountries);
27
28
        foreach ($notTranslatedCountries as $notTranslatedCountry) {
29
            $this->assertFalse($notTranslatedCountry->hasTranslation('en'));
30
        }
31
    }
32
33 View Code Duplication
    public function test_not_translated_in_scope_works_with_default_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
34
    {
35
        App::setLocale('en');
36
        $notTranslatedCountries = Country::notTranslatedIn()->get();
0 ignored issues
show
Bug introduced by
The method notTranslatedIn() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean translate()?

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...
37
        $this->assertCount(2, $notTranslatedCountries);
38
39
        foreach ($notTranslatedCountries as $notTranslatedCountry) {
40
            $this->assertFalse($notTranslatedCountry->hasTranslation('en'));
41
        }
42
    }
43
44
    public function test_translated_scope_returns_records_with_at_least_one_translation()
45
    {
46
        $translatedCountries = Country::translated()->get();
47
        $this->assertEquals($translatedCountries->count(), 2);
48
    }
49
50
    public function test_lists_of_translated_fields()
51
    {
52
        App::setLocale('de');
53
        $list = [[
54
            'id'   => '1',
55
            'name' => 'Griechenland',
56
        ]];
57
        $this->assertArraySubset($list, Country::listsTranslations('name')->get()->toArray());
0 ignored issues
show
Bug introduced by
The method listsTranslations() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean translations()?

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...
58
    }
59
60
    public function test_lists_of_translated_fields_with_fallback()
61
    {
62
        App::make('config')->set('translatable.fallback_locale', 'en');
63
        App::setLocale('de');
64
        $country = new Country();
65
        $country->useTranslationFallback = true;
0 ignored issues
show
Documentation introduced by
The property useTranslationFallback 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...
66
        $list = [[
67
            'id'   => '1',
68
            'name' => 'Griechenland',
69
        ], [
70
            'id'   => '2',
71
            'name' => 'France',
72
        ]];
73
        $this->assertArraySubset($list, $country->listsTranslations('name')->get()->toArray());
0 ignored issues
show
Bug introduced by
The method listsTranslations() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean translations()?

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...
74
    }
75
76
    public function test_scope_withTranslation_without_fallback()
77
    {
78
        $result = Country::withTranslation()->first();
0 ignored issues
show
Bug introduced by
The method withTranslation() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean replicateWithTranslations()?

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...
79
        $loadedTranslations = $result->toArray()['translations'];
80
        $this->assertCount(1, $loadedTranslations);
81
        $this->assertSame('Greece', $loadedTranslations[0]['name']);
82
    }
83
84
    public function test_scope_withTranslation_with_fallback()
85
    {
86
        App::make('config')->set('translatable.fallback_locale', 'de');
87
        App::make('config')->set('translatable.use_fallback', true);
88
89
        $result = Country::withTranslation()->first();
0 ignored issues
show
Bug introduced by
The method withTranslation() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean replicateWithTranslations()?

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...
90
        $loadedTranslations = $result->toArray()['translations'];
91
        $this->assertCount(2, $loadedTranslations);
92
        $this->assertSame('Greece', $loadedTranslations[0]['name']);
93
        $this->assertSame('Griechenland', $loadedTranslations[1]['name']);
94
    }
95
96
    public function test_scope_withTranslation_with_country_based_fallback() {
97
        App::make('config')->set('translatable.fallback_locale', 'en');
98
        App::make('config')->set('translatable.use_fallback', true);
99
        App::setLocale('en-GB');
100
        $result = Vegetable::withTranslation()->find(1)->toArray();
0 ignored issues
show
Bug introduced by
The method withTranslation() does not exist on Dimsav\Translatable\Test\Model\Vegetable. Did you maybe mean replicateWithTranslations()?

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...
101
        $this->assertSame('courgette', $result['name']);
102
103
        App::setLocale('de-CH');
104
        $result = Vegetable::withTranslation()->find(1)->toArray();
0 ignored issues
show
Bug introduced by
The method withTranslation() does not exist on Dimsav\Translatable\Test\Model\Vegetable. Did you maybe mean replicateWithTranslations()?

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...
105
        $expectedTranslations = [
106
            ['name' => 'zucchini', 'locale' => 'en',],
107
            ['name' => 'Zucchini', 'locale' => 'de',],
108
            ['name' => 'Zucchetti', 'locale' => 'de-CH',],
109
        ];
110
        $this->assertArraySubset($expectedTranslations, $result['translations']);
111
    }
112
113
    public function test_whereTranslation_filters_by_translation()
114
    {
115
        /** @var Country $country */
116
        $country = Country::whereTranslation('name', 'Greece')->first();
0 ignored issues
show
Bug introduced by
The method whereTranslation() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslation()?

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...
117
        $this->assertSame('gr', $country->code);
0 ignored issues
show
Documentation introduced by
The property code 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...
118
    }
119
120 View Code Duplication
    public function test_orWhereTranslation_filters_by_translation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
121
    {
122
        $result = Country::whereTranslation('name', 'Greece')->orWhereTranslation('name', 'France')->get();
0 ignored issues
show
Bug introduced by
The method whereTranslation() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslation()?

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...
123
        $this->assertCount(2, $result);
124
        $this->assertSame('Greece', $result[0]->name);
125
        $this->assertSame('France', $result[1]->name);
126
    }
127
128 View Code Duplication
    public function test_whereTranslation_filters_by_translation_and_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
129
    {
130
        Country::create(['code' => 'some-code', 'name' => 'Griechenland']);
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...
131
132
        $this->assertSame(2, Country::whereTranslation('name', 'Griechenland')->count());
0 ignored issues
show
Bug introduced by
The method whereTranslation() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslation()?

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...
133
134
        $result = Country::whereTranslation('name', 'Griechenland', 'de')->get();
0 ignored issues
show
Bug introduced by
The method whereTranslation() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslation()?

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...
135
        $this->assertSame(1, $result->count());
136
        $this->assertSame('gr', $result->first()->code);
137
    }
138
139
    public function test_whereTranslationLike_filters_by_translation()
140
    {
141
        /** @var Country $country */
142
        $country = Country::whereTranslationLike('name', '%Greec%')->first();
0 ignored issues
show
Bug introduced by
The method whereTranslationLike() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslationLike()?

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...
143
        $this->assertSame('gr', $country->code);
0 ignored issues
show
Documentation introduced by
The property code 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...
144
    }
145
146 View Code Duplication
    public function test_orWhereTranslationLike_filters_by_translation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
147
    {
148
        $result = Country::whereTranslationLike('name', '%eece%')->orWhereTranslationLike('name', '%ance%')->get();
0 ignored issues
show
Bug introduced by
The method whereTranslationLike() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslationLike()?

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...
149
        $this->assertCount(2, $result);
150
        $this->assertSame('Greece', $result[0]->name);
151
        $this->assertSame('France', $result[1]->name);
152
    }
153
154 View Code Duplication
    public function test_whereTranslationLike_filters_by_translation_and_locale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
155
    {
156
        Country::create(['code' => 'some-code', 'name' => 'Griechenland']);
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...
157
158
        $this->assertSame(2, Country::whereTranslationLike('name', 'Griechen%')->count());
0 ignored issues
show
Bug introduced by
The method whereTranslationLike() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslationLike()?

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...
159
160
        $result = Country::whereTranslationLike('name', '%riechenlan%', 'de')->get();
0 ignored issues
show
Bug introduced by
The method whereTranslationLike() does not exist on Dimsav\Translatable\Test\Model\Country. Did you maybe mean scopeWhereTranslationLike()?

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...
161
        $this->assertSame(1, $result->count());
162
        $this->assertSame('gr', $result->first()->code);
163
    }
164
}
165