responses = $responses; $this->deserialize = $deserialize; if (is_null($status)) { $status = new MockStatus(Code::OK); } $this->status = $status; } /** * @return mixed|null * @throws ApiException */ public function read() { if (count($this->responses) > 0) { $resp = array_shift($this->responses); if (is_null($resp)) { // Null was added to the responses list to simulate a failed stream // To ensure that getStatus can now be called, we clear the remaining // responses and set writesDone to true $this->responses = []; $this->writesDone(); return null; } $obj = $this->deserializeMessage($resp, $this->deserialize); return $obj; } elseif ($this->writesDone) { return null; } else { throw new ApiException( "No more responses to read, but closeWrite() not called - " . "this would be blocking", Grpc\STATUS_INTERNAL, null ); } } /** * @return MockStatus|null|\stdClass * @throws ApiException */ public function getStatus() { if (count($this->responses) > 0) { throw new ApiException( "Calls to getStatus() will block if all responses are not read", Grpc\STATUS_INTERNAL, null ); } if (!$this->writesDone) { throw new ApiException( "Calls to getStatus() will block if closeWrite() not called", Grpc\STATUS_INTERNAL, null ); } return $this->status; } /** * Save the request object, to be retrieved via getReceivedCalls() * @param \Google\Protobuf\Internal\Message|mixed $request The request object * @param array $options An array of options. * @throws ApiException */ public function write($request, array $options = []) { if ($this->writesDone) { throw new ApiException( "Cannot call write() after writesDone()", Grpc\STATUS_INTERNAL, null ); } if (is_a($request, '\Google\Protobuf\Internal\Message')) { $newRequest = new $request(); $newRequest->mergeFromString($request->serializeToString()); $request = $newRequest; } $this->receivedWrites[] = $request; } /** * Set writesDone to true */ public function writesDone() { $this->writesDone = true; } /** * Return a list of calls made to write(), and clear $receivedFuncCalls. * * @return mixed[] An array of received requests */ public function popReceivedCalls() { $receivedFuncCallsTemp = $this->receivedWrites; $this->receivedWrites = []; return $receivedFuncCallsTemp; } }