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\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