Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add replaceDoc and replaceMany methods to prevent merge #19

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -111,6 +111,36 @@ public function it_updates_a_subset_of_a_doc()
$this->assertEquals(42, $filteredDocs[0]['some']['other']['nested']);
}

/**
* @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 @@ -549,6 +579,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