Commit c6a2df9a authored by Katherine Walker's avatar Katherine Walker

Implement getCommandDocument for all findAndModify operations

parent cbb7fc7c
...@@ -80,11 +80,11 @@ class Explain implements Executable ...@@ -80,11 +80,11 @@ class Explain implements Executable
throw UnsupportedException::explainNotSupported(); throw UnsupportedException::explainNotSupported();
} }
if ($this->explainable instanceof \MongoDB\Operation\Distinct && ! \MongoDB\server_supports_feature($server, self::$wireVersionForDistinct)) { if ($this->explainable instanceof Distinct && ! \MongoDB\server_supports_feature($server, self::$wireVersionForDistinct)) {
throw UnsupportedException::explainNotSupported(); throw UnsupportedException::explainNotSupported();
} }
if ($this->explainable instanceof \MongoDB\Operation\FindAndModify && ! \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModify)) { if ($this->isFindAndModify($this->explainable) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModify)) {
throw UnsupportedException::explainNotSupported(); throw UnsupportedException::explainNotSupported();
} }
...@@ -102,4 +102,12 @@ class Explain implements Executable ...@@ -102,4 +102,12 @@ class Explain implements Executable
return current($cursor->toArray()); return current($cursor->toArray());
} }
private function isFindAndModify($explainable)
{
if ($explainable instanceof FindAndModify || $explainable instanceof FindOneAndDelete || $explainable instanceof FindOneAndReplace || $explainable instanceof FindOneAndUpdate) {
return true;
}
return false;
}
} }
...@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::findOneAndDelete() * @see \MongoDB\Collection::findOneAndDelete()
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/ */
class FindOneAndDelete implements Executable class FindOneAndDelete implements Executable, Explainable
{ {
private $findAndModify; private $findAndModify;
...@@ -105,4 +105,9 @@ class FindOneAndDelete implements Executable ...@@ -105,4 +105,9 @@ class FindOneAndDelete implements Executable
{ {
return $this->findAndModify->execute($server); return $this->findAndModify->execute($server);
} }
public function getCommandDocument()
{
return $this->findAndModify->getCommandDocument();
}
} }
...@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::findOneAndReplace() * @see \MongoDB\Collection::findOneAndReplace()
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/ */
class FindOneAndReplace implements Executable class FindOneAndReplace implements Executable, Explainable
{ {
const RETURN_DOCUMENT_BEFORE = 1; const RETURN_DOCUMENT_BEFORE = 1;
const RETURN_DOCUMENT_AFTER = 2; const RETURN_DOCUMENT_AFTER = 2;
...@@ -148,4 +148,9 @@ class FindOneAndReplace implements Executable ...@@ -148,4 +148,9 @@ class FindOneAndReplace implements Executable
{ {
return $this->findAndModify->execute($server); return $this->findAndModify->execute($server);
} }
public function getCommandDocument()
{
return $this->findAndModify->getCommandDocument();
}
} }
...@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException; ...@@ -29,7 +29,7 @@ use MongoDB\Exception\UnsupportedException;
* @see \MongoDB\Collection::findOneAndUpdate() * @see \MongoDB\Collection::findOneAndUpdate()
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/ * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/ */
class FindOneAndUpdate implements Executable class FindOneAndUpdate implements Executable, Explainable
{ {
const RETURN_DOCUMENT_BEFORE = 1; const RETURN_DOCUMENT_BEFORE = 1;
const RETURN_DOCUMENT_AFTER = 2; const RETURN_DOCUMENT_AFTER = 2;
...@@ -151,4 +151,9 @@ class FindOneAndUpdate implements Executable ...@@ -151,4 +151,9 @@ class FindOneAndUpdate implements Executable
{ {
return $this->findAndModify->execute($server); return $this->findAndModify->execute($server);
} }
public function getCommandDocument()
{
return $this->findAndModify->getCommandDocument();
}
} }
...@@ -4,7 +4,6 @@ namespace MongoDB\Tests\Operation; ...@@ -4,7 +4,6 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Operation\Count; use MongoDB\Operation\Count;
use MongoDB\Operation\CreateIndexes; use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\Explain;
use MongoDB\Operation\InsertMany; use MongoDB\Operation\InsertMany;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use stdClass; use stdClass;
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Operation\Distinct; use MongoDB\Operation\Distinct;
use MongoDB\Operation\Explain;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use stdClass; use stdClass;
......
...@@ -12,6 +12,9 @@ use MongoDB\Operation\Explain; ...@@ -12,6 +12,9 @@ use MongoDB\Operation\Explain;
use MongoDB\Operation\Find; use MongoDB\Operation\Find;
use MongoDB\Operation\FindAndModify; use MongoDB\Operation\FindAndModify;
use MongoDB\Operation\FindOne; use MongoDB\Operation\FindOne;
use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate;
use MongoDB\Operation\Update; use MongoDB\Operation\Update;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use stdClass; use stdClass;
...@@ -101,7 +104,7 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -101,7 +104,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
{ {
$this->createFixtures(3); $this->createFixtures(3);
$operation = new Find($this->getDatabaseName(), $this->getCollectionName(), [], ['readConcern' => $this->createDefaultReadConcern()]); $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), []);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]); $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer()); $result = $explainOperation->execute($this->getPrimaryServer());
...@@ -159,7 +162,7 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -159,7 +162,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
$this->getDatabaseName(), $this->getDatabaseName(),
$this->getCollectionName(), $this->getCollectionName(),
[], [],
['readConcern' => $this->createDefaultReadConcern(), 'modifiers' => ['$max' => ['_id' => 2.2]]] ['modifiers' => ['$orderby' => ['_id' => 1]]]
); );
(new CommandObserver)->observe( (new CommandObserver)->observe(
...@@ -168,7 +171,7 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -168,7 +171,7 @@ class ExplainFunctionalTest extends FunctionalTestCase
$explainOperation->execute($this->getPrimaryServer()); $explainOperation->execute($this->getPrimaryServer());
}, },
function(stdClass $command) { function(stdClass $command) {
$this->assertObjectHasAttribute('max', $command->explain); $this->assertObjectHasAttribute('sort', $command->explain);
$this->assertObjectNotHasAttribute('modifiers', $command->explain); $this->assertObjectNotHasAttribute('modifiers', $command->explain);
} }
); );
...@@ -190,6 +193,57 @@ class ExplainFunctionalTest extends FunctionalTestCase ...@@ -190,6 +193,57 @@ class ExplainFunctionalTest extends FunctionalTestCase
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected); $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
} }
/**
* @dataProvider provideVerbosityInformation
*/
public function testFindOneAndDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
$this->markTestSkipped('Explaining findOneAndDelete command requires server version >= 3.2');
}
$operation = new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), []);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/**
* @dataProvider provideVerbosityInformation
*/
public function testFindOneAndReplace($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
$this->markTestSkipped('Explaining findOneAndReplace command requires server version >= 3.2');
}
$operation = new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1.1], ['x' => 5]);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/**
* @dataProvider provideVerbosityInformation
*/
public function testFindOneAndUpdate($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
{
if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
$this->markTestSkipped('Explaining findOneAndUpdate command requires server version >= 3.2');
}
$operation = new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], ['$rename' => ['x' => 'y']]);
$explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
$result = $explainOperation->execute($this->getPrimaryServer());
$this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
}
/** /**
* @dataProvider provideVerbosityInformation * @dataProvider provideVerbosityInformation
*/ */
......
...@@ -9,13 +9,12 @@ use MongoDB\Operation\Explain; ...@@ -9,13 +9,12 @@ use MongoDB\Operation\Explain;
class ExplainTest extends TestCase class ExplainTest extends TestCase
{ {
/** /**
* @requires PHPUnit 5.4.0
* @expectedException MongoDB\Exception\InvalidArgumentException * @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorOptions * @dataProvider provideInvalidConstructorOptions
*/ */
public function testConstructorOptionTypeChecks(array $options) public function testConstructorOptionTypeChecks(array $options)
{ {
$explainable = $this->createMock('MongoDB\Operation\Explainable'); $explainable = $this->getMockBuilder('MongoDB\Operation\Explainable')->getMock();
new Explain($this->getDatabaseName(), $explainable, $options); new Explain($this->getDatabaseName(), $explainable, $options);
} }
......
...@@ -4,7 +4,6 @@ namespace MongoDB\Tests\Operation; ...@@ -4,7 +4,6 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite; use MongoDB\Driver\BulkWrite;
use MongoDB\Model\BSONDocument; use MongoDB\Model\BSONDocument;
use MongoDB\Operation\Explain;
use MongoDB\Operation\FindAndModify; use MongoDB\Operation\FindAndModify;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use stdClass; use stdClass;
......
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