Skip to content

Commit 5da0c69

Browse files
committed
add dote notation on updateCore
1 parent 6e82d7c commit 5da0c69

File tree

3 files changed

+98
-50
lines changed

3 files changed

+98
-50
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/vendor/
33
/build/
44
/composer.lock
5+
.phpunit.result.cache

src/MockCollection.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MockCollection extends Collection
7777
];
7878

7979
/**
80-
* @param string $name
80+
* @param string $name
8181
* @param MockDatabase $db
8282
*/
8383
public function __construct(string $name = 'collection', MockDatabase $db = null, array $options = [])
@@ -206,7 +206,19 @@ private function updateCore(&$doc, $update)
206206
}
207207

208208
foreach ($update['$set'] ?? [] as $k => $v) {
209-
$doc[$k] = $v;
209+
$dot = strpos($k, ".");
210+
if ($dot !== false) {
211+
$tmp = &$doc;
212+
$keys = explode(".", $k);
213+
if ($keys !== null) {
214+
foreach ($keys as $key) {
215+
$tmp = &$tmp[$key];
216+
}
217+
$tmp = $v;
218+
}
219+
} else {
220+
$doc[$k] = $v;
221+
}
210222
}
211223

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

261273
if ($av > $bv) {
262274
return $dir;
263-
} else if ($av < $bv) {
264-
return -$dir;
275+
} else {
276+
if ($av < $bv) {
277+
return -$dir;
278+
}
265279
}
266280
}
267281
return 0;
@@ -472,11 +486,11 @@ public function listIndexes(array $options = [])
472486

473487
foreach ($this->indices as $name => $index) {
474488
$indices[] = [
475-
'v' => 1,
489+
'v' => 1,
476490
'unique' => isset($index->getOptions()['unique']) ? $index->getOptions()['unique'] : false,
477-
'key' => $index->getKey(),
478-
'name' => $name,
479-
'ns' => $dbName . '.' . $this->name,
491+
'key' => $index->getKey(),
492+
'name' => $name,
493+
'ns' => $dbName . '.' . $this->name,
480494
];
481495
}
482496

@@ -554,7 +568,7 @@ private function matcherFromQuery(array $query): callable
554568
}
555569
}
556570
return true;
557-
}elseif ($field === '$isolated') {
571+
} elseif ($field === '$isolated') {
558572
return true;
559573
} else {
560574
// needed for case of $exists query filter and field is inexistant

tests/MockCollectionTest.php

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public function testInsertOneDocumentWithExistingId()
4949
]);
5050

5151
$this->expectException(DriverRuntimeException::class);
52-
52+
5353
// inserting a document with the same _id (baz)
5454
$result = $this->col->insertOne([
5555
'_id' => 'baz',
5656
'bat' => 'dog'
57-
]);
57+
]);
5858
}
5959

6060
/**
@@ -162,8 +162,8 @@ public function testFindWithInvertedFilter()
162162
$this->col->insertMany([
163163
['foo' => 'bar'],
164164
['foo' => 'baz']
165-
]);
166-
165+
]);
166+
167167
$result = $this->col->count(['foo' => ['$not' => ['$in' => ['bar', 'baz']]]]);
168168
assertThat($result, equalTo(0));
169169

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

175-
$find = $this->col->find(['foo' => ['$not' =>
176-
['$not' =>
177-
['$eq' => 'bar']
175+
$find = $this->col->find([
176+
'foo' => [
177+
'$not' =>
178+
[
179+
'$not' =>
180+
['$eq' => 'bar']
181+
]
178182
]
179-
]]);
183+
]);
180184
$result = $find->toArray();
181185
assertThat(count($result), equalTo(1));
182186
assertThat($result[0]['foo'], equalTo('bar'));
@@ -191,7 +195,7 @@ public function testFindWithInFilter()
191195
['foo' => ['bar', 'baz', 'bad']],
192196
['foo' => ['baz', 'bad']],
193197
['foo' => ['foobar', 'baroof']]
194-
]);
198+
]);
195199

196200
$result = $this->col->count(['foo' => ['$in' => ['barbar']]]);
197201
assertThat($result, equalTo(0));
@@ -475,7 +479,7 @@ public function testUpdateIncrement()
475479
*/
476480
public function testUpdatePush()
477481
{
478-
$this->col->insertOne(
482+
$this->col->insertOne(
479483
['foo' => 'foo', 'bar' => []]
480484
);
481485

@@ -637,7 +641,7 @@ public function testFindWorksWithCallableOperators()
637641
}
638642
]);
639643
$result = iterator_to_array($result);
640-
644+
641645
assertThat(count($result), equalTo(1));
642646
assertThat($result[0]['foo'], equalTo('bar'));
643647
}
@@ -656,7 +660,7 @@ public function testFindWorksWithPhpUnitConstraints()
656660
'foo' => equalTo('bar')
657661
]);
658662
$result = iterator_to_array($result);
659-
663+
660664
assertThat(count($result), equalTo(1));
661665
assertThat($result[0]['foo'], equalTo('bar'));
662666
}
@@ -817,10 +821,12 @@ public function testManyByOrAndQuery()
817821
['foo' => 'baz', 'bar' => 2],
818822
]);
819823

820-
$result = $this->col->find(['$or' => [
821-
['$and' => [['foo' => 'foo'], ['bar' => '3']]],
822-
['$and' => [['foo' => 'baz'], ['bar' => '2']]],
823-
]]);
824+
$result = $this->col->find([
825+
'$or' => [
826+
['$and' => [['foo' => 'foo'], ['bar' => '3']]],
827+
['$and' => [['foo' => 'baz'], ['bar' => '2']]],
828+
]
829+
]);
824830

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

843-
$result = $this->col->find(['$and' => [
844-
['$or' => [['foo' => 1], ['foo' => 'foo']]],
845-
['$or' => [['bar' => 'foo'], ['bar' => 3]]],
846-
]]);
849+
$result = $this->col->find([
850+
'$and' => [
851+
['$or' => [['foo' => 1], ['foo' => 'foo']]],
852+
['$or' => [['bar' => 'foo'], ['bar' => 3]]],
853+
]
854+
]);
847855

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

865-
$result = $this->col->count(['$nor' => [
866-
'foo' => ['$eq' => 'foo'],
867-
'foo' => ['$eq' => 'bar'],
868-
'foo' => ['$eq' => 'baz']
869-
]]);
873+
$result = $this->col->count([
874+
'$nor' => [
875+
'foo' => ['$eq' => 'foo'],
876+
'foo' => ['$eq' => 'bar'],
877+
'foo' => ['$eq' => 'baz']
878+
]
879+
]);
870880
assertThat($result, equalTo(0));
871881

872882
/* Finding ['foo' => 'foo', 'bar' => 3] */
873-
$result = $this->col->count(['$nor' => [
874-
['foo' => ['$eq' => 'bar']],
875-
['foo' => ['$eq' => 'baz']],
876-
['bar' => ['$lt' => 3]]
877-
]]);
883+
$result = $this->col->count([
884+
'$nor' => [
885+
['foo' => ['$eq' => 'bar']],
886+
['foo' => ['$eq' => 'baz']],
887+
['bar' => ['$lt' => 3]]
888+
]
889+
]);
878890
assertThat($result, equalTo(1));
879891

880892
/* Finding ['foo' => 'bar', 'bar' => 1] */
881-
$result = $this->col->count(['$nor' => [
882-
['foo' =>
883-
['$not' => ['$eq' => 'bar']]
884-
],
885-
['bar' =>
886-
['$not' => ['$eq' => 1]]
893+
$result = $this->col->count([
894+
'$nor' => [
895+
[
896+
'foo' =>
897+
['$not' => ['$eq' => 'bar']]
898+
],
899+
[
900+
'bar' =>
901+
['$not' => ['$eq' => 1]]
902+
]
887903
]
888-
]]);
904+
]);
889905
assertThat($result, equalTo(1));
890906
}
891907

@@ -943,10 +959,12 @@ public function testManyOrGteAndLteQuery()
943959
['foo' => 'baz', 'bar' => 2],
944960
]);
945961

946-
$result = $this->col->find(['$or' => [
947-
['bar' => ['$lte' => 1]],
948-
['bar' => ['$gte' => 3]]
949-
]]);
962+
$result = $this->col->find([
963+
'$or' => [
964+
['bar' => ['$lte' => 1]],
965+
['bar' => ['$gte' => 3]]
966+
]
967+
]);
950968

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

1139+
public function testSetMultiDimensionalArray()
1140+
{
1141+
$col = new MockCollection('foo');
1142+
$insertOneResult = $col->insertOne(['foo' => ['foo' => ['bar' => "test"]], 'bar' => 42]);
1143+
1144+
$col->updateOne(
1145+
['_id' => $insertOneResult->getInsertedId()],
1146+
[
1147+
'$set' => ["foo.foo.bar" => "azerty"]
1148+
]
1149+
);
1150+
1151+
$documentAfterUpdate = $col->findOne(['_id' => $insertOneResult->getInsertedId()]);
1152+
assertThat($documentAfterUpdate['foo']['foo']['bar'], equalTo("azerty"), $documentAfterUpdate['foo']['foo']['bar']);
1153+
}
11211154
}

0 commit comments

Comments
 (0)