Skip to content

Commit 3b128d3

Browse files
committed
Merge pull request #70
2 parents cb5cf0b + fdaed29 commit 3b128d3

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/Operation/CreateCollection.php

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

1110
/**
1211
* Operation for the create command.
@@ -40,6 +39,9 @@ class CreateCollection implements Executable
4039
* bitwise combination USE_POWER_OF_2_SIZES and NO_PADDING. The default
4140
* is USE_POWER_OF_2_SIZES.
4241
*
42+
* * indexOptionDefaults (document): Default configuration for indexes when
43+
* creating the collection.
44+
*
4345
* * max (integer): The maximum number of documents allowed in the capped
4446
* collection. The size option takes precedence over this limit.
4547
*
@@ -70,6 +72,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
7072
throw new InvalidArgumentTypeException('"flags" option', $options['flags'], 'integer');
7173
}
7274

75+
if (isset($options['indexOptionDefaults']) && ! is_array($options['indexOptionDefaults']) && ! is_object($options['indexOptionDefaults'])) {
76+
throw new InvalidArgumentTypeException('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'array or object');
77+
}
78+
7379
if (isset($options['max']) && ! is_integer($options['max'])) {
7480
throw new InvalidArgumentTypeException('"max" option', $options['max'], 'integer');
7581
}
@@ -120,6 +126,10 @@ private function createCommand()
120126
}
121127
}
122128

129+
if ( ! empty($this->options['indexOptionDefaults'])) {
130+
$cmd['indexOptionDefaults'] = (object) $this->options['indexOptionDefaults'];
131+
}
132+
123133
if ( ! empty($this->options['storageEngine'])) {
124134
$cmd['storageEngine'] = (object) $this->options['storageEngine'];
125135
}
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)