Skip to content

Commit

Permalink
Clone key objects when filling to a number (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpedrie authored and dwsupplee committed Apr 5, 2018
1 parent 8ced32f commit 341365d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Datastore/src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function key($kind, $identifier = null, array $options = [])
* @type string $name The Name for the last pathElement.
* }
* @return Key[]
* @throws \InvalidArgumentException If the number of keys is less than 1.
*/
public function keys($kind, array $options = [])
{
Expand All @@ -143,6 +144,10 @@ public function keys($kind, array $options = [])
'name' => null
];

if ($options['number'] < 1) {
throw new \InvalidArgumentException('Number of keys cannot be less than 1.');
}

$path = [];
if (count($options['ancestors']) > 0) {
$path = $options['ancestors'];
Expand All @@ -159,7 +164,11 @@ public function keys($kind, array $options = [])
'namespaceId' => $this->namespaceId
]);

$keys = array_fill(0, $options['number'], $key);
$keys = [$key];

for ($i = 1; $i < $options['number']; $i++) {
$keys[] = clone $key;
}

return $keys;
}
Expand Down
21 changes: 21 additions & 0 deletions Datastore/tests/Unit/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ public function testKeysNumber()
$this->assertCount(10, $keys);
}

public function testKeysNumberCopy()
{
$keys = $this->operation->keys('Foo', [
'number' => 2
]);

$keys[0]->setLastElementIdentifier(10);
$this->assertEquals(10, $keys[0]->pathEndIdentifier());
$this->assertNull($keys[1]->pathEndIdentifier());
}

/**
* @expectedException InvalidArgumentException
*/
public function testKeysNumberZero()
{
$this->operation->keys('Foo', [
'number' => 0
]);
}

public function testKeysAncestors()
{
$keys = $this->operation->keys('Foo', [
Expand Down

0 comments on commit 341365d

Please sign in to comment.