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 */ 19 20class ServerTest extends \PHPUnit\Framework\TestCase 21{ 22 private $server; 23 24 public function setUp(): void 25 { 26 $this->server = null; 27 } 28 29 public function tearDown(): void 30 { 31 unset($this->server); 32 } 33 34 public function testConstructorWithNull() 35 { 36 $this->server = new Grpc\Server(); 37 $this->assertNotNull($this->server); 38 } 39 40 public function testConstructorWithNullArray() 41 { 42 $this->server = new Grpc\Server([]); 43 $this->assertNotNull($this->server); 44 } 45 46 public function testConstructorWithArray() 47 { 48 // key of array must be string 49 $this->server = new Grpc\Server(['ip' => '127.0.0.1', 50 'port' => '8080', ]); 51 $this->assertNotNull($this->server); 52 } 53 54 public function testRequestCall() 55 { 56 $this->server = new Grpc\Server(); 57 $port = $this->server->addHttp2Port('0.0.0.0:0'); 58 $this->server->start(); 59 $channel = new Grpc\Channel('localhost:'.$port, 60 [ 61 'force_new' => true, 62 'credentials' => Grpc\ChannelCredentials::createInsecure() 63 ]); 64 65 $deadline = Grpc\Timeval::infFuture(); 66 $call = new Grpc\Call($channel, 'phony_method', $deadline); 67 68 $event = $call->startBatch([Grpc\OP_SEND_INITIAL_METADATA => [], 69 Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, 70 ]); 71 72 $c = $this->server->requestCall(); 73 $this->assertObjectHasAttribute('call', $c); 74 $this->assertObjectHasAttribute('method', $c); 75 $this->assertSame('phony_method', $c->method); 76 $this->assertObjectHasAttribute('host', $c); 77 $this->assertTrue(is_string($c->host)); 78 $this->assertObjectHasAttribute('absolute_deadline', $c); 79 $this->assertObjectHasAttribute('metadata', $c); 80 81 unset($call); 82 unset($channel); 83 } 84 85 private function createSslObj() 86 { 87 $server_credentials = Grpc\ServerCredentials::createSsl( 88 null, 89 file_get_contents(dirname(__FILE__).'/../data/server1.key'), 90 file_get_contents(dirname(__FILE__).'/../data/server1.pem')); 91 92 return $server_credentials; 93 } 94 95 public function testInvalidConstructor() 96 { 97 $this->expectException(\InvalidArgumentException::class); 98 $this->server = new Grpc\Server('invalid_host'); 99 $this->assertNull($this->server); 100 } 101 102 public function testInvalidConstructorWithNumKeyOfArray() 103 { 104 $this->expectException(\InvalidArgumentException::class); 105 $this->server = new Grpc\Server([10 => '127.0.0.1', 106 20 => '8080', ]); 107 $this->assertNull($this->server); 108 } 109 110 public function testInvalidConstructorWithList() 111 { 112 $this->expectException(\InvalidArgumentException::class); 113 $this->server = new Grpc\Server(['127.0.0.1', '8080']); 114 $this->assertNull($this->server); 115 } 116 117 public function testInvalidAddHttp2Port() 118 { 119 $this->expectException(\InvalidArgumentException::class); 120 $this->server = new Grpc\Server([]); 121 $port = $this->server->addHttp2Port(['0.0.0.0:0']); 122 } 123 124 public function testInvalidAddSecureHttp2Port() 125 { 126 $this->expectException(\InvalidArgumentException::class); 127 $this->server = new Grpc\Server([]); 128 $port = $this->server->addSecureHttp2Port(['0.0.0.0:0']); 129 } 130 131 public function testInvalidAddSecureHttp2Port2() 132 { 133 $this->expectException(\InvalidArgumentException::class); 134 $this->server = new Grpc\Server(); 135 $port = $this->server->addSecureHttp2Port('0.0.0.0:0'); 136 } 137 138 public function testInvalidAddSecureHttp2Port3() 139 { 140 $this->expectException(\InvalidArgumentException::class); 141 $this->server = new Grpc\Server(); 142 $port = $this->server->addSecureHttp2Port('0.0.0.0:0', 'invalid'); 143 } 144} 145