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: Mimic how postgres-document-store works, where documents are not merged recursively. #18

Merged
merged 1 commit into from
Sep 30, 2020
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
25 changes: 4 additions & 21 deletions src/InMemoryDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
$this->assertDocExists($collectionName, $docId);
$this->assertUniqueConstraints($collectionName, $docId, $docOrSubset);

$this->inMemoryConnection['documents'][$collectionName][$docId] = $this->arrayReplaceRecursiveAssocOnly(
$this->inMemoryConnection['documents'][$collectionName][$docId] = $this->arrayReplace(
$this->inMemoryConnection['documents'][$collectionName][$docId],
$docOrSubset
);
Expand Down Expand Up @@ -550,7 +550,7 @@ private function assertMultiFieldUniqueConstraint(string $collectionName, string

if($this->hasDoc($collectionName, $docId)) {
$effectedDoc = $this->getDoc($collectionName, $docId);
$docOrSubset = $this->arrayReplaceRecursiveAssocOnly($effectedDoc, $docOrSubset);
$docOrSubset = $this->arrayReplace($effectedDoc, $docOrSubset);
}

$reader = new ArrayReader($docOrSubset);
Expand Down Expand Up @@ -672,32 +672,15 @@ private function sort(&$docs, OrderBy $orderBy)
* @param array $array2
* @return array
*/
private function arrayReplaceRecursiveAssocOnly(array $array1, array $array2): array
private function arrayReplace(array $array1, array $array2): array
{
foreach ($array2 as $key2 => $value2) {
$bothValuesArraysAndAtLeastOneAssoc = \is_array($value2) &&
isset($array1[$key2]) && \is_array($array1[$key2]) &&
!($this->isSequentialArray($value2) && $this->isSequentialArray($array1[$key2]));

if ($bothValuesArraysAndAtLeastOneAssoc) {
$array1[$key2] = $this->arrayReplaceRecursiveAssocOnly($array1[$key2], $value2);
} else {
$array1[$key2] = $value2;
}
$array1[$key2] = $value2;
}

return $array1;
}

private function isSequentialArray(array $array): bool
{
if (\count($array) === 0) {
return true;
}

return \array_keys($array) === \range(0, \count($array) - 1);
}

private function transformToPartialDoc(array $doc, PartialSelect $partialSelect): array
{
$partialDoc = [];
Expand Down
19 changes: 9 additions & 10 deletions tests/InMemoryDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public function it_updates_a_subset_of_a_doc()
$doc = [
'some' => [
'prop' => 'foo',
'other' => [
'nested' => 42
]
'other' => 'bar',
],
'baz' => 'bat',
];
Expand All @@ -108,7 +106,7 @@ public function it_updates_a_subset_of_a_doc()
]);

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

/**
Expand Down Expand Up @@ -424,6 +422,7 @@ public function it_ensures_unique_constraints_for_multiple_fields()

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

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

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

/**
Expand Down Expand Up @@ -574,7 +573,7 @@ public function it_deletes_many()
/**
* @test
*/
public function it_does_not_update_numeric_arrays_recursively()
public function it_does_not_update_arrays_recursively()
{
$this->store->addCollection('test');

Expand Down Expand Up @@ -604,13 +603,13 @@ public function it_does_not_update_numeric_arrays_recursively()

$this->assertEquals(
[
'a' => ['a' => 10, 'b' => 21, 'c' => 30],
'a' => ['b' => 21, 'c' => 30],
'b' => [10, 30],
'c' => [true],
'd' => [],
'e' => ['a' => 'b'],
'f' => [10, 20, 'x' => 10, 'y' => 20],
'g' => ['x' => 10, 'y' => 20, 30, 40],
'e' => [],
'f' => ['x' => 10, 'y' => 20],
'g' => [30, 40],
'h' => [11],
'i' => [22],
'j' => ['bar']
Expand Down