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
$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()
{
$this->createFixtures(5);
......
<?php
namespace MongoDB\Tests\Collection;
namespace MongoDB\Tests\Operation;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\ListIndexes;
use InvalidArgumentException;
/**
* Functional tests for index management methods.
*
* @see https://github.com/mongodb/specifications/blob/master/source/index-management.rst
*/
class IndexManagementFunctionalTest extends FunctionalTestCase
class CreateIndexesFunctionalTest 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->assertIndexExists('x_1', function(IndexInfo $info) use ($that) {
$that->assertTrue($info->isSparse());
$that->assertTrue($info->isUnique());
$that->assertFalse($info->isTtl());
$this->assertSame('x_1', $createdIndexNames[0]);
$this->assertIndexExists('x_1', function(IndexInfo $info) {
$this->assertTrue($info->isSparse());
$this->assertTrue($info->isUnique());
$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->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) {
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique());
$that->assertFalse($info->isTtl());
$this->assertSame('y_-1_z_1', $createdIndexNames[0]);
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) {
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isUnique());
$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]));
$this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) {
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique());
$that->assertFalse($info->isTtl());
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('g_2dsphere_z_1', $createdIndexNames[0]);
$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']));
$this->assertIndexExists('my_ttl', function(IndexInfo $info) use ($that) {
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique());
$that->assertTrue($info->isTtl());
public function testCreateTTLIndex()
{
$indexes = [['key' => ['t' => 1], 'expireAfterSeconds' => 0, 'name' => 'my_ttl']];
$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()
{
$that = $this;
$expectedNames = ['x_1', 'y_-1_z_1', 'g_2dsphere_z_1', 'my_ttl'];
$indexes = [
......@@ -58,103 +83,78 @@ class IndexManagementFunctionalTest extends FunctionalTestCase
['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) {
$that->assertTrue($info->isSparse());
$that->assertTrue($info->isUnique());
$that->assertFalse($info->isTtl());
$this->assertIndexExists('x_1', function(IndexInfo $info) {
$this->assertTrue($info->isSparse());
$this->assertTrue($info->isUnique());
$this->assertFalse($info->isTtl());
});
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) {
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique());
$that->assertFalse($info->isTtl());
$this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) {
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isUnique());
$this->assertFalse($info->isTtl());
});
$this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) {
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique());
$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->assertIndexExists('my_ttl', function(IndexInfo $info) use ($that) {
$that->assertFalse($info->isSparse());
$that->assertFalse($info->isUnique());
$that->assertTrue($info->isTtl());
$this->assertIndexExists('my_ttl', function(IndexInfo $info) {
$this->assertFalse($info->isSparse());
$this->assertFalse($info->isUnique());
$this->assertTrue($info->isTtl());
});
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedException MongoDB\Driver\Exception\RuntimeException
*/
public function testCreateIndexesRequiresAtLeastOneIndex()
public function testCreateConflictingIndexesWithCommand()
{
$this->assertSame([], $this->collection->createIndexes([]));
}
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');
}
if ( ! \MongoDB\server_supports_feature($this->getPrimaryServer(), self::$wireVersionForCommand)) {
$this->markTestSkipped('createIndexes command is not supported');
}
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public function testDropIndexShouldNotAllowEmptyIndexName()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$this->assertIndexExists('x_1');
$this->collection->dropIndex('');
}
$indexes = [
['key' => ['x' => 1], 'sparse' => true, 'unique' => false],
['key' => ['x' => 1], 'sparse' => false, 'unique' => true],
];
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public function testDropIndexShouldNotAllowWildcardCharacter()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$this->assertIndexExists('x_1');
$this->collection->dropIndex('*');
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
}
public function testDropIndexes()
public function testCreateConflictingIndexesWithLegacyInsert()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$this->assertSame('y_1', $this->collection->createIndex(['y' => 1]));
$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');
}
if (\MongoDB\server_supports_feature($this->getPrimaryServer(), self::$wireVersionForCommand)) {
$this->markTestSkipped('Index creation does not use legacy insertion');
}
}
public function testListIndexes()
{
$this->assertSame('x_1', $this->collection->createIndex(['x' => 1]));
$indexes = $this->collection->listIndexes();
$this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes);
$indexes = [
['key' => ['x' => 1], 'sparse' => true, 'unique' => false],
['key' => ['x' => 1], 'sparse' => false, 'unique' => true],
];
foreach ($indexes as $index) {
$this->assertInstanceOf('MongoDB\Model\IndexInfo', $index);
}
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
$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
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;
......
<?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 @@
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\Server;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListIndexes;
......@@ -11,18 +10,20 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
{
public function testListIndexesForNewlyCreatedCollection()
{
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);
$operation->execute($this->getPrimaryServer());
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
$writeResult = $insertOne->execute($server);
$writeResult = $insertOne->execute($this->getPrimaryServer());
$this->assertEquals(1, $writeResult->getInsertedCount());
$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
$indexes = iterator_to_array($operation->execute($server));
$indexes = iterator_to_array($indexes);
$this->assertCount(1, $indexes);
......@@ -34,13 +35,11 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
public function testListIndexesForNonexistentCollection()
{
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);
$operation->execute($this->getPrimaryServer());
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($server);
$indexes = $operation->execute($this->getPrimaryServer());
$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