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 */ 19class CallTest extends \PHPUnit\Framework\TestCase 20{ 21 public static $server; 22 public static $port; 23 private $channel; 24 private $call; 25 26 public static function setUpBeforeClass(): void 27 { 28 self::$server = new Grpc\Server([]); 29 self::$port = self::$server->addHttp2Port('0.0.0.0:53000'); 30 self::$server->start(); 31 } 32 33 public function setUp(): void 34 { 35 $this->channel = new Grpc\Channel('localhost:'.self::$port, [ 36 'force_new' => true, 37 ]); 38 $this->call = new Grpc\Call($this->channel, 39 '/foo', 40 Grpc\Timeval::infFuture()); 41 } 42 43 public function tearDown(): void 44 { 45 $this->channel->close(); 46 } 47 48 public function testConstructor() 49 { 50 $this->assertSame('Grpc\Call', get_class($this->call)); 51 $this->assertObjectHasAttribute('channel', $this->call); 52 } 53 54 public function testAddEmptyMetadata() 55 { 56 $batch = [ 57 Grpc\OP_SEND_INITIAL_METADATA => [], 58 ]; 59 $result = $this->call->startBatch($batch); 60 $this->assertTrue($result->send_metadata); 61 } 62 63 public function testAddSingleMetadata() 64 { 65 $batch = [ 66 Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value']], 67 ]; 68 $result = $this->call->startBatch($batch); 69 $this->assertTrue($result->send_metadata); 70 } 71 72 public function testAddMultiValueMetadata() 73 { 74 $batch = [ 75 Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value1', 'value2']], 76 ]; 77 $result = $this->call->startBatch($batch); 78 $this->assertTrue($result->send_metadata); 79 } 80 81 public function testAddSingleAndMultiValueMetadata() 82 { 83 $batch = [ 84 Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1'], 85 'key2' => ['value2', 86 'value3', ], ], 87 ]; 88 $result = $this->call->startBatch($batch); 89 $this->assertTrue($result->send_metadata); 90 } 91 92 public function testAddMultiAndMultiValueMetadata() 93 { 94 $batch = [ 95 Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1', 'value2'], 96 'key2' => ['value3', 'value4'],], 97 ]; 98 $result = $this->call->startBatch($batch); 99 $this->assertTrue($result->send_metadata); 100 } 101 102 public function testGetPeer() 103 { 104 $this->assertTrue(is_string($this->call->getPeer())); 105 } 106 107 public function testCancel() 108 { 109 $this->assertNull($this->call->cancel()); 110 } 111 112 public function testInvalidStartBatchKey() 113 { 114 $this->expectException(\InvalidArgumentException::class); 115 $batch = [ 116 'invalid' => ['key1' => 'value1'], 117 ]; 118 $result = $this->call->startBatch($batch); 119 } 120 121 public function testInvalidMetadataStrKey() 122 { 123 $this->expectException(\InvalidArgumentException::class); 124 $batch = [ 125 Grpc\OP_SEND_INITIAL_METADATA => ['Key' => ['value1', 'value2']], 126 ]; 127 $result = $this->call->startBatch($batch); 128 } 129 130 public function testInvalidMetadataIntKey() 131 { 132 $this->expectException(\InvalidArgumentException::class); 133 $batch = [ 134 Grpc\OP_SEND_INITIAL_METADATA => [1 => ['value1', 'value2']], 135 ]; 136 $result = $this->call->startBatch($batch); 137 } 138 139 public function testInvalidMetadataInnerValue() 140 { 141 $this->expectException(\InvalidArgumentException::class); 142 $batch = [ 143 Grpc\OP_SEND_INITIAL_METADATA => ['key1' => 'value1'], 144 ]; 145 $result = $this->call->startBatch($batch); 146 } 147 148 public function testInvalidConstuctor() 149 { 150 $this->expectException(\InvalidArgumentException::class); 151 $this->call = new Grpc\Call(); 152 $this->assertNull($this->call); 153 } 154 155 public function testInvalidConstuctor2() 156 { 157 $this->expectException(\InvalidArgumentException::class); 158 $this->call = new Grpc\Call('hi', 'hi', 'hi'); 159 $this->assertNull($this->call); 160 } 161 162 public function testInvalidSetCredentials() 163 { 164 $this->expectException(\InvalidArgumentException::class); 165 $this->call->setCredentials('hi'); 166 } 167 168 public function testInvalidSetCredentials2() 169 { 170 $this->expectException(\InvalidArgumentException::class); 171 $this->call->setCredentials([]); 172 } 173} 174