Skip to content

Commit

Permalink
Add unit testing for GRPC only clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Apr 24, 2024
1 parent 8355a80 commit b667e63
Show file tree
Hide file tree
Showing 15 changed files with 774 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"Testing\\DisableSnippets\\": "tests/Unit/ProtoTests/DisableSnippets/out/src",
"Testing\\GrpcServiceConfig\\": "tests/Unit/ProtoTests/GrpcServiceConfig/out/src",
"Testing\\ResourceNames\\": "tests/Unit/ProtoTests/ResourceNames/out/src",
"Testing\\RoutingHeaders\\": "tests/Unit/ProtoTests/RoutingHeaders/out/src"
"Testing\\RoutingHeaders\\": "tests/Unit/ProtoTests/RoutingHeaders/out/src",
"Testing\\BasicGrpcOnly\\": "tests/Unit/ProtoTests/BasicGrpcOnly/out/src"
}
},
"require": {
Expand Down
2 changes: 1 addition & 1 deletion rules_php_gapic/php_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def php_gapic_srcjar(
if transport == None:
transport = "grpc+rest"
if transport != "grpc+rest" and transport != "rest" and transport != "grpc":
fail("Error: Only 'grpc+rest' or 'rest' transports are supported")
fail("Error: Only 'grpc+rest', 'rest' or `grpc` transports are supported")

# Set plugin arguments.
plugin_args = ["metadata"] # Generate the gapic_metadata.json file.
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function parseTransport(?string $transport): int
return static::REST;
}
if ($transport === "grpc") {
throw new \Exception("gRPC-only PHP clients are not supported at this time");
return static::GRPC;
}

throw new \Exception("Transport $transport not supported");
Expand Down
53 changes: 53 additions & 0 deletions tests/Unit/ProtoTests/BasicGrpcOnly/basicGrpcOnly.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
syntax = "proto3";

package testing.basicGrpcOnly;

// php_namespace option not included; to test generating namespace from proto package.

import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/api/field_behavior.proto";

// This is a basicGrpcOnly service.
service BasicGrpcOnly {
option (google.api.default_host) = "basicGrpcOnly.example.com";
option (google.api.oauth_scopes) = "scope1,scope2";

// Test summary text for AMethod
rpc AMethod(Request) returns(Response) {
option (google.api.http) = {
post: "/path:aMethod"
body: "*"
};
}

// Test including method args.
rpc MethodWithArgs(RequestWithArgs) returns(Response) {
option (google.api.http) = {
post: "/path:methodWithArgs"
body: "*"
};
};
}

message Request {
}

message PartOfRequestA {}
message PartOfRequestB {}
message PartOfRequestC {}

message RequestWithArgs {
// A required field...
string a_string = 1 [(google.api.field_behavior) = REQUIRED];
// ...and an optional field.
int32 an_int = 2;
// ...and a repeated message type, which checks that an extra import is *not* added,
// in contrast to a paginated method where an extra import *is* added.
repeated PartOfRequestA part_of_request_a = 4 [(google.api.field_behavior) = REQUIRED];
repeated PartOfRequestB part_of_request_b = 5;
PartOfRequestC part_of_request_c = 6;
}

message Response {
}
10 changes: 10 additions & 0 deletions tests/Unit/ProtoTests/BasicGrpcOnly/basic_grpc_only_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: google.api.Service
config_version: 3

authentication:
rules:
- selector: 'testing.basicGrpcOnly.BasicGrpcOnly.*'
oauth:
canonical_scopes: |-
scope1,
scope2
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/

require_once __DIR__ . '/../../../vendor/autoload.php';

// [START basicGrpcOnly_generated_BasicGrpcOnly_AMethod_sync]
use Google\ApiCore\ApiException;
use Testing\BasicGrpcOnly\Client\BasicGrpcOnlyClient;
use Testing\BasicGrpcOnly\Request;
use Testing\BasicGrpcOnly\Response;

/**
* Test summary text for AMethod
*
* This sample has been automatically generated and should be regarded as a code
* template only. It will require modifications to work:
* - It may require correct/in-range values for request initialization.
* - It may require specifying regional endpoints when creating the service client,
* please see the apiEndpoint client configuration option for more details.
*/
function a_method_sample(): void
{
// Create a client.
$basicGrpcOnlyClient = new BasicGrpcOnlyClient();

// Prepare the request message.
$request = new Request();

// Call the API and handle any network failures.
try {
/** @var Response $response */
$response = $basicGrpcOnlyClient->aMethod($request);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}
// [END basicGrpcOnly_generated_BasicGrpcOnly_AMethod_sync]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/

require_once __DIR__ . '/../../../vendor/autoload.php';

// [START basicGrpcOnly_generated_BasicGrpcOnly_MethodWithArgs_sync]
use Google\ApiCore\ApiException;
use Testing\BasicGrpcOnly\Client\BasicGrpcOnlyClient;
use Testing\BasicGrpcOnly\PartOfRequestA;
use Testing\BasicGrpcOnly\RequestWithArgs;
use Testing\BasicGrpcOnly\Response;

/**
* Test including method args.
*
* @param string $aString A required field...
*/
function method_with_args_sample(string $aString): void
{
// Create a client.
$basicGrpcOnlyClient = new BasicGrpcOnlyClient();

// Prepare the request message.
$partOfRequestA = [new PartOfRequestA()];
$request = (new RequestWithArgs())
->setAString($aString)
->setPartOfRequestA($partOfRequestA);

// Call the API and handle any network failures.
try {
/** @var Response $response */
$response = $basicGrpcOnlyClient->methodWithArgs($request);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}

/**
* Helper to execute the sample.
*
* This sample has been automatically generated and should be regarded as a code
* template only. It will require modifications to work:
* - It may require correct/in-range values for request initialization.
* - It may require specifying regional endpoints when creating the service client,
* please see the apiEndpoint client configuration option for more details.
*/
function callSample(): void
{
$aString = '[A_STRING]';

method_with_args_sample($aString);
}
// [END basicGrpcOnly_generated_BasicGrpcOnly_MethodWithArgs_sync]

0 comments on commit b667e63

Please sign in to comment.