Skip to content

Commit

Permalink
PHPLIB-591: CreateIndexes should not specify "ns" for index specifica…
Browse files Browse the repository at this point in the history
…tions

Also adds an extra option to the BSON serialization test.
  • Loading branch information
jmikola committed Oct 8, 2020
1 parent b35af66 commit 38b6851
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 29 deletions.
8 changes: 0 additions & 8 deletions src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ public function __construct(array $index)
}
}

if (! isset($index['ns'])) {
throw new InvalidArgumentException('Required "ns" option is missing from index specification');
}

if (! is_string($index['ns'])) {
throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string');
}

if (! isset($index['name'])) {
$index['name'] = generate_index_name($index['key']);
}
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public function __construct($databaseName, $collectionName, array $indexes, arra
throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array');
}

if (! isset($index['ns'])) {
$index['ns'] = $databaseName . '.' . $collectionName;
}

if (isset($index['collation'])) {
$this->isCollationUsed = true;
}
Expand Down
22 changes: 5 additions & 17 deletions tests/Model/IndexInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,18 @@ public function provideInvalidFieldOrderValues()
return $this->wrapValuesForDataProvider([true, [], new stdClass()]);
}

public function testConstructorShouldRequireNamespace()
{
$this->expectException(InvalidArgumentException::class);
new IndexInput(['key' => ['x' => 1]]);
}

public function testConstructorShouldRequireNamespaceToBeString()
{
$this->expectException(InvalidArgumentException::class);
new IndexInput(['key' => ['x' => 1], 'ns' => 1]);
}

public function testConstructorShouldRequireNameToBeString()
{
$this->expectException(InvalidArgumentException::class);
new IndexInput(['key' => ['x' => 1], 'ns' => 'foo.bar', 'name' => 1]);
new IndexInput(['key' => ['x' => 1], 'name' => 1]);
}

/**
* @dataProvider provideExpectedNameAndKey
*/
public function testNameGeneration($expectedName, array $key)
{
$this->assertSame($expectedName, (string) new IndexInput(['key' => $key, 'ns' => 'foo.bar']));
$this->assertSame($expectedName, (string) new IndexInput(['key' => $key]));
}

public function provideExpectedNameAndKey()
Expand All @@ -77,16 +65,16 @@ public function testBsonSerialization()
{
$expected = [
'key' => ['x' => 1],
'ns' => 'foo.bar',
'unique' => true,
'name' => 'x_1',
];

$indexInput = new IndexInput([
'key' => ['x' => 1],
'ns' => 'foo.bar',
'unique' => true,
]);

$this->assertInstanceOf(Serializable::class, $indexInput);
$this->assertEquals($expected, $indexInput->bsonSerialize());
$this->assertSame($expected, $indexInput->bsonSerialize());
}
}

0 comments on commit 38b6851

Please sign in to comment.