Skip to content

Commit 93e53ec

Browse files
committed
Adding date tests for issue #51
1 parent 802e6c3 commit 93e53ec

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

tests/ModelTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,18 @@ public function testUnset()
309309
$this->assertFalse(isset($user2->note2));
310310
}
311311

312+
public function testDates()
313+
{
314+
$user1 = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
315+
$user2 = User::create(array('name' => 'Jane Doe', 'birthday' => new DateTime('1981/1/1')));
316+
317+
$this->assertInstanceOf('DateTime', $user1->birthday);
318+
319+
// Re-fetch to be sure
320+
$user1 = User::find($user1->_id);
321+
$user2 = User::find($user2->_id);
322+
323+
$this->assertInstanceOf('DateTime', $user1->birthday);
324+
}
325+
312326
}

tests/QueryBuilderTest.php

+21-6
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,31 @@ public function testUnset()
415415

416416
public function testUpdateSubdocument()
417417
{
418-
DB::collection('users')->insertGetId(array(
419-
'name' => 'John Doe',
420-
'address' => array('country' => 'Belgium')
418+
$id = DB::collection('users')->insertGetId(array('name' => 'John Doe', 'address' => array('country' => 'Belgium')));
419+
420+
DB::collection('users')->where('_id', $id)->update(array('address.country' => 'England'));
421+
422+
$check = DB::collection('users')->find($id);
423+
$this->assertEquals('England', $check['address']['country']);
424+
}
425+
426+
public function testDates()
427+
{
428+
DB::collection('users')->insert(array(
429+
array('name' => 'John Doe', 'birthday' => new MongoDate(strtotime("1980-01-01 00:00:00"))),
430+
array('name' => 'Jane Doe', 'birthday' => new MongoDate(strtotime("1981-01-01 00:00:00"))),
431+
array('name' => 'Robert Roe', 'birthday' => new MongoDate(strtotime("1982-01-01 00:00:00"))),
432+
array('name' => 'Mark Moe', 'birthday' => new MongoDate(strtotime("1983-01-01 00:00:00"))),
421433
));
422434

423-
DB::collection('users')->where('name', 'John Doe')->update(array('address.country' => 'England'));
435+
$user = DB::collection('users')->where('birthday', new MongoDate(strtotime("1980-01-01 00:00:00")))->first();
436+
$this->assertEquals('John Doe', $user['name']);
424437

425-
$check = DB::collection('users')->where('name', 'John Doe')->first();
438+
$start = new MongoDate(strtotime("1981-01-01 00:00:00"));
439+
$stop = new MongoDate(strtotime("1982-01-01 00:00:00"));
426440

427-
$this->assertEquals('England', $check['address']['country']);
441+
$users = DB::collection('users')->whereBetween('birthday', array($start, $stop))->get();
442+
$this->assertEquals(2, count($users));
428443
}
429444

430445
}

tests/models/User.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
99

1010
protected $collection = 'users';
1111

12+
protected $dates = array('birthday');
13+
1214
protected static $unguarded = true;
1315

1416
public function books()
@@ -56,4 +58,4 @@ public function getReminderEmail()
5658
return $this->email;
5759
}
5860

59-
}
61+
}

0 commit comments

Comments
 (0)