From e458f4b1a4f79078e748cada4f5e9c4968f12b18 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 23 Dec 2015 13:44:23 -0500 Subject: [PATCH 1/2] PHPLIB-121: Support indexOptionDefaults option for CreateCollection --- src/Operation/CreateCollection.php | 13 +++++- tests/Operation/CreateCollectionTest.php | 56 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/Operation/CreateCollectionTest.php diff --git a/src/Operation/CreateCollection.php b/src/Operation/CreateCollection.php index c18f947d9..3f3ba8c5c 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -5,7 +5,7 @@ use MongoDB\Driver\Command; use MongoDB\Driver\Server; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\UnexpectedTypeException; +use MongoDB\Exception\InvalidArgumentTypeException; use MongoDB\Model\IndexInput; /** @@ -40,6 +40,9 @@ class CreateCollection implements Executable * bitwise combination USE_POWER_OF_2_SIZES and NO_PADDING. The default * is USE_POWER_OF_2_SIZES. * + * * indexOptionDefaults (document): Default configuration for indexes when + * creating the collection. + * * * max (integer): The maximum number of documents allowed in the capped * collection. The size option takes precedence over this limit. * @@ -70,6 +73,10 @@ public function __construct($databaseName, $collectionName, array $options = []) throw new InvalidArgumentTypeException('"flags" option', $options['flags'], 'integer'); } + if (isset($options['indexOptionDefaults']) && ! is_array($options['indexOptionDefaults']) && ! is_object($options['indexOptionDefaults'])) { + throw new InvalidArgumentTypeException('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'array or object'); + } + if (isset($options['max']) && ! is_integer($options['max'])) { throw new InvalidArgumentTypeException('"max" option', $options['max'], 'integer'); } @@ -120,6 +127,10 @@ private function createCommand() } } + if ( ! empty($this->options['indexOptionDefaults'])) { + $cmd['indexOptionDefaults'] = (object) $this->options['indexOptionDefaults']; + } + if ( ! empty($this->options['storageEngine'])) { $cmd['storageEngine'] = (object) $this->options['storageEngine']; } diff --git a/tests/Operation/CreateCollectionTest.php b/tests/Operation/CreateCollectionTest.php new file mode 100644 index 000000000..55ee12858 --- /dev/null +++ b/tests/Operation/CreateCollectionTest.php @@ -0,0 +1,56 @@ +getDatabaseName(), $this->getCollectionName(), $options); + } + + public function provideInvalidConstructorOptions() + { + $options = []; + + foreach ($this->getInvalidBooleanValues() as $value) { + $options[][] = ['autoIndexId' => $value]; + } + + foreach ($this->getInvalidBooleanValues() as $value) { + $options[][] = ['capped' => $value]; + } + + foreach ($this->getInvalidIntegerValues() as $value) { + $options[][] = ['flags' => $value]; + } + + foreach ($this->getInvalidDocumentValues() as $value) { + $options[][] = ['indexOptionDefaults' => $value]; + } + + foreach ($this->getInvalidIntegerValues() as $value) { + $options[][] = ['max' => $value]; + } + + foreach ($this->getInvalidIntegerValues() as $value) { + $options[][] = ['maxTimeMS' => $value]; + } + + foreach ($this->getInvalidIntegerValues() as $value) { + $options[][] = ['size' => $value]; + } + + foreach ($this->getInvalidDocumentValues() as $value) { + $options[][] = ['storageEngine' => $value]; + } + + return $options; + } +} From fdaed29ca808c2e890351526717eef747ea859cd Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 23 Dec 2015 13:46:43 -0500 Subject: [PATCH 2/2] Remove unnecessary use statement --- src/Operation/CreateCollection.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Operation/CreateCollection.php b/src/Operation/CreateCollection.php index 3f3ba8c5c..40a665c87 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -6,7 +6,6 @@ use MongoDB\Driver\Server; use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentTypeException; -use MongoDB\Model\IndexInput; /** * Operation for the create command.