Skip to content

Commit c7618b4

Browse files
committed
Modify the operation class to use the new validateOptions method
1 parent b8e27ef commit c7618b4

File tree

3 files changed

+34
-57
lines changed

3 files changed

+34
-57
lines changed

Datastore/src/DatastoreClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ public function updateBatch(array $entities, array $options = [])
753753
'allowOverwrite' => false
754754
];
755755

756-
$this->operation->checkOverwrite($entities, $options['allowOverwrite']);
756+
$this->operation->checkOverwrite($entities, $this->pluck('allowOverwrite', $options));
757757
$mutations = [];
758758
foreach ($entities as $entity) {
759759
$mutations[] = $this->operation->mutation('update', $entity, Entity::class);
@@ -917,7 +917,7 @@ public function deleteBatch(array $keys, array $options = [])
917917

918918
$mutations = [];
919919
foreach ($keys as $key) {
920-
$mutations[] = $this->operation->mutation('delete', $key, Key::class, $options['baseVersion']);
920+
$mutations[] = $this->operation->mutation('delete', $key, Key::class, $this->pluck('baseVersion', $options, false));
921921
}
922922

923923
return $this->operation->commit($mutations, $options);

Datastore/src/Operation.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
namespace Google\Cloud\Datastore;
1919

20+
use Google\ApiCore\Options\CallOptions;
2021
use Google\ApiCore\Serializer;
22+
use Google\Cloud\Core\ApiHelperTrait;
2123
use Google\Cloud\Core\Timestamp;
2224
use Google\Cloud\Core\TimestampTrait;
2325
use Google\Cloud\Core\ValidateTrait;
@@ -62,6 +64,7 @@ class Operation
6264
use DatastoreTrait;
6365
use ValidateTrait;
6466
use TimestampTrait;
67+
use ApiHelperTrait;
6568

6669
/**
6770
* @var DatastoreClient
@@ -682,30 +685,28 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option
682685
public function commit(array $mutations, array $options = [])
683686
{
684687
$options += [
685-
'transaction' => null,
686688
'databaseId' => $this->databaseId,
689+
'projectId' => $this->projectId,
690+
'mutations' => $mutations,
687691
];
688692

689-
$transactionMode = isset($options['transaction']) ? Mode::TRANSACTIONAL : Mode::NON_TRANSACTIONAL;
690-
691-
$protoMutations = [];
692-
foreach ($mutations as $mutation) {
693-
$protoMutation = new Mutation();
694-
$protoMutation->mergeFromJsonString(json_encode($mutation));
695-
$protoMutations[] = $protoMutation;
696-
}
697-
698-
$commitRequest = (new CommitRequest())
699-
->setMutations($protoMutations)
700-
->setDatabaseId($options['databaseId'])
701-
->setProjectId($this->projectId)
702-
->setMode($transactionMode);
693+
/**
694+
* @var CallOptions $callOptions
695+
* @var CommitRequest $commitRequest
696+
*/
697+
[$commitRequest, $callOptions] = $this->validateOptions(
698+
$options,
699+
new CommitRequest(),
700+
CallOptions::class,
701+
);
703702

704-
if ($transactionMode === Mode::TRANSACTIONAL) {
705-
$commitRequest->setTransaction(base64_decode($options['transaction']));
706-
}
703+
$commitRequest->setMode(
704+
empty($commitRequest->getTransaction())
705+
? MODE::NON_TRANSACTIONAL
706+
: MODE::TRANSACTIONAL
707+
);
707708

708-
$commitResponse = $this->gapicClient->commit($commitRequest, $options);
709+
$commitResponse = $this->gapicClient->commit($commitRequest, $callOptions);
709710

710711
return $this->serializer->encodeMessage($commitResponse);
711712
}

Datastore/tests/Unit/DatastoreClientTest.php

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,18 @@ public function testDatastoreCrudOperations()
374374

375375
// 1. Test Insert
376376
$this->gapicClient->commit(Argument::that(function(CommitRequest $request) {
377-
return $request->getMutations()[0]->getOperation() == 'insert';
378-
}), [
379-
'transaction' => null,
380-
'databaseId' => self::DATABASE
381-
])->shouldBeCalledTimes(1)
377+
switch ($request->getMutations()[0]->getOperation()) {
378+
case 'insert':
379+
case 'update':
380+
case 'upsert':
381+
case 'delete':
382+
return true;
383+
default:
384+
$this->fail('Unexpected value received to the commit function');
385+
return false;
386+
}
387+
return true;
388+
}), Argument::any())->shouldBeCalledTimes(4)
382389
->willReturn($commitResponse);
383390

384391
$this->client->insert($entity);
@@ -387,46 +394,15 @@ public function testDatastoreCrudOperations()
387394
$updateData = ['firstName' => 'Jeffrey'];
388395
$updateEntity = $this->client->entity($key, $updateData, ['populatedByService' => true]);
389396

390-
$this->gapicClient->commit(
391-
Argument::that(function (CommitRequest $request) {
392-
return $request->getMutations()[0]->getOperation() == 'update';
393-
}),
394-
[
395-
'transaction' => null,
396-
'databaseId' => self::DATABASE,
397-
'allowOverwrite' => false,
398-
]
399-
)->shouldBeCalledTimes(1)
400-
->willReturn($commitResponse);
401-
402397
$this->client->update($updateEntity);
403398

404399
// 3. Test Upsert
405400
$upsertData = ['firstName' => 'Geoff'];
406401
$upsertEntity = $this->client->entity($key, $upsertData);
407402

408-
$this->gapicClient->commit(
409-
Argument::that(function (CommitRequest $request) {
410-
return $request->getMutations()[0]->getOperation() == 'upsert';
411-
}),
412-
[
413-
'transaction' => null,
414-
'databaseId' => self::DATABASE,
415-
]
416-
)->shouldBeCalledTimes(1)
417-
->willReturn($commitResponse);
418-
419403
$this->client->upsert($upsertEntity);
420404

421405
// 4. Test Delete
422-
$this->gapicClient->commit(Argument::that(function(CommitRequest $request) {
423-
return $request->getMutations()[0]->getOperation() == 'delete';
424-
}), [
425-
'baseVersion' => null,
426-
'transaction' => null,
427-
'databaseId' => self::DATABASE,
428-
])->shouldBeCalledTimes(1)->willReturn($commitResponse);
429-
430406
$this->client->delete($key);
431407
}
432408

0 commit comments

Comments
 (0)