assertSame($call, $stream->getServerStreamingCall()); $this->assertSame([], iterator_to_array($stream->readAll())); } /** * @expectedException \Google\GAX\ApiException * @expectedExceptionMessage empty failure */ public function testEmptyFailure() { $call = new MockServerStreamingCall([], null, new MockStatus(Grpc\STATUS_INTERNAL, 'empty failure')); $stream = new ServerStream($call); $this->assertSame($call, $stream->getServerStreamingCall()); iterator_to_array($stream->readAll()); } public function testStringsSuccess() { $responses = ['abc', 'def']; $call = new MockServerStreamingCall($responses); $stream = new ServerStream($call); $this->assertSame($call, $stream->getServerStreamingCall()); $this->assertSame($responses, iterator_to_array($stream->readAll())); } /** * @expectedException \Google\GAX\ApiException * @expectedExceptionMessage strings failure */ public function testStringsFailure() { $responses = ['abc', 'def']; $call = new MockServerStreamingCall( $responses, null, new MockStatus(Grpc\STATUS_INTERNAL, 'strings failure') ); $stream = new ServerStream($call); $this->assertSame($call, $stream->getServerStreamingCall()); $index = 0; try { foreach ($stream->readAll() as $response) { $this->assertSame($response, $responses[$index]); $index++; } } finally { $this->assertSame(2, $index); } } public function testObjectsSuccess() { $responses = [ ServerStreamTest::createStatus(Grpc\STATUS_OK, 'response1'), ServerStreamTest::createStatus(Grpc\STATUS_OK, 'response2') ]; $serializedResponses = []; foreach ($responses as $response) { $serializedResponses[] = $response->serialize(); } $call = new MockServerStreamingCall($serializedResponses, '\google\rpc\Status::deserialize'); $stream = new ServerStream($call); $this->assertSame($call, $stream->getServerStreamingCall()); $this->assertEquals($responses, iterator_to_array($stream->readAll())); } /** * @expectedException \Google\GAX\ApiException * @expectedExceptionMessage objects failure */ public function testObjectsFailure() { $responses = [ ServerStreamTest::createStatus(Grpc\STATUS_OK, 'response1'), ServerStreamTest::createStatus(Grpc\STATUS_OK, 'response2') ]; $serializedResponses = []; foreach ($responses as $response) { $serializedResponses[] = $response->serialize(); } $call = new MockServerStreamingCall( $serializedResponses, '\google\rpc\Status::deserialize', new MockStatus(Grpc\STATUS_INTERNAL, 'objects failure') ); $stream = new ServerStream($call); $this->assertSame($call, $stream->getServerStreamingCall()); $index = 0; try { foreach ($stream->readAll() as $response) { $this->assertEquals($response, $responses[$index]); $index++; } } finally { $this->assertSame(2, $index); } } public function testResourcesSuccess() { $resources = ['resource1', 'resource2', 'resource3']; $responses = [ MockPageStreamingResponse::createPageStreamingResponse('nextPageToken1', ['resource1']), MockPageStreamingResponse::createPageStreamingResponse('nextPageToken1', ['resource2', 'resource3']) ]; $call = new MockServerStreamingCall($responses); $stream = new ServerStream($call, [ 'resourcesField' => 'getResourcesList' ]); $this->assertSame($call, $stream->getServerStreamingCall()); $this->assertEquals($resources, iterator_to_array($stream->readAll())); } /** * @expectedException \Google\GAX\ApiException * @expectedExceptionMessage resources failure */ public function testResourcesFailure() { $resources = ['resource1', 'resource2', 'resource3']; $responses = [ MockPageStreamingResponse::createPageStreamingResponse('nextPageToken1', ['resource1']), MockPageStreamingResponse::createPageStreamingResponse('nextPageToken1', ['resource2', 'resource3']) ]; $call = new MockServerStreamingCall( $responses, null, new MockStatus(Grpc\STATUS_INTERNAL, 'resources failure') ); $stream = new ServerStream($call, [ 'resourcesField' => 'getResourcesList' ]); $this->assertSame($call, $stream->getServerStreamingCall()); $index = 0; try { foreach ($stream->readAll() as $response) { $this->assertSame($response, $resources[$index]); $index++; } } finally { $this->assertSame(3, $index); } } private static function createStatus($code, $message) { $status = new Status(); $status->setCode($code)->setMessage($message); return $status; } }