Commit 209b3013 authored by Jens Segers's avatar Jens Segers

Tweaked tests and fixed pluck bug

parent 03cee24c
...@@ -359,6 +359,26 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -359,6 +359,26 @@ class Builder extends \Illuminate\Database\Query\Builder {
return $this->increment($column, -1 * $amount, $extra); 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. * Delete a record from the database.
* *
......
...@@ -5,9 +5,14 @@ use Illuminate\Support\Facades\DB; ...@@ -5,9 +5,14 @@ use Illuminate\Support\Facades\DB;
class CacheTest extends PHPUnit_Framework_TestCase { class CacheTest extends PHPUnit_Framework_TestCase {
protected $cache;
public function setUp() 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' => 'John Doe', 'age' => 35, 'title' => 'admin'));
User::create(array('name' => 'Jane Doe', 'age' => 33, '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' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
...@@ -16,15 +21,11 @@ class CacheTest extends PHPUnit_Framework_TestCase { ...@@ -16,15 +21,11 @@ class CacheTest extends PHPUnit_Framework_TestCase {
public function tearDown() public function tearDown()
{ {
User::truncate(); User::truncate();
$this->cache->forget('db.users');
} }
public function testCache() public function testCache()
{ {
# get from cache driver
global $app;
$cache = $app['cache'];
$cache->forget('db.users');
# auto generate cache key # auto generate cache key
$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get(); $users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
...@@ -36,7 +37,7 @@ class CacheTest extends PHPUnit_Framework_TestCase { ...@@ -36,7 +37,7 @@ class CacheTest extends PHPUnit_Framework_TestCase {
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get(); $users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
$users = $cache->get('db.users'); $users = $this->cache->get('db.users');
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
} }
......
<?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
<?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
<?php <?php
require_once('tests/app.php'); require_once('tests/app.php');
use Illuminate\Support\Facades\DB;
class QueryTest extends PHPUnit_Framework_TestCase { class QueryTest extends PHPUnit_Framework_TestCase {
public function setUp() {} public function setUp()
public function tearDown()
{ {
DB::collection('users')->truncate(); // test data
DB::collection('items')->truncate(); 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() public function testGet()
{ {
$users = DB::collection('users')->get(); $users = User::get();
$this->assertEquals(0, count($users));
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(); public function testFirst()
$this->assertEquals(1, count($users)); {
$user = User::get()->first();
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$this->assertEquals('John Doe', $user->name);
} }
public function testInsert() public function testWhere()
{ {
$user = array( $users = User::where('age', 35)->get();
'tags' => array('tag1', 'tag2'), $this->assertEquals(3, count($users));
'name' => 'John Doe',
);
DB::collection('users')->insert($user);
$users = DB::collection('users')->get(); $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)); $this->assertEquals(1, count($users));
$user = $users[0]; $users = User::where('age', '!=', 35)->get();
$this->assertEquals('John Doe', $user['name']); $this->assertEquals(6, count($users));
$this->assertTrue(is_array($user['tags']));
$users = User::where('age', '<>', 35)->get();
$this->assertEquals(6, count($users));
} }
public function testBatchInsert() public function testAndWhere()
{ {
$users = array( $users = User::where('age', 35)->where('title', 'admin')->get();
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();
$this->assertEquals(2, count($users)); $this->assertEquals(2, count($users));
$user = $users[0]; $users = User::where('age', '>=', 35)->where('title', 'user')->get();
$this->assertEquals('Jane Doe', $user['name']); $this->assertEquals(2, count($users));
$this->assertTrue(is_array($user['tags']));
} }
public function testFind() public function testLike()
{ {
$user = array('name' => 'John Doe'); $users = User::where('name', 'like', '%doe')->get();
$id = DB::collection('users')->insertGetId($user); $this->assertEquals(2, count($users));
$this->assertNotNull($id); $users = User::where('name', 'like', '%y%')->get();
$this->assertTrue(is_string($id)); $this->assertEquals(3, count($users));
$user = DB::collection('users')->find($id); $users = User::where('name', 'like', 't%')->get();
$this->assertEquals('John Doe', $user['name']); $this->assertEquals(1, count($users));
} }
public function testUpdate() public function testSelect()
{ {
DB::collection('users')->insert(array('name' => 'John Doe', 'age' => 10)); $user = User::select('name')->first();
DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
$user = User::where('name', 'John Doe')->first(); $this->assertEquals('John Doe', $user->name);
$this->assertEquals(100, $user->age); $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 testDelete() public function testOrWhere()
{ {
DB::collection('users')->insert(array('name' => 'John Doe', 'age' => 100)); $users = User::where('age', 13)->orWhere('title', 'admin')->get();
$this->assertEquals(4, count($users));
DB::collection('users')->where('age', '<', 30)->delete(); $users = User::where('age', 13)->orWhere('age', 23)->get();
$this->assertEquals(1, DB::collection('users')->count()); $this->assertEquals(2, count($users));
DB::collection('users')->where('age', '>', 30)->delete();
$this->assertEquals(0, DB::collection('users')->count());
} }
public function testTruncate() public function testBetween()
{ {
DB::collection('users')->insert(array('name' => 'John Doe', 'age' => 100)); $users = User::whereBetween('age', array(0, 25))->get();
DB::collection('users')->truncate(); $this->assertEquals(2, count($users));
$this->assertEquals(0, DB::collection('users')->count());
$users = User::whereBetween('age', array(13, 23))->get();
$this->assertEquals(2, count($users));
} }
public function testSubKey() public function testIn()
{ {
$user1 = array( $users = User::whereIn('age', array(13, 23))->get();
'name' => 'John Doe', $this->assertEquals(2, count($users));
'address' => array(
'country' => 'Belgium',
'city' => 'Ghent'
)
);
$user2 = array( $users = User::whereIn('age', array(33, 35, 13))->get();
'name' => 'Jane Doe', $this->assertEquals(6, count($users));
'address' => array(
'country' => 'France',
'city' => 'Paris'
)
);
DB::collection('users')->insert(array($user1, $user2)); $users = User::whereNotIn('age', array(33, 35))->get();
$this->assertEquals(4, count($users));
$users = DB::collection('users')->where('address.country', 'Belgium')->get(); $users = User::whereNotNull('age')
$this->assertEquals(1, count($users)); ->whereNotIn('age', array(33, 35))->get();
$this->assertEquals('John Doe', $users[0]['name']); $this->assertEquals(3, count($users));
} }
public function testInArray() public function testWhereNull()
{ {
$item1 = array( $users = User::whereNull('age')->get();
'tags' => array('tag1', 'tag2', 'tag3', 'tag4') $this->assertEquals(1, count($users));
); }
$item2 = array(
'tags' => array('tag2')
);
DB::collection('items')->insert(array($item1, $item2)); public function testWhereNotNull()
{
$users = User::whereNotNull('age')->get();
$this->assertEquals(8, count($users));
}
$items = DB::collection('items')->where('tags', 'tag2')->get(); public function testOrder()
$this->assertEquals(2, count($items)); {
$user = User::whereNotNull('age')->orderBy('age', 'asc')->first();
$this->assertEquals(13, $user->age);
$items = DB::collection('items')->where('tags', 'tag1')->get(); $user = User::whereNotNull('age')->orderBy('age', 'desc')->first();
$this->assertEquals(1, count($items)); $this->assertEquals(37, $user->age);
} }
public function testRaw() public function testIncrements()
{ {
DB::collection('users')->insert(array('name' => 'John Doe')); User::where('name', 'John Doe')->increment('age');
User::where('name', 'John Doe')->increment('age', 2, array('title' => 'user'));
$cursor = DB::collection('users')->raw(function($collection) { $user = User::where('name', 'John Doe')->first();
return $collection->find(); $this->assertEquals(38, $user->age);
}); $this->assertEquals('user', $user->title);
$this->assertInstanceOf('MongoCursor', $cursor); User::where('name', 'John Doe')->decrement('age');
$this->assertEquals(1, $cursor->count()); $num = User::where('name', 'John Doe')->decrement('age', 2, array('title' => 'admin'));
$collection = DB::collection('users')->raw(); $user = User::where('name', 'John Doe')->first();
$this->assertInstanceOf('MongoCollection', $collection); $this->assertEquals(35, $user->age);
} $this->assertEquals('admin', $user->title);
$this->assertEquals(1, $num);
public function testPush() User::increment('age');
{ User::increment('age', 2);
$user = array('name' => 'John Doe', 'tags' => array());
$id = DB::collection('users')->insertGetId($user);
DB::collection('users')->where('_id', $id)->push('tags', 'tag1'); $user = User::where('name', 'Mark Moe')->first();
$user = DB::collection('users')->find($id); $this->assertEquals(26, $user->age);
$this->assertTrue(is_array($user['tags'])); User::decrement('age', 2);
$this->assertEquals(1, count($user['tags'])); $num = User::decrement('age');
$this->assertEquals('tag1', $user['tags'][0]);
DB::collection('users')->where('_id', $id)->push('tags', 'tag2'); $user = User::where('name', 'Mark Moe')->first();
$user = DB::collection('users')->find($id); $this->assertEquals(23, $user->age);
$this->assertEquals(8, $num);
}
$this->assertTrue(is_array($user['tags'])); public function testAggregates()
$this->assertEquals(2, count($user['tags'])); {
$this->assertEquals('tag2', $user['tags'][1]); $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'));
DB::collection('users')->where('_id', $id)->push('tags', array('tag3', 'tag4')); $this->assertEquals(35, User::where('title', 'admin')->max('age'));
$user = DB::collection('users')->find($id); $this->assertEquals(37, User::where('title', 'user')->max('age'));
$this->assertTrue(is_array($user['tags'])); $this->assertEquals(33, User::where('title', 'admin')->min('age'));
$this->assertEquals(4, count($user['tags'])); $this->assertEquals(13, User::where('title', 'user')->min('age'));
$this->assertEquals('tag4', $user['tags'][3]);
} }
public function testPull() public function testGroupBy()
{ {
$user = array('name' => 'John Doe', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4')); $users = User::groupBy('title')->get();
$id = DB::collection('users')->insertGetId($user); $this->assertEquals(3, count($users));
$users = User::groupBy('age')->get();
$this->assertEquals(6, count($users));
DB::collection('users')->where('_id', $id)->pull('tags', 'tag3'); $users = User::groupBy('age')->skip(1)->get();
$user = DB::collection('users')->find($id); $this->assertEquals(5, count($users));
$this->assertTrue(is_array($user['tags'])); $users = User::groupBy('age')->take(2)->get();
$this->assertEquals(3, count($user['tags'])); $this->assertEquals(2, count($users));
$this->assertEquals('tag4', $user['tags'][2]);
DB::collection('users')->where('_id', $id)->pull('tags', array('tag2', 'tag4')); $users = User::groupBy('age')->orderBy('age', 'desc')->get();
$user = DB::collection('users')->find($id); $this->assertEquals(37, $users[0]->age);
$this->assertEquals(35, $users[1]->age);
$this->assertEquals(33, $users[2]->age);
$this->assertTrue(is_array($user['tags'])); $users = User::groupBy('age')->skip(1)->take(2)->orderBy('age', 'desc')->get();
$this->assertEquals(1, count($user['tags'])); $this->assertEquals(35, $users[0]->age);
$this->assertEquals('tag1', $user['tags'][0]); $this->assertEquals(33, $users[1]->age);
} }
public function testDistinct() 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)
{ {
DB::collection('items')->insert(array( $query->where('age', 35)
array('name' => 'knife', 'type' => 'sharp', 'amount' => 34), ->orWhere('name', 'like', '%harry%');
array('name' => 'fork', 'type' => 'sharp', 'amount' => 20), })
array('name' => 'spoon', 'type' => 'round', 'amount' => 3), ->get();
array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
)); $this->assertEquals(2, count($users));
$items = DB::collection('items')->distinct('name')->get(); $users = User::where('age', 35)->orWhere(function($query)
$this->assertEquals(array('knife', 'fork', 'spoon'), $items); {
$query->where('title', 'admin')
->orWhere('name', 'Error');
})
->get();
$types = DB::collection('items')->distinct('type')->get(); $this->assertEquals(5, count($users));
$this->assertEquals(array('sharp', 'round'), $types);
} }
public function testCustomId() public function testRaw()
{ {
DB::collection('items')->insert(array( $where = array('age' => array('$gt' => 30, '$lt' => 40));
array('_id' => 'knife', 'type' => 'sharp', 'amount' => 34), $users = User::whereRaw($where)->get();
array('_id' => 'fork', 'type' => 'sharp', 'amount' => 20),
array('_id' => 'spoon', 'type' => 'round', 'amount' => 3) $this->assertEquals(6, count($users));
));
$item = DB::collection('items')->find('knife'); $where1 = array('age' => array('$gt' => 30, '$lte' => 35));
$this->assertEquals('knife', $item['_id']); $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(6, count($users));
$this->assertEquals('fork', $item['_id']);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment