Skip to content

Commit 303321d

Browse files
author
Gary Lockett
committed
feature: add replaceDoc and replaceMany methods to prevent merge
1 parent 1e980c8 commit 303321d

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
@@ -111,6 +111,36 @@ public function it_updates_a_subset_of_a_doc()
111111
$this->assertEquals(42, $filteredDocs[0]['some']['other']['nested']);
112112
}
113113

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

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

0 commit comments

Comments
 (0)