1
+ <?php
2
+
3
+ class EloquentTest extends PHPUnit_Framework_TestCase {
4
+
5
+ /**
6
+ * Test the Model constructor.
7
+ *
8
+ * @group laravel
9
+ */
10
+ public function testAttributesAreSetByConstructor ()
11
+ {
12
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'setter ' => 'foo ' );
13
+
14
+ $ model = new Model ($ array );
15
+
16
+ $ this ->assertEquals ('Taylor ' , $ model ->name );
17
+ $ this ->assertEquals (25 , $ model ->age );
18
+ $ this ->assertEquals ('setter: foo ' , $ model ->setter );
19
+ }
20
+
21
+ /**
22
+ * Test the Model::fill method.
23
+ *
24
+ * @group laravel
25
+ */
26
+ public function testAttributesAreSetByFillMethod ()
27
+ {
28
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'setter ' => 'foo ' );
29
+
30
+ $ model = new Model ();
31
+ $ model ->fill ($ array );
32
+
33
+ $ this ->assertEquals ('Taylor ' , $ model ->name );
34
+ $ this ->assertEquals (25 , $ model ->age );
35
+ $ this ->assertEquals ('setter: foo ' , $ model ->setter );
36
+ }
37
+
38
+ /**
39
+ * Test the Model::fill_raw method.
40
+ *
41
+ * @group laravel
42
+ */
43
+ public function testAttributesAreSetByFillRawMethod ()
44
+ {
45
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'setter ' => 'foo ' );
46
+
47
+ $ model = new Model ();
48
+ $ model ->fill_raw ($ array );
49
+
50
+ $ this ->assertEquals ($ array , $ model ->attributes );
51
+ }
52
+
53
+ /**
54
+ * Test the Model::fill method with accessible.
55
+ *
56
+ * @group laravel
57
+ */
58
+ public function testAttributesAreSetByFillMethodWithAccessible ()
59
+ {
60
+ Model::$ accessible = array ('name ' , 'age ' );
61
+
62
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'foo ' => 'bar ' );
63
+
64
+ $ model = new Model ();
65
+ $ model ->fill ($ array );
66
+
67
+ $ this ->assertEquals ('Taylor ' , $ model ->name );
68
+ $ this ->assertEquals (25 , $ model ->age );
69
+ $ this ->assertNull ($ model ->foo );
70
+
71
+ Model::$ accessible = null ;
72
+ }
73
+
74
+ /**
75
+ * Test the Model::fill method with empty accessible array.
76
+ *
77
+ * @group laravel
78
+ */
79
+ public function testAttributesAreSetByFillMethodWithEmptyAccessible ()
80
+ {
81
+ Model::$ accessible = array ();
82
+
83
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'foo ' => 'bar ' );
84
+
85
+ $ model = new Model ();
86
+ $ model ->fill ($ array );
87
+
88
+ $ this ->assertEquals (array (), $ model ->attributes );
89
+ $ this ->assertNull ($ model ->name );
90
+ $ this ->assertNull ($ model ->age );
91
+ $ this ->assertNull ($ model ->foo );
92
+
93
+ Model::$ accessible = null ;
94
+ }
95
+
96
+ /**
97
+ * Test the Model::fill_raw method with accessible.
98
+ *
99
+ * @group laravel
100
+ */
101
+ public function testAttributesAreSetByFillRawMethodWithAccessible ()
102
+ {
103
+ Model::$ accessible = array ('name ' , 'age ' );
104
+
105
+ $ array = array ('name ' => 'taylor ' , 'age ' => 25 , 'setter ' => 'foo ' );
106
+
107
+ $ model = new Model ();
108
+ $ model ->fill_raw ($ array );
109
+
110
+ $ this ->assertEquals ($ array , $ model ->attributes );
111
+
112
+ Model::$ accessible = null ;
113
+ }
114
+
115
+ /**
116
+ * Test the Model::__set method.
117
+ *
118
+ * @group laravel
119
+ */
120
+ public function testAttributeMagicSetterMethodChangesAttribute ()
121
+ {
122
+ Model::$ accessible = array ('setter ' );
123
+
124
+ $ array = array ('setter ' => 'foo ' , 'getter ' => 'bar ' );
125
+
126
+ $ model = new Model ($ array );
127
+ $ model ->setter = 'bar ' ;
128
+ $ model ->getter = 'foo ' ;
129
+
130
+ $ this ->assertEquals ('setter: bar ' , $ model ->get_attribute ('setter ' ));
131
+ $ this ->assertEquals ('foo ' , $ model ->get_attribute ('getter ' ));
132
+
133
+ Model::$ accessible = null ;
134
+ }
135
+
136
+ /**
137
+ * Test the Model::__get method.
138
+ *
139
+ * @group laravel
140
+ */
141
+ public function testAttributeMagicGetterMethodReturnsAttribute ()
142
+ {
143
+ $ array = array ('setter ' => 'foo ' , 'getter ' => 'bar ' );
144
+
145
+ $ model = new Model ($ array );
146
+
147
+ $ this ->assertEquals ('setter: foo ' , $ model ->setter );
148
+ $ this ->assertEquals ('getter: bar ' , $ model ->getter );
149
+ }
150
+
151
+ /**
152
+ * Test the Model::set_* method.
153
+ *
154
+ * @group laravel
155
+ */
156
+ public function testAttributeSetterMethodChangesAttribute ()
157
+ {
158
+ Model::$ accessible = array ('setter ' );
159
+
160
+ $ array = array ('setter ' => 'foo ' , 'getter ' => 'bar ' );
161
+
162
+ $ model = new Model ($ array );
163
+ $ model ->set_setter ('bar ' );
164
+ $ model ->set_getter ('foo ' );
165
+
166
+ $ this ->assertEquals ('setter: bar ' , $ model ->get_attribute ('setter ' ));
167
+ $ this ->assertEquals ('foo ' , $ model ->get_attribute ('getter ' ));
168
+
169
+ Model::$ accessible = null ;
170
+ }
171
+
172
+ /**
173
+ * Test the Model::get_* method.
174
+ *
175
+ * @group laravel
176
+ */
177
+ public function testAttributeGetterMethodReturnsAttribute ()
178
+ {
179
+ $ array = array ('setter ' => 'foo ' , 'getter ' => 'bar ' );
180
+
181
+ $ model = new Model ($ array );
182
+
183
+ $ this ->assertEquals ('setter: foo ' , $ model ->get_setter ());
184
+ $ this ->assertEquals ('getter: bar ' , $ model ->get_getter ());
185
+ }
186
+
187
+ /**
188
+ * Test determination of dirty/changed attributes.
189
+ *
190
+ * @group laravel
191
+ */
192
+ public function testDeterminationOfChangedAttributes ()
193
+ {
194
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'foo ' => null );
195
+
196
+ $ model = new Model ($ array , true );
197
+ $ model ->name = 'Otwell ' ;
198
+ $ model ->new = null ;
199
+
200
+ $ this ->assertTrue ($ model ->changed ('name ' ));
201
+ $ this ->assertFalse ($ model ->changed ('age ' ));
202
+ $ this ->assertFalse ($ model ->changed ('foo ' ));
203
+ $ this ->assertFalse ($ model ->changed ('new ' ));
204
+ $ this ->assertTrue ($ model ->dirty ());
205
+ $ this ->assertEquals (array ('name ' => 'Otwell ' , 'new ' => null ), $ model ->get_dirty ());
206
+
207
+ $ model ->sync ();
208
+
209
+ $ this ->assertFalse ($ model ->changed ('name ' ));
210
+ $ this ->assertFalse ($ model ->changed ('age ' ));
211
+ $ this ->assertFalse ($ model ->changed ('foo ' ));
212
+ $ this ->assertFalse ($ model ->changed ('new ' ));
213
+ $ this ->assertFalse ($ model ->dirty ());
214
+ $ this ->assertEquals (array (), $ model ->get_dirty ());
215
+ }
216
+
217
+ /**
218
+ * Test the Model::purge method.
219
+ *
220
+ * @group laravel
221
+ */
222
+ public function testAttributePurge ()
223
+ {
224
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 );
225
+
226
+ $ model = new Model ($ array );
227
+ $ model ->name = 'Otwell ' ;
228
+ $ model ->age = 26 ;
229
+
230
+ $ model ->purge ('name ' );
231
+
232
+ $ this ->assertFalse ($ model ->changed ('name ' ));
233
+ $ this ->assertNull ($ model ->name );
234
+ $ this ->assertTrue ($ model ->changed ('age ' ));
235
+ $ this ->assertEquals (26 , $ model ->age );
236
+ $ this ->assertEquals (array ('age ' => 26 ), $ model ->get_dirty ());
237
+ }
238
+
239
+ /**
240
+ * Test the Model::table method.
241
+ *
242
+ * @group laravel
243
+ */
244
+ public function testTableMethodReturnsCorrectName ()
245
+ {
246
+ $ model = new Model ();
247
+ $ this ->assertEquals ('models ' , $ model ->table ());
248
+
249
+ Model::$ table = 'table ' ;
250
+ $ this ->assertEquals ('table ' , $ model ->table ());
251
+
252
+ Model::$ table = null ;
253
+ $ this ->assertEquals ('models ' , $ model ->table ());
254
+ }
255
+
256
+ /**
257
+ * Test the Model::to_array method.
258
+ *
259
+ * @group laravel
260
+ */
261
+ public function testConvertingToArray ()
262
+ {
263
+ Model::$ hidden = array ('password ' , 'hidden ' );
264
+
265
+ $ array = array ('name ' => 'Taylor ' , 'age ' => 25 , 'password ' => 'laravel ' , 'null ' => null );
266
+
267
+ $ model = new Model ($ array );
268
+
269
+ $ first = new Model (array ('first ' => 'foo ' , 'password ' => 'hidden ' ));
270
+ $ second = new Model (array ('second ' => 'bar ' , 'password ' => 'hidden ' ));
271
+ $ third = new Model (array ('third ' => 'baz ' , 'password ' => 'hidden ' ));
272
+
273
+ $ model ->relationships ['one ' ] = new Model (array ('foo ' => 'bar ' , 'password ' => 'hidden ' ));
274
+ $ model ->relationships ['many ' ] = array ($ first , $ second , $ third );
275
+ $ model ->relationships ['hidden ' ] = new Model (array ('should ' => 'visible ' ));
276
+ $ model ->relationships ['null ' ] = null ;
277
+
278
+ $ this ->assertEquals (array (
279
+ 'name ' => 'Taylor ' , 'age ' => 25 , 'null ' => null ,
280
+ 'one ' => array ('foo ' => 'bar ' ),
281
+ 'many ' => array (
282
+ array ('first ' => 'foo ' ),
283
+ array ('second ' => 'bar ' ),
284
+ array ('third ' => 'baz ' ),
285
+ ),
286
+ 'hidden ' => array ('should ' => 'visible ' ),
287
+ 'null ' => null ,
288
+ ), $ model ->to_array ());
289
+
290
+ }
291
+
292
+ }
0 commit comments