Skip to content

Commit c520df8

Browse files
committed
Split Database and Collection functional tests
1 parent f3492fb commit c520df8

8 files changed

+146
-85
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Tests\FixtureGenerator;
6+
7+
/**
8+
* Functional tests for the Collection class.
9+
*/
10+
class CollectionFunctionalTest extends FunctionalTestCase
11+
{
12+
public function testDrop()
13+
{
14+
$writeResult = $this->collection->insertOne(array('x' => 1));
15+
$this->assertEquals(1, $writeResult->getInsertedCount());
16+
17+
$commandResult = $this->collection->drop();
18+
$this->assertCommandSucceeded($commandResult);
19+
$this->assertCollectionCount($this->getNamespace(), 0);
20+
}
21+
22+
function testInsertAndRetrieve()
23+
{
24+
$generator = new FixtureGenerator();
25+
26+
for ($i = 0; $i < 10; $i++) {
27+
$user = $generator->createUser();
28+
$result = $this->collection->insertOne($user);
29+
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
30+
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
31+
$this->assertEquals(24, strlen($result->getInsertedId()));
32+
33+
$user["_id"] = $result->getInsertedId();
34+
$document = $this->collection->findOne(array("_id" => $result->getInsertedId()));
35+
$this->assertEquals($document, $user, "The inserted and returned objects are the same");
36+
}
37+
38+
$this->assertEquals(10, $i);
39+
40+
$query = array("firstName" => "Ransom");
41+
$count = $this->collection->count($query);
42+
$this->assertEquals(1, $count);
43+
$cursor = $this->collection->find($query);
44+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
45+
46+
foreach($cursor as $n => $person) {
47+
$this->assertInternalType("array", $person);
48+
}
49+
$this->assertEquals(0, $n);
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\Collection;
6+
use MongoDB\Database;
7+
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
8+
9+
/**
10+
* Base class for Collection functional tests.
11+
*/
12+
abstract class FunctionalTestCase extends BaseFunctionalTestCase
13+
{
14+
protected $collection;
15+
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
20+
$this->collection = new Collection($this->manager, $this->getNamespace());
21+
$this->dropCollectionIfItExists($this->collection);
22+
}
23+
24+
/**
25+
* Drop the collection if it exists.
26+
*
27+
* @param Collection $collection
28+
*/
29+
protected function dropCollectionIfItExists(Collection $collection)
30+
{
31+
$database = new Database($this->manager, $collection->getDatabaseName());
32+
$collections = $database->listCollections(array('filter' => array('name' => $collection->getCollectionName())));
33+
34+
if (iterator_count($collections) > 0) {
35+
$this->assertCommandSucceeded($collection->drop());
36+
}
37+
}
38+
}

tests/CollectionFunctionalTest.php renamed to tests/Collection/IndexManagementFunctionalTest.php

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,17 @@
11
<?php
22

3-
namespace MongoDB\Tests;
3+
namespace MongoDB\Tests\Collection;
44

5-
use MongoDB\Collection;
6-
use MongoDB\Driver\Manager;
75
use MongoDB\Model\IndexInfo;
86
use InvalidArgumentException;
97

10-
class CollectionFunctionalTest extends FunctionalTestCase
8+
/**
9+
* Functional tests for index management methods.
10+
*
11+
* @see https://github.com/mongodb/specifications/blob/master/source/index-management.rst
12+
*/
13+
class IndexManagementFunctionalTest extends FunctionalTestCase
1114
{
12-
private $collection;
13-
14-
public function setUp()
15-
{
16-
parent::setUp();
17-
18-
$this->collection = new Collection($this->manager, $this->getNamespace());
19-
$this->collection->deleteMany(array());
20-
}
21-
22-
public function testDrop()
23-
{
24-
$writeResult = $this->collection->insertOne(array('x' => 1));
25-
$this->assertEquals(1, $writeResult->getInsertedCount());
26-
27-
$commandResult = $this->collection->drop();
28-
$this->assertCommandSucceeded($commandResult);
29-
$this->assertCollectionCount($this->getNamespace(), 0);
30-
}
31-
32-
function testInsertAndRetrieve()
33-
{
34-
$generator = new FixtureGenerator();
35-
36-
for ($i = 0; $i < 10; $i++) {
37-
$user = $generator->createUser();
38-
$result = $this->collection->insertOne($user);
39-
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
40-
$this->assertInstanceOf('BSON\ObjectId', $result->getInsertedId());
41-
$this->assertEquals(24, strlen($result->getInsertedId()));
42-
43-
$user["_id"] = $result->getInsertedId();
44-
$document = $this->collection->findOne(array("_id" => $result->getInsertedId()));
45-
$this->assertEquals($document, $user, "The inserted and returned objects are the same");
46-
}
47-
48-
$this->assertEquals(10, $i);
49-
50-
$query = array("firstName" => "Ransom");
51-
$count = $this->collection->count($query);
52-
$this->assertEquals(1, $count);
53-
$cursor = $this->collection->find($query);
54-
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
55-
56-
foreach($cursor as $n => $person) {
57-
$this->assertInternalType("array", $person);
58-
}
59-
$this->assertEquals(0, $n);
60-
}
61-
6215
public function testCreateIndex()
6316
{
6417
$that = $this;

tests/DatabaseFunctionalTest.php renamed to tests/Database/CollectionManagementFunctionalTest.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
<?php
22

3-
namespace MongoDB\Tests;
3+
namespace MongoDB\Tests\Database;
44

5-
use MongoDB\Client;
6-
use MongoDB\Database;
75
use MongoDB\Model\CollectionInfo;
86
use InvalidArgumentException;
97

108
/**
11-
* Functional tests for the Database class.
9+
* Functional tests for collection management methods.
1210
*/
13-
class DatabaseFunctionalTest extends FunctionalTestCase
11+
class CollectionManagementFunctionalTest extends FunctionalTestCase
1412
{
15-
private $database;
16-
17-
public function setUp()
18-
{
19-
parent::setUp();
20-
21-
$this->database = new Database($this->manager, $this->getDatabaseName());
22-
$this->database->drop();
23-
}
24-
2513
public function testCreateCollection()
2614
{
2715
$that = $this;
@@ -49,16 +37,6 @@ public function testCreateCollection()
4937
});
5038
}
5139

52-
public function testDrop()
53-
{
54-
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
55-
$this->assertEquals(1, $writeResult->getInsertedCount());
56-
57-
$commandResult = $this->database->drop();
58-
$this->assertCommandSucceeded($commandResult);
59-
$this->assertCollectionCount($this->getNamespace(), 0);
60-
}
61-
6240
public function testDropCollection()
6341
{
6442
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Database;
4+
5+
/**
6+
* Functional tests for the Database class.
7+
*/
8+
class DatabaseFunctionalTest extends FunctionalTestCase
9+
{
10+
public function testDrop()
11+
{
12+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
13+
$this->assertEquals(1, $writeResult->getInsertedCount());
14+
15+
$commandResult = $this->database->drop();
16+
$this->assertCommandSucceeded($commandResult);
17+
$this->assertCollectionCount($this->getNamespace(), 0);
18+
}
19+
}

tests/Database/FunctionalTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Database;
4+
5+
use MongoDB\Database;
6+
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
7+
8+
/**
9+
* Base class for Database functional tests.
10+
*/
11+
abstract class FunctionalTestCase extends BaseFunctionalTestCase
12+
{
13+
protected $database;
14+
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
$this->database = new Database($this->manager, $this->getDatabaseName());
20+
$this->database->drop();
21+
}
22+
}

tests/FunctionalTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function setUp()
1515
$this->manager = new Manager($this->getUri());
1616
}
1717

18-
public function assertCollectionCount($namespace, $count)
18+
protected function assertCollectionCount($namespace, $count)
1919
{
2020
list($databaseName, $collectionName) = explode('.', $namespace, 2);
2121

@@ -26,7 +26,7 @@ public function assertCollectionCount($namespace, $count)
2626
$this->assertEquals($count, $document['n']);
2727
}
2828

29-
public function assertCommandSucceeded(Cursor $cursor)
29+
protected function assertCommandSucceeded(Cursor $cursor)
3030
{
3131
$document = current($cursor->toArray());
3232
$this->assertArrayHasKey('ok', $document);

tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
1111
*
1212
* @return string
1313
*/
14-
public function getCollectionName()
14+
protected function getCollectionName()
1515
{
1616
$class = new ReflectionClass($this);
1717

@@ -23,7 +23,7 @@ public function getCollectionName()
2323
*
2424
* @return string
2525
*/
26-
public function getDatabaseName()
26+
protected function getDatabaseName()
2727
{
2828
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
2929
}
@@ -33,7 +33,7 @@ public function getDatabaseName()
3333
*
3434
* @return string
3535
*/
36-
public function getNamespace()
36+
protected function getNamespace()
3737
{
3838
return sprintf('%s.%s', $this->getDatabaseName(), $this->getCollectionName());
3939
}
@@ -43,7 +43,7 @@ public function getNamespace()
4343
*
4444
* @return string
4545
*/
46-
public function getUri()
46+
protected function getUri()
4747
{
4848
return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
4949
}

0 commit comments

Comments
 (0)