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