• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(): void
23    {
24        $this->server = null;
25    }
26
27    public function tearDown(): void
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    public function testInvalidConstructor()
94    {
95        $this->expectException(\InvalidArgumentException::class);
96        $this->server = new Grpc\Server('invalid_host');
97        $this->assertNull($this->server);
98    }
99
100    public function testInvalidConstructorWithNumKeyOfArray()
101    {
102        $this->expectException(\InvalidArgumentException::class);
103        $this->server = new Grpc\Server([10 => '127.0.0.1',
104                                         20 => '8080', ]);
105        $this->assertNull($this->server);
106    }
107
108    public function testInvalidConstructorWithList()
109    {
110        $this->expectException(\InvalidArgumentException::class);
111        $this->server = new Grpc\Server(['127.0.0.1', '8080']);
112        $this->assertNull($this->server);
113    }
114
115    public function testInvalidAddHttp2Port()
116    {
117        $this->expectException(\InvalidArgumentException::class);
118        $this->server = new Grpc\Server([]);
119        $port = $this->server->addHttp2Port(['0.0.0.0:0']);
120    }
121
122    public function testInvalidAddSecureHttp2Port()
123    {
124        $this->expectException(\InvalidArgumentException::class);
125        $this->server = new Grpc\Server([]);
126        $port = $this->server->addSecureHttp2Port(['0.0.0.0:0']);
127    }
128
129    public function testInvalidAddSecureHttp2Port2()
130    {
131        $this->expectException(\InvalidArgumentException::class);
132        $this->server = new Grpc\Server();
133        $port = $this->server->addSecureHttp2Port('0.0.0.0:0');
134    }
135
136    public function testInvalidAddSecureHttp2Port3()
137    {
138        $this->expectException(\InvalidArgumentException::class);
139        $this->server = new Grpc\Server();
140        $port = $this->server->addSecureHttp2Port('0.0.0.0:0', 'invalid');
141    }
142}
143