Skip to content
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

Fix inserts with numeric keys #54

Merged
merged 1 commit into from
Feb 10, 2016
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
4 changes: 2 additions & 2 deletions lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,8 @@ private function ensureDocumentHasMongoId(&$document)
{
$checkKeys = function($array) {
foreach (array_keys($array) as $key) {
if (empty($key) || strpos($key, '*') === 1) {
throw new \MongoException('document contain invalid key');
if (empty($key) && $key !== 0) {
throw new \MongoException('zero-length keys are not allowed, did you use $ with double quotes?');
}
}
};
Expand Down
56 changes: 56 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ public function testInsertObjectWithPrivateProperties()
$this->getCollection()->insert($document);
}

public function testInsertWithInvalidKey()
{
$document = ['*' => 'foo'];
$this->getCollection()->insert($document);

$this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
}

public function testInsertWithEmptyKey()
{
$this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');

$document = ['' => 'foo'];
$this->getCollection()->insert($document);
}

public function testInsertWithNumericKey()
{
$document = ['foo'];
$this->getCollection()->insert($document);

$this->assertSame(1, $this->getCollection()->count(['foo']));
}

public function testInsertDuplicate()
{
$collection = $this->getCollection();
Expand Down Expand Up @@ -183,6 +207,38 @@ public function testBatchInsertException()
$this->getCollection()->batchInsert($documents);
}

public function testBatchInsertObjectWithPrivateProperties()
{
$this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');

$documents = [new PrivatePropertiesStub()];
$this->getCollection()->batchInsert($documents);
}

public function testBatchInsertWithInvalidKey()
{
$documents = [['*' => 'foo']];
$this->getCollection()->batchInsert($documents);

$this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
}

public function testBatchInsertWithEmptyKey()
{
$this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');

$documents = [['' => 'foo']];
$this->getCollection()->batchInsert($documents);
}

public function testBatchInsertWithNumericKey()
{
$documents = [['foo']];
$this->getCollection()->batchInsert($documents);

$this->assertSame(1, $this->getCollection()->count(['foo']));
}

public function testBatchInsertEmptyBatchException()
{
$this->setExpectedException('MongoException', 'No write ops were included in the batch');
Expand Down