Commit 4dc36f62 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #399

parents de1f5fb2 236657b5
......@@ -5,10 +5,6 @@ sudo: false
services:
- mongodb
env:
global:
- DRIVER_VERSION=stable SERVER_VERSION=3.4
addons:
apt:
packages: &common_packages
......@@ -18,7 +14,7 @@ matrix:
fast_finish: true
include:
- php: 5.5
env: &common_env DRIVER_VERSION=stable SERVER_VERSION=3.4
env: &common_env DRIVER_VERSION=1.3.0RC1 SERVER_VERSION=3.4
addons: &common_addons
apt:
sources: [ mongodb-3.4-precise ]
......@@ -36,25 +32,25 @@ matrix:
env: *common_env
addons: *common_addons
- php: 7.0
env: DRIVER_VERSION=1.2.0 SERVER_VERSION=2.4
env: DRIVER_VERSION=1.3.0RC1 SERVER_VERSION=2.4
addons:
apt:
sources: [ mongodb-upstart ]
packages: [ mongodb-10gen, *common_packages ]
- php: 7.0
env: DRIVER_VERSION=1.2.0 SERVER_VERSION=2.6
env: DRIVER_VERSION=1.3.0RC1 SERVER_VERSION=2.6
addons:
apt:
sources: [ mongodb-upstart ]
packages: [ mongodb-org, *common_packages ]
- php: 7.0
env: DRIVER_VERSION=1.2.0 SERVER_VERSION=3.0
env: DRIVER_VERSION=1.3.0RC1 SERVER_VERSION=3.0
addons:
apt:
sources: [ mongodb-3.0-precise ]
packages: [ mongodb-org, *common_packages ]
- php: 7.0
env: DRIVER_VERSION=1.2.0 SERVER_VERSION=3.2
env: DRIVER_VERSION=1.3.0RC1 SERVER_VERSION=3.2
addons:
apt:
sources: [ mongodb-3.2-precise ]
......
......@@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.4",
"ext-mongodb": "^1.2.0"
"ext-mongodb": "^1.3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
......
......@@ -182,6 +182,14 @@ class Aggregate implements Executable
throw new InvalidArgumentException('"typeMap" option should not be used if "useCursor" is false');
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->pipeline = $pipeline;
......
......@@ -266,6 +266,10 @@ class BulkWrite implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->operations = $operations;
......
......@@ -117,6 +117,10 @@ class Count implements Executable
throw InvalidArgumentException::invalidType('"skip" option', $options['skip'], 'integer');
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->filter = $filter;
......
......@@ -157,6 +157,10 @@ class CreateCollection implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->options = $options;
......
......@@ -96,6 +96,10 @@ class CreateIndexes implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->options = $options;
......
......@@ -83,6 +83,10 @@ class Delete implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->filter = $filter;
......
......@@ -93,6 +93,10 @@ class Distinct implements Executable
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->fieldName = (string) $fieldName;
......
......@@ -69,6 +69,10 @@ class DropCollection implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->options = $options;
......
......@@ -66,6 +66,10 @@ class DropDatabase implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->options = $options;
}
......
......@@ -75,6 +75,10 @@ class DropIndexes implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->indexName = $indexName;
......
......@@ -190,6 +190,10 @@ class Find implements Executable
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->filter = $filter;
......
......@@ -157,6 +157,10 @@ class FindAndModify implements Executable
throw new InvalidArgumentException('The "remove" option must be true or an "update" document must be specified, but not both');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->options = $options;
......
......@@ -97,6 +97,10 @@ class InsertMany implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->documents = $documents;
......
......@@ -73,6 +73,10 @@ class InsertOne implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->document = $document;
......
......@@ -203,6 +203,14 @@ class MapReduce implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
unset($options['readConcern']);
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->map = $map;
......
......@@ -117,6 +117,10 @@ class Update implements Executable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
unset($options['writeConcern']);
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->filter = $filter;
......
<?php
namespace MongoDB\Tests;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
/**
* Observes command documents using the driver's monitoring API.
*/
class CommandObserver implements CommandSubscriber
{
private $commands = [];
public function observe(callable $execution, callable $commandCallback)
{
$this->commands = [];
\MongoDB\Driver\Monitoring\addSubscriber($this);
call_user_func($execution);
\MongoDB\Driver\Monitoring\removeSubscriber($this);
foreach ($this->commands as $command) {
call_user_func($commandCallback, $command);
}
}
public function commandStarted(CommandStartedEvent $event)
{
$this->commands[] = $event->getCommand();
}
public function commandSucceeded(CommandSucceededEvent $event)
{
}
public function commandFailed(CommandFailedEvent $event)
{
}
}
......@@ -4,11 +4,55 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite;
use MongoDB\Operation\Aggregate;
use MongoDB\Tests\CommandObserver;
use stdClass;
class AggregateFunctionalTest extends FunctionalTestCase
{
private static $wireVersionForCursor = 2;
public function testDefaultReadConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new Aggregate(
$this->getDatabaseName(),
$this->getCollectionName(),
[['$match' => ['x' => 1]]],
['readConcern' => $this->createDefaultReadConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('readConcern', $command);
}
);
}
public function testDefaultWriteConcernIsOmitted()
{
if (version_compare($this->getServerVersion(), '2.6.0', '<')) {
$this->markTestSkipped('$out pipeline operator is not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new Aggregate(
$this->getDatabaseName(),
$this->getCollectionName(),
[['$out' => $this->getCollectionName() . '.output']],
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
/**
* @expectedException MongoDB\Driver\Exception\RuntimeException
*/
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\Count;
use MongoDB\Tests\CommandObserver;
use stdClass;
class CountFunctionalTest extends FunctionalTestCase
{
public function testDefaultReadConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new Count(
$this->getDatabaseName(),
$this->getCollectionName(),
[],
['readConcern' => $this->createDefaultReadConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('readConcern', $command);
}
);
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\CreateCollection;
use MongoDB\Tests\CommandObserver;
use stdClass;
class CreateCollectionFunctionalTest extends FunctionalTestCase
{
public function testDefaultWriteConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new CreateCollection(
$this->getDatabaseName(),
$this->getCollectionName(),
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
}
......@@ -6,7 +6,9 @@ use MongoDB\Model\IndexInfo;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\ListIndexes;
use MongoDB\Tests\CommandObserver;
use InvalidArgumentException;
use stdClass;
class CreateIndexesFunctionalTest extends FunctionalTestCase
{
......@@ -157,6 +159,32 @@ class CreateIndexesFunctionalTest extends FunctionalTestCase
});
}
public function testDefaultWriteConcernIsOmitted()
{
/* Earlier server versions do not support the createIndexes command. Per
* the Index Management specification, inserts on system.indexes must
* use the write concern {w:1}. */
if (version_compare($this->getServerVersion(), '2.6.0', '<')) {
$this->markTestSkipped('createIndexes command is not supported');
}
(new CommandObserver)->observe(
function() {
$operation = new CreateIndexes(
$this->getDatabaseName(),
$this->getCollectionName(),
[['key' => ['x' => 1]]],
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
/**
* Asserts that an index with the given name exists for the collection.
*
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\Distinct;
use MongoDB\Tests\CommandObserver;
use stdClass;
class DistinctFunctionalTest extends FunctionalTestCase
{
public function testDefaultReadConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new Distinct(
$this->getDatabaseName(),
$this->getCollectionName(),
'x',
[],
['readConcern' => $this->createDefaultReadConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('readConcern', $command);
}
);
}
}
......@@ -5,9 +5,29 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListCollections;
use MongoDB\Tests\CommandObserver;
use stdClass;
class DropCollectionFunctionalTest extends FunctionalTestCase
{
public function testDefaultWriteConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new DropCollection(
$this->getDatabaseName(),
$this->getCollectionName(),
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
public function testDropExistingCollection()
{
$server = $this->getPrimaryServer();
......
......@@ -6,9 +6,28 @@ use MongoDB\Driver\Server;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\InsertOne;
use MongoDB\Operation\ListDatabases;
use MongoDB\Tests\CommandObserver;
use stdClass;
class DropDatabaseFunctionalTest extends FunctionalTestCase
{
public function testDefaultWriteConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new DropDatabase(
$this->getDatabaseName(),
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
public function testDropExistingDatabase()
{
$server = $this->getPrimaryServer();
......
......@@ -5,10 +5,34 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\ListIndexes;
use MongoDB\Tests\CommandObserver;
use InvalidArgumentException;
use stdClass;
class DropIndexesFunctionalTest extends FunctionalTestCase
{
public function testDefaultWriteConcernIsOmitted()
{
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [['key' => ['x' => 1]]]);
$operation->execute($this->getPrimaryServer());
(new CommandObserver)->observe(
function() {
$operation = new DropIndexes(
$this->getDatabaseName(),
$this->getCollectionName(),
'x_1',
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
public function testDropOneIndexByName()
{
$indexes = [['key' => ['x' => 1]]];
......
......@@ -5,9 +5,29 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\FindAndModify;
use MongoDB\Tests\CommandObserver;
use stdClass;
class FindAndModifyFunctionalTest extends FunctionalTestCase
{
public function testDefaultWriteConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new FindAndModify(
$this->getDatabaseName(),
$this->getCollectionName(),
['remove' => true, 'writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocument
*/
......
......@@ -4,9 +4,30 @@ namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite;
use MongoDB\Operation\Find;
use MongoDB\Tests\CommandObserver;
use stdClass;
class FindFunctionalTest extends FunctionalTestCase
{
public function testDefaultReadConcernIsOmitted()
{
(new CommandObserver)->observe(
function() {
$operation = new Find(
$this->getDatabaseName(),
$this->getCollectionName(),
[],
['readConcern' => $this->createDefaultReadConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('readConcern', $command);
}
);
}
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
......
......@@ -3,7 +3,9 @@
namespace MongoDB\Tests\Operation;
use MongoDB\Collection;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\DropCollection;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
......@@ -29,4 +31,14 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
}
protected function createDefaultReadConcern()
{
return new ReadConcern;
}
protected function createDefaultWriteConcern()
{
return new WriteConcern(-2);
}
}
......@@ -4,12 +4,66 @@ namespace MongoDB\Tests\Operation;
use MongoDB\BSON\Javascript;
use MongoDB\Driver\BulkWrite;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\Find;
use MongoDB\Operation\MapReduce;
use MongoDB\Tests\CommandObserver;
use stdClass;
class MapReduceFunctionalTest extends FunctionalTestCase
{
public function testDefaultReadConcernIsOmitted()
{
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
(new CommandObserver)->observe(
function() {
$operation = new MapReduce(
$this->getDatabaseName(),
$this->getCollectionName(),
new Javascript('function() { emit(this.x, this.y); }'),
new Javascript('function(key, values) { return Array.sum(values); }'),
['inline' => 1],
['readConcern' => $this->createDefaultReadConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('readConcern', $command);
}
);
}
public function testDefaultWriteConcernIsOmitted()
{
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
(new CommandObserver)->observe(
function() {
$operation = new MapReduce(
$this->getDatabaseName(),
$this->getCollectionName(),
new Javascript('function() { emit(this.x, this.y); }'),
new Javascript('function(key, values) { return Array.sum(values); }'),
$this->getCollectionName() . '.output',
['writeConcern' => $this->createDefaultWriteConcern()]
);
$operation->execute($this->getPrimaryServer());
},
function(stdClass $command) {
$this->assertObjectNotHasAttribute('writeConcern', $command);
}
);
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName() . '.output');
$operation->execute($this->getPrimaryServer());
}
public function testResult()
{
$this->createFixtures(3);
......
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