Completed
Push — master ( efeaa7...112f38 )
by Dimitrios
07:29
created

TestCoreModelExtension   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 178
Duplicated Lines 16.29 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 13
Bugs 2 Features 4
Metric Value
wmc 19
c 13
b 2
f 4
lcom 1
cbo 6
dl 29
loc 178
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A test_it_saves_empty_instances() 0 10 1
A test_it_saves_translations_when_existing_and_dirty() 0 9 1
A test_it_throws_query_exception_if_code_is_null() 0 7 1
A test_it_throws_query_exception_if_saving_and_name_is_null() 0 7 1
A test_it_returns_false_if_exists_and_dirty_and_parent_save_returns_false() 0 13 2
A test_it_returns_false_if_does_not_exist_and_parent_save_returns_false() 0 11 2
A test_it_throws_exception_if_filling_a_protected_property() 0 6 1
A test_it_deletes_translations() 12 12 1
A test_it_does_not_delete_translations_when_attempting_to_delete_translatable() 17 17 2
A test_it_does_not_delete_translations_while_force_deleting() 0 7 1
A test_to_array_returs_translated_attributes() 0 6 1
A test_to_array_wont_break_if_no_translations_exist() 0 6 1
A test_it_fakes_isset_for_translated_attributes() 0 5 1
A test_it_should_hide_attributes_after_to_array() 0 12 1
A test_it_finds_custom_primary_keys() 0 5 1
A test_setAttribute_returns_this() 0 6 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\City;
4
use Dimsav\Translatable\Test\Model\CityTranslation;
5
use Dimsav\Translatable\Test\Model\Company;
6
use Dimsav\Translatable\Test\Model\Continent;
7
use Dimsav\Translatable\Test\Model\Country;
8
use Dimsav\Translatable\Test\Model\CountryGuarded;
9
use Dimsav\Translatable\Test\Model\CountryStrict;
10
use Dimsav\Translatable\Test\Model\CountryTranslation;
11
use Dimsav\Translatable\Test\Model\Vegetable;
12
13
class TestCoreModelExtension 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...
14
{
15
    // Saving
16
17
    public function test_it_saves_empty_instances()
18
    {
19
        $company = new Company();
20
        $company->save();
21
        $this->assertGreaterThan(0, $company->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Dimsav\Translatable\Test\Model\Company>. 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...
22
23
        $country = new Continent();
24
        $country->save();
25
        $this->assertGreaterThan(0, $country->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Dimsav\Translatable\Test\Model\Continent>. 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...
26
    }
27
28
    public function test_it_saves_translations_when_existing_and_dirty()
29
    {
30
        $country = Country::find(1);
31
        $country->code = 'make_model_dirty';
32
        $country->name = 'abc';
33
        $this->assertTrue($country->save());
34
        $country = Country::find(1);
35
        $this->assertEquals($country->name, 'abc');
36
    }
37
38
    // Failing saving
39
40
    /**
41
     * @expectedException \Exception
42
     */
43
    public function test_it_throws_query_exception_if_code_is_null()
44
    {
45
        $country = new Country();
46
        $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...
47
        $country->code = null;
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...
48
        $country->save();
49
    }
50
51
    /**
52
     * @expectedException \Exception
53
     */
54
    public function test_it_throws_query_exception_if_saving_and_name_is_null()
55
    {
56
        $country = new Country();
57
        $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...
58
        $country->name = null;
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...
59
        $country->save();
60
    }
61
62
    public function test_it_returns_false_if_exists_and_dirty_and_parent_save_returns_false()
63
    {
64
        $that = $this;
65
        $event = App::make('events');
66
        $event->listen('eloquent*', function ($model) use ($that) {
67
            return get_class($model) == 'Dimsav\Translatable\Test\Model\Country' ? false : true;
68
        });
69
70
        $country = Country::find(1);
71
        $country->code = 'make_model_dirty';
72
        $country->name = 'abc';
73
        $this->assertFalse($country->save());
74
    }
75
76
    public function test_it_returns_false_if_does_not_exist_and_parent_save_returns_false()
77
    {
78
        $that = $this;
79
        $event = App::make('events');
80
        $event->listen('eloquent*', function ($model) use ($that) {
81
            return get_class($model) == 'Dimsav\Translatable\Test\Model\Continent' ? false : true;
82
        });
83
84
        $continent = new Continent();
85
        $this->assertFalse($continent->save());
86
    }
87
88
    // Filling
89
90
    /**
91
     * @expectedException Illuminate\Database\Eloquent\MassAssignmentException
92
     */
93
    public function test_it_throws_exception_if_filling_a_protected_property()
94
    {
95
        $country = new CountryGuarded();
96
        $this->assertTrue($country->totallyGuarded());
97
        $country->fill(['en' => ['name' => 'Italy']]);
98
    }
99
100
    // Deleting
101
102 View Code Duplication
    public function test_it_deletes_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...
103
    {
104
        $city = City::find(1);
105
        $cityId = $city->id;
106
        $translation = $city->translate('en');
107
        $this->assertTrue(is_object($translation));
108
        $city->delete();
109
        $city = City::find($cityId);
110
        $this->assertNull($city);
111
        $translations = CityTranslation::where('city_id', '=', $cityId)->get();
112
        $this->assertEquals(0, count($translations));
113
    }
114
115 View Code Duplication
    public function test_it_does_not_delete_translations_when_attempting_to_delete_translatable()
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...
116
    {
117
        $country = Country::find(1);
118
        $countryId = $country->id;
119
        $translation = $country->translate('en');
120
        $this->assertTrue(is_object($translation));
121
        try {
122
            $country->delete();
123
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
124
        }
125
126
        $country = Country::find(1);
127
        $this->assertNotNull($country);
128
129
        $translations = CountryTranslation::where('country_id', '=', $countryId)->get();
130
        $this->assertEquals(4, count($translations));
131
    }
132
133
    public function test_it_does_not_delete_translations_while_force_deleting()
134
    {
135
        $country = CountryStrict::find(2);
136
        $country->forceDelete();
137
        $after = CountryTranslation::where('country_id', '=', 2)->get();
138
        $this->assertEquals(0, count($after));
139
    }
140
141
    public function test_to_array_returs_translated_attributes()
142
    {
143
        $country = Country::find(1);
144
        $this->assertArrayHasKey('name', $country->toArray());
145
        $this->assertArrayHasKey('code', $country->toArray());
146
    }
147
148
    public function test_to_array_wont_break_if_no_translations_exist()
149
    {
150
        $country = new Country(['code' => 'test']);
151
        $country->save();
152
        $country->toArray();
153
    }
154
155
    // Forms
156
157
    public function test_it_fakes_isset_for_translated_attributes()
158
    {
159
        $country = Country::find(1);
160
        $this->assertEquals(true, isset($country->name));
161
    }
162
163
    // Hidden attributes
164
165
    public function test_it_should_hide_attributes_after_to_array()
166
    {
167
        $country = Country::find(1);
168
169
        $this->assertEquals(true, isset($country->toArray()['name']));
170
171
        // it is equivalent to set
172
        //      protected $hidden = ['name'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
173
        // in Eloquent
174
        $country->setHidden(['name']);
175
        $this->assertEquals(false, isset($country->toArray()['name']));
176
    }
177
178
    public function test_it_finds_custom_primary_keys()
179
    {
180
        $vegetable = new Vegetable;
181
        $this->assertSame('vegetable_identity', $vegetable->getRelationKey());
182
    }
183
184
    public function test_setAttribute_returns_this()
185
    {
186
        $country = new Country;
187
        $this->assertSame($country, $country->setAttribute('code', 'ch'));
188
        $this->assertSame($country, $country->setAttribute('name', 'China'));
189
    }
190
}
191