Skip to content

Commit

Permalink
Merge pull request #19 from internalsystemerror/feature-replace-doc
Browse files Browse the repository at this point in the history
feature: add replaceDoc and replaceMany methods to prevent merge
  • Loading branch information
codeliner authored Sep 30, 2020
2 parents 7b53822 + 303321d commit 6f89111
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/DocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ public function updateMany(string $collectionName, Filter $filter, array $set):
*/
public function upsertDoc(string $collectionName, string $docId, array $docOrSubset): void;

/**
* @param string $collectionName
* @param string $docId
* @param array $doc
* @throws UnknownCollection
* @throws RuntimeException if updating did not succeed
*/
public function replaceDoc(string $collectionName, string $docId, array $doc): void;

/**
* @param string $collectionName
* @param Filter $filter
* @param array $set
* @throws UnknownCollection
* @throws RuntimeException in case of connection error or other issues
*/
public function replaceMany(string $collectionName, Filter $filter, array $set): void;

/**
* @param string $collectionName
* @param string $docId
Expand Down
33 changes: 33 additions & 0 deletions src/InMemoryDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,39 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
}
}

/**
* @param string $collectionName
* @param string $docId
* @param array $doc
* @throws \THrowable if replacing did not succeed
*/
public function replaceDoc(string $collectionName, string $docId, array $doc): void
{
$this->assertDocExists($collectionName, $docId);
$this->assertUniqueConstraints($collectionName, $docId, $doc);

$this->inMemoryConnection['documents'][$collectionName][$docId] = $doc;
}

/**
* @param string $collectionName
* @param Filter $filter
* @param array $set
* @throws \Throwable in case of connection error or other issues
*/
public function replaceMany(string $collectionName, Filter $filter, array $set): void
{
$this->assertHasCollection($collectionName);

$docs = $this->inMemoryConnection['documents'][$collectionName];

foreach ($docs as $docId => $doc) {
if ($filter->match($doc, (string)$docId)) {
$this->replaceDoc($collectionName, (string)$docId, $set);
}
}
}

/**
* @param string $collectionName
* @param string $docId
Expand Down
54 changes: 54 additions & 0 deletions tests/InMemoryDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,36 @@ public function it_updates_a_subset_of_a_doc()
$this->assertArrayNotHasKey('other', $filteredDocs[0]['some']);
}

/**
* @test
*/
public function it_replaces_a_doc()
{
$this->store->addCollection('test');

$doc = [
'some' => [
'prop' => 'foo',
'other' => [
'nested' => 42
]
],
'baz' => 'bat',
];

$this->store->addDoc('test', '1', $doc);

$doc = ['baz' => 'changed val'];

$this->store->replaceDoc('test', '1', $doc);

$filter = new EqFilter('baz', 'changed val');

$filteredDocs = $this->store->findDocs('test', $filter);

$this->assertCount(1, $filteredDocs);
}

/**
* @test
*/
Expand Down Expand Up @@ -548,6 +578,30 @@ public function it_updates_many()
$this->assertEquals('fuzz', $filteredDocs[1]['some']['prop']);
}

/**
* @test
*/
public function it_replaces_many()
{
$this->store->addCollection('test');

$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->store->replaceMany(
'test',
new EqFilter('some.other.prop', 'bat'),
['some' => ['prop' => 'fuzz']]
);

$filteredDocs = array_values(iterator_to_array($this->store->findDocs('test', new EqFilter('some.prop', 'fuzz'))));

$this->assertCount(2, $filteredDocs);
$this->assertEquals(['some' => ['prop' => 'fuzz']], $filteredDocs[0]);
$this->assertEquals(['some' => ['prop' => 'fuzz']], $filteredDocs[1]);
}

/**
* @test
*/
Expand Down

0 comments on commit 6f89111

Please sign in to comment.