Skip to content

Commit fc0cbcf

Browse files
committed
Add test for insertOne(), findOne(), count(), ..
1 parent 7ddd1c1 commit fc0cbcf

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"require": {
55
"ext-phongo": ">=0.1.2"
66
},
7+
"require-dev": {
8+
"fzaninotto/faker": "~1.0"
9+
},
710
"license": "BSD-2-Clause",
811
"authors": [
912
{

tests/CollectionTest.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
<?php
2-
class CollectionTest extends PHPUnit_Framework_TestCase
3-
{
4-
public function testPushAndPop()
5-
{
6-
$stack = array();
7-
$this->assertEquals(0, count($stack));
8-
9-
array_push($stack, 'foo');
10-
$this->assertEquals('foo', $stack[count($stack)-1]);
11-
$this->assertEquals(1, count($stack));
12-
13-
$this->assertEquals('foo', array_pop($stack));
14-
$this->assertEquals(0, count($stack));
2+
class CollectionTest extends PHPUnit_Framework_TestCase {
3+
4+
function setUp() {
5+
require __DIR__ . "/" . "utils.inc";
6+
$this->faker = Faker\Factory::create();
7+
$this->faker->seed(1234);
8+
9+
$this->manager = new MongoDB\Manager("mongodb://localhost");
10+
$this->collection = new MongoDB\Collection($this->manager, "test.case");
11+
$this->collection->deleteMany(array());
12+
}
13+
14+
function testInsertAndRetrieve() {
15+
$collection = $this->collection;
16+
17+
for($i=0; $i<10;$i++) {
18+
$user = createUser($this->faker);
19+
$result = $collection->insertOne($user);
20+
$this->assertInstanceOf("MongoDB\\InsertResult", $result);
21+
$this->assertInstanceOf("BSON\ObjectId", $result->getInsertedId());
22+
$this->assertEquals(24, strlen($result->getInsertedId()));
23+
24+
$user["_id"] = $result->getInsertedId();
25+
$document = $collection->findOne(array("_id" => $result->getInsertedId()));
26+
$this->assertEquals($document, $user, "The inserted and returned objects are the same");
27+
}
28+
29+
$this->assertEquals(10, $i);
30+
31+
32+
$query = array("firstName" => "Ransom");
33+
$count = $collection->count($query);
34+
$this->assertEquals(1, $count);
35+
$cursor = $collection->find($query);
36+
$this->assertInstanceOf("MongoDB\\QueryResult", $cursor);
37+
38+
foreach($cursor as $n => $person) {
39+
$this->assertInternalType("array", $person);
40+
}
41+
$this->assertEquals(0, $n);
1542
}
1643
}
1744

0 commit comments

Comments
 (0)