From 5da0c696ccb73381946ba0eb8b6f12cf36e67597 Mon Sep 17 00:00:00 2001 From: Johan-Uplab Date: Wed, 5 Jun 2019 16:13:44 +0200 Subject: [PATCH] add dote notation on updateCore --- .gitignore | 1 + src/MockCollection.php | 32 +++++++--- tests/MockCollectionTest.php | 115 ++++++++++++++++++++++------------- 3 files changed, 98 insertions(+), 50 deletions(-) diff --git a/.gitignore b/.gitignore index 7487b06..5b73692 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /vendor/ /build/ /composer.lock +.phpunit.result.cache \ No newline at end of file diff --git a/src/MockCollection.php b/src/MockCollection.php index e8bdbc0..2c143f6 100644 --- a/src/MockCollection.php +++ b/src/MockCollection.php @@ -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 = []) @@ -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) { @@ -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; @@ -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, ]; } @@ -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 diff --git a/tests/MockCollectionTest.php b/tests/MockCollectionTest.php index 500b43a..b574caa 100644 --- a/tests/MockCollectionTest.php +++ b/tests/MockCollectionTest.php @@ -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' - ]); + ]); } /** @@ -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)); @@ -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')); @@ -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)); @@ -475,7 +479,7 @@ public function testUpdateIncrement() */ public function testUpdatePush() { - $this->col->insertOne( + $this->col->insertOne( ['foo' => 'foo', 'bar' => []] ); @@ -637,7 +641,7 @@ public function testFindWorksWithCallableOperators() } ]); $result = iterator_to_array($result); - + assertThat(count($result), equalTo(1)); assertThat($result[0]['foo'], equalTo('bar')); } @@ -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')); } @@ -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(); @@ -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(); @@ -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)); } @@ -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(); @@ -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']); + } }