• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2/*
3 *
4 * Copyright 2015 gRPC authors.
5 *
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
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 */
19require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php');
20
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';
25
26abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase
27{
28    /**
29     * These tests require that a server exporting the math service must be
30     * running on $GRPC_TEST_HOST.
31     */
32    protected static $client;
33
34    public function testWaitForNotReady()
35    {
36        $this->assertFalse(self::$client->waitForReady(1));
37    }
38
39    public function testWaitForReady()
40    {
41        $this->assertTrue(self::$client->waitForReady(250000));
42    }
43
44    public function testAlreadyReady()
45    {
46        $this->assertTrue(self::$client->waitForReady(250000));
47        $this->assertTrue(self::$client->waitForReady(100));
48    }
49
50    public function testGetTarget()
51    {
52        $this->assertTrue(is_string(self::$client->getTarget()));
53    }
54
55    /**
56     * @expectedException InvalidArgumentException
57     */
58    public function testClose()
59    {
60        self::$client->close();
61        $div_arg = new Math\DivArgs();
62        $call = self::$client->Div($div_arg);
63    }
64
65    /**
66     * @expectedException InvalidArgumentException
67     */
68    public function testInvalidMetadata()
69    {
70        $div_arg = new Math\DivArgs();
71        $call = self::$client->Div($div_arg, [' ' => 'abc123']);
72    }
73
74    public function testMetadata()
75    {
76        $div_arg = new Math\DivArgs();
77        $call = self::$client->Div($div_arg, ['somekey' => ['abc123']]);
78    }
79
80    public function testMetadataKey()
81    {
82        $div_arg = new Math\DivArgs();
83        $call = self::$client->Div($div_arg, ['somekey_-1' => ['abc123']]);
84    }
85
86    public function testMetadataKeyWithDot()
87    {
88        $div_arg = new Math\DivArgs();
89        $call = self::$client->Div($div_arg, ['someKEY._-1' => ['abc123']]);
90    }
91
92    /**
93     * @expectedException InvalidArgumentException
94     */
95    public function testMetadataInvalidKey()
96    {
97        $div_arg = new Math\DivArgs();
98        $call = self::$client->Div($div_arg, ['(somekey)' => ['abc123']]);
99    }
100
101    public function testGetCallMetadata()
102    {
103        $div_arg = new Math\DivArgs();
104        $call = self::$client->Div($div_arg);
105        $this->assertTrue(is_array($call->getMetadata()));
106    }
107
108    public function testTimeout()
109    {
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);
116    }
117
118    public function testCancel()
119    {
120        $div_arg = new Math\DivArgs();
121        $call = self::$client->Div($div_arg);
122        $call->cancel();
123        list($response, $status) = $call->wait();
124        $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
125    }
126
127    public function testCallCredentialsCallback()
128    {
129        $div_arg = new Math\DivArgs();
130        $call = self::$client->Div($div_arg, array(), array(
131            'call_credentials_callback' => function ($context) {
132                return array();
133            },
134        ));
135        $call->cancel();
136        list($response, $status) = $call->wait();
137        $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
138    }
139
140    public function testCallCredentialsCallback2()
141    {
142        $div_arg = new Math\DivArgs();
143        $call = self::$client->Div($div_arg);
144        $call_credentials = Grpc\CallCredentials::createFromPlugin(
145            function ($context) {
146                return array();
147            }
148        );
149        $call->setCallCredentials($call_credentials);
150        $call->cancel();
151        list($response, $status) = $call->wait();
152        $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
153    }
154
155    /**
156     * @expectedException InvalidArgumentException
157     */
158    public function testInvalidMethodName()
159    {
160        $invalid_client = new DummyInvalidClient('host', [
161            'credentials' => Grpc\ChannelCredentials::createInsecure(),
162        ]);
163        $div_arg = new Math\DivArgs();
164        $invalid_client->InvalidUnaryCall($div_arg);
165    }
166
167    /**
168     * @expectedException Exception
169     */
170    public function testMissingCredentials()
171    {
172        $invalid_client = new DummyInvalidClient('host', [
173        ]);
174    }
175
176    public function testPrimaryUserAgentString()
177    {
178        $invalid_client = new DummyInvalidClient('host', [
179            'credentials' => Grpc\ChannelCredentials::createInsecure(),
180            'grpc.primary_user_agent' => 'testUserAgent',
181        ]);
182    }
183
184    public function testWriteFlags()
185    {
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);
196    }
197
198    public function testWriteFlagsServerStreaming()
199    {
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);
207    }
208
209    public function testWriteFlagsClientStreaming()
210    {
211        $call = self::$client->Sum();
212        $num = new Math\Num();
213        $num->setNum(1);
214        $call->write($num, ['flags' => Grpc\WRITE_NO_COMPRESS]);
215        list($response, $status) = $call->wait();
216        $this->assertSame(\Grpc\STATUS_OK, $status->code);
217    }
218
219    public function testWriteFlagsBidiStreaming()
220    {
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();
227        $call->writesDone();
228        $status = $call->getStatus();
229        $this->assertSame(\Grpc\STATUS_OK, $status->code);
230    }
231
232    public function testSimpleRequest()
233    {
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);
243    }
244
245    public function testServerStreaming()
246    {
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();
254                       };
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);
259    }
260
261    public function testClientStreaming()
262    {
263        $call = self::$client->Sum();
264        $this->assertTrue(is_string($call->getPeer()));
265        for ($i = 0; $i < 7; ++$i) {
266            $num = new Math\Num();
267            $num->setNum($i);
268            $call->write($num);
269        }
270        list($response, $status) = $call->wait();
271        $this->assertSame(21, $response->getNum());
272        $this->assertSame(\Grpc\STATUS_OK, $status->code);
273    }
274
275    public function testBidiStreaming()
276    {
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());
287        }
288        $call->writesDone();
289        $status = $call->getStatus();
290        $this->assertSame(\Grpc\STATUS_OK, $status->code);
291    }
292}
293
294class DummyInvalidClient extends \Grpc\BaseStub
295{
296    public function InvalidUnaryCall(\Math\DivArgs $argument,
297                                     $metadata = [],
298                                     $options = [])
299    {
300        return $this->_simpleRequest('invalidMethodName',
301                                     $argument,
302                                     function () {},
303                                     $metadata,
304                                     $options);
305    }
306}
307