Skip to content

PHPLIB-121: Support indexOptionDefaults option for CreateCollection #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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'];
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Operation/CreateCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\CreateCollection;

class CreateCollectionTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new CreateCollection($this->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;
}
}