Commit 85bef166 authored by Jeremy Mikola's avatar Jeremy Mikola

Split IndexManagementFunctionalTest into Operation tests

parent ce26ec3c
...@@ -61,6 +61,15 @@ class CollectionFunctionalTest extends FunctionalTestCase ...@@ -61,6 +61,15 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this->assertCollectionCount($this->getNamespace(), 0); $this->assertCollectionCount($this->getNamespace(), 0);
} }
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @todo Move this to a unit test once Manager can be mocked
*/
public function testDropIndexShouldNotAllowWildcardCharacter()
{
$this->collection->dropIndex('*');
}
public function testFindOne() public function testFindOne()
{ {
$this->createFixtures(5); $this->createFixtures(5);
......
<?php <?php
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Operation;
use MongoDB\Model\IndexInfo; use MongoDB\Model\IndexInfo;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\ListIndexes;
use InvalidArgumentException; use InvalidArgumentException;
/** class CreateIndexesFunctionalTest extends FunctionalTestCase
* Functional tests for index management methods.
*
* @see https://github.com/mongodb/specifications/blob/master/source/index-management.rst
*/
class IndexManagementFunctionalTest extends FunctionalTestCase
{ {
public function testCreateIndex() private static $wireVersionForCommand = 2;
public function testCreateSparseUniqueIndex()
{ {
$that = $this; $indexes = [['key' => ['x' => 1], 'sparse' => true, 'unique' => true]];
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1], ['sparse' => true, 'unique' => true])); $this->assertSame('x_1', $createdIndexNames[0]);
$this->assertIndexExists('x_1', function(IndexInfo $info) use ($that) { $this->assertIndexExists('x_1', function(IndexInfo $info) {
$that->assertTrue($info->isSparse()); $this->assertTrue($info->isSparse());
$that->assertTrue($info->isUnique()); $this->assertTrue($info->isUnique());
$that->assertFalse($info->isTtl()); $this->assertFalse($info->isTtl());
}); });
}
public function testCreateCompoundIndex()
{
$indexes = [['key' => ['y' => -1, 'z' => 1]]];
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('y_-1_z_1', $this->collection->createIndex(['y' => -1, 'z' => 1])); $this->assertSame('y_-1_z_1', $createdIndexNames[0]);
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) { $this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) {
$that->assertFalse($info->isSparse()); $this->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique()); $this->assertFalse($info->isUnique());
$that->assertFalse($info->isTtl()); $this->assertFalse($info->isTtl());
}); });
}
public function testCreateGeospatialIndex()
{
$indexes = [['key' => ['g' => '2dsphere', 'z' => 1]]];
$this->assertSame('g_2dsphere_z_1', $this->collection->createIndex(['g' => '2dsphere', 'z' => 1])); $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) { $createdIndexNames = $operation->execute($this->getPrimaryServer());
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique()); $this->assertSame('g_2dsphere_z_1', $createdIndexNames[0]);
$that->assertFalse($info->isTtl()); $this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) {
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isUnique());
$this->assertFalse($info->isTtl());
}); });
}
$this->assertSame('my_ttl', $this->collection->createIndex(['t' => 1], ['expireAfterSeconds' => 0, 'name' => 'my_ttl'])); public function testCreateTTLIndex()
$this->assertIndexExists('my_ttl', function(IndexInfo $info) use ($that) { {
$that->assertFalse($info->isSparse()); $indexes = [['key' => ['t' => 1], 'expireAfterSeconds' => 0, 'name' => 'my_ttl']];
$that->assertFalse($info->isUnique());
$that->assertTrue($info->isTtl()); $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('my_ttl', $createdIndexNames[0]);
$this->assertIndexExists('my_ttl', function(IndexInfo $info) {
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isUnique());
$this->assertTrue($info->isTtl());
}); });
} }
public function testCreateIndexes() public function testCreateIndexes()
{ {
$that = $this;
$expectedNames = ['x_1', 'y_-1_z_1', 'g_2dsphere_z_1', 'my_ttl']; $expectedNames = ['x_1', 'y_-1_z_1', 'g_2dsphere_z_1', 'my_ttl'];
$indexes = [ $indexes = [
...@@ -58,103 +83,78 @@ class IndexManagementFunctionalTest extends FunctionalTestCase ...@@ -58,103 +83,78 @@ class IndexManagementFunctionalTest extends FunctionalTestCase
['key' => ['t' => 1], 'expireAfterSeconds' => 0, 'name' => 'my_ttl'], ['key' => ['t' => 1], 'expireAfterSeconds' => 0, 'name' => 'my_ttl'],
]; ];
$this->assertSame($expectedNames, $this->collection->createIndexes($indexes)); $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame($expectedNames, $createdIndexNames);
$this->assertIndexExists('x_1', function(IndexInfo $info) use ($that) { $this->assertIndexExists('x_1', function(IndexInfo $info) {
$that->assertTrue($info->isSparse()); $this->assertTrue($info->isSparse());
$that->assertTrue($info->isUnique()); $this->assertTrue($info->isUnique());
$that->assertFalse($info->isTtl()); $this->assertFalse($info->isTtl());
}); });
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) { $this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) {
$that->assertFalse($info->isSparse()); $this->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique()); $this->assertFalse($info->isUnique());
$that->assertFalse($info->isTtl()); $this->assertFalse($info->isTtl());
}); });
$this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) { $this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) {
$that->assertFalse($info->isSparse()); $this->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique()); $this->assertFalse($info->isUnique());
$that->assertFalse($info->isTtl()); $this->assertFalse($info->isTtl());
}); });
$this->assertIndexExists('my_ttl', function(IndexInfo $info) use ($that) { $this->assertIndexExists('my_ttl', function(IndexInfo $info) {
$that->assertFalse($info->isSparse()); $this->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique()); $this->assertFalse($info->isUnique());
$that->assertTrue($info->isTtl()); $this->assertTrue($info->isTtl());
}); });
} }
/** /**
* @expectedException MongoDB\Exception\InvalidArgumentException * @expectedException MongoDB\Driver\Exception\RuntimeException
*/ */
public function testCreateIndexesRequiresAtLeastOneIndex() public function testCreateConflictingIndexesWithCommand()
{ {
$this->assertSame([], $this->collection->createIndexes([])); if ( ! \MongoDB\server_supports_feature($this->getPrimaryServer(), self::$wireVersionForCommand)) {
} $this->markTestSkipped('createIndexes command is not supported');
public function testDropIndex()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$this->assertIndexExists('x_1');
$this->assertCommandSucceeded($this->collection->dropIndex('x_1'));
foreach ($this->collection->listIndexes() as $index) {
if ($index->getName() === 'x_1') {
$this->fail('The "x_1" index should have been deleted');
}
} }
}
/** $indexes = [
* @expectedException MongoDB\Exception\InvalidArgumentException ['key' => ['x' => 1], 'sparse' => true, 'unique' => false],
*/ ['key' => ['x' => 1], 'sparse' => false, 'unique' => true],
public function testDropIndexShouldNotAllowEmptyIndexName() ];
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$this->assertIndexExists('x_1');
$this->collection->dropIndex('');
}
/** $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
* @expectedException MongoDB\Exception\InvalidArgumentException $createdIndexNames = $operation->execute($this->getPrimaryServer());
*/
public function testDropIndexShouldNotAllowWildcardCharacter()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$this->assertIndexExists('x_1');
$this->collection->dropIndex('*');
} }
public function testDropIndexes() public function testCreateConflictingIndexesWithLegacyInsert()
{ {
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1])); if (\MongoDB\server_supports_feature($this->getPrimaryServer(), self::$wireVersionForCommand)) {
$this->assertSame('y_1', $this->collection->createIndex(['y' => 1])); $this->markTestSkipped('Index creation does not use legacy insertion');
$this->assertIndexExists('x_1');
$this->assertIndexExists('y_1');
$this->assertCommandSucceeded($this->collection->dropIndexes());
foreach ($this->collection->listIndexes() as $index) {
if ($index->getName() === 'x_1') {
$this->fail('The "x_1" index should have been deleted');
}
if ($index->getName() === 'y_1') {
$this->fail('The "y_1" index should have been deleted');
}
} }
}
public function testListIndexes()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$indexes = $this->collection->listIndexes(); $indexes = [
$this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes); ['key' => ['x' => 1], 'sparse' => true, 'unique' => false],
['key' => ['x' => 1], 'sparse' => false, 'unique' => true],
];
foreach ($indexes as $index) { $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$this->assertInstanceOf('MongoDB\Model\IndexInfo', $index); $createdIndexNames = $operation->execute($this->getPrimaryServer());
}
/* When creating indexes with legacy insert operations, the server
* ignores conflicting index specifications and leaves the original
* index in place.
*/
$this->assertSame('x_1', $createdIndexNames[0]);
$this->assertIndexExists('x_1', function(IndexInfo $info) {
$this->assertTrue($info->isSparse());
$this->assertFalse($info->isUnique());
$this->assertFalse($info->isTtl());
});
} }
/** /**
...@@ -173,7 +173,8 @@ class IndexManagementFunctionalTest extends FunctionalTestCase ...@@ -173,7 +173,8 @@ class IndexManagementFunctionalTest extends FunctionalTestCase
throw new InvalidArgumentException('$callback is not a callable'); throw new InvalidArgumentException('$callback is not a callable');
} }
$indexes = $this->collection->listIndexes(); $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($this->getPrimaryServer());
$foundIndex = null; $foundIndex = null;
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\CreateIndexes;
class CreateIndexesTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public function testCreateIndexesRequiresAtLeastOneIndex()
{
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), []);
}
/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @dataProvider provideInvalidIndexSpecificationTypes
*/
public function testCreateIndexesRequiresIndexSpecificationsToBeAnArray($index)
{
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [$index]);
}
public function provideInvalidIndexSpecificationTypes()
{
return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\ListIndexes;
use InvalidArgumentException;
class DropIndexesFunctionalTest extends FunctionalTestCase
{
public function testDropOneIndexByName()
{
$indexes = [['key' => ['x' => 1]]];
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('x_1', $createdIndexNames[0]);
$this->assertIndexExists('x_1');
$operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), 'x_1');
$this->assertCommandSucceeded($operation->execute($this->getPrimaryServer()));
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($this->getPrimaryServer());
foreach ($indexes as $index) {
if ($index->getName() === 'x_1') {
$this->fail('The "x_1" index should have been deleted');
}
}
}
public function testDropAllIndexesByWildcard()
{
$indexes = [
['key' => ['x' => 1]],
['key' => ['y' => 1]],
];
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('x_1', $createdIndexNames[0]);
$this->assertSame('y_1', $createdIndexNames[1]);
$this->assertIndexExists('x_1');
$this->assertIndexExists('y_1');
$operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), '*');
$this->assertCommandSucceeded($operation->execute($this->getPrimaryServer()));
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($this->getPrimaryServer());
foreach ($indexes as $index) {
if ($index->getName() === 'x_1') {
$this->fail('The "x_1" index should have been deleted');
}
if ($index->getName() === 'y_1') {
$this->fail('The "y_1" index should have been deleted');
}
}
}
/**
* Asserts that an index with the given name exists for the collection.
*
* An optional $callback may be provided, which should take an IndexInfo
* argument as its first and only parameter. If an IndexInfo matching the
* given name is found, it will be passed to the callback, which may perform
* additional assertions.
*
* @param callable $callback
*/
private function assertIndexExists($indexName, $callback = null)
{
if ($callback !== null && ! is_callable($callback)) {
throw new InvalidArgumentException('$callback is not a callable');
}
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($this->getPrimaryServer());
$foundIndex = null;
foreach ($indexes as $index) {
if ($index->getName() === $indexName) {
$foundIndex = $index;
break;
}
}
$this->assertNotNull($foundIndex, sprintf('Found %s index for the collection', $indexName));
if ($callback !== null) {
call_user_func($callback, $foundIndex);
}
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\DropIndexes;
class DropIndexesTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public function testDropIndexShouldNotAllowEmptyIndexName()
{
new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), '');
}
}
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Driver\Server;
use MongoDB\Operation\DropCollection; use MongoDB\Operation\DropCollection;
use MongoDB\Operation\InsertOne; use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListIndexes; use MongoDB\Operation\ListIndexes;
...@@ -11,18 +10,20 @@ class ListIndexesFunctionalTest extends FunctionalTestCase ...@@ -11,18 +10,20 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
{ {
public function testListIndexesForNewlyCreatedCollection() public function testListIndexesForNewlyCreatedCollection()
{ {
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName()); $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server); $operation->execute($this->getPrimaryServer());
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]); $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
$writeResult = $insertOne->execute($server); $writeResult = $insertOne->execute($this->getPrimaryServer());
$this->assertEquals(1, $writeResult->getInsertedCount()); $this->assertEquals(1, $writeResult->getInsertedCount());
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName()); $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($this->getPrimaryServer());
$this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes);
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor // Convert the CursorInfoIterator to an array since we cannot rewind its cursor
$indexes = iterator_to_array($operation->execute($server)); $indexes = iterator_to_array($indexes);
$this->assertCount(1, $indexes); $this->assertCount(1, $indexes);
...@@ -34,13 +35,11 @@ class ListIndexesFunctionalTest extends FunctionalTestCase ...@@ -34,13 +35,11 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
public function testListIndexesForNonexistentCollection() public function testListIndexesForNonexistentCollection()
{ {
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName()); $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server); $operation->execute($this->getPrimaryServer());
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName()); $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($server); $indexes = $operation->execute($this->getPrimaryServer());
$this->assertCount(0, $indexes); $this->assertCount(0, $indexes);
} }
......
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