GeneratedTest.php
3 years ago
MessageAwareArrayComparator.php
3 years ago
MessageAwareExporter.php
3 years ago
MockBidiStreamingCall.php
3 years ago
MockClientStreamingCall.php
3 years ago
MockGrpcTransport.php
3 years ago
MockRequest.php
3 years ago
MockRequestBody.php
3 years ago
MockResponse.php
3 years ago
MockServerStreamingCall.php
3 years ago
MockStatus.php
3 years ago
MockStubTrait.php
3 years ago
MockTransport.php
3 years ago
MockUnaryCall.php
3 years ago
ProtobufGPBEmptyComparator.php
3 years ago
ProtobufMessageComparator.php
3 years ago
ReceivedRequest.php
3 years ago
SerializationTrait.php
3 years ago
mocks.proto
3 years ago
MockTransport.php
115 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2018 Google LLC |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | namespace Google\ApiCore\Testing; |
| 34 | |
| 35 | use Google\ApiCore\ApiException; |
| 36 | use Google\ApiCore\BidiStream; |
| 37 | use Google\ApiCore\Call; |
| 38 | use Google\ApiCore\ClientStream; |
| 39 | use Google\ApiCore\ServerStream; |
| 40 | use Google\ApiCore\Transport\TransportInterface; |
| 41 | use Google\Rpc\Code; |
| 42 | use GuzzleHttp\Promise\Promise; |
| 43 | |
| 44 | /** |
| 45 | * @internal |
| 46 | */ |
| 47 | class MockTransport implements TransportInterface |
| 48 | { |
| 49 | use MockStubTrait; |
| 50 | |
| 51 | private $agentHeaderDescriptor; // @phpstan-ignore-line |
| 52 | |
| 53 | public function setAgentHeaderDescriptor($agentHeaderDescriptor) |
| 54 | { |
| 55 | $this->agentHeaderDescriptor = $agentHeaderDescriptor; |
| 56 | } |
| 57 | |
| 58 | public function startUnaryCall(Call $call, array $options) |
| 59 | { |
| 60 | $call = call_user_func([$this, $call->getMethod()], $call, $options); |
| 61 | return $promise = new Promise( |
| 62 | function () use ($call, &$promise) { |
| 63 | list($response, $status) = $call->wait(); |
| 64 | |
| 65 | if ($status->code == Code::OK) { |
| 66 | $promise->resolve($response); |
| 67 | } else { |
| 68 | throw ApiException::createFromStdClass($status); |
| 69 | } |
| 70 | }, |
| 71 | [$call, 'cancel'] |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | public function startBidiStreamingCall(Call $call, array $options) |
| 76 | { |
| 77 | $newArgs = ['/' . $call->getMethod(), $this->deserialize, $options, $options]; |
| 78 | $response = call_user_func_array(array($this, '_bidiRequest'), $newArgs); |
| 79 | return new BidiStream($response, $call->getDescriptor()); |
| 80 | } |
| 81 | |
| 82 | public function startClientStreamingCall(Call $call, array $options) |
| 83 | { |
| 84 | $newArgs = ['/' . $call->getMethod(), $this->deserialize, $options, $options]; |
| 85 | $response = call_user_func_array(array($this, '_clientStreamRequest'), $newArgs); |
| 86 | return new ClientStream($response, $call->getDescriptor()); |
| 87 | } |
| 88 | |
| 89 | public function startServerStreamingCall(Call $call, array $options) |
| 90 | { |
| 91 | $newArgs = ['/' . $call->getMethod(), $call->getMessage(), $this->deserialize, $options, $options]; |
| 92 | $response = call_user_func_array(array($this, '_serverStreamRequest'), $newArgs); |
| 93 | return new ServerStream($response, $call->getDescriptor()); |
| 94 | } |
| 95 | |
| 96 | public function __call(string $name, array $arguments) |
| 97 | { |
| 98 | $call = $arguments[0]; |
| 99 | $options = $arguments[1]; |
| 100 | $decode = $call->getDecodeType() ? [$call->getDecodeType(), 'decode'] : null; |
| 101 | return $this->_simpleRequest( |
| 102 | '/' . $call->getMethod(), |
| 103 | $call->getMessage(), |
| 104 | $decode, |
| 105 | isset($options['headers']) ? $options['headers'] : [], |
| 106 | $options |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | public function close() |
| 111 | { |
| 112 | // does nothing |
| 113 | } |
| 114 | } |
| 115 |