From 21016d9c462eba89fd49f1dfaf61866f0c2bfdd7 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 17 Dec 2024 22:14:28 +0000 Subject: [PATCH] chore!: promote MediaTranslation to v2 --- .repo-metadata-full.json | 4 +- MediaTranslation/README.md | 5 +- MediaTranslation/owlbot.py | 53 ++-- .../Client/SpeechTranslationServiceClient.php | 13 +- .../SpeechTranslationServiceGapicClient.php | 258 ------------------ .../SpeechTranslationServiceClient.php | 36 --- .../SpeechTranslationServiceGrpcClient.php | 48 ---- .../StreamingTranslateSpeechConfig.php | 4 +- .../StreamingTranslateSpeechResponse.php | 6 +- .../SpeechEventType.php | 2 - ...ranslateSpeechResponse_SpeechEventType.php | 16 -- .../TextTranslationResult.php | 6 +- ...lateSpeechResult_TextTranslationResult.php | 16 -- .../src/V1beta1/TranslateSpeechConfig.php | 10 +- .../SpeechTranslationServiceClientTest.php | 31 ++- .../SpeechTranslationServiceClientTest.php | 151 ---------- 16 files changed, 60 insertions(+), 599 deletions(-) delete mode 100644 MediaTranslation/src/V1beta1/Gapic/SpeechTranslationServiceGapicClient.php delete mode 100644 MediaTranslation/src/V1beta1/SpeechTranslationServiceClient.php delete mode 100644 MediaTranslation/src/V1beta1/SpeechTranslationServiceGrpcClient.php delete mode 100644 MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse_SpeechEventType.php delete mode 100644 MediaTranslation/src/V1beta1/StreamingTranslateSpeechResult_TextTranslationResult.php delete mode 100644 MediaTranslation/tests/Unit/V1beta1/SpeechTranslationServiceClientTest.php diff --git a/.repo-metadata-full.json b/.repo-metadata-full.json index 47293b0e4b2a..c8f1b02d4fcb 100644 --- a/.repo-metadata-full.json +++ b/.repo-metadata-full.json @@ -897,7 +897,7 @@ "MediaTranslation": { "language": "php", "distribution_name": "google/cloud-media-translation", - "release_level": "preview", + "release_level": "stable", "client_documentation": "https://cloud.google.com/php/docs/reference/cloud-media-translation/latest", "library_type": "GAPIC_AUTO", "api_shortname": "mediatranslation" @@ -1599,4 +1599,4 @@ "library_type": "GAPIC_AUTO", "api_shortname": "workflows" } -} +} \ No newline at end of file diff --git a/MediaTranslation/README.md b/MediaTranslation/README.md index 2831caff3f82..1b7a45098cfd 100644 --- a/MediaTranslation/README.md +++ b/MediaTranslation/README.md @@ -35,9 +35,8 @@ for more information about the debugging tools. ### Version -This component is considered beta. As such, it should be expected to be mostly -stable and we're working towards a release candidate. We will address issues -and requests with a higher priority. +This component is considered GA (generally available). As such, it will not introduce backwards-incompatible changes in +any minor or patch releases. We will address issues and requests with the highest priority. ### Next Steps diff --git a/MediaTranslation/owlbot.py b/MediaTranslation/owlbot.py index f269a4a94d58..491f11569c5b 100644 --- a/MediaTranslation/owlbot.py +++ b/MediaTranslation/owlbot.py @@ -1,4 +1,4 @@ -# Copyright 2021 Google LLC +# Copyright 2024 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -32,34 +32,25 @@ php.owlbot_main(src=src, dest=dest) -# Change the wording for the deprecation warning. +# remove class_alias code s.replace( - 'src/*/*_*.php', - r'will be removed in the next major release', - 'will be removed in a future release') - -### [START] protoc backwards compatibility fixes - -# roll back to private properties. -s.replace( - "src/**/V*/**/*.php", - r"Generated from protobuf field ([^\n]{0,})\n\s{5}\*/\n\s{4}protected \$", - r"""Generated from protobuf field \1 - */ - private $""") - -# Replace "Unwrapped" with "Value" for method names. -s.replace( - "src/**/V*/**/*.php", - r"public function ([s|g]\w{3,})Unwrapped", - r"public function \1Value" -) - -### [END] protoc backwards compatibility fixes - -# fix relative cloud.google.com links -s.replace( - "src/**/V*/**/*.php", - r"(.{0,})\]\((/.{0,})\)", - r"\1](https://cloud.google.com\2)" -) + "src/V*/**/*.php", + r"^// Adding a class alias for backwards compatibility with the previous class name.$" + + "\n" + + r"^class_alias\(.*\);$" + + "\n", + '') + +# format generated clients +subprocess.run([ + 'npm', + 'exec', + '--yes', + '--package=@prettier/plugin-php@^0.16', + '--', + 'prettier', + '**/Client/*', + '--write', + '--parser=php', + '--single-quote', + '--print-width=120']) diff --git a/MediaTranslation/src/V1beta1/Client/SpeechTranslationServiceClient.php b/MediaTranslation/src/V1beta1/Client/SpeechTranslationServiceClient.php index 43e2cd73f121..42651c5798cd 100644 --- a/MediaTranslation/src/V1beta1/Client/SpeechTranslationServiceClient.php +++ b/MediaTranslation/src/V1beta1/Client/SpeechTranslationServiceClient.php @@ -1,6 +1,6 @@ [ 'rest' => [ - 'restClientConfigPath' => __DIR__ . '/../resources/speech_translation_service_rest_client_config.php', + 'restClientConfigPath' => + __DIR__ . '/../resources/speech_translation_service_rest_client_config.php', ], ], ]; @@ -140,9 +138,6 @@ private static function getClientDefaults() * @type callable $clientCertSource * A callable which returns the client cert as a string. This can be used to * provide a certificate and private key to the transport layer for mTLS. - * @type false|LoggerInterface $logger - * A PSR-3 compliant logger. If set to false, logging is disabled, ignoring the - * 'GOOGLE_SDK_PHP_LOGGING' environment flag * } * * @throws ValidationException diff --git a/MediaTranslation/src/V1beta1/Gapic/SpeechTranslationServiceGapicClient.php b/MediaTranslation/src/V1beta1/Gapic/SpeechTranslationServiceGapicClient.php deleted file mode 100644 index 0992926a25fb..000000000000 --- a/MediaTranslation/src/V1beta1/Gapic/SpeechTranslationServiceGapicClient.php +++ /dev/null @@ -1,258 +0,0 @@ -streamingTranslateSpeech(); - * $stream->writeAll($requests); - * foreach ($stream->closeWriteAndReadAll() as $element) { - * // doSomethingWith($element); - * } - * // Alternatively: - * // Write requests individually, making read() calls if - * // required. Call closeWrite() once writes are complete, and read the - * // remaining responses from the server. - * $requests = [ - * $request, - * ]; - * $stream = $speechTranslationServiceClient->streamingTranslateSpeech(); - * foreach ($requests as $request) { - * $stream->write($request); - * // if required, read a single response from the stream - * $element = $stream->read(); - * // doSomethingWith($element) - * } - * $stream->closeWrite(); - * $element = $stream->read(); - * while (!is_null($element)) { - * // doSomethingWith($element) - * $element = $stream->read(); - * } - * } finally { - * $speechTranslationServiceClient->close(); - * } - * ``` - * - * @experimental - * - * @deprecated Please use the new service client {@see \Google\Cloud\MediaTranslation\V1beta1\Client\SpeechTranslationServiceClient}. - */ -class SpeechTranslationServiceGapicClient -{ - use GapicClientTrait; - - /** The name of the service. */ - const SERVICE_NAME = 'google.cloud.mediatranslation.v1beta1.SpeechTranslationService'; - - /** - * The default address of the service. - * - * @deprecated SERVICE_ADDRESS_TEMPLATE should be used instead. - */ - const SERVICE_ADDRESS = 'mediatranslation.googleapis.com'; - - /** The address template of the service. */ - private const SERVICE_ADDRESS_TEMPLATE = 'mediatranslation.UNIVERSE_DOMAIN'; - - /** The default port of the service. */ - const DEFAULT_SERVICE_PORT = 443; - - /** The name of the code generator, to be included in the agent header. */ - const CODEGEN_NAME = 'gapic'; - - /** The default scopes required by the service. */ - public static $serviceScopes = [ - 'https://www.googleapis.com/auth/cloud-platform', - ]; - - private static function getClientDefaults() - { - return [ - 'serviceName' => self::SERVICE_NAME, - 'apiEndpoint' => self::SERVICE_ADDRESS . ':' . self::DEFAULT_SERVICE_PORT, - 'clientConfig' => __DIR__ . '/../resources/speech_translation_service_client_config.json', - 'descriptorsConfigPath' => __DIR__ . '/../resources/speech_translation_service_descriptor_config.php', - 'gcpApiConfigPath' => __DIR__ . '/../resources/speech_translation_service_grpc_config.json', - 'credentialsConfig' => [ - 'defaultScopes' => self::$serviceScopes, - ], - 'transportConfig' => [ - 'rest' => [ - 'restClientConfigPath' => __DIR__ . '/../resources/speech_translation_service_rest_client_config.php', - ], - ], - ]; - } - - /** - * Constructor. - * - * @param array $options { - * Optional. Options for configuring the service API wrapper. - * - * @type string $apiEndpoint - * The address of the API remote host. May optionally include the port, formatted - * as ":". Default 'mediatranslation.googleapis.com:443'. - * @type string|array|FetchAuthTokenInterface|CredentialsWrapper $credentials - * The credentials to be used by the client to authorize API calls. This option - * accepts either a path to a credentials file, or a decoded credentials file as a - * PHP array. - * *Advanced usage*: In addition, this option can also accept a pre-constructed - * {@see \Google\Auth\FetchAuthTokenInterface} object or - * {@see \Google\ApiCore\CredentialsWrapper} object. Note that when one of these - * objects are provided, any settings in $credentialsConfig will be ignored. - * @type array $credentialsConfig - * Options used to configure credentials, including auth token caching, for the - * client. For a full list of supporting configuration options, see - * {@see \Google\ApiCore\CredentialsWrapper::build()} . - * @type bool $disableRetries - * Determines whether or not retries defined by the client configuration should be - * disabled. Defaults to `false`. - * @type string|array $clientConfig - * Client method configuration, including retry settings. This option can be either - * a path to a JSON file, or a PHP array containing the decoded JSON data. By - * default this settings points to the default client config file, which is - * provided in the resources folder. - * @type string|TransportInterface $transport - * The transport used for executing network requests. May be either the string - * `rest` or `grpc`. Defaults to `grpc` if gRPC support is detected on the system. - * *Advanced usage*: Additionally, it is possible to pass in an already - * instantiated {@see \Google\ApiCore\Transport\TransportInterface} object. Note - * that when this object is provided, any settings in $transportConfig, and any - * $apiEndpoint setting, will be ignored. - * @type array $transportConfig - * Configuration options that will be used to construct the transport. Options for - * each supported transport type should be passed in a key for that transport. For - * example: - * $transportConfig = [ - * 'grpc' => [...], - * 'rest' => [...], - * ]; - * See the {@see \Google\ApiCore\Transport\GrpcTransport::build()} and - * {@see \Google\ApiCore\Transport\RestTransport::build()} methods for the - * supported options. - * @type callable $clientCertSource - * A callable which returns the client cert as a string. This can be used to - * provide a certificate and private key to the transport layer for mTLS. - * } - * - * @throws ValidationException - * - * @experimental - */ - public function __construct(array $options = []) - { - $clientOptions = $this->buildClientOptions($options); - $this->setClientOptions($clientOptions); - } - - /** - * Performs bidirectional streaming speech translation: receive results while - * sending audio. This method is only available via the gRPC API (not REST). - * - * Sample code: - * ``` - * $speechTranslationServiceClient = new SpeechTranslationServiceClient(); - * try { - * $request = new StreamingTranslateSpeechRequest(); - * // Write all requests to the server, then read all responses until the - * // stream is complete - * $requests = [ - * $request, - * ]; - * $stream = $speechTranslationServiceClient->streamingTranslateSpeech(); - * $stream->writeAll($requests); - * foreach ($stream->closeWriteAndReadAll() as $element) { - * // doSomethingWith($element); - * } - * // Alternatively: - * // Write requests individually, making read() calls if - * // required. Call closeWrite() once writes are complete, and read the - * // remaining responses from the server. - * $requests = [ - * $request, - * ]; - * $stream = $speechTranslationServiceClient->streamingTranslateSpeech(); - * foreach ($requests as $request) { - * $stream->write($request); - * // if required, read a single response from the stream - * $element = $stream->read(); - * // doSomethingWith($element) - * } - * $stream->closeWrite(); - * $element = $stream->read(); - * while (!is_null($element)) { - * // doSomethingWith($element) - * $element = $stream->read(); - * } - * } finally { - * $speechTranslationServiceClient->close(); - * } - * ``` - * - * @param array $optionalArgs { - * Optional. - * - * @type int $timeoutMillis - * Timeout to use for this call. - * } - * - * @return \Google\ApiCore\BidiStream - * - * @throws ApiException if the remote call fails - * - * @experimental - */ - public function streamingTranslateSpeech(array $optionalArgs = []) - { - return $this->startCall('StreamingTranslateSpeech', StreamingTranslateSpeechResponse::class, $optionalArgs, null, Call::BIDI_STREAMING_CALL); - } -} diff --git a/MediaTranslation/src/V1beta1/SpeechTranslationServiceClient.php b/MediaTranslation/src/V1beta1/SpeechTranslationServiceClient.php deleted file mode 100644 index 104562676677..000000000000 --- a/MediaTranslation/src/V1beta1/SpeechTranslationServiceClient.php +++ /dev/null @@ -1,36 +0,0 @@ -_bidiRequest('/google.cloud.mediatranslation.v1beta1.SpeechTranslationService/StreamingTranslateSpeech', - ['\Google\Cloud\MediaTranslation\V1beta1\StreamingTranslateSpeechResponse','decode'], - $metadata, $options); - } - -} diff --git a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechConfig.php b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechConfig.php index aa6eaf9fb9a8..410ed6ffea0f 100644 --- a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechConfig.php +++ b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechConfig.php @@ -20,7 +20,7 @@ class StreamingTranslateSpeechConfig extends \Google\Protobuf\Internal\Message * * Generated from protobuf field .google.cloud.mediatranslation.v1beta1.TranslateSpeechConfig audio_config = 1 [(.google.api.field_behavior) = REQUIRED]; */ - private $audio_config = null; + protected $audio_config = null; /** * Optional. If `false` or omitted, the system performs * continuous translation (continuing to wait for and process audio even if @@ -38,7 +38,7 @@ class StreamingTranslateSpeechConfig extends \Google\Protobuf\Internal\Message * * Generated from protobuf field bool single_utterance = 2 [(.google.api.field_behavior) = OPTIONAL]; */ - private $single_utterance = false; + protected $single_utterance = false; /** * Constructor. diff --git a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse.php b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse.php index 509cd896b86b..8760ccf25ab8 100644 --- a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse.php +++ b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse.php @@ -22,20 +22,20 @@ class StreamingTranslateSpeechResponse extends \Google\Protobuf\Internal\Message * * Generated from protobuf field .google.rpc.Status error = 1 [(.google.api.field_behavior) = OUTPUT_ONLY]; */ - private $error = null; + protected $error = null; /** * Output only. The translation result that is currently being processed (is_final could be * true or false). * * Generated from protobuf field .google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResult result = 2 [(.google.api.field_behavior) = OUTPUT_ONLY]; */ - private $result = null; + protected $result = null; /** * Output only. Indicates the type of speech event. * * Generated from protobuf field .google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResponse.SpeechEventType speech_event_type = 3 [(.google.api.field_behavior) = OUTPUT_ONLY]; */ - private $speech_event_type = 0; + protected $speech_event_type = 0; /** * Constructor. diff --git a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse/SpeechEventType.php b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse/SpeechEventType.php index f8dd4b40d7ee..11f47e8b70d5 100644 --- a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse/SpeechEventType.php +++ b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse/SpeechEventType.php @@ -61,6 +61,4 @@ public static function value($name) } } -// Adding a class alias for backwards compatibility with the previous class name. -class_alias(SpeechEventType::class, \Google\Cloud\MediaTranslation\V1beta1\StreamingTranslateSpeechResponse_SpeechEventType::class); diff --git a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse_SpeechEventType.php b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse_SpeechEventType.php deleted file mode 100644 index dca5982be07b..000000000000 --- a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResponse_SpeechEventType.php +++ /dev/null @@ -1,16 +0,0 @@ -string translation = 1 [(.google.api.field_behavior) = OUTPUT_ONLY]; */ - private $translation = ''; + protected $translation = ''; /** * Output only. If `false`, this `StreamingTranslateSpeechResult` represents * an interim result that may change. If `true`, this is the final time the @@ -31,7 +31,7 @@ class TextTranslationResult extends \Google\Protobuf\Internal\Message * * Generated from protobuf field bool is_final = 2 [(.google.api.field_behavior) = OUTPUT_ONLY]; */ - private $is_final = false; + protected $is_final = false; /** * Constructor. @@ -119,6 +119,4 @@ public function setIsFinal($var) } -// Adding a class alias for backwards compatibility with the previous class name. -class_alias(TextTranslationResult::class, \Google\Cloud\MediaTranslation\V1beta1\StreamingTranslateSpeechResult_TextTranslationResult::class); diff --git a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResult_TextTranslationResult.php b/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResult_TextTranslationResult.php deleted file mode 100644 index ba1a8700cd42..000000000000 --- a/MediaTranslation/src/V1beta1/StreamingTranslateSpeechResult_TextTranslationResult.php +++ /dev/null @@ -1,16 +0,0 @@ -string audio_encoding = 1 [(.google.api.field_behavior) = REQUIRED]; */ - private $audio_encoding = ''; + protected $audio_encoding = ''; /** * Required. Source language code (BCP-47) of the input audio. * * Generated from protobuf field string source_language_code = 2 [(.google.api.field_behavior) = REQUIRED]; */ - private $source_language_code = ''; + protected $source_language_code = ''; /** * Required. Target language code (BCP-47) of the output. * * Generated from protobuf field string target_language_code = 3 [(.google.api.field_behavior) = REQUIRED]; */ - private $target_language_code = ''; + protected $target_language_code = ''; /** * Optional. Sample rate in Hertz of the audio data. Valid values are: * 8000-48000. 16000 is optimal. For best results, set the sampling rate of @@ -63,7 +63,7 @@ class TranslateSpeechConfig extends \Google\Protobuf\Internal\Message * * Generated from protobuf field int32 sample_rate_hertz = 4 [(.google.api.field_behavior) = OPTIONAL]; */ - private $sample_rate_hertz = 0; + protected $sample_rate_hertz = 0; /** * Optional. `google-provided-model/video` and * `google-provided-model/enhanced-phone-call` are premium models. @@ -71,7 +71,7 @@ class TranslateSpeechConfig extends \Google\Protobuf\Internal\Message * * Generated from protobuf field string model = 5 [(.google.api.field_behavior) = OPTIONAL]; */ - private $model = ''; + protected $model = ''; /** * Constructor. diff --git a/MediaTranslation/tests/Unit/V1beta1/Client/SpeechTranslationServiceClientTest.php b/MediaTranslation/tests/Unit/V1beta1/Client/SpeechTranslationServiceClientTest.php index 9713533addc6..b3bcb226de95 100644 --- a/MediaTranslation/tests/Unit/V1beta1/Client/SpeechTranslationServiceClientTest.php +++ b/MediaTranslation/tests/Unit/V1beta1/Client/SpeechTranslationServiceClientTest.php @@ -1,6 +1,6 @@ getMockBuilder(CredentialsWrapper::class)->disableOriginalConstructor()->getMock(); + return $this->getMockBuilder(CredentialsWrapper::class) + ->disableOriginalConstructor() + ->getMock(); } /** @return SpeechTranslationServiceClient */ @@ -85,10 +87,7 @@ public function streamingTranslateSpeechTest() $bidi->write($request); $responses = []; $responses[] = $bidi->read(); - $bidi->writeAll([ - $request2, - $request3, - ]); + $bidi->writeAll([$request2, $request3]); foreach ($bidi->closeWriteAndReadAll() as $response) { $responses[] = $response; } @@ -102,7 +101,10 @@ public function streamingTranslateSpeechTest() $this->assertSame(1, count($createStreamRequests)); $streamFuncCall = $createStreamRequests[0]->getFuncCall(); $streamRequestObject = $createStreamRequests[0]->getRequestObject(); - $this->assertSame('/google.cloud.mediatranslation.v1beta1.SpeechTranslationService/StreamingTranslateSpeech', $streamFuncCall); + $this->assertSame( + '/google.cloud.mediatranslation.v1beta1.SpeechTranslationService/StreamingTranslateSpeech', + $streamFuncCall + ); $this->assertNull($streamRequestObject); $callObjects = $transport->popCallObjects(); $this->assertSame(1, count($callObjects)); @@ -126,12 +128,15 @@ public function streamingTranslateSpeechExceptionTest() $status = new stdClass(); $status->code = Code::DATA_LOSS; $status->details = 'internal error'; - $expectedExceptionMessage = json_encode([ - 'message' => 'internal error', - 'code' => Code::DATA_LOSS, - 'status' => 'DATA_LOSS', - 'details' => [], - ], JSON_PRETTY_PRINT); + $expectedExceptionMessage = json_encode( + [ + 'message' => 'internal error', + 'code' => Code::DATA_LOSS, + 'status' => 'DATA_LOSS', + 'details' => [], + ], + JSON_PRETTY_PRINT + ); $transport->setStreamingStatus($status); $this->assertTrue($transport->isExhausted()); $bidi = $gapicClient->streamingTranslateSpeech(); diff --git a/MediaTranslation/tests/Unit/V1beta1/SpeechTranslationServiceClientTest.php b/MediaTranslation/tests/Unit/V1beta1/SpeechTranslationServiceClientTest.php deleted file mode 100644 index 09f122664dec..000000000000 --- a/MediaTranslation/tests/Unit/V1beta1/SpeechTranslationServiceClientTest.php +++ /dev/null @@ -1,151 +0,0 @@ -getMockBuilder(CredentialsWrapper::class)->disableOriginalConstructor()->getMock(); - } - - /** @return SpeechTranslationServiceClient */ - private function createClient(array $options = []) - { - $options += [ - 'credentials' => $this->createCredentials(), - ]; - return new SpeechTranslationServiceClient($options); - } - - /** @test */ - public function streamingTranslateSpeechTest() - { - $transport = $this->createTransport(); - $gapicClient = $this->createClient([ - 'transport' => $transport, - ]); - $this->assertTrue($transport->isExhausted()); - // Mock response - $expectedResponse = new StreamingTranslateSpeechResponse(); - $transport->addResponse($expectedResponse); - $expectedResponse2 = new StreamingTranslateSpeechResponse(); - $transport->addResponse($expectedResponse2); - $expectedResponse3 = new StreamingTranslateSpeechResponse(); - $transport->addResponse($expectedResponse3); - // Mock request - $request = new StreamingTranslateSpeechRequest(); - $request2 = new StreamingTranslateSpeechRequest(); - $request3 = new StreamingTranslateSpeechRequest(); - $bidi = $gapicClient->streamingTranslateSpeech(); - $this->assertInstanceOf(BidiStream::class, $bidi); - $bidi->write($request); - $responses = []; - $responses[] = $bidi->read(); - $bidi->writeAll([ - $request2, - $request3, - ]); - foreach ($bidi->closeWriteAndReadAll() as $response) { - $responses[] = $response; - } - - $expectedResponses = []; - $expectedResponses[] = $expectedResponse; - $expectedResponses[] = $expectedResponse2; - $expectedResponses[] = $expectedResponse3; - $this->assertEquals($expectedResponses, $responses); - $createStreamRequests = $transport->popReceivedCalls(); - $this->assertSame(1, count($createStreamRequests)); - $streamFuncCall = $createStreamRequests[0]->getFuncCall(); - $streamRequestObject = $createStreamRequests[0]->getRequestObject(); - $this->assertSame('/google.cloud.mediatranslation.v1beta1.SpeechTranslationService/StreamingTranslateSpeech', $streamFuncCall); - $this->assertNull($streamRequestObject); - $callObjects = $transport->popCallObjects(); - $this->assertSame(1, count($callObjects)); - $bidiCall = $callObjects[0]; - $writeRequests = $bidiCall->popReceivedCalls(); - $expectedRequests = []; - $expectedRequests[] = $request; - $expectedRequests[] = $request2; - $expectedRequests[] = $request3; - $this->assertEquals($expectedRequests, $writeRequests); - $this->assertTrue($transport->isExhausted()); - } - - /** @test */ - public function streamingTranslateSpeechExceptionTest() - { - $transport = $this->createTransport(); - $gapicClient = $this->createClient([ - 'transport' => $transport, - ]); - $status = new stdClass(); - $status->code = Code::DATA_LOSS; - $status->details = 'internal error'; - $expectedExceptionMessage = json_encode([ - 'message' => 'internal error', - 'code' => Code::DATA_LOSS, - 'status' => 'DATA_LOSS', - 'details' => [], - ], JSON_PRETTY_PRINT); - $transport->setStreamingStatus($status); - $this->assertTrue($transport->isExhausted()); - $bidi = $gapicClient->streamingTranslateSpeech(); - $results = $bidi->closeWriteAndReadAll(); - try { - iterator_to_array($results); - // If the close stream method call did not throw, fail the test - $this->fail('Expected an ApiException, but no exception was thrown.'); - } catch (ApiException $ex) { - $this->assertEquals($status->code, $ex->getCode()); - $this->assertEquals($expectedExceptionMessage, $ex->getMessage()); - } - // Call popReceivedCalls to ensure the stub is exhausted - $transport->popReceivedCalls(); - $this->assertTrue($transport->isExhausted()); - } -}