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 public function testClose() 56 { 57 $this->expectException(\InvalidArgumentException::class); 58 self::$client->close(); 59 $div_arg = new Math\DivArgs(); 60 $call = self::$client->Div($div_arg); 61 } 62 63 public function testInvalidMetadata() 64 { 65 $this->expectException(\InvalidArgumentException::class); 66 $div_arg = new Math\DivArgs(); 67 $call = self::$client->Div($div_arg, [' ' => 'abc123']); 68 } 69 70 public function testMetadata() 71 { 72 $div_arg = new Math\DivArgs(); 73 $div_arg->setDividend(7); 74 $div_arg->setDivisor(4); 75 $call = self::$client->Div($div_arg, ['somekey' => ['abc123']]); 76 // $this->assertNotNull($call); 77 list($response, $status) = $call->wait(); 78 $this->assertSame(\Grpc\STATUS_OK, $status->code); 79 } 80 81 public function testMetadataKey() 82 { 83 $div_arg = new Math\DivArgs(); 84 $div_arg->setDividend(7); 85 $div_arg->setDivisor(4); 86 $call = self::$client->Div($div_arg, ['somekey_-1' => ['abc123']]); 87 list($response, $status) = $call->wait(); 88 $this->assertSame(\Grpc\STATUS_OK, $status->code); 89 } 90 91 public function testMetadataKeyWithDot() 92 { 93 $div_arg = new Math\DivArgs(); 94 $div_arg->setDividend(7); 95 $div_arg->setDivisor(4); 96 $call = self::$client->Div($div_arg, ['someKEY._-1' => ['abc123']]); 97 list($response, $status) = $call->wait(); 98 $this->assertSame(\Grpc\STATUS_OK, $status->code); 99 } 100 101 public function testMetadataInvalidKey() 102 { 103 $this->expectException(\InvalidArgumentException::class); 104 $div_arg = new Math\DivArgs(); 105 $call = self::$client->Div($div_arg, ['(somekey)' => ['abc123']]); 106 } 107 108 public function testGetCallMetadata() 109 { 110 $div_arg = new Math\DivArgs(); 111 $call = self::$client->Div($div_arg); 112 $this->assertTrue(is_array($call->getMetadata())); 113 } 114 115 public function testTimeout() 116 { 117 $div_arg = new Math\DivArgs(); 118 $div_arg->setDividend(7); 119 $div_arg->setDivisor(4); 120 $call = self::$client->Div($div_arg, [], ['timeout' => 1]); 121 list($response, $status) = $call->wait(); 122 $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code); 123 } 124 125 public function testCancel() 126 { 127 $div_arg = new Math\DivArgs(); 128 $call = self::$client->Div($div_arg); 129 $call->cancel(); 130 list($response, $status) = $call->wait(); 131 $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); 132 } 133 134 public function testCallCredentialsCallback() 135 { 136 $div_arg = new Math\DivArgs(); 137 $div_arg->setDividend(7); 138 $div_arg->setDivisor(4); 139 $call = self::$client->Div($div_arg, array(), array( 140 'call_credentials_callback' => function ($context) { 141 return array(); 142 }, 143 )); 144 list($response, $status) = $call->wait(); 145 $this->assertSame(\Grpc\STATUS_OK, $status->code); 146 } 147 148 public function testInsecureChannelCallCredentialsCallback() 149 { 150 $div_arg = new Math\DivArgs(); 151 $div_arg->setDividend(7); 152 $div_arg->setDivisor(4); 153 $client = new Math\MathClient( 154 getenv('GRPC_TEST_INSECURE_HOST'), [ 155 'credentials' => Grpc\ChannelCredentials::createInsecure(), 156 ]); 157 $call = $client->Div($div_arg, array(), array( 158 'call_credentials_callback' => function ($context) { 159 return array(); 160 }, 161 )); 162 list($response, $status) = $call->wait(); 163 $this->assertSame(\Grpc\STATUS_UNAUTHENTICATED, $status->code); 164 } 165 166 public function testInvalidMethodName() 167 { 168 $this->expectException(\InvalidArgumentException::class); 169 $invalid_client = new DummyInvalidClient('host', [ 170 'credentials' => Grpc\ChannelCredentials::createInsecure(), 171 ]); 172 $div_arg = new Math\DivArgs(); 173 $invalid_client->InvalidUnaryCall($div_arg); 174 } 175 176 public function testMissingCredentials() 177 { 178 $this->expectException(\Exception::class); 179 $this->expectExceptionMessage("The opts['credentials'] key is now required."); 180 $invalid_client = new DummyInvalidClient('host', [ 181 ]); 182 } 183 184 public function testPrimaryUserAgentString() 185 { 186 $invalid_client = new DummyInvalidClient('host', [ 187 'credentials' => Grpc\ChannelCredentials::createInsecure(), 188 'grpc.primary_user_agent' => 'testUserAgent', 189 ]); 190 $this->assertTrue(TRUE); // to avoid no assert warning 191 } 192 193 public function testWriteFlags() 194 { 195 $div_arg = new Math\DivArgs(); 196 $div_arg->setDividend(7); 197 $div_arg->setDivisor(4); 198 $call = self::$client->Div($div_arg, [], 199 ['flags' => Grpc\WRITE_NO_COMPRESS]); 200 $this->assertTrue(is_string($call->getPeer())); 201 list($response, $status) = $call->wait(); 202 $this->assertSame(1, $response->getQuotient()); 203 $this->assertSame(3, $response->getRemainder()); 204 $this->assertSame(\Grpc\STATUS_OK, $status->code); 205 } 206 207 public function testWriteFlagsServerStreaming() 208 { 209 $fib_arg = new Math\FibArgs(); 210 $fib_arg->setLimit(7); 211 $call = self::$client->Fib($fib_arg, [], 212 ['flags' => Grpc\WRITE_NO_COMPRESS]); 213 $result_array = iterator_to_array($call->responses()); 214 $status = $call->getStatus(); 215 $this->assertSame(\Grpc\STATUS_OK, $status->code); 216 } 217 218 public function testWriteFlagsClientStreaming() 219 { 220 $call = self::$client->Sum(); 221 $num = new Math\Num(); 222 $num->setNum(1); 223 $call->write($num, ['flags' => Grpc\WRITE_NO_COMPRESS]); 224 list($response, $status) = $call->wait(); 225 $this->assertSame(\Grpc\STATUS_OK, $status->code); 226 } 227 228 public function testWriteFlagsBidiStreaming() 229 { 230 $call = self::$client->DivMany(); 231 $div_arg = new Math\DivArgs(); 232 $div_arg->setDividend(7); 233 $div_arg->setDivisor(4); 234 $call->write($div_arg, ['flags' => Grpc\WRITE_NO_COMPRESS]); 235 $response = $call->read(); 236 $call->writesDone(); 237 $status = $call->getStatus(); 238 $this->assertSame(\Grpc\STATUS_OK, $status->code); 239 } 240 241 public function testSimpleRequest() 242 { 243 $div_arg = new Math\DivArgs(); 244 $div_arg->setDividend(7); 245 $div_arg->setDivisor(4); 246 $call = self::$client->Div($div_arg); 247 $this->assertTrue(is_string($call->getPeer())); 248 list($response, $status) = $call->wait(); 249 $this->assertSame(1, $response->getQuotient()); 250 $this->assertSame(3, $response->getRemainder()); 251 $this->assertSame(\Grpc\STATUS_OK, $status->code); 252 } 253 254 public function testServerStreaming() 255 { 256 $fib_arg = new Math\FibArgs(); 257 $fib_arg->setLimit(7); 258 $call = self::$client->Fib($fib_arg); 259 $this->assertTrue(is_string($call->getPeer())); 260 $result_array = iterator_to_array($call->responses()); 261 $extract_num = function ($num) { 262 return $num->getNum(); 263 }; 264 $values = array_map($extract_num, $result_array); 265 $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values); 266 $status = $call->getStatus(); 267 $this->assertSame(\Grpc\STATUS_OK, $status->code); 268 } 269 270 public function testClientStreaming() 271 { 272 $call = self::$client->Sum(); 273 $this->assertTrue(is_string($call->getPeer())); 274 for ($i = 0; $i < 7; ++$i) { 275 $num = new Math\Num(); 276 $num->setNum($i); 277 $call->write($num); 278 } 279 list($response, $status) = $call->wait(); 280 $this->assertSame(21, $response->getNum()); 281 $this->assertSame(\Grpc\STATUS_OK, $status->code); 282 } 283 284 public function testBidiStreaming() 285 { 286 $call = self::$client->DivMany(); 287 $this->assertTrue(is_string($call->getPeer())); 288 for ($i = 0; $i < 7; ++$i) { 289 $div_arg = new Math\DivArgs(); 290 $div_arg->setDividend(2 * $i + 1); 291 $div_arg->setDivisor(2); 292 $call->write($div_arg); 293 $response = $call->read(); 294 $this->assertSame($i, $response->getQuotient()); 295 $this->assertSame(1, $response->getRemainder()); 296 } 297 $call->writesDone(); 298 $status = $call->getStatus(); 299 $this->assertSame(\Grpc\STATUS_OK, $status->code); 300 } 301 302 public function testReuseCall() 303 { 304 $this->expectException(\LogicException::class); 305 $this->expectExceptionMessage("start_batch was called incorrectly"); 306 $div_arg = new Math\DivArgs(); 307 $div_arg->setDividend(7); 308 $div_arg->setDivisor(4); 309 $call = self::$client->Div($div_arg, [], ['timeout' => 1000000]); 310 311 list($response, $status) = $call->wait(); 312 $this->assertSame(\Grpc\STATUS_OK, $status->code); 313 list($response, $status) = $call->wait(); 314 } 315} 316 317class DummyInvalidClient extends \Grpc\BaseStub 318{ 319 public function InvalidUnaryCall(\Math\DivArgs $argument, 320 $metadata = [], 321 $options = []) 322 { 323 return $this->_simpleRequest('invalidMethodName', 324 $argument, 325 function () {}, 326 $metadata, 327 $options); 328 } 329} 330