Commit fc0cbcf9 authored by Hannes Magnusson's avatar Hannes Magnusson

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

parent 7ddd1c13
......@@ -4,6 +4,9 @@
"require": {
"ext-phongo": ">=0.1.2"
},
"require-dev": {
"fzaninotto/faker": "~1.0"
},
"license": "BSD-2-Clause",
"authors": [
{
......
<?php
class CollectionTest extends PHPUnit_Framework_TestCase
{
public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
class CollectionTest extends PHPUnit_Framework_TestCase {
function setUp() {
require __DIR__ . "/" . "utils.inc";
$this->faker = Faker\Factory::create();
$this->faker->seed(1234);
$this->manager = new MongoDB\Manager("mongodb://localhost");
$this->collection = new MongoDB\Collection($this->manager, "test.case");
$this->collection->deleteMany(array());
}
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