Commit e9a5e8a7 authored by Jeremy Mikola's avatar Jeremy Mikola

Restructure CollectionTest and fixture generation functions

This also changes the existing collection functional test to inherit FunctionTestCase
parent 46d0767e
<?php
namespace MongoDB\Tests;
use MongoDB\Collection;
use MongoDB\Driver\Manager;
class CollectionFunctionalTest extends FunctionalTestCase
{
private $collection;
public function setUp()
{
parent::setUp();
$this->collection = new Collection($this->manager, $this->getNamespace());
$this->collection->deleteMany(array());
}
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\Result', $cursor);
foreach($cursor as $n => $person) {
$this->assertInternalType("array", $person);
}
$this->assertEquals(0, $n);
}
}
<?php
use MongoDB\Collection;
use MongoDB\Driver\Manager;
namespace MongoDB\Tests;
class CollectionTest extends PHPUnit_Framework_TestCase {
function setUp() {
require_once __DIR__ . "/" . "utils.inc";
$this->faker = Faker\Factory::create();
$this->faker->seed(1234);
$this->manager = new Manager("mongodb://localhost");
$this->collection = new 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\InsertOneResult', $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\Driver\Result', $cursor);
foreach($cursor as $n => $person) {
$this->assertInternalType("array", $person);
}
$this->assertEquals(0, $n);
}
use ReflectionClass;
use ReflectionMethod;
class CollectionTest extends TestCase
{
public function testMethodOrder()
{
$class = new ReflectionClass('MongoDB\Collection');
......@@ -68,4 +30,3 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
}
}
}
<?php
namespace MongoDB\Tests;
use Faker\Factory;
class FixtureGenerator
{
private $faker;
public function __construct()
{
$this->faker = Factory::create();
$this->faker->seed(1234);
}
public function createUser()
{
return array(
"username" => $this->faker->unique()->userName,
"password" => $this->faker->sha256,
"email" => $this->faker->unique()->safeEmail,
"firstName" => $this->faker->firstName,
"lastName" => $this->faker->lastName,
"phoneNumber" => $this->faker->phoneNumber,
"altPhoneNumber" => $this->faker->optional(0.1)->phoneNumber,
"company" => $this->faker->company,
"bio" => $this->faker->paragraph,
"createdAt" => $this->faker->dateTimeBetween("2008-01-01T00:00:00+0000", "2014-08-01T00:00:00+0000")->getTimestamp(),
"addresses" => array(
$this->createAddress(),
$this->createAddress(),
$this->createAddress(),
),
);
}
public function createAddress()
{
return (object) array(
"streetAddress" => $this->faker->streetAddress,
"city" => $this->faker->city,
"state" => $this->faker->state,
"postalCode" => $this->faker->postcode,
"loc" => $this->createGeoJsonPoint(),
);
}
public function createGeoJsonPoint()
{
return (object) array(
"type" => "Point",
"coordinates" => (object) array(
$this->faker->longitude,
$this->faker->latitude
),
);
}
}
<?php
require_once __DIR__ . "/" . "../vendor/autoload.php";
function createUser($faker)
{
return array(
"username" => $faker->unique()->userName,
"password" => $faker->sha256,
"email" => $faker->unique()->safeEmail,
"firstName" => $faker->firstName,
"lastName" => $faker->lastName,
"phoneNumber" => $faker->phoneNumber,
"altPhoneNumber" => $faker->optional(0.1)->phoneNumber,
"company" => $faker->company,
"bio" => $faker->paragraph,
"createdAt" => $faker->dateTimeBetween("2008-01-01T00:00:00+0000", "2014-08-01T00:00:00+0000")->getTimestamp(),
"addresses" => (object)array(
createAddress($faker),
createAddress($faker),
createAddress($faker),
),
);
}
function createAddress($faker)
{
return (object)array(
"streetAddress" => $faker->streetAddress,
"city" => $faker->city,
"state" => $faker->state,
"postalCode" => $faker->postcode,
"loc" => createGeoJsonPoint($faker),
);
}
function createGeoJsonPoint($faker)
{
return (object)array(
"type" => "Point",
"coordinates" => (object)array($faker->longitude, $faker->latitude),
);
}
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