Skip to content

Commit

Permalink
Merge pull request #31 from catchr/master
Browse files Browse the repository at this point in the history
Add dot notation on updateCore.
  • Loading branch information
martin-helmich authored Jun 6, 2019
2 parents 6e82d7c + 5da0c69 commit 58c71e2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor/
/build/
/composer.lock
.phpunit.result.cache
32 changes: 23 additions & 9 deletions src/MockCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MockCollection extends Collection
];

/**
* @param string $name
* @param string $name
* @param MockDatabase $db
*/
public function __construct(string $name = 'collection', MockDatabase $db = null, array $options = [])
Expand Down Expand Up @@ -206,7 +206,19 @@ private function updateCore(&$doc, $update)
}

foreach ($update['$set'] ?? [] as $k => $v) {
$doc[$k] = $v;
$dot = strpos($k, ".");
if ($dot !== false) {
$tmp = &$doc;
$keys = explode(".", $k);
if ($keys !== null) {
foreach ($keys as $key) {
$tmp = &$tmp[$key];
}
$tmp = $v;
}
} else {
$doc[$k] = $v;
}
}

foreach ($update['$unset'] ?? [] as $k => $v) {
Expand Down Expand Up @@ -260,8 +272,10 @@ public function find($filter = [], array $options = []): MockCursor

if ($av > $bv) {
return $dir;
} else if ($av < $bv) {
return -$dir;
} else {
if ($av < $bv) {
return -$dir;
}
}
}
return 0;
Expand Down Expand Up @@ -472,11 +486,11 @@ public function listIndexes(array $options = [])

foreach ($this->indices as $name => $index) {
$indices[] = [
'v' => 1,
'v' => 1,
'unique' => isset($index->getOptions()['unique']) ? $index->getOptions()['unique'] : false,
'key' => $index->getKey(),
'name' => $name,
'ns' => $dbName . '.' . $this->name,
'key' => $index->getKey(),
'name' => $name,
'ns' => $dbName . '.' . $this->name,
];
}

Expand Down Expand Up @@ -554,7 +568,7 @@ private function matcherFromQuery(array $query): callable
}
}
return true;
}elseif ($field === '$isolated') {
} elseif ($field === '$isolated') {
return true;
} else {
// needed for case of $exists query filter and field is inexistant
Expand Down
115 changes: 74 additions & 41 deletions tests/MockCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public function testInsertOneDocumentWithExistingId()
]);

$this->expectException(DriverRuntimeException::class);

// inserting a document with the same _id (baz)
$result = $this->col->insertOne([
'_id' => 'baz',
'bat' => 'dog'
]);
]);
}

/**
Expand Down Expand Up @@ -162,8 +162,8 @@ public function testFindWithInvertedFilter()
$this->col->insertMany([
['foo' => 'bar'],
['foo' => 'baz']
]);
]);

$result = $this->col->count(['foo' => ['$not' => ['$in' => ['bar', 'baz']]]]);
assertThat($result, equalTo(0));

Expand All @@ -172,11 +172,15 @@ public function testFindWithInvertedFilter()
assertThat(count($result), equalTo(1));
assertThat($result[0]['foo'], equalTo('bar'));

$find = $this->col->find(['foo' => ['$not' =>
['$not' =>
['$eq' => 'bar']
$find = $this->col->find([
'foo' => [
'$not' =>
[
'$not' =>
['$eq' => 'bar']
]
]
]]);
]);
$result = $find->toArray();
assertThat(count($result), equalTo(1));
assertThat($result[0]['foo'], equalTo('bar'));
Expand All @@ -191,7 +195,7 @@ public function testFindWithInFilter()
['foo' => ['bar', 'baz', 'bad']],
['foo' => ['baz', 'bad']],
['foo' => ['foobar', 'baroof']]
]);
]);

$result = $this->col->count(['foo' => ['$in' => ['barbar']]]);
assertThat($result, equalTo(0));
Expand Down Expand Up @@ -475,7 +479,7 @@ public function testUpdateIncrement()
*/
public function testUpdatePush()
{
$this->col->insertOne(
$this->col->insertOne(
['foo' => 'foo', 'bar' => []]
);

Expand Down Expand Up @@ -637,7 +641,7 @@ public function testFindWorksWithCallableOperators()
}
]);
$result = iterator_to_array($result);

assertThat(count($result), equalTo(1));
assertThat($result[0]['foo'], equalTo('bar'));
}
Expand All @@ -656,7 +660,7 @@ public function testFindWorksWithPhpUnitConstraints()
'foo' => equalTo('bar')
]);
$result = iterator_to_array($result);

assertThat(count($result), equalTo(1));
assertThat($result[0]['foo'], equalTo('bar'));
}
Expand Down Expand Up @@ -817,10 +821,12 @@ public function testManyByOrAndQuery()
['foo' => 'baz', 'bar' => 2],
]);

$result = $this->col->find(['$or' => [
['$and' => [['foo' => 'foo'], ['bar' => '3']]],
['$and' => [['foo' => 'baz'], ['bar' => '2']]],
]]);
$result = $this->col->find([
'$or' => [
['$and' => [['foo' => 'foo'], ['bar' => '3']]],
['$and' => [['foo' => 'baz'], ['bar' => '2']]],
]
]);

assertThat($result, isInstanceOf(MockCursor::class));
$result = $result->toArray();
Expand All @@ -840,10 +846,12 @@ public function testManyByAndOrQuery()
['foo' => 'baz', 'bar' => 2],
]);

$result = $this->col->find(['$and' => [
['$or' => [['foo' => 1], ['foo' => 'foo']]],
['$or' => [['bar' => 'foo'], ['bar' => 3]]],
]]);
$result = $this->col->find([
'$and' => [
['$or' => [['foo' => 1], ['foo' => 'foo']]],
['$or' => [['bar' => 'foo'], ['bar' => 3]]],
]
]);

assertThat($result, isInstanceOf(MockCursor::class));
$result = $result->toArray();
Expand All @@ -862,30 +870,38 @@ public function testManyByNorQuery()
['foo' => 'baz', 'bar' => 2],
]);

$result = $this->col->count(['$nor' => [
'foo' => ['$eq' => 'foo'],
'foo' => ['$eq' => 'bar'],
'foo' => ['$eq' => 'baz']
]]);
$result = $this->col->count([
'$nor' => [
'foo' => ['$eq' => 'foo'],
'foo' => ['$eq' => 'bar'],
'foo' => ['$eq' => 'baz']
]
]);
assertThat($result, equalTo(0));

/* Finding ['foo' => 'foo', 'bar' => 3] */
$result = $this->col->count(['$nor' => [
['foo' => ['$eq' => 'bar']],
['foo' => ['$eq' => 'baz']],
['bar' => ['$lt' => 3]]
]]);
$result = $this->col->count([
'$nor' => [
['foo' => ['$eq' => 'bar']],
['foo' => ['$eq' => 'baz']],
['bar' => ['$lt' => 3]]
]
]);
assertThat($result, equalTo(1));

/* Finding ['foo' => 'bar', 'bar' => 1] */
$result = $this->col->count(['$nor' => [
['foo' =>
['$not' => ['$eq' => 'bar']]
],
['bar' =>
['$not' => ['$eq' => 1]]
$result = $this->col->count([
'$nor' => [
[
'foo' =>
['$not' => ['$eq' => 'bar']]
],
[
'bar' =>
['$not' => ['$eq' => 1]]
]
]
]]);
]);
assertThat($result, equalTo(1));
}

Expand Down Expand Up @@ -943,10 +959,12 @@ public function testManyOrGteAndLteQuery()
['foo' => 'baz', 'bar' => 2],
]);

$result = $this->col->find(['$or' => [
['bar' => ['$lte' => 1]],
['bar' => ['$gte' => 3]]
]]);
$result = $this->col->find([
'$or' => [
['bar' => ['$lte' => 1]],
['bar' => ['$gte' => 3]]
]
]);

assertThat($result, isInstanceOf(MockCursor::class));
$result = $result->toArray();
Expand Down Expand Up @@ -1118,4 +1136,19 @@ public function testFindOneAndUpdateWithReturnDocumentAfter()
assertThat($documentAfterUpdate['bar'], equalTo(23));
}

public function testSetMultiDimensionalArray()
{
$col = new MockCollection('foo');
$insertOneResult = $col->insertOne(['foo' => ['foo' => ['bar' => "test"]], 'bar' => 42]);

$col->updateOne(
['_id' => $insertOneResult->getInsertedId()],
[
'$set' => ["foo.foo.bar" => "azerty"]
]
);

$documentAfterUpdate = $col->findOne(['_id' => $insertOneResult->getInsertedId()]);
assertThat($documentAfterUpdate['foo']['foo']['bar'], equalTo("azerty"), $documentAfterUpdate['foo']['foo']['bar']);
}
}

0 comments on commit 58c71e2

Please sign in to comment.