-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMongoTest.php
More file actions
335 lines (280 loc) · 10.2 KB
/
MongoTest.php
File metadata and controls
335 lines (280 loc) · 10.2 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
namespace Utopia\Tests;
use MongoDB\BSON\ObjectId;
use PHPUnit\Framework\TestCase;
use Utopia\Mongo\Client;
use Utopia\Mongo\Exception;
class MongoTest extends TestCase
{
public static ?Client $db = null;
/**
* @throws Exception
*/
public static function getDatabase(): Client
{
if (!is_null(self::$db)) {
return self::$db;
}
$client = new Client('testing', 'mongo', 27017, 'root', 'example', false);
$client->connect();
self::$db = $client;
return self::$db;
}
public function testDeleteDatabase()
{
self::assertTrue($this->getDatabase()->dropDatabase([]));
}
/**
* @throws Exception
*/
public function testCreateCollection()
{
self::assertTrue($this->getDatabase()->createCollection('movies'));
self::assertTrue($this->getDatabase()->dropCollection('movies'));
self::assertTrue($this->getDatabase()->createCollection('movies'));
self::expectException(Exception::class);
self::assertTrue($this->getDatabase()->createCollection('movies'));
}
public function testListDatabases()
{
self::assertCount(4, $this->getDatabase()->listDatabaseNames()->databases);
}
public function testCreateDocument()
{
$doc = $this->getDatabase()->insert(
'movies',
[
'name' => 'Armageddon',
'country' => 'USA',
'language' => 'English'
]
);
$id = (string)$doc['_id'];
self::assertEquals(24, strlen($id));
$doc = $this->getDatabase()->find('movies', ['name' => 'Armageddon'])->cursor->firstBatch ?? [];
self::assertCount(1, $doc);
$doc = $this->getDatabase()->find('movies', ['_id' => new ObjectId($id)])->cursor->firstBatch ?? [];
self::assertCount(1, $doc);
$doc = $this->getDatabase()->insert('movies', ['9 Monkeys']);
$id = (string)$doc['_id'];
self::assertEquals(24, strlen($id));
$doc = $this->getDatabase()->insert(
'movies',
[
'name' => 300,
'country' => 'USA',
'language' => 'English'
]
);
$doc = $this->getDatabase()->insert(
'movies',
[
'_id' => 999,
'array' => ['USA', 'UK', 'India'],
'language' => 'English',
'float' => 9.9,
'integer' => 9,
'is_open' => true,
'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'),
'date_object' => new \DateTime()
]
);
$doc = $this->getDatabase()->find('movies', ['_id' => 999])->cursor->firstBatch ?? [];
$doc = $doc[0];
self::assertTrue($doc->is_open);
self::assertIsFloat($doc->float);
self::assertIsInt($doc->integer);
self::assertIsArray($doc->array);
self::assertIsString($doc->date_string);
self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back
}
public function testCreateDocuments(): array
{
$docs = $this->getDatabase()->insertMany(
'movies',
[
[
'name' => 'Armageddon',
'country' => 'USA',
'language' => 'English'
],
[
'name' => '9 Monkeys',
'country' => 'USA',
'language' => 'English'
],
[
'name' => 300,
'country' => 'USA',
'language' => 'English'
]
]
);
self::assertCount(3, $docs);
return $docs;
}
public function testUpdateDocument(): void
{
$this->getDatabase()->insert(
'movies',
[
'name' => 'Armageddon',
'country' => 'AUS',
'language' => 'English'
]
);
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon'],
['$set' => ['name' => 'Armageddon 2']]
);
$doc = $this->getDatabase()->find(
'movies',
['name' => 'Armageddon 2']
)->cursor->firstBatch ?? [];
self::assertCount(1, $doc);
}
/**
* @depends testCreateDocuments
* @return void
* @throws Exception
*/
public function testUpdateDocuments(array $documents): void
{
$this->getDatabase()->update(
'movies',
updates: ['$set' => ['name' => 'Armageddon 2']],
multi: true
);
$docs = $this->getDatabase()->find(
'movies',
filters: ['name' => 'Armageddon 2']
)->cursor->firstBatch ?? [];
self::assertCount(8, $docs);
}
public function testUpdateMultipleDocuments(): void
{
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon 2'],
['$rename' => ['name' => 'title']],
multi: true
);
$docs = $this->getDatabase()->find(
'movies',
['title' => 'Armageddon 2']
)->cursor->firstBatch ?? [];
self::assertCount(8, $docs);
}
public function testDriverException()
{
self::expectException('MongoDB\Driver\Exception\InvalidArgumentException');
self::expectExceptionCode(0);
new ObjectId('triggerAnError');
}
public function testDuplicationException()
{
self::expectException(Exception::class);
self::expectExceptionCode(11000);
$this->getDatabase()->insert('movies', ['_id' => 999]);
}
public function testExceedTimeException()
{
self::expectException(Exception::class);
self::expectExceptionCode(50);
$this->getDatabase()->find(
'movies',
['$where' => 'sleep(1000) || true'],
['maxTimeMS'=> 1]
)->cursor->firstBatch ?? [];
}
public function testUpsert()
{
$this->getDatabase()->insert(
'movies_upsert',
[
'name' => 'Gone with the wind',
'language' => 'English',
'country' => 'UK',
'counter' => 1
]
);
$this->getDatabase()->upsert('movies_upsert', [
[
'filter' => ['name' => 'Gone with the wind'],
'update' => [
'$set' => ['country' => 'USA'],
'$inc' => ['counter' => 3]
]
],
[
'filter' => ['name' => 'The godfather'],
'update' => [
'$set' => ['name' => 'The godfather 2', 'country' => 'USA', 'language' => 'English']
]
],
]);
$documents = $this->getDatabase()->find('movies_upsert')->cursor->firstBatch ?? [];
self::assertCount(2, $documents);
self::assertEquals(4, $documents[0]->counter);
self::assertEquals('The godfather 2', $documents[1]->name);
self::assertEquals('USA', $documents[1]->country);
self::assertEquals('English', $documents[1]->language);
}
public function testCountMethod()
{
$collectionName = 'count_test';
$this->getDatabase()->createCollection($collectionName);
$documents = [];
for ($i = 1; $i <= 30; $i++) {
$documents[] = [
'name' => "Document {$i}",
'number' => $i,
'category' => 'test',
'created_at' => new \DateTime()
];
}
$this->getDatabase()->insertMany($collectionName, $documents);
$total = $this->getDatabase()->count($collectionName, [], []);
self::assertEquals(30, $total);
// Test count with filter (should be 1 for each specific number)
$total = $this->getDatabase()->count($collectionName, ['number' => 15], []);
self::assertEquals(1, $total);
// Test count with range filter (should be 10 for numbers 1-10)
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], []);
self::assertEquals(10, $total);
// Test count with limit (should be 5 for first 5 documents)
$total = $this->getDatabase()->count($collectionName, [], ['limit' => 5]);
self::assertEquals(5, $total);
// Test count with filter and limit (should be 3 for first 3 documents with number <= 10)
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], ['limit' => 3]);
self::assertEquals(3, $total);
// Test aggregation count - total documents
$aggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$count' => 'total']
]);
self::assertEquals(30, $aggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with filter
$filteredAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['number' => ['$lte' => 10]]],
['$count' => 'total']
]);
self::assertEquals(10, $filteredAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with limit
$limitedAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$limit' => 7],
['$count' => 'total']
]);
self::assertEquals(7, $limitedAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with group by
$groupedAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$group' => [
'_id' => '$category', // Group by category
'count' => ['$sum' => 1] // Count of documents in the group
]]
]);
self::assertEquals(30, $groupedAggregationResult->cursor->firstBatch[0]->count);
self::assertEquals('test', $groupedAggregationResult->cursor->firstBatch[0]->_id);
$this->getDatabase()->dropCollection($collectionName);
}
}