Skip to content

Commit e458f4b

Browse files
committed
PHPLIB-121: Support indexOptionDefaults option for CreateCollection
1 parent cb5cf0b commit e458f4b

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Operation/CreateCollection.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
77
use MongoDB\Exception\InvalidArgumentException;
8-
use MongoDB\Exception\UnexpectedTypeException;
8+
use MongoDB\Exception\InvalidArgumentTypeException;
99
use MongoDB\Model\IndexInput;
1010

1111
/**
@@ -40,6 +40,9 @@ class CreateCollection implements Executable
4040
* bitwise combination USE_POWER_OF_2_SIZES and NO_PADDING. The default
4141
* is USE_POWER_OF_2_SIZES.
4242
*
43+
* * indexOptionDefaults (document): Default configuration for indexes when
44+
* creating the collection.
45+
*
4346
* * max (integer): The maximum number of documents allowed in the capped
4447
* collection. The size option takes precedence over this limit.
4548
*
@@ -70,6 +73,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
7073
throw new InvalidArgumentTypeException('"flags" option', $options['flags'], 'integer');
7174
}
7275

76+
if (isset($options['indexOptionDefaults']) && ! is_array($options['indexOptionDefaults']) && ! is_object($options['indexOptionDefaults'])) {
77+
throw new InvalidArgumentTypeException('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'array or object');
78+
}
79+
7380
if (isset($options['max']) && ! is_integer($options['max'])) {
7481
throw new InvalidArgumentTypeException('"max" option', $options['max'], 'integer');
7582
}
@@ -120,6 +127,10 @@ private function createCommand()
120127
}
121128
}
122129

130+
if ( ! empty($this->options['indexOptionDefaults'])) {
131+
$cmd['indexOptionDefaults'] = (object) $this->options['indexOptionDefaults'];
132+
}
133+
123134
if ( ! empty($this->options['storageEngine'])) {
124135
$cmd['storageEngine'] = (object) $this->options['storageEngine'];
125136
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\CreateCollection;
6+
7+
class CreateCollectionTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @dataProvider provideInvalidConstructorOptions
12+
*/
13+
public function testConstructorOptionTypeChecks(array $options)
14+
{
15+
new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
16+
}
17+
18+
public function provideInvalidConstructorOptions()
19+
{
20+
$options = [];
21+
22+
foreach ($this->getInvalidBooleanValues() as $value) {
23+
$options[][] = ['autoIndexId' => $value];
24+
}
25+
26+
foreach ($this->getInvalidBooleanValues() as $value) {
27+
$options[][] = ['capped' => $value];
28+
}
29+
30+
foreach ($this->getInvalidIntegerValues() as $value) {
31+
$options[][] = ['flags' => $value];
32+
}
33+
34+
foreach ($this->getInvalidDocumentValues() as $value) {
35+
$options[][] = ['indexOptionDefaults' => $value];
36+
}
37+
38+
foreach ($this->getInvalidIntegerValues() as $value) {
39+
$options[][] = ['max' => $value];
40+
}
41+
42+
foreach ($this->getInvalidIntegerValues() as $value) {
43+
$options[][] = ['maxTimeMS' => $value];
44+
}
45+
46+
foreach ($this->getInvalidIntegerValues() as $value) {
47+
$options[][] = ['size' => $value];
48+
}
49+
50+
foreach ($this->getInvalidDocumentValues() as $value) {
51+
$options[][] = ['storageEngine' => $value];
52+
}
53+
54+
return $options;
55+
}
56+
}

0 commit comments

Comments
 (0)