diff --git a/composer.json b/composer.json index 18554a7282dcc77a59fbb42010dd8b4f4db27837..cb3b69db6d61b933aa5afa95f806d76baa26fc70 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,9 @@ "require": { "ext-phongo": ">=0.1.2" }, + "require-dev": { + "fzaninotto/faker": "~1.0" + }, "license": "BSD-2-Clause", "authors": [ { diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 03e65ac6d7e64f206bfc42828c06cdec04fb555d..f61076f4b8f2a1f2e71f240be3217c9347b532d3 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1,17 +1,44 @@ <?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); } }