diff --git a/src/Operation/CreateCollection.php b/src/Operation/CreateCollection.php index c18f947d9..40a665c87 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -5,8 +5,7 @@ use MongoDB\Driver\Command; use MongoDB\Driver\Server; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\UnexpectedTypeException; -use MongoDB\Model\IndexInput; +use MongoDB\Exception\InvalidArgumentTypeException; /** * Operation for the create command. @@ -40,6 +39,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 +72,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 +126,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; + } +}