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