Skip to content

Commit 7b53822

Browse files
author
Alexander Miertsch
authored
Merge pull request #18 from internalsystemerror/fix-merging-associative-arrays
fix: Mimic how postgres-document-store works, where documents are not merged recursively.
2 parents 1e980c8 + 15d3034 commit 7b53822

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

src/InMemoryDocumentStore.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
205205
$this->assertDocExists($collectionName, $docId);
206206
$this->assertUniqueConstraints($collectionName, $docId, $docOrSubset);
207207

208-
$this->inMemoryConnection['documents'][$collectionName][$docId] = $this->arrayReplaceRecursiveAssocOnly(
208+
$this->inMemoryConnection['documents'][$collectionName][$docId] = $this->arrayReplace(
209209
$this->inMemoryConnection['documents'][$collectionName][$docId],
210210
$docOrSubset
211211
);
@@ -550,7 +550,7 @@ private function assertMultiFieldUniqueConstraint(string $collectionName, string
550550

551551
if($this->hasDoc($collectionName, $docId)) {
552552
$effectedDoc = $this->getDoc($collectionName, $docId);
553-
$docOrSubset = $this->arrayReplaceRecursiveAssocOnly($effectedDoc, $docOrSubset);
553+
$docOrSubset = $this->arrayReplace($effectedDoc, $docOrSubset);
554554
}
555555

556556
$reader = new ArrayReader($docOrSubset);
@@ -672,32 +672,15 @@ private function sort(&$docs, OrderBy $orderBy)
672672
* @param array $array2
673673
* @return array
674674
*/
675-
private function arrayReplaceRecursiveAssocOnly(array $array1, array $array2): array
675+
private function arrayReplace(array $array1, array $array2): array
676676
{
677677
foreach ($array2 as $key2 => $value2) {
678-
$bothValuesArraysAndAtLeastOneAssoc = \is_array($value2) &&
679-
isset($array1[$key2]) && \is_array($array1[$key2]) &&
680-
!($this->isSequentialArray($value2) && $this->isSequentialArray($array1[$key2]));
681-
682-
if ($bothValuesArraysAndAtLeastOneAssoc) {
683-
$array1[$key2] = $this->arrayReplaceRecursiveAssocOnly($array1[$key2], $value2);
684-
} else {
685-
$array1[$key2] = $value2;
686-
}
678+
$array1[$key2] = $value2;
687679
}
688680

689681
return $array1;
690682
}
691683

692-
private function isSequentialArray(array $array): bool
693-
{
694-
if (\count($array) === 0) {
695-
return true;
696-
}
697-
698-
return \array_keys($array) === \range(0, \count($array) - 1);
699-
}
700-
701684
private function transformToPartialDoc(array $doc, PartialSelect $partialSelect): array
702685
{
703686
$partialDoc = [];

tests/InMemoryDocumentStoreTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ public function it_updates_a_subset_of_a_doc()
9292
$doc = [
9393
'some' => [
9494
'prop' => 'foo',
95-
'other' => [
96-
'nested' => 42
97-
]
95+
'other' => 'bar',
9896
],
9997
'baz' => 'bat',
10098
];
@@ -108,7 +106,7 @@ public function it_updates_a_subset_of_a_doc()
108106
]);
109107

110108
$filteredDocs = array_values(iterator_to_array($this->store->findDocs('test', new EqFilter('some.prop', 'fuzz'))));
111-
$this->assertEquals(42, $filteredDocs[0]['some']['other']['nested']);
109+
$this->assertArrayNotHasKey('other', $filteredDocs[0]['some']);
112110
}
113111

114112
/**
@@ -424,6 +422,7 @@ public function it_ensures_unique_constraints_for_multiple_fields()
424422

425423
$this->store->addDoc('test', '1', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
426424
$this->store->addDoc('test', '2', ['some' => ['prop' => 'bar', 'other' => ['prop' => 'bat']]]);
425+
$this->store->addDoc('test', '3', ['some' => ['prop' => 'bar']]);
427426

428427
$this->expectExceptionMessageRegExp('/^Unique constraint violation/');
429428
$this->store->addDoc('test', '4', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
@@ -443,7 +442,7 @@ public function it_ensures_unique_constraints_for_multiple_fields_for_update()
443442
$this->store->addDoc('test', '3', ['some' => ['prop' => 'bar']]);
444443

445444
$this->expectExceptionMessageRegExp('/^Unique constraint violation/');
446-
$this->store->updateDoc('test', '2', ['some' => ['prop' => 'foo']]);
445+
$this->store->updateDoc('test', '2', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
447446
}
448447

449448
/**
@@ -574,7 +573,7 @@ public function it_deletes_many()
574573
/**
575574
* @test
576575
*/
577-
public function it_does_not_update_numeric_arrays_recursively()
576+
public function it_does_not_update_arrays_recursively()
578577
{
579578
$this->store->addCollection('test');
580579

@@ -604,13 +603,13 @@ public function it_does_not_update_numeric_arrays_recursively()
604603

605604
$this->assertEquals(
606605
[
607-
'a' => ['a' => 10, 'b' => 21, 'c' => 30],
606+
'a' => ['b' => 21, 'c' => 30],
608607
'b' => [10, 30],
609608
'c' => [true],
610609
'd' => [],
611-
'e' => ['a' => 'b'],
612-
'f' => [10, 20, 'x' => 10, 'y' => 20],
613-
'g' => ['x' => 10, 'y' => 20, 30, 40],
610+
'e' => [],
611+
'f' => ['x' => 10, 'y' => 20],
612+
'g' => [30, 40],
614613
'h' => [11],
615614
'i' => [22],
616615
'j' => ['bar']

0 commit comments

Comments
 (0)