Skip to content

Commit bd64355

Browse files
author
Alexander Miertsch
authored
Merge pull request #10 from bitExpert-forks/fix-update-delete-many
Fix updateMany() and deleteMay() of InMemoryDocumentStore
2 parents a49cbe3 + cfca5ee commit bd64355

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/InMemoryDocumentStore.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,14 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
215215
*/
216216
public function updateMany(string $collectionName, Filter $filter, array $set): void
217217
{
218-
$docs = $this->filterDocs($collectionName, $filter);
218+
$this->assertHasCollection($collectionName);
219+
220+
$docs = $this->inMemoryConnection['documents'][$collectionName];
219221

220222
foreach ($docs as $docId => $doc) {
221-
$this->updateDoc($collectionName, $docId, $set);
223+
if ($filter->match($doc, (string)$docId)) {
224+
$this->updateDoc($collectionName, (string)$docId, $set);
225+
}
222226
}
223227
}
224228

@@ -258,10 +262,14 @@ public function deleteDoc(string $collectionName, string $docId): void
258262
*/
259263
public function deleteMany(string $collectionName, Filter $filter): void
260264
{
261-
$docs = $this->filterDocs($collectionName, $filter);
265+
$this->assertHasCollection($collectionName);
266+
267+
$docs = $this->inMemoryConnection['documents'][$collectionName];
262268

263269
foreach ($docs as $docId => $doc) {
264-
$this->deleteDoc($collectionName, $docId);
270+
if ($filter->match($doc, (string)$docId)) {
271+
$this->deleteDoc($collectionName, (string)$docId);
272+
}
265273
}
266274
}
267275

tests/InMemoryDocumentStoreTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace EventEngineTest\DocumentStore;
55

66
use EventEngine\DocumentStore\FieldIndex;
7+
use EventEngine\DocumentStore\Filter\AnyFilter;
78
use EventEngine\DocumentStore\Filter\EqFilter;
89
use EventEngine\DocumentStore\InMemoryDocumentStore;
910
use EventEngine\DocumentStore\MultiFieldIndex;
@@ -200,4 +201,50 @@ public function it_does_not_block_adding_a_unique_multi_field_index_if_no_confli
200201
$this->store->addCollectionIndex('test', $uniqueIndex);
201202
$this->assertTrue($this->store->hasCollectionIndex('test', 'test_idx'));
202203
}
204+
205+
/**
206+
* @test
207+
*/
208+
public function it_updates_many()
209+
{
210+
$this->store->addCollection('test');
211+
212+
$this->store->addDoc('test', '1', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
213+
$this->store->addDoc('test', '2', ['some' => ['prop' => 'bar', 'other' => ['prop' => 'bat']]]);
214+
$this->store->addDoc('test', '3', ['some' => ['prop' => 'bar']]);
215+
216+
$this->store->updateMany(
217+
'test',
218+
new EqFilter('some.other.prop', 'bat'),
219+
['some' => ['prop' => 'fuzz']]
220+
);
221+
222+
$filteredDocs = iterator_to_array($this->store->filterDocs('test', new EqFilter('some.prop', 'fuzz')));
223+
224+
$this->assertCount(2, $filteredDocs);
225+
$this->assertEquals('fuzz', $filteredDocs[0]['some']['prop']);
226+
$this->assertEquals('fuzz', $filteredDocs[1]['some']['prop']);
227+
}
228+
229+
/**
230+
* @test
231+
*/
232+
public function it_deletes_many()
233+
{
234+
$this->store->addCollection('test');
235+
236+
$this->store->addDoc('test', '1', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]);
237+
$this->store->addDoc('test', '2', ['some' => ['prop' => 'bar', 'other' => ['prop' => 'bat']]]);
238+
$this->store->addDoc('test', '3', ['some' => ['prop' => 'bar']]);
239+
240+
$this->store->deleteMany(
241+
'test',
242+
new EqFilter('some.other.prop', 'bat')
243+
);
244+
245+
$filteredDocs = iterator_to_array($this->store->filterDocs('test', new AnyFilter()));
246+
247+
$this->assertCount(1, $filteredDocs);
248+
$this->assertEquals(['some' => ['prop' => 'bar']], $filteredDocs[0]);
249+
}
203250
}

0 commit comments

Comments
 (0)