Commit fc0cbcf9 authored by Hannes Magnusson's avatar Hannes Magnusson

Add test for insertOne(), findOne(), count(), ..

parent 7ddd1c13
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
"require": { "require": {
"ext-phongo": ">=0.1.2" "ext-phongo": ">=0.1.2"
}, },
"require-dev": {
"fzaninotto/faker": "~1.0"
},
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"authors": [ "authors": [
{ {
......
<?php <?php
class CollectionTest extends PHPUnit_Framework_TestCase class CollectionTest extends PHPUnit_Framework_TestCase {
{
public function testPushAndPop() function setUp() {
{ require __DIR__ . "/" . "utils.inc";
$stack = array(); $this->faker = Faker\Factory::create();
$this->assertEquals(0, count($stack)); $this->faker->seed(1234);
array_push($stack, 'foo'); $this->manager = new MongoDB\Manager("mongodb://localhost");
$this->assertEquals('foo', $stack[count($stack)-1]); $this->collection = new MongoDB\Collection($this->manager, "test.case");
$this->assertEquals(1, count($stack)); $this->collection->deleteMany(array());
}
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack)); function testInsertAndRetrieve() {
$collection = $this->collection;
for($i=0; $i<10;$i++) {
$user = createUser($this->faker);
$result = $collection->insertOne($user);
$this->assertInstanceOf("MongoDB\\InsertResult", $result);
$this->assertInstanceOf("BSON\ObjectId", $result->getInsertedId());
$this->assertEquals(24, strlen($result->getInsertedId()));
$user["_id"] = $result->getInsertedId();
$document = $collection->findOne(array("_id" => $result->getInsertedId()));
$this->assertEquals($document, $user, "The inserted and returned objects are the same");
}
$this->assertEquals(10, $i);
$query = array("firstName" => "Ransom");
$count = $collection->count($query);
$this->assertEquals(1, $count);
$cursor = $collection->find($query);
$this->assertInstanceOf("MongoDB\\QueryResult", $cursor);
foreach($cursor as $n => $person) {
$this->assertInternalType("array", $person);
}
$this->assertEquals(0, $n);
} }
} }
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