diff --git a/Datastore/src/Connection/Rest.php b/Datastore/src/Connection/Rest.php index 8636917c52da..b1ecccd6d00a 100644 --- a/Datastore/src/Connection/Rest.php +++ b/Datastore/src/Connection/Rest.php @@ -77,8 +77,8 @@ public function allocateIds(array $args) */ public function beginTransaction(array $args) { - $this->setReadTime($args); - return $this->send('projects', 'beginTransaction', $args); + $argsWithReadTime = $this->setReadTime($args); + return $this->send('projects', 'beginTransaction', $argsWithReadTime); } /** @@ -94,8 +94,8 @@ public function commit(array $args) */ public function lookup(array $args) { - $this->setReadTime($args); - return $this->send('projects', 'lookup', $args); + $argsWithReadTime = $this->setReadTime($args); + return $this->send('projects', 'lookup', $argsWithReadTime); } /** @@ -111,27 +111,29 @@ public function rollback(array $args) */ public function runQuery(array $args) { - $this->setReadTime($args); - return $this->send('projects', 'runQuery', $args); + $argsWithReadTime = $this->setReadTime($args); + return $this->send('projects', 'runQuery', $argsWithReadTime); } /** * @param array $args */ - private function setReadTime(array &$args) + private function setReadTime(array $args) { if (isset($args['readOptions']['readTime'])) { - $time = $args['readOptions']['readTime']->getSeconds(); - $date = date('Y/m/d H:i:s.u\Z', $time); - $args['readOptions']['readTime'] = new Timestamp( - new \DateTime($date) - ); + $this->setReadTimeOption($args['readOptions']['readTime']); } elseif (isset($args['transactionOptions']['readOnly']['readTime'])) { - $time = $args['transactionOptions']['readOnly']['readTime']->getSeconds(); - $date = date('Y/m/d H:i:s.u\Z', $time); - $args['transactionOptions']['readOnly']['readTime'] = new Timestamp( - new \DateTime($date) - ); + $this->setReadTimeOption($args['transactionOptions']['readOnly']['readTime']); } + return $args; + } + + /** + * @param timestamp $readTime + */ + private function setReadTimeOption(&$readTime) + { + $date = date('Y/m/d H:i:s.u\Z', $readTime->getSeconds()); + $readTime = new Timestamp(new \DateTime($date)); } } diff --git a/Datastore/tests/System/RunQueryTest.php b/Datastore/tests/System/RunQueryTest.php index 06ac632a16ec..b3a8f0683ee3 100644 --- a/Datastore/tests/System/RunQueryTest.php +++ b/Datastore/tests/System/RunQueryTest.php @@ -363,20 +363,14 @@ public function testRunQueryWithReadTime(DatastoreClient $client) ->kind($kind) ->filter('__key__', '=', $key); $result = $client->runQuery($query); - $personListEntities = []; - foreach ($result as $person) { - $personListEntities[] = $person; - } + $personListEntities = iterator_to_array($result); // Person lastName should be the lastName AFTER update $this->assertEquals($personListEntities[0]['lastName'], $newLastName); // Person lastName should be the lastName BEFORE update $result = $client->runQuery($query, ['readTime' => $time]); - $personListEntities = []; - foreach ($result as $person) { - $personListEntities[] = $person; - } + $personListEntities = iterator_to_array($result); $this->assertEquals($personListEntities[0]['lastName'], $lastName); } diff --git a/Datastore/tests/System/RunTransactionTest.php b/Datastore/tests/System/RunTransactionTest.php index d177ccb8551e..b4e30dec4e42 100644 --- a/Datastore/tests/System/RunTransactionTest.php +++ b/Datastore/tests/System/RunTransactionTest.php @@ -117,11 +117,7 @@ public function testTransactionWithReadTime(DatastoreClient $client) ->filter('__key__', '=', $key) ->hasAncestor($ancKey); $result = $client->runQuery($query); - $personListEntities = []; - foreach ($result as $person) { - $personListEntities[] = $person; - } - + $personListEntities = iterator_to_array($result); // Person lastName should be the lastName AFTER update $this->assertEquals($personListEntities[0]['lastName'], $newLastName); @@ -130,10 +126,7 @@ public function testTransactionWithReadTime(DatastoreClient $client) ); // runQuery function: Person lastName should be the lastName BEFORE update $persons = $transaction2->runQuery($query); - $personListEntities = []; - foreach ($persons as $person) { - $personListEntities[] = $person; - } + $personListEntities = iterator_to_array($persons); $this->assertEquals($personListEntities[0]['lastName'], $lastName); // lookUp function: Person lastName should be the lastName BEFORE update