Skip to content

Commit a5c7c74

Browse files
committed
add validateOptions helper method
1 parent 84034cc commit a5c7c74

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Core/src/ApiHelperTrait.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Google\ApiCore\ArrayTrait;
2121
use Google\ApiCore\Options\CallOptions;
2222
use Google\Protobuf\NullValue;
23+
use Google\Protobuf\Internal\Message;
2324

2425
/**
2526
* @internal
@@ -264,4 +265,42 @@ private function splitOptionalArgs(array $input, array $extraAllowedKeys = []):
264265

265266
return [$input, $callOptions];
266267
}
268+
269+
/**
270+
* Helper method used to validate optons based on the supplied $optionTypes
271+
* $optionTypes can be an array of string keys, a protobuf Message classname, or a
272+
* the CallOptions classname. Parameters are split and returned in the order
273+
* that the options types are provided.
274+
*/
275+
private function validateOptions(array $options, array|string ...$optionTypes): array
276+
{
277+
$splitOptions = [];
278+
foreach ($optionTypes as $optionType) {
279+
if (is_array($optionType)) {
280+
$splitOptions[] = $this->pluckArray($optionType, $options);
281+
} elseif (is_string($optionType)) {
282+
if (is_subclass_of($optionType, Message::class)) {
283+
$messageKeys = array_map(
284+
fn ($method) => lcfirst(substr($method, 3)),
285+
array_filter(
286+
get_class_methods($optionType),
287+
fn ($m) => 0 === strpos($m, 'get')
288+
)
289+
);
290+
$splitOptions[] = $this->pluckArray($messageKeys, $options);
291+
} elseif ($optionType === CallOptions::class) {
292+
$callOptionKeys = array_keys((new CallOptions([]))->toArray());
293+
$splitOptions[] = $this->pluckArray($callOptionKeys, $options);
294+
}
295+
}
296+
}
297+
298+
if (!empty($options)) {
299+
throw new \Exception(
300+
'Unexpected option(s) provided: ' . implode(', ', array_keys($options))
301+
);
302+
}
303+
304+
return $splitOptions;
305+
}
267306
}

Core/tests/Unit/ApiHelperTraitTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
namespace Google\Cloud\Core\Tests\Unit;
1919

20+
use Google\ApiCore\Options\CallOptions;
2021
use Google\Cloud\Core\Duration;
2122
use Google\Cloud\Core\Testing\GrpcTestTrait;
2223
use Google\Cloud\Core\Tests\Unit\Stubs\ApiHelpersTraitImpl;
24+
use Google\Cloud\Core\Tests\Unit\Stubs\TestMessage;
2325
use PHPUnit\Framework\TestCase;
2426
use Prophecy\PhpUnit\ProphecyTrait;
2527

@@ -258,4 +260,83 @@ public function unpackValueProvider()
258260
]
259261
];
260262
}
263+
264+
/**
265+
* @dataProvider validateOptionsProvider
266+
*/
267+
public function testValidateOptions($options, $optionTypes, $expected)
268+
{
269+
$this->assertEquals(
270+
$expected,
271+
$this->implementation->validateOptions($options, ...$optionTypes)
272+
);
273+
}
274+
275+
public function validateOptionsProvider()
276+
{
277+
return [
278+
[
279+
[
280+
'foo' => 'bar',
281+
'baz' => 'bat',
282+
'qux' => 'quux',
283+
],
284+
[
285+
['foo', 'baz', 'qux'],
286+
],
287+
[
288+
[
289+
'foo' => 'bar',
290+
'baz' => 'bat',
291+
'qux' => 'quux',
292+
],
293+
]
294+
],
295+
[
296+
[
297+
'baz' => 'bat',
298+
'qux' => 'quux',
299+
'timeoutMillis' => 123,
300+
],
301+
[
302+
CallOptions::class,
303+
TestMessage::class,
304+
['qux'],
305+
],
306+
[
307+
['timeoutMillis' => 123],
308+
['baz' => 'bat'],
309+
['qux' => 'quux'],
310+
]
311+
],
312+
[
313+
[
314+
'baz' => 'bat',
315+
],
316+
[
317+
['baz'],
318+
TestMessage::class,
319+
CallOptions::class,
320+
],
321+
[
322+
['baz' => 'bat'],
323+
[],
324+
[],
325+
]
326+
],
327+
];
328+
}
329+
330+
public function testValidateOptionsThrowsException()
331+
{
332+
$this->expectException(\Exception::class);
333+
$this->expectExceptionMessage('Unexpected option(s) provided: bar');
334+
335+
$options = [
336+
'foo' => 'bar',
337+
'bar' => 'baz',
338+
];
339+
340+
$this->implementation->validateOptions($options, ['foo']);
341+
}
261342
}

Core/tests/Unit/Stubs/ApiHelpersTraitImpl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ class ApiHelpersTraitImpl
3131
formatDurationForApi as public;
3232
formatValueForApi as public;
3333
unpackValue as public;
34+
validateOptions as public;
3435
}
3536
}

0 commit comments

Comments
 (0)