Skip to content

Commit

Permalink
feat(set): Support merge parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop committed Jun 3, 2018
1 parent ac256f2 commit b2344a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,24 @@ public function getValue()
*
* @return Reference
*/
public function set($value): self
public function set($value, $merge = false): self
{
$value = [
$payload = [
'name' => basename($this->uri->getPath()),
'fields' => $this->valueMapper->encodeValues($value)
'fields' => $this->valueMapper->encodeValues($value),
];

$this->apiClient->set($this->uri, $value);
if ($merge) {
$paths = $this->valueMapper->encodeFieldPaths($value);
$prefix = '&updateMask.fieldPaths=';
$query = $prefix . implode($prefix, $paths);
$uri = $this->uri->withQuery("updateMask.fieldPaths=message$query");
}
else {
$uri = $this->uri;
}

$this->apiClient->set($uri, $payload);

return $this;
}
Expand Down
14 changes: 6 additions & 8 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,26 @@ public function testSetAndGet()
}
}

public function testUpdate()
public function testSetMergeAndGet()
{
$this->markTestIncomplete('This test has not been implemented yet.');

$ref = $this->ref->getChild(__FUNCTION__);
$ref->set([
$doc = $this->collection->getDocument(__FUNCTION__);
$doc->set([
'first' => 'value',
'second' => 'value',
]);

$ref->update([
$doc->set([
'first' => 'updated',
'third' => 'new',
]);
], true);

$expected = [
'first' => 'updated',
'second' => 'value',
'third' => 'new',
];

$this->assertEquals($expected, $ref->getValue());
$this->assertEquals($expected, $doc->getSnapshot()->data());
}

public function testPush()
Expand Down

0 comments on commit b2344a4

Please sign in to comment.