Skip to content

Commit 163f726

Browse files
committed
Add tests for #1131
1 parent f400b85 commit 163f726

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

tests/ModelTest.php

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

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+
39
class ModelTest extends TestCase
410
{
511
public function tearDown()
@@ -13,7 +19,7 @@ public function tearDown()
1319
public function testNewModel()
1420
{
1521
$user = new User;
16-
$this->assertInstanceOf('Jenssegers\Mongodb\Eloquent\Model', $user);
22+
$this->assertInstanceOf(Model::class, $user);
1723
$this->assertInstanceOf('Jenssegers\Mongodb\Connection', $user->getConnection());
1824
$this->assertEquals(false, $user->exists);
1925
$this->assertEquals('users', $user->getTable());
@@ -36,10 +42,10 @@ public function testInsert()
3642
$this->assertTrue(is_string($user->_id));
3743
$this->assertNotEquals('', (string) $user->_id);
3844
$this->assertNotEquals(0, strlen((string) $user->_id));
39-
$this->assertInstanceOf('Carbon\Carbon', $user->created_at);
45+
$this->assertInstanceOf(Carbon::class, $user->created_at);
4046

4147
$raw = $user->getAttributes();
42-
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
48+
$this->assertInstanceOf(ObjectID::class, $raw['_id']);
4349

4450
$this->assertEquals('John Doe', $user->name);
4551
$this->assertEquals(35, $user->age);
@@ -54,16 +60,16 @@ public function testUpdate()
5460
$user->save();
5561

5662
$raw = $user->getAttributes();
57-
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
63+
$this->assertInstanceOf(ObjectID::class, $raw['_id']);
5864

5965
$check = User::find($user->_id);
6066

6167
$check->age = 36;
6268
$check->save();
6369

6470
$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);
6773
$this->assertEquals(1, User::count());
6874

6975
$this->assertEquals('John Doe', $check->name);
@@ -72,7 +78,7 @@ public function testUpdate()
7278
$user->update(['age' => 20]);
7379

7480
$raw = $user->getAttributes();
75-
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
81+
$this->assertInstanceOf(ObjectID::class, $raw['_id']);
7682

7783
$check = User::find($user->_id);
7884
$this->assertEquals(20, $check->age);
@@ -91,7 +97,7 @@ public function testManualStringId()
9197
$this->assertEquals('4af9f23d8ead0e1d32000000', $user->_id);
9298

9399
$raw = $user->getAttributes();
94-
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
100+
$this->assertInstanceOf(ObjectID::class, $raw['_id']);
95101

96102
$user = new User;
97103
$user->_id = 'customId';
@@ -170,7 +176,7 @@ public function testFind()
170176

171177
$check = User::find($user->_id);
172178

173-
$this->assertInstanceOf('Jenssegers\Mongodb\Eloquent\Model', $check);
179+
$this->assertInstanceOf(Model::class, $check);
174180
$this->assertEquals(true, $check->exists);
175181
$this->assertEquals($user->_id, $check->_id);
176182

@@ -187,8 +193,8 @@ public function testGet()
187193

188194
$users = User::get();
189195
$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]);
192198
}
193199

194200
public function testFirst()
@@ -199,14 +205,14 @@ public function testFirst()
199205
]);
200206

201207
$user = User::first();
202-
$this->assertInstanceOf('Jenssegers\Mongodb\Eloquent\Model', $user);
208+
$this->assertInstanceOf(Model::class, $user);
203209
$this->assertEquals('John Doe', $user->name);
204210
}
205211

206212
public function testNoDocument()
207213
{
208214
$items = Item::where('name', 'nothing')->get();
209-
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $items);
215+
$this->assertInstanceOf(Collection::class, $items);
210216
$this->assertEquals(0, $items->count());
211217

212218
$item = Item::where('name', 'nothing')->first();
@@ -218,15 +224,15 @@ public function testNoDocument()
218224

219225
public function testFindOrfail()
220226
{
221-
$this->expectException('Illuminate\Database\Eloquent\ModelNotFoundException');
227+
$this->expectException(Illuminate\Database\Eloquent\ModelNotFoundException::class);
222228
User::findOrfail('51c33d8981fec6813e00000a');
223229
}
224230

225231
public function testCreate()
226232
{
227233
$user = User::create(['name' => 'Jane Poe']);
228234

229-
$this->assertInstanceOf('Jenssegers\Mongodb\Eloquent\Model', $user);
235+
$this->assertInstanceOf(Model::class, $user);
230236
$this->assertEquals(true, $user->exists);
231237
$this->assertEquals('Jane Poe', $user->name);
232238

@@ -288,7 +294,7 @@ public function testSoftDelete()
288294

289295
$user = Soft::withTrashed()->where('name', 'John Doe')->first();
290296
$this->assertNotNull($user);
291-
$this->assertInstanceOf('Carbon\Carbon', $user->deleted_at);
297+
$this->assertInstanceOf(Carbon::class, $user->deleted_at);
292298
$this->assertEquals(true, $user->trashed());
293299

294300
$user->restore();
@@ -370,10 +376,10 @@ public function testDates()
370376
{
371377
$birthday = new DateTime('1980/1/1');
372378
$user = User::create(['name' => 'John Doe', 'birthday' => $birthday]);
373-
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
379+
$this->assertInstanceOf(Carbon::class, $user->birthday);
374380

375381
$check = User::find($user->_id);
376-
$this->assertInstanceOf('Carbon\Carbon', $check->birthday);
382+
$this->assertInstanceOf(Carbon::class, $check->birthday);
377383
$this->assertEquals($user->birthday, $check->birthday);
378384

379385
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
@@ -384,28 +390,36 @@ public function testDates()
384390
$this->assertEquals($user->birthday->format('l jS \of F Y h:i:s A'), $json['birthday']);
385391
$this->assertEquals($user->created_at->format('l jS \of F Y h:i:s A'), $json['created_at']);
386392

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+
387401
// test default date format for json output
388402
$item = Item::create(['name' => 'sword']);
389403
$json = $item->toArray();
390404
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
391405

392406
$user = User::create(['name' => 'Jane Doe', 'birthday' => time()]);
393-
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
407+
$this->assertInstanceOf(Carbon::class, $user->birthday);
394408

395409
$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);
397411

398412
$user = User::create(['name' => 'Jane Doe', 'birthday' => '2005-08-08']);
399-
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
413+
$this->assertInstanceOf(Carbon::class, $user->birthday);
400414

401415
$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'));
403417

404418
$user->setAttribute('entry.date', new DateTime);
405-
$this->assertInstanceOf('Carbon\Carbon', $user->getAttribute('entry.date'));
419+
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
406420

407421
$data = $user->toArray();
408-
$this->assertNotInstanceOf('MongoDB\BSON\UTCDateTime', $data['entry']['date']);
422+
$this->assertNotInstanceOf(UTCDateTime::class, $data['entry']['date']);
409423
$this->assertEquals((string) $user->getAttribute('entry.date')->format('Y-m-d H:i:s'), $data['entry']['date']);
410424
}
411425

@@ -453,14 +467,14 @@ public function testRaw()
453467
$users = User::raw(function ($collection) {
454468
return $collection->find(['age' => 35]);
455469
});
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]);
458472

459473
$user = User::raw(function ($collection) {
460474
return $collection->findOne(['age' => 35]);
461475
});
462476

463-
$this->assertInstanceOf('Jenssegers\Mongodb\Eloquent\Model', $user);
477+
$this->assertInstanceOf(Model::class, $user);
464478

465479
$count = User::raw(function ($collection) {
466480
return $collection->count();
@@ -476,9 +490,9 @@ public function testRaw()
476490
public function testDotNotation()
477491
{
478492
$user = User::create([
479-
'name' => 'John Doe',
493+
'name' => 'John Doe',
480494
'address' => [
481-
'city' => 'Paris',
495+
'city' => 'Paris',
482496
'country' => 'France',
483497
],
484498
]);

0 commit comments

Comments
 (0)