1
1
<?php
2
2
3
+ use Carbon \Carbon ;
4
+ use Illuminate \Database \Eloquent \Collection ;
5
+ use Jenssegers \Mongodb \Eloquent \Model ;
6
+ use MongoDB \BSON \ObjectID ;
7
+ use MongoDB \BSON \UTCDateTime ;
8
+
3
9
class ModelTest extends TestCase
4
10
{
5
11
public function tearDown ()
@@ -13,7 +19,7 @@ public function tearDown()
13
19
public function testNewModel ()
14
20
{
15
21
$ user = new User ;
16
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ user );
22
+ $ this ->assertInstanceOf (Model::class , $ user );
17
23
$ this ->assertInstanceOf ('Jenssegers\Mongodb\Connection ' , $ user ->getConnection ());
18
24
$ this ->assertEquals (false , $ user ->exists );
19
25
$ this ->assertEquals ('users ' , $ user ->getTable ());
@@ -36,10 +42,10 @@ public function testInsert()
36
42
$ this ->assertTrue (is_string ($ user ->_id ));
37
43
$ this ->assertNotEquals ('' , (string ) $ user ->_id );
38
44
$ this ->assertNotEquals (0 , strlen ((string ) $ user ->_id ));
39
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->created_at );
45
+ $ this ->assertInstanceOf (Carbon::class , $ user ->created_at );
40
46
41
47
$ raw = $ user ->getAttributes ();
42
- $ this ->assertInstanceOf (' MongoDB\BSON\ ObjectID' , $ raw ['_id ' ]);
48
+ $ this ->assertInstanceOf (ObjectID::class , $ raw ['_id ' ]);
43
49
44
50
$ this ->assertEquals ('John Doe ' , $ user ->name );
45
51
$ this ->assertEquals (35 , $ user ->age );
@@ -54,16 +60,16 @@ public function testUpdate()
54
60
$ user ->save ();
55
61
56
62
$ raw = $ user ->getAttributes ();
57
- $ this ->assertInstanceOf (' MongoDB\BSON\ ObjectID' , $ raw ['_id ' ]);
63
+ $ this ->assertInstanceOf (ObjectID::class , $ raw ['_id ' ]);
58
64
59
65
$ check = User::find ($ user ->_id );
60
66
61
67
$ check ->age = 36 ;
62
68
$ check ->save ();
63
69
64
70
$ this ->assertEquals (true , $ check ->exists );
65
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ check ->created_at );
66
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ check ->updated_at );
71
+ $ this ->assertInstanceOf (Carbon::class , $ check ->created_at );
72
+ $ this ->assertInstanceOf (Carbon::class , $ check ->updated_at );
67
73
$ this ->assertEquals (1 , User::count ());
68
74
69
75
$ this ->assertEquals ('John Doe ' , $ check ->name );
@@ -72,7 +78,7 @@ public function testUpdate()
72
78
$ user ->update (['age ' => 20 ]);
73
79
74
80
$ raw = $ user ->getAttributes ();
75
- $ this ->assertInstanceOf (' MongoDB\BSON\ ObjectID' , $ raw ['_id ' ]);
81
+ $ this ->assertInstanceOf (ObjectID::class , $ raw ['_id ' ]);
76
82
77
83
$ check = User::find ($ user ->_id );
78
84
$ this ->assertEquals (20 , $ check ->age );
@@ -91,7 +97,7 @@ public function testManualStringId()
91
97
$ this ->assertEquals ('4af9f23d8ead0e1d32000000 ' , $ user ->_id );
92
98
93
99
$ raw = $ user ->getAttributes ();
94
- $ this ->assertInstanceOf (' MongoDB\BSON\ ObjectID' , $ raw ['_id ' ]);
100
+ $ this ->assertInstanceOf (ObjectID::class , $ raw ['_id ' ]);
95
101
96
102
$ user = new User ;
97
103
$ user ->_id = 'customId ' ;
@@ -170,7 +176,7 @@ public function testFind()
170
176
171
177
$ check = User::find ($ user ->_id );
172
178
173
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ check );
179
+ $ this ->assertInstanceOf (Model::class , $ check );
174
180
$ this ->assertEquals (true , $ check ->exists );
175
181
$ this ->assertEquals ($ user ->_id , $ check ->_id );
176
182
@@ -187,8 +193,8 @@ public function testGet()
187
193
188
194
$ users = User::get ();
189
195
$ this ->assertEquals (2 , count ($ users ));
190
- $ this ->assertInstanceOf (' Illuminate\Database\Eloquent\ Collection' , $ users );
191
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ users [0 ]);
196
+ $ this ->assertInstanceOf (Collection::class , $ users );
197
+ $ this ->assertInstanceOf (Model::class , $ users [0 ]);
192
198
}
193
199
194
200
public function testFirst ()
@@ -199,14 +205,14 @@ public function testFirst()
199
205
]);
200
206
201
207
$ user = User::first ();
202
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ user );
208
+ $ this ->assertInstanceOf (Model::class , $ user );
203
209
$ this ->assertEquals ('John Doe ' , $ user ->name );
204
210
}
205
211
206
212
public function testNoDocument ()
207
213
{
208
214
$ items = Item::where ('name ' , 'nothing ' )->get ();
209
- $ this ->assertInstanceOf (' Illuminate\Database\Eloquent\ Collection' , $ items );
215
+ $ this ->assertInstanceOf (Collection::class , $ items );
210
216
$ this ->assertEquals (0 , $ items ->count ());
211
217
212
218
$ item = Item::where ('name ' , 'nothing ' )->first ();
@@ -218,15 +224,15 @@ public function testNoDocument()
218
224
219
225
public function testFindOrfail ()
220
226
{
221
- $ this ->expectException (' Illuminate\Database\Eloquent\ModelNotFoundException ' );
227
+ $ this ->expectException (Illuminate \Database \Eloquent \ModelNotFoundException::class );
222
228
User::findOrfail ('51c33d8981fec6813e00000a ' );
223
229
}
224
230
225
231
public function testCreate ()
226
232
{
227
233
$ user = User::create (['name ' => 'Jane Poe ' ]);
228
234
229
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ user );
235
+ $ this ->assertInstanceOf (Model::class , $ user );
230
236
$ this ->assertEquals (true , $ user ->exists );
231
237
$ this ->assertEquals ('Jane Poe ' , $ user ->name );
232
238
@@ -288,7 +294,7 @@ public function testSoftDelete()
288
294
289
295
$ user = Soft::withTrashed ()->where ('name ' , 'John Doe ' )->first ();
290
296
$ this ->assertNotNull ($ user );
291
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->deleted_at );
297
+ $ this ->assertInstanceOf (Carbon::class , $ user ->deleted_at );
292
298
$ this ->assertEquals (true , $ user ->trashed ());
293
299
294
300
$ user ->restore ();
@@ -370,10 +376,10 @@ public function testDates()
370
376
{
371
377
$ birthday = new DateTime ('1980/1/1 ' );
372
378
$ user = User::create (['name ' => 'John Doe ' , 'birthday ' => $ birthday ]);
373
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->birthday );
379
+ $ this ->assertInstanceOf (Carbon::class , $ user ->birthday );
374
380
375
381
$ check = User::find ($ user ->_id );
376
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ check ->birthday );
382
+ $ this ->assertInstanceOf (Carbon::class , $ check ->birthday );
377
383
$ this ->assertEquals ($ user ->birthday , $ check ->birthday );
378
384
379
385
$ user = User::where ('birthday ' , '> ' , new DateTime ('1975/1/1 ' ))->first ();
@@ -384,28 +390,36 @@ public function testDates()
384
390
$ this ->assertEquals ($ user ->birthday ->format ('l jS \of F Y h:i:s A ' ), $ json ['birthday ' ]);
385
391
$ this ->assertEquals ($ user ->created_at ->format ('l jS \of F Y h:i:s A ' ), $ json ['created_at ' ]);
386
392
393
+ // test created_at
394
+ $ item = Item::create (['name ' => 'sword ' ]);
395
+ $ this ->assertInstanceOf (UTCDateTime::class, $ item ->getOriginal ('created_at ' ));
396
+ $ this ->assertEquals ($ item ->getOriginal ('created_at ' )
397
+ ->toDateTime ()
398
+ ->getTimestamp (), $ item ->created_at ->getTimestamp ());
399
+ $ this ->assertTrue (abs (time () - $ item ->created_at ->getTimestamp ()) < 2 );
400
+
387
401
// test default date format for json output
388
402
$ item = Item::create (['name ' => 'sword ' ]);
389
403
$ json = $ item ->toArray ();
390
404
$ this ->assertEquals ($ item ->created_at ->format ('Y-m-d H:i:s ' ), $ json ['created_at ' ]);
391
405
392
406
$ user = User::create (['name ' => 'Jane Doe ' , 'birthday ' => time ()]);
393
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->birthday );
407
+ $ this ->assertInstanceOf (Carbon::class , $ user ->birthday );
394
408
395
409
$ user = User::create (['name ' => 'Jane Doe ' , 'birthday ' => 'Monday 8th of August 2005 03:12:46 PM ' ]);
396
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->birthday );
410
+ $ this ->assertInstanceOf (Carbon::class , $ user ->birthday );
397
411
398
412
$ user = User::create (['name ' => 'Jane Doe ' , 'birthday ' => '2005-08-08 ' ]);
399
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->birthday );
413
+ $ this ->assertInstanceOf (Carbon::class , $ user ->birthday );
400
414
401
415
$ user = User::create (['name ' => 'Jane Doe ' , 'entry ' => ['date ' => '2005-08-08 ' ]]);
402
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->getAttribute ('entry.date ' ));
416
+ $ this ->assertInstanceOf (Carbon::class , $ user ->getAttribute ('entry.date ' ));
403
417
404
418
$ user ->setAttribute ('entry.date ' , new DateTime );
405
- $ this ->assertInstanceOf (' Carbon\Carbon ' , $ user ->getAttribute ('entry.date ' ));
419
+ $ this ->assertInstanceOf (Carbon::class , $ user ->getAttribute ('entry.date ' ));
406
420
407
421
$ data = $ user ->toArray ();
408
- $ this ->assertNotInstanceOf (' MongoDB\BSON\ UTCDateTime' , $ data ['entry ' ]['date ' ]);
422
+ $ this ->assertNotInstanceOf (UTCDateTime::class , $ data ['entry ' ]['date ' ]);
409
423
$ this ->assertEquals ((string ) $ user ->getAttribute ('entry.date ' )->format ('Y-m-d H:i:s ' ), $ data ['entry ' ]['date ' ]);
410
424
}
411
425
@@ -453,14 +467,14 @@ public function testRaw()
453
467
$ users = User::raw (function ($ collection ) {
454
468
return $ collection ->find (['age ' => 35 ]);
455
469
});
456
- $ this ->assertInstanceOf (' Illuminate\Database\Eloquent\ Collection' , $ users );
457
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ users [0 ]);
470
+ $ this ->assertInstanceOf (Collection::class , $ users );
471
+ $ this ->assertInstanceOf (Model::class , $ users [0 ]);
458
472
459
473
$ user = User::raw (function ($ collection ) {
460
474
return $ collection ->findOne (['age ' => 35 ]);
461
475
});
462
476
463
- $ this ->assertInstanceOf (' Jenssegers\Mongodb\Eloquent\ Model' , $ user );
477
+ $ this ->assertInstanceOf (Model::class , $ user );
464
478
465
479
$ count = User::raw (function ($ collection ) {
466
480
return $ collection ->count ();
@@ -476,9 +490,9 @@ public function testRaw()
476
490
public function testDotNotation ()
477
491
{
478
492
$ user = User::create ([
479
- 'name ' => 'John Doe ' ,
493
+ 'name ' => 'John Doe ' ,
480
494
'address ' => [
481
- 'city ' => 'Paris ' ,
495
+ 'city ' => 'Paris ' ,
482
496
'country ' => 'France ' ,
483
497
],
484
498
]);
0 commit comments