Skip to content

Commit cfca5ee

Browse files
committed
Fix updateMany() and deleteMay() of InMemoryDocumentStore
Those methods have used filterDocs() and wrongly assumed that the returned result is indexed by the docId.
1 parent c68ea51 commit cfca5ee

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
@@ -216,10 +216,14 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
216216
*/
217217
public function updateMany(string $collectionName, Filter $filter, array $set): void
218218
{
219-
$docs = $this->filterDocs($collectionName, $filter);
219+
$this->assertHasCollection($collectionName);
220+
221+
$docs = $this->inMemoryConnection['documents'][$collectionName];
220222

221223
foreach ($docs as $docId => $doc) {
222-
$this->updateDoc($collectionName, $docId, $set);
224+
if ($filter->match($doc, (string)$docId)) {
225+
$this->updateDoc($collectionName, (string)$docId, $set);
226+
}
223227
}
224228
}
225229

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

264270
foreach ($docs as $docId => $doc) {
265-
$this->deleteDoc($collectionName, $docId);
271+
if ($filter->match($doc, (string)$docId)) {
272+
$this->deleteDoc($collectionName, (string)$docId);
273+
}
266274
}
267275
}
268276

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)