Skip to content

Commit 03b985a

Browse files
committed
Fix the TransactionTest unit tests
1 parent 963c632 commit 03b985a

3 files changed

Lines changed: 235 additions & 226 deletions

File tree

Datastore/src/Operation.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ public function runQuery(QueryInterface $query, array $options = [])
543543
if (isset($remainingLimit)) {
544544
$requestQueryArr['limit'] = $remainingLimit;
545545
}
546+
546547
$request = [
547548
'projectId' => $this->projectId,
548549
'partitionId' => $this->partitionId(
@@ -924,6 +925,10 @@ private function readOptions(array $options = [])
924925
'readTime' => $options['readTime']
925926
]);
926927

928+
if (count($readOptions) > 1) {
929+
throw new InvalidArgumentException('ReadOptions can only be one of `readConsistency`, `transaction` or `readTime`.');
930+
}
931+
927932
return array_filter([
928933
'readOptions' => $readOptions,
929934
]);
@@ -961,29 +966,33 @@ private function sortEntities(array $entities, array $keys)
961966
*/
962967
private function createReadOptions(array $options)
963968
{
964-
$empty = true;
969+
$totalSet = 0;
965970

966971
$readOptions = new ReadOptions();
967972
if (isset($options['transaction'])) {
968-
$empty = false;
973+
$totalSet++;
969974
$readOptions->setTransaction(base64_decode($options['transaction']));
970975
}
971976
if (isset($options['readConsistency'])) {
972-
$empty = false;
977+
$totalSet++;
973978
$readOptions->setReadConsistency($options['readConsistency']);
974979
}
975980
if (isset($options['readTime'])) {
976-
$empty = false;
981+
$totalSet++;
977982
$protoTime = new ProtobufTimestamp();
978983
// Timestamps can be passed as an array or a Timestamp object.
979984
$protoTime->mergeFromJsonString(json_encode($options['readTime']));
980985
$readOptions->setReadTime($protoTime);
981986
}
982987

983-
if ($empty) {
988+
if ($totalSet === 0) {
984989
return null;
985990
}
986991

992+
if ($totalSet > 1) {
993+
throw new InvalidArgumentException('Only one of `readConsistency`, `transaction` or `readTime` may be set.');
994+
}
995+
987996
return $readOptions;
988997
}
989998

Datastore/src/TransactionTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Google\Cloud\Datastore\Query\AggregationQuery;
2121
use Google\Cloud\Datastore\Query\QueryInterface;
22+
use InvalidArgumentException;
2223

2324
/**
2425
* Common operations for datastore transactions.
@@ -126,6 +127,13 @@ public function lookup(Key $key, array $options = [])
126127
*/
127128
public function lookupBatch(array $keys, array $options = [])
128129
{
130+
if (isset($options['readTime']) || isset($options['readConsistency']) || isset($options['newTransaction'])) {
131+
throw new InvalidArgumentException(
132+
'Transaction::lookup and Transaction::batchLookup methods ' .
133+
'do not take a `readTime`, `readConsistency` or `newTransaction` option.'
134+
);
135+
}
136+
129137
return $this->operation->lookup($keys, $options + [
130138
'transaction' => $this->transactionId
131139
]);

0 commit comments

Comments
 (0)