4 * Copyright 2015 gRPC authors.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php');
21 // The following includes are needed when using protobuf 3.1.0
22 // and will suppress warnings when using protobuf 3.2.0+
23 @include_once dirname(__FILE__).'/math.pb.php';
24 @include_once dirname(__FILE__).'/math_grpc_pb.php';
26 abstract class AbstractGeneratedCodeTest extends \PHPUnit\Framework\TestCase
29 * These tests require that a server exporting the math service must be
30 * running on $GRPC_TEST_HOST.
32 protected static $client;
34 public function testWaitForNotReady()
36 $this->assertFalse(self::$client->waitForReady(1));
39 public function testWaitForReady()
41 $this->assertTrue(self::$client->waitForReady(250000));
44 public function testAlreadyReady()
46 $this->assertTrue(self::$client->waitForReady(250000));
47 $this->assertTrue(self::$client->waitForReady(100));
50 public function testGetTarget()
52 $this->assertTrue(is_string(self::$client->getTarget()));
56 * @expectedException InvalidArgumentException
58 public function testClose()
60 self::$client->close();
61 $div_arg = new Math\DivArgs();
62 $call = self::$client->Div($div_arg);
66 * @expectedException InvalidArgumentException
68 public function testInvalidMetadata()
70 $div_arg = new Math\DivArgs();
71 $call = self::$client->Div($div_arg, [' ' => 'abc123']);
74 public function testMetadata()
76 $div_arg = new Math\DivArgs();
77 $call = self::$client->Div($div_arg, ['somekey' => ['abc123']]);
80 public function testMetadataKey()
82 $div_arg = new Math\DivArgs();
83 $call = self::$client->Div($div_arg, ['somekey_-1' => ['abc123']]);
86 public function testMetadataKeyWithDot()
88 $div_arg = new Math\DivArgs();
89 $call = self::$client->Div($div_arg, ['someKEY._-1' => ['abc123']]);
93 * @expectedException InvalidArgumentException
95 public function testMetadataInvalidKey()
97 $div_arg = new Math\DivArgs();
98 $call = self::$client->Div($div_arg, ['(somekey)' => ['abc123']]);
101 public function testGetCallMetadata()
103 $div_arg = new Math\DivArgs();
104 $call = self::$client->Div($div_arg);
105 $this->assertTrue(is_array($call->getMetadata()));
108 public function testTimeout()
110 $div_arg = new Math\DivArgs();
111 $div_arg->setDividend(7);
112 $div_arg->setDivisor(4);
113 $call = self::$client->Div($div_arg, [], ['timeout' => 1]);
114 list($response, $status) = $call->wait();
115 $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code);
118 public function testCancel()
120 $div_arg = new Math\DivArgs();
121 $call = self::$client->Div($div_arg);
123 list($response, $status) = $call->wait();
124 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
127 public function testCallCredentialsCallback()
129 $div_arg = new Math\DivArgs();
130 $call = self::$client->Div($div_arg, array(), array(
131 'call_credentials_callback' => function ($context) {
136 list($response, $status) = $call->wait();
137 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
140 public function testCallCredentialsCallback2()
142 $div_arg = new Math\DivArgs();
143 $call = self::$client->Div($div_arg);
144 $call_credentials = Grpc\CallCredentials::createFromPlugin(
145 function ($context) {
149 $call->setCallCredentials($call_credentials);
151 list($response, $status) = $call->wait();
152 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
156 * @expectedException InvalidArgumentException
158 public function testInvalidMethodName()
160 $invalid_client = new DummyInvalidClient('host', [
161 'credentials' => Grpc\ChannelCredentials::createInsecure(),
163 $div_arg = new Math\DivArgs();
164 $invalid_client->InvalidUnaryCall($div_arg);
168 * @expectedException Exception
170 public function testMissingCredentials()
172 $invalid_client = new DummyInvalidClient('host', [
176 public function testPrimaryUserAgentString()
178 $invalid_client = new DummyInvalidClient('host', [
179 'credentials' => Grpc\ChannelCredentials::createInsecure(),
180 'grpc.primary_user_agent' => 'testUserAgent',
184 public function testWriteFlags()
186 $div_arg = new Math\DivArgs();
187 $div_arg->setDividend(7);
188 $div_arg->setDivisor(4);
189 $call = self::$client->Div($div_arg, [],
190 ['flags' => Grpc\WRITE_NO_COMPRESS]);
191 $this->assertTrue(is_string($call->getPeer()));
192 list($response, $status) = $call->wait();
193 $this->assertSame(1, $response->getQuotient());
194 $this->assertSame(3, $response->getRemainder());
195 $this->assertSame(\Grpc\STATUS_OK, $status->code);
198 public function testWriteFlagsServerStreaming()
200 $fib_arg = new Math\FibArgs();
201 $fib_arg->setLimit(7);
202 $call = self::$client->Fib($fib_arg, [],
203 ['flags' => Grpc\WRITE_NO_COMPRESS]);
204 $result_array = iterator_to_array($call->responses());
205 $status = $call->getStatus();
206 $this->assertSame(\Grpc\STATUS_OK, $status->code);
209 public function testWriteFlagsClientStreaming()
211 $call = self::$client->Sum();
212 $num = new Math\Num();
214 $call->write($num, ['flags' => Grpc\WRITE_NO_COMPRESS]);
215 list($response, $status) = $call->wait();
216 $this->assertSame(\Grpc\STATUS_OK, $status->code);
219 public function testWriteFlagsBidiStreaming()
221 $call = self::$client->DivMany();
222 $div_arg = new Math\DivArgs();
223 $div_arg->setDividend(7);
224 $div_arg->setDivisor(4);
225 $call->write($div_arg, ['flags' => Grpc\WRITE_NO_COMPRESS]);
226 $response = $call->read();
228 $status = $call->getStatus();
229 $this->assertSame(\Grpc\STATUS_OK, $status->code);
232 public function testSimpleRequest()
234 $div_arg = new Math\DivArgs();
235 $div_arg->setDividend(7);
236 $div_arg->setDivisor(4);
237 $call = self::$client->Div($div_arg);
238 $this->assertTrue(is_string($call->getPeer()));
239 list($response, $status) = $call->wait();
240 $this->assertSame(1, $response->getQuotient());
241 $this->assertSame(3, $response->getRemainder());
242 $this->assertSame(\Grpc\STATUS_OK, $status->code);
245 public function testServerStreaming()
247 $fib_arg = new Math\FibArgs();
248 $fib_arg->setLimit(7);
249 $call = self::$client->Fib($fib_arg);
250 $this->assertTrue(is_string($call->getPeer()));
251 $result_array = iterator_to_array($call->responses());
252 $extract_num = function ($num) {
253 return $num->getNum();
255 $values = array_map($extract_num, $result_array);
256 $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values);
257 $status = $call->getStatus();
258 $this->assertSame(\Grpc\STATUS_OK, $status->code);
261 public function testClientStreaming()
263 $call = self::$client->Sum();
264 $this->assertTrue(is_string($call->getPeer()));
265 for ($i = 0; $i < 7; ++$i) {
266 $num = new Math\Num();
270 list($response, $status) = $call->wait();
271 $this->assertSame(21, $response->getNum());
272 $this->assertSame(\Grpc\STATUS_OK, $status->code);
275 public function testBidiStreaming()
277 $call = self::$client->DivMany();
278 $this->assertTrue(is_string($call->getPeer()));
279 for ($i = 0; $i < 7; ++$i) {
280 $div_arg = new Math\DivArgs();
281 $div_arg->setDividend(2 * $i + 1);
282 $div_arg->setDivisor(2);
283 $call->write($div_arg);
284 $response = $call->read();
285 $this->assertSame($i, $response->getQuotient());
286 $this->assertSame(1, $response->getRemainder());
289 $status = $call->getStatus();
290 $this->assertSame(\Grpc\STATUS_OK, $status->code);
294 class DummyInvalidClient extends \Grpc\BaseStub
296 public function InvalidUnaryCall(\Math\DivArgs $argument,
300 return $this->_simpleRequest('invalidMethodName',