1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Dimsav\Translatable\Test\Model; |
4
|
|
|
use Dimsav\Translatable\Test\Model\City; |
5
|
|
|
use Dimsav\Translatable\Test\Model\Company; |
6
|
|
|
use Dimsav\Translatable\Test\Model\Country; |
7
|
|
|
use Dimsav\Translatable\Test\Model\Continent; |
8
|
|
|
use Dimsav\Translatable\Test\Model\Vegetable; |
9
|
|
|
use Dimsav\Translatable\Test\Model\CountryStrict; |
10
|
|
|
use Dimsav\Translatable\Test\Model\CountryGuarded; |
11
|
|
|
use Dimsav\Translatable\Test\Model\CityTranslation; |
12
|
|
|
use Dimsav\Translatable\Test\Model\CountryTranslation; |
13
|
|
|
|
14
|
|
|
class TestCoreModelExtension extends TestsBase |
15
|
|
|
{ |
16
|
|
|
// Saving |
17
|
|
|
|
18
|
|
|
public function test_it_saves_empty_instances() |
19
|
|
|
{ |
20
|
|
|
$company = new Company(); |
21
|
|
|
$company->save(); |
22
|
|
|
$this->assertGreaterThan(0, $company->id); |
23
|
|
|
|
24
|
|
|
$country = new Continent(); |
25
|
|
|
$country->save(); |
26
|
|
|
$this->assertGreaterThan(0, $country->id); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function test_it_saves_translations_when_existing_and_dirty() |
30
|
|
|
{ |
31
|
|
|
$country = Country::find(1); |
32
|
|
|
$country->code = 'make_model_dirty'; |
33
|
|
|
$country->name = 'abc'; |
34
|
|
|
$this->assertTrue($country->save()); |
35
|
|
|
$country = Country::find(1); |
36
|
|
|
$this->assertEquals($country->name, 'abc'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Failing saving |
40
|
|
|
|
41
|
|
|
public function test_it_throws_query_exception_if_code_is_null() |
42
|
|
|
{ |
43
|
|
|
$this->expectException('\Exception'); |
44
|
|
|
|
45
|
|
|
$country = new Country(); |
46
|
|
|
$country->name = 'Belgium'; |
47
|
|
|
$country->code = null; |
48
|
|
|
$country->save(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function test_it_throws_query_exception_if_saving_and_name_is_null() |
52
|
|
|
{ |
53
|
|
|
$this->expectException(\Exception::class); |
54
|
|
|
|
55
|
|
|
$country = new Country(); |
56
|
|
|
$country->code = 'be'; |
57
|
|
|
$country->name = null; |
58
|
|
|
$country->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function test_it_returns_false_if_exists_and_dirty_and_parent_save_returns_false() |
62
|
|
|
{ |
63
|
|
|
$that = $this; |
64
|
|
|
$event = App::make('events'); |
65
|
|
|
$event->listen('eloquent*', function ($event, $models) use ($that) { |
66
|
|
|
return get_class(reset($models)) == 'Dimsav\Translatable\Test\Model\Country' ? false : true; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$country = Country::find(1); |
70
|
|
|
$country->code = 'make_model_dirty'; |
71
|
|
|
$country->name = 'abc'; |
72
|
|
|
$this->assertFalse($country->save()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function test_it_returns_false_if_does_not_exist_and_parent_save_returns_false() |
76
|
|
|
{ |
77
|
|
|
$that = $this; |
78
|
|
|
$event = App::make('events'); |
79
|
|
|
$event->listen('eloquent*', function ($event, $models) use ($that) { |
80
|
|
|
return get_class(reset($models)) == 'Dimsav\Translatable\Test\Model\Continent' ? false : true; |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$continent = new Continent(); |
84
|
|
|
$this->assertFalse($continent->save()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Filling |
88
|
|
|
|
89
|
|
|
public function test_it_throws_exception_if_filling_a_protected_property() |
90
|
|
|
{ |
91
|
|
|
$this->expectException(Illuminate\Database\Eloquent\MassAssignmentException::class); |
92
|
|
|
|
93
|
|
|
$country = new CountryGuarded(); |
94
|
|
|
$this->assertTrue($country->totallyGuarded()); |
95
|
|
|
$country->fill(['code' => 'it', 'en' => ['name' => 'Italy']]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function test_translation_throws_exception_if_filling_a_protected_property() |
99
|
|
|
{ |
100
|
|
|
$this->expectException(Illuminate\Database\Eloquent\MassAssignmentException::class); |
101
|
|
|
|
102
|
|
|
$country = new Country(); |
103
|
|
|
$country->translationModel = Model\CountryTranslationGuarded::class; |
104
|
|
|
$country->fill(['code' => 'it', 'en' => ['name' => 'Italy']]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Deleting |
108
|
|
|
|
109
|
|
View Code Duplication |
public function test_it_deletes_translations() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$city = City::find(1); |
112
|
|
|
$cityId = $city->id; |
113
|
|
|
$translation = $city->translate('en'); |
114
|
|
|
$this->assertTrue(is_object($translation)); |
115
|
|
|
$city->delete(); |
116
|
|
|
$city = City::find($cityId); |
117
|
|
|
$this->assertNull($city); |
118
|
|
|
$translations = CityTranslation::where('city_id', '=', $cityId)->get(); |
119
|
|
|
$this->assertEquals(0, count($translations)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function test_it_does_not_delete_translations_when_attempting_to_delete_translatable() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$country = Country::find(1); |
125
|
|
|
$countryId = $country->id; |
126
|
|
|
$translation = $country->translate('en'); |
127
|
|
|
$this->assertTrue(is_object($translation)); |
128
|
|
|
try { |
129
|
|
|
$country->delete(); |
130
|
|
|
} catch (\Exception $e) { |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$country = Country::find(1); |
134
|
|
|
$this->assertNotNull($country); |
135
|
|
|
|
136
|
|
|
$translations = CountryTranslation::where('country_id', '=', $countryId)->get(); |
137
|
|
|
$this->assertEquals(4, count($translations)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function test_it_does_not_delete_translations_while_force_deleting() |
141
|
|
|
{ |
142
|
|
|
$country = CountryStrict::find(2); |
143
|
|
|
$country->forceDelete(); |
144
|
|
|
$after = CountryTranslation::where('country_id', '=', 2)->get(); |
145
|
|
|
$this->assertEquals(0, count($after)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function test_to_array_returns_translated_attributes() |
149
|
|
|
{ |
150
|
|
|
$country = Country::find(1); |
151
|
|
|
$this->assertArrayHasKey('name', $country->toArray()); |
152
|
|
|
$this->assertArrayHasKey('code', $country->toArray()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function test_to_array_wont_break_if_no_translations_exist() |
156
|
|
|
{ |
157
|
|
|
$country = new Country(['code' => 'test']); |
158
|
|
|
$country->save(); |
159
|
|
|
$this->assertArrayHasKey('code', $country->toArray()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Forms |
163
|
|
|
|
164
|
|
|
public function test_it_fakes_isset_for_translated_attributes() |
165
|
|
|
{ |
166
|
|
|
$country = Country::find(1); |
167
|
|
|
$this->assertEquals(true, isset($country->name)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Hidden attributes |
171
|
|
|
|
172
|
|
|
public function test_it_should_hide_attributes_after_to_array() |
173
|
|
|
{ |
174
|
|
|
$country = Country::find(1); |
175
|
|
|
|
176
|
|
|
$this->assertEquals(true, isset($country->toArray()['name'])); |
177
|
|
|
|
178
|
|
|
// it is equivalent to set |
179
|
|
|
// protected $hidden = ['name']; |
180
|
|
|
// in Eloquent |
181
|
|
|
$country->setHidden(['name']); |
182
|
|
|
$this->assertEquals(false, isset($country->toArray()['name'])); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function test_it_finds_custom_primary_keys() |
186
|
|
|
{ |
187
|
|
|
$vegetable = new Vegetable; |
188
|
|
|
$this->assertSame('vegetable_identity', $vegetable->getRelationKey()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function test_setAttribute_returns_this() |
192
|
|
|
{ |
193
|
|
|
$country = new Country; |
194
|
|
|
$this->assertSame($country, $country->setAttribute('code', 'ch')); |
195
|
|
|
$this->assertSame($country, $country->setAttribute('name', 'China')); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
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.