Skip to content

PHPLIB-591: CreateIndexes should not specify "ns" for index specifications #790

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 1 commit into from
Oct 8, 2020
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
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());
}
}