Skip to content

Commit 30ced49

Browse files
committed
Restructure CollectionTest and fixture generation functions
This also changes the existing collection functional test to inherit FunctionTestCase
1 parent 2f97ce5 commit 30ced49

File tree

4 files changed

+113
-88
lines changed

4 files changed

+113
-88
lines changed

tests/CollectionFunctionalTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Collection;
6+
use MongoDB\Driver\Manager;
7+
8+
class CollectionFunctionalTest extends FunctionalTestCase
9+
{
10+
private $collection;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->collection = new Collection($this->manager, $this->getNamespace());
17+
$this->collection->deleteMany(array());
18+
}
19+
20+
function testInsertAndRetrieve()
21+
{
22+
$generator = new FixtureGenerator();
23+
24+
for ($i = 0; $i < 10; $i++) {
25+
$user = $generator->createUser();
26+
$result = $this->collection->insertOne($user);
27+
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
28+
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
29+
$this->assertEquals(24, strlen($result->getInsertedId()));
30+
31+
$user["_id"] = $result->getInsertedId();
32+
$document = $this->collection->findOne(array("_id" => $result->getInsertedId()));
33+
$this->assertEquals($document, $user, "The inserted and returned objects are the same");
34+
}
35+
36+
$this->assertEquals(10, $i);
37+
38+
$query = array("firstName" => "Ransom");
39+
$count = $this->collection->count($query);
40+
$this->assertEquals(1, $count);
41+
$cursor = $this->collection->find($query);
42+
$this->assertInstanceOf('MongoDB\Driver\Result', $cursor);
43+
44+
foreach($cursor as $n => $person) {
45+
$this->assertInternalType("array", $person);
46+
}
47+
$this->assertEquals(0, $n);
48+
}
49+
}

tests/CollectionTest.php

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,12 @@
11
<?php
22

3-
use MongoDB\Collection;
4-
use MongoDB\Driver\Manager;
3+
namespace MongoDB\Tests;
54

6-
class CollectionTest extends PHPUnit_Framework_TestCase {
7-
8-
function setUp() {
9-
require_once __DIR__ . "/" . "utils.inc";
10-
$this->faker = Faker\Factory::create();
11-
$this->faker->seed(1234);
12-
13-
$this->manager = new Manager("mongodb://localhost");
14-
$this->collection = new Collection($this->manager, "test.case");
15-
$this->collection->deleteMany(array());
16-
}
17-
18-
function testInsertAndRetrieve() {
19-
$collection = $this->collection;
20-
21-
for($i=0; $i<10;$i++) {
22-
$user = createUser($this->faker);
23-
$result = $collection->insertOne($user);
24-
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
25-
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
26-
$this->assertEquals(24, strlen($result->getInsertedId()));
27-
28-
$user["_id"] = $result->getInsertedId();
29-
$document = $collection->findOne(array("_id" => $result->getInsertedId()));
30-
$this->assertEquals($document, $user, "The inserted and returned objects are the same");
31-
}
32-
33-
$this->assertEquals(10, $i);
34-
35-
36-
$query = array("firstName" => "Ransom");
37-
$count = $collection->count($query);
38-
$this->assertEquals(1, $count);
39-
$cursor = $collection->find($query);
40-
$this->assertInstanceOf('MongoDB\Driver\Result', $cursor);
41-
42-
foreach($cursor as $n => $person) {
43-
$this->assertInternalType("array", $person);
44-
}
45-
$this->assertEquals(0, $n);
46-
}
5+
use ReflectionClass;
6+
use ReflectionMethod;
477

8+
class CollectionTest extends TestCase
9+
{
4810
public function testMethodOrder()
4911
{
5012
$class = new ReflectionClass('MongoDB\Collection');
@@ -68,4 +30,3 @@ function(ReflectionMethod $method) { return $method->getName(); },
6830
}
6931
}
7032
}
71-

tests/FixtureGenerator.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use Faker\Factory;
6+
7+
class FixtureGenerator
8+
{
9+
private $faker;
10+
11+
public function __construct()
12+
{
13+
$this->faker = Factory::create();
14+
$this->faker->seed(1234);
15+
}
16+
17+
public function createUser()
18+
{
19+
return array(
20+
"username" => $this->faker->unique()->userName,
21+
"password" => $this->faker->sha256,
22+
"email" => $this->faker->unique()->safeEmail,
23+
"firstName" => $this->faker->firstName,
24+
"lastName" => $this->faker->lastName,
25+
"phoneNumber" => $this->faker->phoneNumber,
26+
"altPhoneNumber" => $this->faker->optional(0.1)->phoneNumber,
27+
"company" => $this->faker->company,
28+
"bio" => $this->faker->paragraph,
29+
"createdAt" => $this->faker->dateTimeBetween("2008-01-01T00:00:00+0000", "2014-08-01T00:00:00+0000")->getTimestamp(),
30+
"addresses" => array(
31+
$this->createAddress(),
32+
$this->createAddress(),
33+
$this->createAddress(),
34+
),
35+
);
36+
}
37+
38+
public function createAddress()
39+
{
40+
return (object) array(
41+
"streetAddress" => $this->faker->streetAddress,
42+
"city" => $this->faker->city,
43+
"state" => $this->faker->state,
44+
"postalCode" => $this->faker->postcode,
45+
"loc" => $this->createGeoJsonPoint(),
46+
);
47+
}
48+
49+
public function createGeoJsonPoint()
50+
{
51+
return (object) array(
52+
"type" => "Point",
53+
"coordinates" => (object) array(
54+
$this->faker->longitude,
55+
$this->faker->latitude
56+
),
57+
);
58+
}
59+
}

tests/utils.inc

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)