From 7239ab74872b17dfa587d7c13bd9889dcc9554c6 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 23 Dec 2015 14:16:43 -0500 Subject: [PATCH 1/2] Use InvalidArgumentTypeException for index option validation --- src/Model/IndexInput.php | 12 ++++++------ src/Operation/CreateIndexes.php | 4 ++-- tests/Model/IndexInputTest.php | 19 +++++++++++++------ tests/Operation/CreateIndexesTest.php | 1 + 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Model/IndexInput.php b/src/Model/IndexInput.php index 3f55a3544..0d89b80cc 100644 --- a/src/Model/IndexInput.php +++ b/src/Model/IndexInput.php @@ -4,7 +4,7 @@ use MongoDB\BSON\Serializable; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\UnexpectedTypeException; +use MongoDB\Exception\InvalidArgumentTypeException; /** * Index input model class. @@ -32,12 +32,12 @@ public function __construct(array $index) } if ( ! is_array($index['key']) && ! is_object($index['key'])) { - throw new UnexpectedTypeException($index['key'], 'array or object'); + throw new InvalidArgumentTypeException('"key" option', $index['key'], 'array or object'); } - foreach ($index['key'] as $order) { + foreach ($index['key'] as $fieldName => $order) { if ( ! is_int($order) && ! is_float($order) && ! is_string($order)) { - throw new UnexpectedTypeException($order, 'numeric or string'); + throw new InvalidArgumentTypeException(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string'); } } @@ -46,7 +46,7 @@ public function __construct(array $index) } if ( ! is_string($index['ns'])) { - throw new UnexpectedTypeException($index['ns'], 'string'); + throw new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string'); } if ( ! isset($index['name'])) { @@ -54,7 +54,7 @@ public function __construct(array $index) } if ( ! is_string($index['name'])) { - throw new UnexpectedTypeException($index['name'], 'string'); + throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string'); } $this->index = $index; diff --git a/src/Operation/CreateIndexes.php b/src/Operation/CreateIndexes.php index 39cd69e6f..88dba2f1a 100644 --- a/src/Operation/CreateIndexes.php +++ b/src/Operation/CreateIndexes.php @@ -7,7 +7,7 @@ use MongoDB\Driver\BulkWrite as Bulk; use MongoDB\Driver\WriteConcern; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\UnexpectedTypeException; +use MongoDB\Exception\InvalidArgumentTypeException; use MongoDB\Model\IndexInput; /** @@ -42,7 +42,7 @@ public function __construct($databaseName, $collectionName, array $indexes) foreach ($indexes as $index) { if ( ! is_array($index)) { - throw new UnexpectedTypeException($index, 'array'); + throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array'); } if ( ! isset($index['ns'])) { diff --git a/tests/Model/IndexInputTest.php b/tests/Model/IndexInputTest.php index 9662eaefe..01a07211d 100644 --- a/tests/Model/IndexInputTest.php +++ b/tests/Model/IndexInputTest.php @@ -4,6 +4,7 @@ use MongoDB\Model\IndexInput; use MongoDB\Tests\TestCase; +use stdClass; class IndexInputTest extends TestCase { @@ -16,7 +17,7 @@ public function testConstructorShouldRequireKey() } /** - * @expectedException MongoDB\Exception\UnexpectedTypeException + * @expectedException MongoDB\Exception\InvalidArgumentTypeException */ public function testConstructorShouldRequireKeyToBeArrayOrObject() { @@ -24,11 +25,17 @@ public function testConstructorShouldRequireKeyToBeArrayOrObject() } /** - * @expectedException MongoDB\Exception\UnexpectedTypeException + * @expectedException MongoDB\Exception\InvalidArgumentTypeException + * @dataProvider provideInvalidFieldOrderValues */ - public function testConstructorShouldRequireKeyOrderToBeScalar() + public function testConstructorShouldRequireKeyFieldOrderToBeNumericOrString($order) { - new IndexInput(['key' => ['x' => []]]); + new IndexInput(['key' => ['x' => $order]]); + } + + public function provideInvalidFieldOrderValues() + { + return $this->wrapValuesForDataProvider([true, [], new stdClass]); } /** @@ -40,7 +47,7 @@ public function testConstructorShouldRequireNamespace() } /** - * @expectedException MongoDB\Exception\UnexpectedTypeException + * @expectedException MongoDB\Exception\InvalidArgumentTypeException */ public function testConstructorShouldRequireNamespaceToBeString() { @@ -48,7 +55,7 @@ public function testConstructorShouldRequireNamespaceToBeString() } /** - * @expectedException MongoDB\Exception\UnexpectedTypeException + * @expectedException MongoDB\Exception\InvalidArgumentTypeException */ public function testConstructorShouldRequireNameToBeString() { diff --git a/tests/Operation/CreateIndexesTest.php b/tests/Operation/CreateIndexesTest.php index cb0e4c958..69a647948 100644 --- a/tests/Operation/CreateIndexesTest.php +++ b/tests/Operation/CreateIndexesTest.php @@ -8,6 +8,7 @@ class CreateIndexesTest extends TestCase { /** * @expectedException MongoDB\Exception\InvalidArgumentException + * @expectedExceptionMessage $indexes is empty */ public function testCreateIndexesRequiresAtLeastOneIndex() { From 6b9501691fff4a9b039a3ce08b695c4b56809eb7 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 23 Dec 2015 14:17:15 -0500 Subject: [PATCH 2/2] CreateIndexes can require that $indexes be a list --- src/Operation/CreateIndexes.php | 10 +++++++++- tests/Operation/CreateIndexesTest.php | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Operation/CreateIndexes.php b/src/Operation/CreateIndexes.php index 88dba2f1a..036a022d8 100644 --- a/src/Operation/CreateIndexes.php +++ b/src/Operation/CreateIndexes.php @@ -40,7 +40,13 @@ public function __construct($databaseName, $collectionName, array $indexes) throw new InvalidArgumentException('$indexes is empty'); } - foreach ($indexes as $index) { + $expectedIndex = 0; + + foreach ($indexes as $i => $index) { + if ($i !== $expectedIndex) { + throw new InvalidArgumentException(sprintf('$indexes is not a list (unexpected index: "%s")', $i)); + } + if ( ! is_array($index)) { throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array'); } @@ -50,6 +56,8 @@ public function __construct($databaseName, $collectionName, array $indexes) } $this->indexes[] = new IndexInput($index); + + $expectedIndex += 1; } $this->databaseName = (string) $databaseName; diff --git a/tests/Operation/CreateIndexesTest.php b/tests/Operation/CreateIndexesTest.php index 69a647948..f5deee200 100644 --- a/tests/Operation/CreateIndexesTest.php +++ b/tests/Operation/CreateIndexesTest.php @@ -16,7 +16,16 @@ public function testCreateIndexesRequiresAtLeastOneIndex() } /** - * @expectedException MongoDB\Exception\UnexpectedTypeException + * @expectedException MongoDB\Exception\InvalidArgumentException + * @expectedExceptionMessage $indexes is not a list (unexpected index: "1") + */ + public function testConstructorIndexesArgumentMustBeAList() + { + new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['key' => ['x' => 1]]]); + } + + /** + * @expectedException MongoDB\Exception\InvalidArgumentTypeException * @dataProvider provideInvalidIndexSpecificationTypes */ public function testCreateIndexesRequiresIndexSpecificationsToBeAnArray($index)