PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.2.0
ShareThis Dashboard for Google Analytics v3.2.0
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / google / gax / src / Testing / MockStubTrait.php
googleanalytics / lib / analytics-admin / vendor / google / gax / src / Testing Last commit date
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
MockStubTrait.php
297 lines
1 <?php
2 /*
3 * Copyright 2016 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\Protobuf\Internal\Message;
36 use UnderflowException;
37 use stdClass;
38
39 /**
40 * The MockStubTrait is used by generated mock stub classes which extent \Grpc\BaseStub
41 * (https://github.com/grpc/grpc/blob/master/src/php/lib/Grpc/BaseStub.php)
42 * It provides functionality to add responses, get received calls, and overrides the _simpleRequest
43 * method so that the elements of $responses are returned instead of making a call to the API.
44 *
45 * @internal
46 */
47 trait MockStubTrait
48 {
49 private $receivedFuncCalls = [];
50 private $responses = [];
51 private $serverStreamingStatus = null;
52 private $callObjects = [];
53 private $deserialize;
54
55 public function __construct(callable $deserialize = null)
56 {
57 $this->deserialize = $deserialize;
58 }
59
60 /**
61 * Overrides the _simpleRequest method in \Grpc\BaseStub
62 * (https://github.com/grpc/grpc/blob/master/src/php/lib/Grpc/BaseStub.php)
63 * Returns a MockUnaryCall object that will return the first item from $responses
64 * @param string $method The API method name to be called
65 * @param \Google\Protobuf\Internal\Message $argument The request object to the API method
66 * @param callable $deserialize A function to deserialize the response object
67 * @param array $metadata
68 * @param array $options
69 * @return MockUnaryCall
70 */
71 public function _simpleRequest(
72 $method,
73 $argument,
74 $deserialize,
75 array $metadata = [],
76 array $options = []
77 ) {
78 $this->receivedFuncCalls[] = new ReceivedRequest($method, $argument, $deserialize, $metadata, $options);
79 if (count($this->responses) < 1) {
80 throw new UnderflowException("ran out of responses");
81 }
82 list($response, $status) = array_shift($this->responses);
83 $call = new MockUnaryCall($response, $deserialize, $status);
84 $this->callObjects[] = $call;
85 return $call;
86 }
87
88 /**
89 * Overrides the _clientStreamRequest method in \Grpc\BaseStub
90 * (https://github.com/grpc/grpc/blob/master/src/php/lib/Grpc/BaseStub.php)
91 * Returns a MockClientStreamingCall object that will return the first item from $responses
92 *
93 * @param string $method The name of the method to call
94 * @param callable $deserialize A function that deserializes the responses
95 * @param array $metadata A metadata map to send to the server
96 * (optional)
97 * @param array $options An array of options (optional)
98 *
99 * @return MockClientStreamingCall The active call object
100 */
101 public function _clientStreamRequest(
102 $method,
103 $deserialize,
104 array $metadata = [],
105 array $options = []
106 ) {
107 $this->receivedFuncCalls[] = new ReceivedRequest($method, null, $deserialize, $metadata, $options);
108 if (count($this->responses) < 1) {
109 throw new UnderflowException("ran out of responses");
110 }
111 list($response, $status) = array_shift($this->responses);
112 $call = new MockClientStreamingCall($response, $deserialize, $status);
113 $this->callObjects[] = $call;
114 return $call;
115 }
116
117 /**
118 * Overrides the _serverStreamRequest method in \Grpc\BaseStub
119 * (https://github.com/grpc/grpc/blob/master/src/php/lib/Grpc/BaseStub.php)
120 * Returns a MockServerStreamingCall object that will stream items from $responses, and return
121 * a final status of $serverStreamingStatus.
122 *
123 * @param string $method The name of the method to call
124 * @param \Google\Protobuf\Internal\Message $argument The argument to the method
125 * @param callable $deserialize A function that deserializes the responses
126 * @param array $metadata A metadata map to send to the server
127 * (optional)
128 * @param array $options An array of options (optional)
129 *
130 * @return MockServerStreamingCall The active call object
131 */
132 public function _serverStreamRequest(
133 $method,
134 $argument,
135 $deserialize,
136 array $metadata = [],
137 array $options = []
138 ) {
139
140 if (is_a($argument, '\Google\Protobuf\Internal\Message')) {
141 /** @var Message $newArgument */
142 $newArgument = new $argument();
143 $newArgument->mergeFromString($argument->serializeToString());
144 $argument = $newArgument;
145 }
146 $this->receivedFuncCalls[] = new ReceivedRequest($method, $argument, $deserialize, $metadata, $options);
147 $responses = self::stripStatusFromResponses($this->responses);
148 $this->responses = [];
149 $call = new MockServerStreamingCall($responses, $deserialize, $this->serverStreamingStatus);
150 $this->callObjects[] = $call;
151 return $call;
152 }
153
154 /**
155 * Overrides the _bidiRequest method in \Grpc\BaseStub
156 * (https://github.com/grpc/grpc/blob/master/src/php/lib/Grpc/BaseStub.php)
157 * Returns a MockBidiStreamingCall object that will stream items from $responses, and return
158 * a final status of $serverStreamingStatus.
159 *
160 * @param string $method The name of the method to call
161 * @param callable $deserialize A function that deserializes the responses
162 * @param array $metadata A metadata map to send to the server
163 * (optional)
164 * @param array $options An array of options (optional)
165 *
166 * @return MockBidiStreamingCall The active call object
167 */
168 public function _bidiRequest(
169 $method,
170 $deserialize,
171 array $metadata = [],
172 array $options = []
173 ) {
174
175 $this->receivedFuncCalls[] = new ReceivedRequest($method, null, $deserialize, $metadata, $options);
176 $responses = self::stripStatusFromResponses($this->responses);
177 $this->responses = [];
178 $call = new MockBidiStreamingCall($responses, $deserialize, $this->serverStreamingStatus);
179 $this->callObjects[] = $call;
180 return $call;
181 }
182
183 public static function stripStatusFromResponses($responses)
184 {
185 $strippedResponses = [];
186 foreach ($responses as $response) {
187 list($resp, $_) = $response;
188 $strippedResponses[] = $resp;
189 }
190 return $strippedResponses;
191 }
192
193 /**
194 * Add a response object, and an optional status, to the list of responses to be returned via
195 * _simpleRequest.
196 * @param \Google\Protobuf\Internal\Message $response
197 * @param stdClass $status
198 */
199 public function addResponse($response, stdClass $status = null)
200 {
201 if (!$this->deserialize && $response) {
202 $this->deserialize = [get_class($response), 'decode'];
203 }
204
205 if (is_a($response, '\Google\Protobuf\Internal\Message')) {
206 $response = $response->serializeToString();
207 }
208 $this->responses[] = [$response, $status];
209 }
210
211 /**
212 * Set the status object to be used when creating streaming calls.
213 *
214 * @param stdClass $status
215 */
216 public function setStreamingStatus(stdClass $status)
217 {
218 $this->serverStreamingStatus = $status;
219 }
220
221 /**
222 * Return a list of calls made to _simpleRequest, and clear $receivedFuncCalls.
223 *
224 * @return ReceivedRequest[] An array of received requests
225 */
226 public function popReceivedCalls()
227 {
228 $receivedFuncCallsTemp = $this->receivedFuncCalls;
229 $this->receivedFuncCalls = [];
230 return $receivedFuncCallsTemp;
231 }
232
233 /**
234 * @return int The number of calls received.
235 */
236 public function getReceivedCallCount()
237 {
238 return count($this->receivedFuncCalls);
239 }
240
241 /**
242 * @return mixed[] The call objects created by calls to the stub
243 */
244 public function popCallObjects()
245 {
246 $callObjectsTemp = $this->callObjects;
247 $this->callObjects = [];
248 return $callObjectsTemp;
249 }
250
251 /**
252 * @return bool True if $receivedFuncCalls and $response are empty.
253 */
254 public function isExhausted()
255 {
256 return count($this->receivedFuncCalls) === 0
257 && count($this->responses) === 0;
258 }
259
260 /**
261 * @param mixed $responseObject
262 * @param stdClass|null $status
263 * @param callable $deserialize
264 * @return static An instance of the current class type.
265 */
266 public static function create($responseObject, stdClass $status = null, callable $deserialize = null)
267 {
268 $stub = new static($deserialize); // @phpstan-ignore-line
269 $stub->addResponse($responseObject, $status);
270 return $stub;
271 }
272
273 /**
274 * Creates a sequence such that the responses are returned in order.
275 * @param mixed[] $sequence
276 * @param callable $deserialize
277 * @param stdClass $finalStatus
278 * @return static An instance of the current class type.
279 */
280 public static function createWithResponseSequence(array $sequence, callable $deserialize = null, stdClass $finalStatus = null)
281 {
282 $stub = new static($deserialize); // @phpstan-ignore-line
283 foreach ($sequence as $elem) {
284 if (count($elem) == 1) {
285 list($resp, $status) = [$elem, null];
286 } else {
287 list($resp, $status) = $elem;
288 }
289 $stub->addResponse($resp, $status);
290 }
291 if ($finalStatus) {
292 $stub->setStreamingStatus($finalStatus);
293 }
294 return $stub;
295 }
296 }
297