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