1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require_once('vendor/autoload.php');
require_once('tests/app.php');
use Jenssegers\Mongodb\Facades\DB;
class QueryTest 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 testInsert()
{
$user = array('name' => 'John Doe');
DB::collection('users')->insert($user);
$users = DB::collection('users')->get();
$this->assertEquals(1, count($users));
$user = DB::collection('users')->first();
$this->assertEquals('John Doe', $user['name']);
}
public function testFind()
{
$user = array('name' => 'John Doe');
$id = DB::collection('users')->insertGetId($user);
$this->assertNotNull($id);
$this->assertTrue(is_string($id));
$user = DB::collection('users')->find($id);
$this->assertEquals('John Doe', $user['name']);
}
public function testSubKey()
{
$user1 = array(
'name' => 'John Doe',
'address' => array(
'country' => 'Belgium',
'city' => 'Ghent'
)
);
$user2 = array(
'name' => 'Jane Doe',
'address' => array(
'country' => 'France',
'city' => 'Paris'
)
);
DB::collection('users')->insert(array($user1, $user2));
$users = DB::collection('users')->where('address.country', 'Belgium')->get();
$this->assertEquals(1, count($users));
$this->assertEquals('John Doe', $users[0]['name']);
}
public function testInArray()
{
$item1 = array(
'tags' => array('tag1', 'tag2', 'tag3', 'tag4')
);
$item2 = array(
'tags' => array('tag2')
);
DB::collection('items')->insert(array($item1, $item2));
$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));
}
}