Commit c520df8a authored by Jeremy Mikola's avatar Jeremy Mikola

Split Database and Collection functional tests

parent f3492fb1
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\Tests\FixtureGenerator;
/**
* Functional tests for the Collection class.
*/
class CollectionFunctionalTest extends FunctionalTestCase
{
public function testDrop()
{
$writeResult = $this->collection->insertOne(array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->collection->drop();
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
function testInsertAndRetrieve()
{
$generator = new FixtureGenerator();
for ($i = 0; $i < 10; $i++) {
$user = $generator->createUser();
$result = $this->collection->insertOne($user);
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
$this->assertEquals(24, strlen($result->getInsertedId()));
$user["_id"] = $result->getInsertedId();
$document = $this->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 = $this->collection->count($query);
$this->assertEquals(1, $count);
$cursor = $this->collection->find($query);
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
foreach($cursor as $n => $person) {
$this->assertInternalType("array", $person);
}
$this->assertEquals(0, $n);
}
}
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/**
* Base class for Collection functional tests.
*/
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
protected $collection;
public function setUp()
{
parent::setUp();
$this->collection = new Collection($this->manager, $this->getNamespace());
$this->dropCollectionIfItExists($this->collection);
}
/**
* Drop the collection if it exists.
*
* @param Collection $collection
*/
protected function dropCollectionIfItExists(Collection $collection)
{
$database = new Database($this->manager, $collection->getDatabaseName());
$collections = $database->listCollections(array('filter' => array('name' => $collection->getCollectionName())));
if (iterator_count($collections) > 0) {
$this->assertCommandSucceeded($collection->drop());
}
}
}
<?php
namespace MongoDB\Tests;
namespace MongoDB\Tests\Collection;
use MongoDB\Collection;
use MongoDB\Driver\Manager;
use MongoDB\Model\IndexInfo;
use InvalidArgumentException;
class CollectionFunctionalTest extends FunctionalTestCase
/**
* Functional tests for index management methods.
*
* @see https://github.com/mongodb/specifications/blob/master/source/index-management.rst
*/
class IndexManagementFunctionalTest extends FunctionalTestCase
{
private $collection;
public function setUp()
{
parent::setUp();
$this->collection = new Collection($this->manager, $this->getNamespace());
$this->collection->deleteMany(array());
}
public function testDrop()
{
$writeResult = $this->collection->insertOne(array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->collection->drop();
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
function testInsertAndRetrieve()
{
$generator = new FixtureGenerator();
for ($i = 0; $i < 10; $i++) {
$user = $generator->createUser();
$result = $this->collection->insertOne($user);
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
$this->assertEquals(24, strlen($result->getInsertedId()));
$user["_id"] = $result->getInsertedId();
$document = $this->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 = $this->collection->count($query);
$this->assertEquals(1, $count);
$cursor = $this->collection->find($query);
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
foreach($cursor as $n => $person) {
$this->assertInternalType("array", $person);
}
$this->assertEquals(0, $n);
}
public function testCreateIndex()
{
$that = $this;
......
<?php
namespace MongoDB\Tests;
namespace MongoDB\Tests\Database;
use MongoDB\Client;
use MongoDB\Database;
use MongoDB\Model\CollectionInfo;
use InvalidArgumentException;
/**
* Functional tests for the Database class.
* Functional tests for collection management methods.
*/
class DatabaseFunctionalTest extends FunctionalTestCase
class CollectionManagementFunctionalTest extends FunctionalTestCase
{
private $database;
public function setUp()
{
parent::setUp();
$this->database = new Database($this->manager, $this->getDatabaseName());
$this->database->drop();
}
public function testCreateCollection()
{
$that = $this;
......@@ -49,16 +37,6 @@ class DatabaseFunctionalTest extends FunctionalTestCase
});
}
public function testDrop()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->database->drop();
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
public function testDropCollection()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
......
<?php
namespace MongoDB\Tests\Database;
/**
* Functional tests for the Database class.
*/
class DatabaseFunctionalTest extends FunctionalTestCase
{
public function testDrop()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->database->drop();
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
}
<?php
namespace MongoDB\Tests\Database;
use MongoDB\Database;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/**
* Base class for Database functional tests.
*/
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
protected $database;
public function setUp()
{
parent::setUp();
$this->database = new Database($this->manager, $this->getDatabaseName());
$this->database->drop();
}
}
......@@ -15,7 +15,7 @@ abstract class FunctionalTestCase extends TestCase
$this->manager = new Manager($this->getUri());
}
public function assertCollectionCount($namespace, $count)
protected function assertCollectionCount($namespace, $count)
{
list($databaseName, $collectionName) = explode('.', $namespace, 2);
......@@ -26,7 +26,7 @@ abstract class FunctionalTestCase extends TestCase
$this->assertEquals($count, $document['n']);
}
public function assertCommandSucceeded(Cursor $cursor)
protected function assertCommandSucceeded(Cursor $cursor)
{
$document = current($cursor->toArray());
$this->assertArrayHasKey('ok', $document);
......
......@@ -11,7 +11,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
*
* @return string
*/
public function getCollectionName()
protected function getCollectionName()
{
$class = new ReflectionClass($this);
......@@ -23,7 +23,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
*
* @return string
*/
public function getDatabaseName()
protected function getDatabaseName()
{
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
}
......@@ -33,7 +33,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
*
* @return string
*/
public function getNamespace()
protected function getNamespace()
{
return sprintf('%s.%s', $this->getDatabaseName(), $this->getCollectionName());
}
......@@ -43,7 +43,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
*
* @return string
*/
public function getUri()
protected function getUri()
{
return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
}
......
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