From 209b3013c8ca817c2b95156e6d6c307ba9c75817 Mon Sep 17 00:00:00 2001
From: Jens Segers <segers.jens@gmail.com>
Date: Thu, 22 Aug 2013 13:56:13 +0200
Subject: [PATCH] Tweaked tests and fixed pluck bug

---
 src/Jenssegers/Mongodb/Builder.php |  20 ++
 tests/CacheTest.php                |  15 +-
 tests/ModelQueryTest.php           | 328 ---------------------------
 tests/QueryBuilderTest.php         | 320 ++++++++++++++++++++++++++
 tests/QueryTest.php                | 349 ++++++++++++++++-------------
 5 files changed, 535 insertions(+), 497 deletions(-)
 delete mode 100644 tests/ModelQueryTest.php
 create mode 100644 tests/QueryBuilderTest.php

diff --git a/src/Jenssegers/Mongodb/Builder.php b/src/Jenssegers/Mongodb/Builder.php
index 53a2696..2ca16d8 100644
--- a/src/Jenssegers/Mongodb/Builder.php
+++ b/src/Jenssegers/Mongodb/Builder.php
@@ -359,6 +359,26 @@ class Builder extends \Illuminate\Database\Query\Builder {
         return $this->increment($column, -1 * $amount, $extra);
     }
 
+    /**
+     * Pluck a single column from the database.
+     *
+     * @param  string  $column
+     * @return mixed
+     */
+    public function pluck($column)
+    {
+        $result = (array) $this->first(array($column));
+
+        // MongoDB returns the _id field even if you did not ask for it, so we need to
+        // remove this from the result.
+        if (array_key_exists('_id', $result))
+        {
+            unset($result['_id']);
+        }
+
+        return count($result) > 0 ? reset($result) : null;
+    }
+
     /**
      * Delete a record from the database.
      *
diff --git a/tests/CacheTest.php b/tests/CacheTest.php
index d1e742c..410da30 100644
--- a/tests/CacheTest.php
+++ b/tests/CacheTest.php
@@ -5,9 +5,14 @@ use Illuminate\Support\Facades\DB;
 
 class CacheTest extends PHPUnit_Framework_TestCase {
 
+	protected $cache;
+
 	public function setUp()
 	{
-		// test data
+		# clear cache
+		global $app;
+		$this->cache = $app['cache'];
+
 		User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
 		User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
 		User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
@@ -16,15 +21,11 @@ class CacheTest extends PHPUnit_Framework_TestCase {
 	public function tearDown()
 	{
 		User::truncate();
+		$this->cache->forget('db.users');
 	}
 
 	public function testCache()
 	{
-		# get from cache driver
-		global $app;
-		$cache = $app['cache'];
-		$cache->forget('db.users');
-
 		# auto generate cache key
 		$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
 		$this->assertEquals(3, count($users));
@@ -36,7 +37,7 @@ class CacheTest extends PHPUnit_Framework_TestCase {
 		$users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
 		$this->assertEquals(3, count($users));
 
-		$users = $cache->get('db.users');
+		$users = $this->cache->get('db.users');
 		$this->assertEquals(3, count($users));
 	}
 
diff --git a/tests/ModelQueryTest.php b/tests/ModelQueryTest.php
deleted file mode 100644
index edfebcd..0000000
--- a/tests/ModelQueryTest.php
+++ /dev/null
@@ -1,328 +0,0 @@
-<?php
-require_once('tests/app.php');
-
-class ModelQueryTest extends PHPUnit_Framework_TestCase {
-
-	public function setUp()
-	{
-		// test data
-		User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
-		User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
-		User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
-		User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user'));
-		User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user'));
-		User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user'));
-		User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user'));
-		User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin'));
-		User::create(array('name' => 'Error', 'age' => null, 'title' => null));
-	}
-
-	public function tearDown()
-	{
-		User::truncate();
-	}
-
-	public function testGet()
-	{
-		$users = User::get();
-
-		$this->assertEquals(9, count($users));
-		$this->assertInstanceOf('Jenssegers\Mongodb\Model', $users[0]);
-	}
-
-	public function testFirst()
-	{
-		$user = User::get()->first();
-		$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
-		$this->assertEquals('John Doe', $user->name);
-	}
-
-	public function testWhere()
-	{
-		$users = User::where('age', 35)->get();
-		$this->assertEquals(3, count($users));
-
-		$users = User::where('age', '=', 35)->get();
-		$this->assertEquals(3, count($users));
-
-		$users = User::where('age', '>=', 35)->get();
-		$this->assertEquals(4, count($users));
-
-		$users = User::where('age', '<=', 18)->get();
-		$this->assertEquals(1, count($users));
-
-		$users = User::where('age', '!=', 35)->get();
-		$this->assertEquals(6, count($users));
-
-		$users = User::where('age', '<>', 35)->get();
-		$this->assertEquals(6, count($users));
-	}
-
-	public function testAndWhere()
-	{
-		$users = User::where('age', 35)->where('title', 'admin')->get();
-		$this->assertEquals(2, count($users));
-
-		$users = User::where('age', '>=', 35)->where('title', 'user')->get();
-		$this->assertEquals(2, count($users));
-	}
-
-	public function testLike()
-	{
-		$users = User::where('name', 'like', '%doe')->get();
-		$this->assertEquals(2, count($users));
-
-		$users = User::where('name', 'like', '%y%')->get();
-		$this->assertEquals(3, count($users));
-
-		$users = User::where('name', 'like', 't%')->get();
-		$this->assertEquals(1, count($users));
-	}
-
-	public function testPluck()
-	{
-		$name = User::where('name', 'John Doe')->pluck('name');
-		$this->assertEquals('John Doe', $name);
-	}
-
-	public function testList()
-	{
-		$list = User::lists('title');
-		$this->assertEquals(9, count($list));
-		$this->assertEquals('admin', $list[0]);
-
-		$list = User::lists('title', 'name');
-		$this->assertEquals(9, count($list));
-		$this->assertEquals('John Doe', key($list));
-		$this->assertEquals('admin', $list['John Doe']);
-	}
-
-	public function testSelect()
-	{
-		$user = User::select('name')->first();
-
-		$this->assertEquals('John Doe', $user->name);
-		$this->assertEquals(null, $user->age);
-
-		$user = User::select('name', 'title')->first();
-
-		$this->assertEquals('John Doe', $user->name);
-		$this->assertEquals('admin', $user->title);
-		$this->assertEquals(null, $user->age);
-
-		$user = User::get(array('name'))->first();
-
-		$this->assertEquals('John Doe', $user->name);
-		$this->assertEquals(null, $user->age);
-	}
-
-	public function testOrWhere()
-	{
-		$users = User::where('age', 13)->orWhere('title', 'admin')->get();
-		$this->assertEquals(4, count($users));
-
-		$users = User::where('age', 13)->orWhere('age', 23)->get();
-		$this->assertEquals(2, count($users));
-	}
-
-	public function testBetween()
-	{
-		$users = User::whereBetween('age', array(0, 25))->get();
-		$this->assertEquals(2, count($users));
-
-		$users = User::whereBetween('age', array(13, 23))->get();
-		$this->assertEquals(2, count($users));
-	}
-
-	public function testIn()
-	{
-		$users = User::whereIn('age', array(13, 23))->get();
-		$this->assertEquals(2, count($users));
-
-		$users = User::whereIn('age', array(33, 35, 13))->get();
-		$this->assertEquals(6, count($users));
-
-		$users = User::whereNotIn('age', array(33, 35))->get();
-		$this->assertEquals(4, count($users));
-
-		$users = User::whereNotNull('age')
-		             ->whereNotIn('age', array(33, 35))->get();
-		$this->assertEquals(3, count($users));
-	}
-
-	public function testWhereNull()
-	{
-		$users = User::whereNull('age')->get();
-		$this->assertEquals(1, count($users));
-	}
-
-	public function testWhereNotNull()
-	{
-		$users = User::whereNotNull('age')->get();
-		$this->assertEquals(8, count($users));
-	}
-
-	public function testOrder()
-	{
-		$user = User::whereNotNull('age')->orderBy('age', 'asc')->first();
-		$this->assertEquals(13, $user->age);
-
-		$user = User::whereNotNull('age')->orderBy('age', 'desc')->first();
-		$this->assertEquals(37, $user->age);
-	}
-
-	public function testTake()
-	{
-		$users = User::take(3)->get();
-		$this->assertEquals(3, count($users));
-	}
-
-	public function testOffset()
-	{
-		$users = User::skip(1)->take(2)->get();
-		$this->assertEquals(2, count($users));
-		$this->assertEquals('Jane Doe', $users[0]->name);
-	}
-
-	public function testIncrements()
-	{
-		User::where('name', 'John Doe')->increment('age');
-		User::where('name', 'John Doe')->increment('age', 2, array('title' => 'user'));
-
-		$user = User::where('name', 'John Doe')->first();
-		$this->assertEquals(38, $user->age);
-		$this->assertEquals('user', $user->title);
-
-		User::where('name', 'John Doe')->decrement('age');
-		$num = User::where('name', 'John Doe')->decrement('age', 2, array('title' => 'admin'));
-
-		$user = User::where('name', 'John Doe')->first();
-		$this->assertEquals(35, $user->age);
-		$this->assertEquals('admin', $user->title);
-		$this->assertEquals(1, $num);
-
-		User::increment('age');
-		User::increment('age', 2);
-
-		$user = User::where('name', 'Mark Moe')->first();
-		$this->assertEquals(26, $user->age);
-
-		User::decrement('age', 2);
-		$num = User::decrement('age');
-
-		$user = User::where('name', 'Mark Moe')->first();
-		$this->assertEquals(23, $user->age);
-		$this->assertEquals(8, $num);
-	}
-
-	public function testAggregates()
-	{
-		$this->assertEquals(9, User::count());
-		$this->assertEquals(37, User::max('age'));
-		$this->assertEquals(13, User::min('age'));
-		$this->assertEquals(30.5, User::avg('age'));
-		$this->assertEquals(244, User::sum('age'));
-
-		$this->assertEquals(35, User::where('title', 'admin')->max('age'));
-		$this->assertEquals(37, User::where('title', 'user')->max('age'));
-	
-		$this->assertEquals(33, User::where('title', 'admin')->min('age'));
-		$this->assertEquals(13, User::where('title', 'user')->min('age'));
-	}
-
-	public function testGroupBy()
-	{
-		$users = User::groupBy('title')->get();
-		$this->assertEquals(3, count($users));
-
-		$users = User::groupBy('age')->get();
-		$this->assertEquals(6, count($users));
-
-		$users = User::groupBy('age')->skip(1)->get();
-		$this->assertEquals(5, count($users));
-
-		$users = User::groupBy('age')->take(2)->get();
-		$this->assertEquals(2, count($users));
-
-		$users = User::groupBy('age')->orderBy('age', 'desc')->get();
-		$this->assertEquals(37, $users[0]->age);
-		$this->assertEquals(35, $users[1]->age);
-		$this->assertEquals(33, $users[2]->age);
-
-		$users = User::groupBy('age')->skip(1)->take(2)->orderBy('age', 'desc')->get();
-		$this->assertEquals(35, $users[0]->age);
-		$this->assertEquals(33, $users[1]->age);
-	}
-
-	public function testSubquery()
-	{
-		$users = User::where('title', 'admin')->orWhere(function($query)
-            {
-                $query->where('name', 'Tommy Toe')
-                      ->orWhere('name', 'Error');
-            })
-            ->get();
-
-        $this->assertEquals(5, count($users));
-
-        $users = User::where('title', 'user')->where(function($query)
-            {
-                $query->where('age', 35)
-                      ->orWhere('name', 'like', '%harry%');
-            })
-            ->get();
-
-        $this->assertEquals(2, count($users));
-
-        $users = User::where('age', 35)->orWhere(function($query)
-            {
-                $query->where('title', 'admin')
-                      ->orWhere('name', 'Error');
-            })
-            ->get();
-
-        $this->assertEquals(5, count($users));
-	}
-
-	public function testInsert()
-	{
-		User::insert(
-		    array('name' => 'Francois', 'age' => 59, 'title' => 'Senior')
-		);
-
-		$this->assertEquals(10, User::count());
-
-		User::insert(array(
-		    array('name' => 'Gaston', 'age' => 60, 'title' => 'Senior'),
-		    array('name' => 'Jaques', 'age' => 61, 'title' => 'Senior')
-		));
-
-		$this->assertEquals(12, User::count());
-	}
-
-	public function testInsertGetId()
-	{
-		$id = User::insertGetId(
-		    array('name' => 'Gaston', 'age' => 60, 'title' => 'Senior')
-		);
-
-		$this->assertEquals(10, User::count());
-		$this->assertNotNull($id);
-		$this->assertTrue(is_string($id));
-	}
-
-	public function testRaw()
-	{
-		$where = array('age' => array('$gt' => 30, '$lt' => 40));
-		$users = User::whereRaw($where)->get();
-
-		$this->assertEquals(6, count($users));
-
-		$where1 = array('age' => array('$gt' => 30, '$lte' => 35));
-		$where2 = array('age' => array('$gt' => 35, '$lt' => 40));
-		$users = User::whereRaw($where1)->orWhereRaw($where2)->get();
-
-		$this->assertEquals(6, count($users));
-	}
-
-}
\ No newline at end of file
diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php
new file mode 100644
index 0000000..da7aacd
--- /dev/null
+++ b/tests/QueryBuilderTest.php
@@ -0,0 +1,320 @@
+<?php
+require_once('tests/app.php');
+
+use Illuminate\Support\Facades\DB;
+
+class QueryBuilderTest extends PHPUnit_Framework_TestCase {
+
+	public function setUp() {}
+
+	public function tearDown()
+	{
+		DB::collection('users')->truncate();
+		DB::collection('items')->truncate();
+	}
+
+	public function testCollection()
+	{
+		$this->assertInstanceOf('Jenssegers\Mongodb\Builder', DB::collection('users'));
+	}
+
+	public function testGet()
+	{
+		$users = DB::collection('users')->get();
+		$this->assertEquals(0, count($users));
+
+		DB::collection('users')->insert(array('name' => 'John Doe'));
+
+		$users = DB::collection('users')->get();
+		$this->assertEquals(1, count($users));
+	}
+
+	public function testInsert()
+	{
+		DB::collection('users')->insert(array(
+			'tags' => array('tag1', 'tag2'),
+			'name' => 'John Doe',
+		));
+
+		$users = DB::collection('users')->get();
+		$this->assertEquals(1, count($users));
+
+		$user = $users[0];
+		$this->assertEquals('John Doe', $user['name']);
+		$this->assertTrue(is_array($user['tags']));
+	}
+
+	public function testInsertGetId() 
+	{
+		$id = DB::collection('users')->insertGetId(array('name' => 'John Doe'));
+
+		$this->assertTrue(is_string($id));
+		$this->assertEquals(24, strlen($id));
+	}
+
+	public function testBatchInsert()
+	{
+		DB::collection('users')->insert(array(
+			array(
+				'tags' => array('tag1', 'tag2'),
+				'name' => 'Jane Doe',	
+			),
+			array(
+				'tags' => array('tag3'),
+				'name' => 'John Doe',
+			),
+		));
+
+		$users = DB::collection('users')->get();
+		$this->assertEquals(2, count($users));
+
+		$user = $users[0];
+		$this->assertEquals('Jane Doe', $user['name']);
+		$this->assertTrue(is_array($user['tags']));
+	}
+
+	public function testFind()
+	{
+		$id = DB::collection('users')->insertGetId(array('name' => 'John Doe'));
+
+		$user = DB::collection('users')->find($id);
+		$this->assertEquals('John Doe', $user['name']);
+	}
+
+	public function testCount()
+	{
+		DB::collection('users')->insert(array(
+			array('name' => 'Jane Doe'),
+			array('name' => 'John Doe')
+		));
+
+		$this->assertEquals(2, DB::collection('users')->count());
+	}
+
+	public function testUpdate()
+	{
+		DB::collection('users')->insert(array(
+			array('name' => 'Jane Doe', 'age' => 20),
+			array('name' => 'John Doe', 'age' => 21)
+		));
+
+		DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
+		$users = DB::collection('users')->get();
+
+		$this->assertEquals(20, $users[0]['age']);
+		$this->assertEquals(100, $users[1]['age']);
+	}
+
+	public function testDelete()
+	{
+		DB::collection('users')->insert(array(
+			array('name' => 'Jane Doe', 'age' => 20),
+			array('name' => 'John Doe', 'age' => 25)
+		));
+
+		DB::collection('users')->where('age', '<', 10)->delete();
+		$this->assertEquals(2, DB::collection('users')->count());
+
+		DB::collection('users')->where('age', '<', 25)->delete();
+		$this->assertEquals(1, DB::collection('users')->count());
+	}
+
+	public function testTruncate()
+	{
+		DB::collection('users')->insert(array('name' => 'John Doe'));
+		DB::collection('users')->truncate();
+		$this->assertEquals(0, DB::collection('users')->count());
+	}
+
+	public function testSubKey()
+	{
+		DB::collection('users')->insert(array(
+			array(
+				'name' => 'John Doe',
+				'address' => array('country' => 'Belgium', 'city' => 'Ghent')
+			),
+			array(
+				'name' => 'Jane Doe',
+				'address' => array('country' => 'France', 'city' => 'Paris')
+			)
+		));
+
+		$users = DB::collection('users')->where('address.country', 'Belgium')->get();
+		$this->assertEquals(1, count($users));
+		$this->assertEquals('John Doe', $users[0]['name']);
+	}
+
+	public function testInArray()
+	{
+		DB::collection('items')->insert(array(
+			array(
+				'tags' => array('tag1', 'tag2', 'tag3', 'tag4')
+			),
+			array(
+				'tags' => array('tag2')
+			)
+		));
+
+		$items = DB::collection('items')->where('tags', 'tag2')->get();
+		$this->assertEquals(2, count($items));
+
+		$items = DB::collection('items')->where('tags', 'tag1')->get();
+		$this->assertEquals(1, count($items));
+	}
+
+	public function testRaw()
+	{
+		DB::collection('users')->insert(array(
+			array('name' => 'Jane Doe', 'age' => 20),
+			array('name' => 'John Doe', 'age' => 25)
+		));
+
+		$cursor = DB::collection('users')->raw(function($collection) {
+			return $collection->find(array('age' => 20));
+		});
+
+		$this->assertInstanceOf('MongoCursor', $cursor);
+		$this->assertEquals(1, $cursor->count());
+
+		$collection = DB::collection('users')->raw();
+		$this->assertInstanceOf('MongoCollection', $collection);
+	}
+
+	public function testPush()
+	{
+		$id = DB::collection('users')->insertGetId(array(
+			'name' => 'John Doe',
+			'tags' => array()
+		));
+
+		DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
+		
+		$user = DB::collection('users')->find($id);
+		$this->assertTrue(is_array($user['tags']));
+		$this->assertEquals(1, count($user['tags']));
+		$this->assertEquals('tag1', $user['tags'][0]);
+
+		DB::collection('users')->where('_id', $id)->push('tags', 'tag2');
+		
+		$user = DB::collection('users')->find($id);
+		$this->assertTrue(is_array($user['tags']));
+		$this->assertEquals(2, count($user['tags']));
+		$this->assertEquals('tag2', $user['tags'][1]);
+
+		DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4'));
+		
+		$user = DB::collection('users')->find($id);
+		$this->assertTrue(is_array($user['tags']));
+		$this->assertEquals(4, count($user['tags']));
+		$this->assertEquals('tag4', $user['tags'][3]);
+	}
+
+	public function testPull()
+	{
+		$id = DB::collection('users')->insertGetId(array(
+			'name' => 'John Doe',
+			'tags' => array('tag1', 'tag2', 'tag3', 'tag4')
+		));
+
+		DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
+		
+		$user = DB::collection('users')->find($id);
+		$this->assertTrue(is_array($user['tags']));
+		$this->assertEquals(3, count($user['tags']));
+		$this->assertEquals('tag4', $user['tags'][2]);
+
+		DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4'));
+		
+		$user = DB::collection('users')->find($id);
+		$this->assertTrue(is_array($user['tags']));
+		$this->assertEquals(1, count($user['tags']));
+		$this->assertEquals('tag1', $user['tags'][0]);
+	}
+
+	public function testDistinct()
+	{
+		DB::collection('items')->insert(array(
+			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
+			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
+		));
+
+		$items = DB::collection('items')->distinct('name')->get();
+		$this->assertEquals(array('knife', 'fork', 'spoon'), $items);
+
+		$types = DB::collection('items')->distinct('type')->get();
+		$this->assertEquals(array('sharp', 'round'), $types);
+	}
+
+	public function testCustomId()
+	{
+		DB::collection('items')->insert(array(
+			array('_id' => 'knife', 'type' => 'sharp', 'amount' => 34),
+			array('_id' => 'fork',  'type' => 'sharp', 'amount' => 20),
+			array('_id' => 'spoon', 'type' => 'round', 'amount' => 3)
+		));
+
+		$item = DB::collection('items')->find('knife');
+		$this->assertEquals('knife', $item['_id']);
+
+		$item = DB::collection('items')->where('_id', 'fork')->first();
+		$this->assertEquals('fork', $item['_id']);
+	}
+
+	public function testTake()
+	{
+		DB::collection('items')->insert(array(
+			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
+			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
+		));
+
+		$items = DB::collection('items')->take(2)->get();
+		$this->assertEquals(2, count($items));
+		$this->assertEquals('knife', $items[0]['name']);
+	}
+
+	public function testSkip()
+	{
+		DB::collection('items')->insert(array(
+			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
+			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
+		));
+
+		$items = DB::collection('items')->skip(2)->get();
+		$this->assertEquals(2, count($items));
+		$this->assertEquals('spoon', $items[0]['name']);
+	}
+
+	public function testPluck()
+	{
+		DB::collection('users')->insert(array(
+			array('name' => 'Jane Doe', 'age' => 20),
+			array('name' => 'John Doe', 'age' => 25)
+		));
+
+		$age = DB::collection('users')->where('name', 'John Doe')->pluck('age');
+		$this->assertEquals(25, $age);
+	}
+
+	public function testList()
+	{
+		DB::collection('items')->insert(array(
+			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
+			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
+			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
+		));
+
+		$list = DB::collection('items')->lists('name');
+		$this->assertEquals(array('knife', 'fork', 'spoon', 'spoon'), $list);
+
+		$list = DB::collection('items')->lists('type', 'name');
+		$this->assertEquals(array('knife' => 'sharp', 'fork' => 'sharp', 'spoon' => 'round'), $list);
+	}
+
+}
\ No newline at end of file
diff --git a/tests/QueryTest.php b/tests/QueryTest.php
index db08479..171729a 100644
--- a/tests/QueryTest.php
+++ b/tests/QueryTest.php
@@ -1,246 +1,271 @@
 <?php
 require_once('tests/app.php');
 
-use Illuminate\Support\Facades\DB;
-
 class QueryTest extends PHPUnit_Framework_TestCase {
 
-	public function setUp() {}
-
-	public function tearDown()
+	public function setUp()
 	{
-		DB::collection('users')->truncate();
-		DB::collection('items')->truncate();
+		// test data
+		User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
+		User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
+		User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
+		User::create(array('name' => 'Robert Roe', 'age' => 37, 'title' => 'user'));
+		User::create(array('name' => 'Mark Moe', 'age' => 23, 'title' => 'user'));
+		User::create(array('name' => 'Brett Boe', 'age' => 35, 'title' => 'user'));
+		User::create(array('name' => 'Tommy Toe', 'age' => 33, 'title' => 'user'));
+		User::create(array('name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin'));
+		User::create(array('name' => 'Error', 'age' => null, 'title' => null));
 	}
 
-	public function testCollection()
+	public function tearDown()
 	{
-		$this->assertInstanceOf('Jenssegers\Mongodb\Builder', DB::collection('users'));
+		User::truncate();
 	}
 
 	public function testGet()
 	{
-		$users = DB::collection('users')->get();
-		$this->assertEquals(0, count($users));
+		$users = User::get();
 
-		DB::collection('users')->insert(array('name' => 'John Doe'));
+		$this->assertEquals(9, count($users));
+		$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
+		$this->assertInstanceOf('Jenssegers\Mongodb\Model', $users[0]);
+	}
 
-		$users = DB::collection('users')->get();
-		$this->assertEquals(1, count($users));
+	public function testFirst()
+	{
+		$user = User::get()->first();
+		$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
+		$this->assertEquals('John Doe', $user->name);
 	}
 
-	public function testInsert()
+	public function testWhere()
 	{
-		$user = array(
-			'tags' => array('tag1', 'tag2'),
-			'name' => 'John Doe',
-		);
-		DB::collection('users')->insert($user);
+		$users = User::where('age', 35)->get();
+		$this->assertEquals(3, count($users));
+
+		$users = User::where('age', '=', 35)->get();
+		$this->assertEquals(3, count($users));
+
+		$users = User::where('age', '>=', 35)->get();
+		$this->assertEquals(4, count($users));
 
-		$users = DB::collection('users')->get();
+		$users = User::where('age', '<=', 18)->get();
 		$this->assertEquals(1, count($users));
 
-		$user = $users[0];
-		$this->assertEquals('John Doe', $user['name']);
-		$this->assertTrue(is_array($user['tags']));
+		$users = User::where('age', '!=', 35)->get();
+		$this->assertEquals(6, count($users));
+
+		$users = User::where('age', '<>', 35)->get();
+		$this->assertEquals(6, count($users));
 	}
 
-	public function testBatchInsert()
+	public function testAndWhere()
 	{
-		$users = array(
-			array(
-				'tags' => array('tag1', 'tag2'),
-				'name' => 'Jane Doe',	
-			),
-			array(
-				'tags' => array('tag3'),
-				'name' => 'John Doe',
-			),
-		);
-		DB::collection('users')->insert($users);
-
-		$users = DB::collection('users')->get();
+		$users = User::where('age', 35)->where('title', 'admin')->get();
 		$this->assertEquals(2, count($users));
 
-		$user = $users[0];
-		$this->assertEquals('Jane Doe', $user['name']);
-		$this->assertTrue(is_array($user['tags']));
+		$users = User::where('age', '>=', 35)->where('title', 'user')->get();
+		$this->assertEquals(2, count($users));
 	}
 
-	public function testFind()
+	public function testLike()
 	{
-		$user = array('name' => 'John Doe');
-		$id = DB::collection('users')->insertGetId($user);
+		$users = User::where('name', 'like', '%doe')->get();
+		$this->assertEquals(2, count($users));
 
-		$this->assertNotNull($id);
-		$this->assertTrue(is_string($id));
+		$users = User::where('name', 'like', '%y%')->get();
+		$this->assertEquals(3, count($users));
 
-		$user = DB::collection('users')->find($id);
-		$this->assertEquals('John Doe', $user['name']);
+		$users = User::where('name', 'like', 't%')->get();
+		$this->assertEquals(1, count($users));
 	}
 
-	public function testUpdate()
+	public function testSelect()
 	{
-		DB::collection('users')->insert(array('name' => 'John Doe', 'age' => 10));
-		DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
+		$user = User::select('name')->first();
 
-        $user = User::where('name', 'John Doe')->first();
-		$this->assertEquals(100, $user->age);
-	}
+		$this->assertEquals('John Doe', $user->name);
+		$this->assertEquals(null, $user->age);
 
-	public function testDelete()
-	{
-		DB::collection('users')->insert(array('name' => 'John Doe', 'age' => 100));
+		$user = User::select('name', 'title')->first();
 
-		DB::collection('users')->where('age', '<', 30)->delete();
-		$this->assertEquals(1, DB::collection('users')->count());
+		$this->assertEquals('John Doe', $user->name);
+		$this->assertEquals('admin', $user->title);
+		$this->assertEquals(null, $user->age);
 
-		DB::collection('users')->where('age', '>', 30)->delete();
-		$this->assertEquals(0, DB::collection('users')->count());
-	}
+		$user = User::get(array('name'))->first();
 
-	public function testTruncate()
-	{
-		DB::collection('users')->insert(array('name' => 'John Doe', 'age' => 100));
-		DB::collection('users')->truncate();
-		$this->assertEquals(0, DB::collection('users')->count());
+		$this->assertEquals('John Doe', $user->name);
+		$this->assertEquals(null, $user->age);
 	}
 
-	public function testSubKey()
+	public function testOrWhere()
 	{
-		$user1 = array(
-			'name' => 'John Doe',
-			'address' => array(
-				'country' => 'Belgium',
-				'city' => 'Ghent'
-				)
-			);
+		$users = User::where('age', 13)->orWhere('title', 'admin')->get();
+		$this->assertEquals(4, count($users));
 
-		$user2 = array(
-			'name' => 'Jane Doe',
-			'address' => array(
-				'country' => 'France',
-				'city' => 'Paris'
-				)
-			);
+		$users = User::where('age', 13)->orWhere('age', 23)->get();
+		$this->assertEquals(2, count($users));
+	}
 
-		DB::collection('users')->insert(array($user1, $user2));
+	public function testBetween()
+	{
+		$users = User::whereBetween('age', array(0, 25))->get();
+		$this->assertEquals(2, count($users));
 
-		$users = DB::collection('users')->where('address.country', 'Belgium')->get();
-		$this->assertEquals(1, count($users));
-		$this->assertEquals('John Doe', $users[0]['name']);
+		$users = User::whereBetween('age', array(13, 23))->get();
+		$this->assertEquals(2, count($users));
 	}
 
-	public function testInArray()
+	public function testIn()
 	{
-		$item1 = array(
-			'tags' => array('tag1', 'tag2', 'tag3', 'tag4')
-		);
-
-		$item2 = array(
-			'tags' => array('tag2')
-		);
+		$users = User::whereIn('age', array(13, 23))->get();
+		$this->assertEquals(2, count($users));
 
-		DB::collection('items')->insert(array($item1, $item2));
+		$users = User::whereIn('age', array(33, 35, 13))->get();
+		$this->assertEquals(6, count($users));
 
-		$items = DB::collection('items')->where('tags', 'tag2')->get();
-		$this->assertEquals(2, count($items));
+		$users = User::whereNotIn('age', array(33, 35))->get();
+		$this->assertEquals(4, count($users));
 
-		$items = DB::collection('items')->where('tags', 'tag1')->get();
-		$this->assertEquals(1, count($items));
+		$users = User::whereNotNull('age')
+		             ->whereNotIn('age', array(33, 35))->get();
+		$this->assertEquals(3, count($users));
 	}
 
-	public function testRaw()
+	public function testWhereNull()
 	{
-		DB::collection('users')->insert(array('name' => 'John Doe'));
+		$users = User::whereNull('age')->get();
+		$this->assertEquals(1, count($users));
+	}
 
-		$cursor = DB::collection('users')->raw(function($collection) {
-			return $collection->find();
-		});
+	public function testWhereNotNull()
+	{
+		$users = User::whereNotNull('age')->get();
+		$this->assertEquals(8, count($users));
+	}
 
-		$this->assertInstanceOf('MongoCursor', $cursor);
-		$this->assertEquals(1, $cursor->count());
+	public function testOrder()
+	{
+		$user = User::whereNotNull('age')->orderBy('age', 'asc')->first();
+		$this->assertEquals(13, $user->age);
 
-		$collection = DB::collection('users')->raw();
-		$this->assertInstanceOf('MongoCollection', $collection);
+		$user = User::whereNotNull('age')->orderBy('age', 'desc')->first();
+		$this->assertEquals(37, $user->age);
 	}
 
-	public function testPush()
+	public function testIncrements()
 	{
-		$user = array('name' => 'John Doe', 'tags' => array());
-		$id = DB::collection('users')->insertGetId($user);
+		User::where('name', 'John Doe')->increment('age');
+		User::where('name', 'John Doe')->increment('age', 2, array('title' => 'user'));
 
-		DB::collection('users')->where('_id', $id)->push('tags', 'tag1');
-		$user = DB::collection('users')->find($id);
+		$user = User::where('name', 'John Doe')->first();
+		$this->assertEquals(38, $user->age);
+		$this->assertEquals('user', $user->title);
 
-		$this->assertTrue(is_array($user['tags']));
-		$this->assertEquals(1, count($user['tags']));
-		$this->assertEquals('tag1', $user['tags'][0]);
+		User::where('name', 'John Doe')->decrement('age');
+		$num = User::where('name', 'John Doe')->decrement('age', 2, array('title' => 'admin'));
 
-		DB::collection('users')->where('_id', $id)->push('tags', 'tag2');
-		$user = DB::collection('users')->find($id);
+		$user = User::where('name', 'John Doe')->first();
+		$this->assertEquals(35, $user->age);
+		$this->assertEquals('admin', $user->title);
+		$this->assertEquals(1, $num);
 
-		$this->assertTrue(is_array($user['tags']));
-		$this->assertEquals(2, count($user['tags']));
-		$this->assertEquals('tag2', $user['tags'][1]);
+		User::increment('age');
+		User::increment('age', 2);
 
-		DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4'));
-		$user = DB::collection('users')->find($id);
+		$user = User::where('name', 'Mark Moe')->first();
+		$this->assertEquals(26, $user->age);
 
-		$this->assertTrue(is_array($user['tags']));
-		$this->assertEquals(4, count($user['tags']));
-		$this->assertEquals('tag4', $user['tags'][3]);
+		User::decrement('age', 2);
+		$num = User::decrement('age');
+
+		$user = User::where('name', 'Mark Moe')->first();
+		$this->assertEquals(23, $user->age);
+		$this->assertEquals(8, $num);
 	}
 
-	public function testPull()
+	public function testAggregates()
 	{
-		$user = array('name' => 'John Doe', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'));
-		$id = DB::collection('users')->insertGetId($user);
+		$this->assertEquals(9, User::count());
+		$this->assertEquals(37, User::max('age'));
+		$this->assertEquals(13, User::min('age'));
+		$this->assertEquals(30.5, User::avg('age'));
+		$this->assertEquals(244, User::sum('age'));
+
+		$this->assertEquals(35, User::where('title', 'admin')->max('age'));
+		$this->assertEquals(37, User::where('title', 'user')->max('age'));
+	
+		$this->assertEquals(33, User::where('title', 'admin')->min('age'));
+		$this->assertEquals(13, User::where('title', 'user')->min('age'));
+	}
 
-		DB::collection('users')->where('_id', $id)->pull('tags', 'tag3');
-		$user = DB::collection('users')->find($id);
+	public function testGroupBy()
+	{
+		$users = User::groupBy('title')->get();
+		$this->assertEquals(3, count($users));
 
-		$this->assertTrue(is_array($user['tags']));
-		$this->assertEquals(3, count($user['tags']));
-		$this->assertEquals('tag4', $user['tags'][2]);
+		$users = User::groupBy('age')->get();
+		$this->assertEquals(6, count($users));
 
-		DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4'));
-		$user = DB::collection('users')->find($id);
+		$users = User::groupBy('age')->skip(1)->get();
+		$this->assertEquals(5, count($users));
 
-		$this->assertTrue(is_array($user['tags']));
-		$this->assertEquals(1, count($user['tags']));
-		$this->assertEquals('tag1', $user['tags'][0]);
-	}
+		$users = User::groupBy('age')->take(2)->get();
+		$this->assertEquals(2, count($users));
 
-	public function testDistinct()
-	{
-		DB::collection('items')->insert(array(
-			array('name' => 'knife', 'type' => 'sharp', 'amount' => 34),
-			array('name' => 'fork',  'type' => 'sharp', 'amount' => 20),
-			array('name' => 'spoon', 'type' => 'round', 'amount' => 3),
-			array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
-		));
+		$users = User::groupBy('age')->orderBy('age', 'desc')->get();
+		$this->assertEquals(37, $users[0]->age);
+		$this->assertEquals(35, $users[1]->age);
+		$this->assertEquals(33, $users[2]->age);
 
-		$items = DB::collection('items')->distinct('name')->get();
-		$this->assertEquals(array('knife', 'fork', 'spoon'), $items);
+		$users = User::groupBy('age')->skip(1)->take(2)->orderBy('age', 'desc')->get();
+		$this->assertEquals(35, $users[0]->age);
+		$this->assertEquals(33, $users[1]->age);
+	}
 
-		$types = DB::collection('items')->distinct('type')->get();
-		$this->assertEquals(array('sharp', 'round'), $types);
+	public function testSubquery()
+	{
+		$users = User::where('title', 'admin')->orWhere(function($query)
+            {
+                $query->where('name', 'Tommy Toe')
+                      ->orWhere('name', 'Error');
+            })
+            ->get();
+
+        $this->assertEquals(5, count($users));
+
+        $users = User::where('title', 'user')->where(function($query)
+            {
+                $query->where('age', 35)
+                      ->orWhere('name', 'like', '%harry%');
+            })
+            ->get();
+
+        $this->assertEquals(2, count($users));
+
+        $users = User::where('age', 35)->orWhere(function($query)
+            {
+                $query->where('title', 'admin')
+                      ->orWhere('name', 'Error');
+            })
+            ->get();
+
+        $this->assertEquals(5, count($users));
 	}
 
-	public function testCustomId()
+	public function testRaw()
 	{
-		DB::collection('items')->insert(array(
-			array('_id' => 'knife', 'type' => 'sharp', 'amount' => 34),
-			array('_id' => 'fork',  'type' => 'sharp', 'amount' => 20),
-			array('_id' => 'spoon', 'type' => 'round', 'amount' => 3)
-		));
+		$where = array('age' => array('$gt' => 30, '$lt' => 40));
+		$users = User::whereRaw($where)->get();
+
+		$this->assertEquals(6, count($users));
 
-		$item = DB::collection('items')->find('knife');
-		$this->assertEquals('knife', $item['_id']);
+		$where1 = array('age' => array('$gt' => 30, '$lte' => 35));
+		$where2 = array('age' => array('$gt' => 35, '$lt' => 40));
+		$users = User::whereRaw($where1)->orWhereRaw($where2)->get();
 
-		$item = DB::collection('items')->where('_id', 'fork')->first();
-		$this->assertEquals('fork', $item['_id']);
+		$this->assertEquals(6, count($users));
 	}
 
 }
\ No newline at end of file
-- 
2.18.1