Skip to content

Commit 6f89111

Browse files
author
Alexander Miertsch
authored
Merge pull request #19 from internalsystemerror/feature-replace-doc
feature: add replaceDoc and replaceMany methods to prevent merge
2 parents 7b53822 + 303321d commit 6f89111

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/DocumentStore.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ public function updateMany(string $collectionName, Filter $filter, array $set):
102102
*/
103103
public function upsertDoc(string $collectionName, string $docId, array $docOrSubset): void;
104104

105+
/**
106+
* @param string $collectionName
107+
* @param string $docId
108+
* @param array $doc
109+
* @throws UnknownCollection
110+
* @throws RuntimeException if updating did not succeed
111+
*/
112+
public function replaceDoc(string $collectionName, string $docId, array $doc): void;
113+
114+
/**
115+
* @param string $collectionName
116+
* @param Filter $filter
117+
* @param array $set
118+
* @throws UnknownCollection
119+
* @throws RuntimeException in case of connection error or other issues
120+
*/
121+
public function replaceMany(string $collectionName, Filter $filter, array $set): void;
122+
105123
/**
106124
* @param string $collectionName
107125
* @param string $docId

src/InMemoryDocumentStore.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,39 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
247247
}
248248
}
249249

250+
/**
251+
* @param string $collectionName
252+
* @param string $docId
253+
* @param array $doc
254+
* @throws \THrowable if replacing did not succeed
255+
*/
256+
public function replaceDoc(string $collectionName, string $docId, array $doc): void
257+
{
258+
$this->assertDocExists($collectionName, $docId);
259+
$this->assertUniqueConstraints($collectionName, $docId, $doc);
260+
261+
$this->inMemoryConnection['documents'][$collectionName][$docId] = $doc;
262+
}
263+
264+
/**
265+
* @param string $collectionName
266+
* @param Filter $filter
267+
* @param array $set
268+
* @throws \Throwable in case of connection error or other issues
269+
*/
270+
public function replaceMany(string $collectionName, Filter $filter, array $set): void
271+
{
272+
$this->assertHasCollection($collectionName);
273+
274+
$docs = $this->inMemoryConnection['documents'][$collectionName];
275+
276+
foreach ($docs as $docId => $doc) {
277+
if ($filter->match($doc, (string)$docId)) {
278+
$this->replaceDoc($collectionName, (string)$docId, $set);
279+
}
280+
}
281+
}
282+
250283
/**
251284
* @param string $collectionName
252285
* @param string $docId

tests/InMemoryDocumentStoreTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@ public function it_updates_a_subset_of_a_doc()
109109
$this->assertArrayNotHasKey('other', $filteredDocs[0]['some']);
110110
}
111111

112+
/**
113+
* @test
114+
*/
115+
public function it_replaces_a_doc()
116+
{
117+
$this->store->addCollection('test');
118+
119+
$doc = [
120+
'some' => [
121+
'prop' => 'foo',
122+
'other' => [
123+
'nested' => 42
124+
]
125+
],
126+
'baz' => 'bat',
127+
];
128+
129+
$this->store->addDoc('test', '1', $doc);
130+
131+
$doc = ['baz' => 'changed val'];
132+
133+
$this->store->replaceDoc('test', '1', $doc);
134+
135+
$filter = new EqFilter('baz', 'changed val');
136+
137+
$filteredDocs = $this->store->findDocs('test', $filter);
138+
139+
$this->assertCount(1, $filteredDocs);
140+
}
141+
112142
/**
113143
* @test
114144
*/
@@ -548,6 +578,30 @@ public function it_updates_many()
548578
$this->assertEquals('fuzz', $filteredDocs[1]['some']['prop']);
549579
}
550580

581+
/**
582+
* @test
583+
*/
584+
public function it_replaces_many()
585+
{
586+
$this->store->addCollection('test');
587+
588+
$this->store->addDoc('test', '1', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
589+
$this->store->addDoc('test', '2', ['some' => ['prop' => 'bar', 'other' => ['prop' => 'bat']]]);
590+
$this->store->addDoc('test', '3', ['some' => ['prop' => 'bar']]);
591+
592+
$this->store->replaceMany(
593+
'test',
594+
new EqFilter('some.other.prop', 'bat'),
595+
['some' => ['prop' => 'fuzz']]
596+
);
597+
598+
$filteredDocs = array_values(iterator_to_array($this->store->findDocs('test', new EqFilter('some.prop', 'fuzz'))));
599+
600+
$this->assertCount(2, $filteredDocs);
601+
$this->assertEquals(['some' => ['prop' => 'fuzz']], $filteredDocs[0]);
602+
$this->assertEquals(['some' => ['prop' => 'fuzz']], $filteredDocs[1]);
603+
}
604+
551605
/**
552606
* @test
553607
*/

0 commit comments

Comments
 (0)