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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace yiiunit\extensions\mongo;
/**
* @group mongo
*/
class CollectionTest extends MongoTestCase
{
protected function tearDown()
{
$this->dropCollection('customer');
parent::tearDown();
}
// Tests :
public function testFind()
{
$collection = $this->getConnection()->getCollection('customer');
$cursor = $collection->find();
$this->assertTrue($cursor instanceof \MongoCursor);
}
public function testInsert()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->insert($data);
$this->assertTrue($id instanceof \MongoId);
$this->assertNotEmpty($id->__toString());
}
/**
* @depends testInsert
*/
public function testFindAll()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->insert($data);
$rows = $collection->findAll();
$this->assertEquals(1, count($rows));
$this->assertEquals($id, $rows[0]['_id']);
}
/**
* @depends testFind
*/
public function testBatchInsert()
{
$collection = $this->getConnection()->getCollection('customer');
$rows = [
[
'name' => 'customer 1',
'address' => 'customer 1 address',
],
[
'name' => 'customer 2',
'address' => 'customer 2 address',
],
];
$insertedRows = $collection->batchInsert($rows);
$this->assertTrue($insertedRows[0]['_id'] instanceof \MongoId);
$this->assertTrue($insertedRows[1]['_id'] instanceof \MongoId);
$this->assertEquals(count($rows), $collection->find()->count());
}
public function testSave()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->save($data);
$this->assertTrue($id instanceof \MongoId);
$this->assertNotEmpty($id->__toString());
}
/**
* @depends testSave
*/
public function testUpdateBySave()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$newId = $collection->save($data);
$updatedId = $collection->save($data);
$this->assertEquals($newId, $updatedId, 'Unable to update data!');
$data['_id'] = $newId->__toString();
$updatedId = $collection->save($data);
$this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
}
/**
* @depends testFindAll
*/
public function testRemove()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->insert($data);
$collection->remove(['_id' => $id]);
$rows = $collection->findAll();
$this->assertEquals(0, count($rows));
}
/**
* @depends testFindAll
*/
public function testUpdate()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->insert($data);
$newData = [
'name' => 'new name'
];
$collection->update(['_id' => $id], $newData);
list($row) = $collection->findAll();
$this->assertEquals($newData['name'], $row['name']);
}
}