|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$country = new Continent(); |
|
24
|
|
|
$country->save(); |
|
25
|
|
|
$this->assertGreaterThan(0, $country->id); |
|
|
|
|
|
|
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'; |
|
|
|
|
|
|
47
|
|
|
$country->code = null; |
|
|
|
|
|
|
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'; |
|
|
|
|
|
|
58
|
|
|
$country->name = null; |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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']; |
|
|
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.