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