fix php code style

pull/8045/head
thinkerou 8 years ago
parent 1b5f057995
commit 9392b04517
  1. 1
      src/php/lib/Grpc/BaseStub.php
  2. 64
      src/php/tests/interop/interop_client.php
  3. 3
      src/php/tests/unit_tests/CallCredentials2Test.php

@ -182,6 +182,7 @@ class BaseStub
} else { } else {
$hostname = $this->hostname; $hostname = $this->hostname;
} }
return 'https://'.$hostname.$service_name; return 'https://'.$hostname.$service_name;
} }

@ -70,7 +70,8 @@ function hardAssertIfStatusOk($status)
*/ */
function emptyUnary($stub) function emptyUnary($stub)
{ {
list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait(); list($result, $status) =
$stub->EmptyCall(new grpc\testing\EmptyMessage())->wait();
hardAssertIfStatusOk($status); hardAssertIfStatusOk($status);
hardAssert($result !== null, 'Call completed with a null response'); hardAssert($result !== null, 'Call completed with a null response');
} }
@ -92,8 +93,8 @@ function largeUnary($stub)
* @param $fillUsername boolean whether to fill result with username * @param $fillUsername boolean whether to fill result with username
* @param $fillOauthScope boolean whether to fill result with oauth scope * @param $fillOauthScope boolean whether to fill result with oauth scope
*/ */
function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false, function performLargeUnary($stub, $fillUsername = false,
$callback = false) $fillOauthScope = false, $callback = false)
{ {
$request_len = 271828; $request_len = 271828;
$response_len = 314159; $response_len = 314159;
@ -141,8 +142,9 @@ function serviceAccountCreds($stub, $args)
$jsonKey = json_decode( $jsonKey = json_decode(
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
true); true);
$result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); $result = performLargeUnary($stub, $fillUsername = true,
hardAssert($result->getUsername() == $jsonKey['client_email'], $fillOauthScope = true);
hardAssert($result->getUsername() === $jsonKey['client_email'],
'invalid email returned'); 'invalid email returned');
hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false, hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false,
'invalid oauth scope returned'); 'invalid oauth scope returned');
@ -163,8 +165,9 @@ function computeEngineCreds($stub, $args)
if (!array_key_exists('default_service_account', $args)) { if (!array_key_exists('default_service_account', $args)) {
throw new Exception('Missing default_service_account'); throw new Exception('Missing default_service_account');
} }
$result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); $result = performLargeUnary($stub, $fillUsername = true,
hardAssert($args['default_service_account'] == $result->getUsername(), $fillOauthScope = true);
hardAssert($args['default_service_account'] === $result->getUsername(),
'invalid email returned'); 'invalid email returned');
} }
@ -179,8 +182,9 @@ function jwtTokenCreds($stub, $args)
$jsonKey = json_decode( $jsonKey = json_decode(
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
true); true);
$result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); $result = performLargeUnary($stub, $fillUsername = true,
hardAssert($result->getUsername() == $jsonKey['client_email'], $fillOauthScope = true);
hardAssert($result->getUsername() === $jsonKey['client_email'],
'invalid email returned'); 'invalid email returned');
} }
@ -195,8 +199,9 @@ function oauth2AuthToken($stub, $args)
$jsonKey = json_decode( $jsonKey = json_decode(
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
true); true);
$result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); $result = performLargeUnary($stub, $fillUsername = true,
hardAssert($result->getUsername() == $jsonKey['client_email'], $fillOauthScope = true);
hardAssert($result->getUsername() === $jsonKey['client_email'],
'invalid email returned'); 'invalid email returned');
} }
@ -211,6 +216,7 @@ function updateAuthMetadataCallback($context)
foreach ($result as $key => $value) { foreach ($result as $key => $value) {
$metadata[strtolower($key)] = $value; $metadata[strtolower($key)] = $value;
} }
return $metadata; return $metadata;
} }
@ -226,9 +232,10 @@ function perRpcCreds($stub, $args)
file_get_contents(getenv(CredentialsLoader::ENV_VAR)), file_get_contents(getenv(CredentialsLoader::ENV_VAR)),
true); true);
$result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true, $result = performLargeUnary($stub, $fillUsername = true,
$fillOauthScope = true,
'updateAuthMetadataCallback'); 'updateAuthMetadataCallback');
hardAssert($result->getUsername() == $jsonKey['client_email'], hardAssert($result->getUsername() === $jsonKey['client_email'],
'invalid email returned'); 'invalid email returned');
} }
@ -283,7 +290,8 @@ function serverStreaming($stub)
foreach ($call->responses() as $value) { foreach ($call->responses() as $value) {
hardAssert($i < 4, 'Too many responses'); hardAssert($i < 4, 'Too many responses');
$payload = $value->getPayload(); $payload = $value->getPayload();
hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, hardAssert(
$payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
'Payload '.$i.' had the wrong type'); 'Payload '.$i.' had the wrong type');
hardAssert(strlen($payload->getBody()) === $sizes[$i], hardAssert(strlen($payload->getBody()) === $sizes[$i],
'Response '.$i.' had the wrong length'); 'Response '.$i.' had the wrong length');
@ -318,7 +326,8 @@ function pingPong($stub)
hardAssert($response !== null, 'Server returned too few responses'); hardAssert($response !== null, 'Server returned too few responses');
$payload = $response->getPayload(); $payload = $response->getPayload();
hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, hardAssert(
$payload->getType() === grpc\testing\PayloadType::COMPRESSABLE,
'Payload '.$i.' had the wrong type'); 'Payload '.$i.' had the wrong type');
hardAssert(strlen($payload->getBody()) === $response_lengths[$i], hardAssert(strlen($payload->getBody()) === $response_lengths[$i],
'Payload '.$i.' had the wrong length'); 'Payload '.$i.' had the wrong length');
@ -425,8 +434,8 @@ function customMetadata($stub)
$initial_metadata = $call->getMetadata(); $initial_metadata = $call->getMetadata();
hardAssert(array_key_exists($ECHO_INITIAL_KEY, $initial_metadata), hardAssert(array_key_exists($ECHO_INITIAL_KEY, $initial_metadata),
'Initial metadata does not contain expected key'); 'Initial metadata does not contain expected key');
hardAssert($initial_metadata[$ECHO_INITIAL_KEY][0] == hardAssert(
$ECHO_INITIAL_VALUE, $initial_metadata[$ECHO_INITIAL_KEY][0] === $ECHO_INITIAL_VALUE,
'Incorrect initial metadata value'); 'Incorrect initial metadata value');
list($result, $status) = $call->wait(); list($result, $status) = $call->wait();
@ -435,8 +444,9 @@ function customMetadata($stub)
$trailing_metadata = $call->getTrailingMetadata(); $trailing_metadata = $call->getTrailingMetadata();
hardAssert(array_key_exists($ECHO_TRAILING_KEY, $trailing_metadata), hardAssert(array_key_exists($ECHO_TRAILING_KEY, $trailing_metadata),
'Trailing metadata does not contain expected key'); 'Trailing metadata does not contain expected key');
hardAssert($trailing_metadata[$ECHO_TRAILING_KEY][0] == hardAssert(
$ECHO_TRAILING_VALUE, 'Incorrect trailing metadata value'); $trailing_metadata[$ECHO_TRAILING_KEY][0] === $ECHO_TRAILING_VALUE,
'Incorrect trailing metadata value');
$streaming_call = $stub->FullDuplexCall($metadata); $streaming_call = $stub->FullDuplexCall($metadata);
@ -451,7 +461,7 @@ function customMetadata($stub)
hardAssert(array_key_exists($ECHO_TRAILING_KEY, hardAssert(array_key_exists($ECHO_TRAILING_KEY,
$streaming_trailing_metadata), $streaming_trailing_metadata),
'Trailing metadata does not contain expected key'); 'Trailing metadata does not contain expected key');
hardAssert($streaming_trailing_metadata[$ECHO_TRAILING_KEY][0] == hardAssert($streaming_trailing_metadata[$ECHO_TRAILING_KEY][0] ===
$ECHO_TRAILING_VALUE, 'Incorrect trailing metadata value'); $ECHO_TRAILING_VALUE, 'Incorrect trailing metadata value');
} }
@ -506,7 +516,7 @@ function _makeStub($args)
throw new Exception('Missing argument: --test_case is required'); throw new Exception('Missing argument: --test_case is required');
} }
if ($args['server_port'] == 443) { if ($args['server_port'] === 443) {
$server_address = $args['server_host']; $server_address = $args['server_host'];
} else { } else {
$server_address = $args['server_host'].':'.$args['server_port']; $server_address = $args['server_host'].':'.$args['server_port'];
@ -548,7 +558,7 @@ function _makeStub($args)
if (in_array($test_case, ['service_account_creds', if (in_array($test_case, ['service_account_creds',
'compute_engine_creds', 'jwt_token_creds', ])) { 'compute_engine_creds', 'jwt_token_creds', ])) {
if ($test_case == 'jwt_token_creds') { if ($test_case === 'jwt_token_creds') {
$auth_credentials = ApplicationDefaultCredentials::getCredentials(); $auth_credentials = ApplicationDefaultCredentials::getCredentials();
} else { } else {
$auth_credentials = ApplicationDefaultCredentials::getCredentials( $auth_credentials = ApplicationDefaultCredentials::getCredentials(
@ -558,7 +568,7 @@ function _makeStub($args)
$opts['update_metadata'] = $auth_credentials->getUpdateMetadataFunc(); $opts['update_metadata'] = $auth_credentials->getUpdateMetadataFunc();
} }
if ($test_case == 'oauth2_auth_token') { if ($test_case === 'oauth2_auth_token') {
$auth_credentials = ApplicationDefaultCredentials::getCredentials( $auth_credentials = ApplicationDefaultCredentials::getCredentials(
$args['oauth_scope'] $args['oauth_scope']
); );
@ -578,8 +588,9 @@ function _makeStub($args)
$opts['update_metadata'] = $update_metadata; $opts['update_metadata'] = $update_metadata;
} }
if ($test_case == 'unimplemented_method') { if ($test_case === 'unimplemented_method') {
$stub = new grpc\testing\UnimplementedServiceClient($server_address, $opts); $stub = new grpc\testing\UnimplementedServiceClient($server_address,
$opts);
} else { } else {
$stub = new grpc\testing\TestServiceClient($server_address, $opts); $stub = new grpc\testing\TestServiceClient($server_address, $opts);
} }
@ -656,7 +667,8 @@ function interop_main($args, $stub = false)
return $stub; return $stub;
} }
if (isset($_SERVER['PHP_SELF']) && preg_match('/interop_client/', $_SERVER['PHP_SELF'])) { if (isset($_SERVER['PHP_SELF']) &&
preg_match('/interop_client/', $_SERVER['PHP_SELF'])) {
$args = getopt('', ['server_host:', 'server_port:', 'test_case:', $args = getopt('', ['server_host:', 'server_port:', 'test_case:',
'use_tls::', 'use_test_ca::', 'use_tls::', 'use_test_ca::',
'server_host_override:', 'oauth_scope:', 'server_host_override:', 'oauth_scope:',

@ -170,7 +170,7 @@ class CallCredentials2Test extends PHPUnit_Framework_TestCase
$this->assertTrue(is_string($context->service_url)); $this->assertTrue(is_string($context->service_url));
$this->assertTrue(is_string($context->method_name)); $this->assertTrue(is_string($context->method_name));
return "a string"; return 'a string';
} }
public function testCallbackWithInvalidReturnValue() public function testCallbackWithInvalidReturnValue()
@ -196,5 +196,4 @@ class CallCredentials2Test extends PHPUnit_Framework_TestCase
$this->assertTrue($event->send_close); $this->assertTrue($event->send_close);
$this->assertTrue($event->status->code == Grpc\STATUS_UNAUTHENTICATED); $this->assertTrue($event->status->code == Grpc\STATUS_UNAUTHENTICATED);
} }
} }

Loading…
Cancel
Save