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 testAddMultiAndMultiValueMetadata()
90    {
91        $batch = [
92            Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1', 'value2'],
93                                              'key2' => ['value3', 'value4'],],
94        ];
95        $result = $this->call->startBatch($batch);
96        $this->assertTrue($result->send_metadata);
97    }
98
99    public function testGetPeer()
100    {
101        $this->assertTrue(is_string($this->call->getPeer()));
102    }
103
104    public function testCancel()
105    {
106        $this->assertNull($this->call->cancel());
107    }
108
109    /**
110     * @expectedException InvalidArgumentException
111     */
112    public function testInvalidStartBatchKey()
113    {
114        $batch = [
115            'invalid' => ['key1' => 'value1'],
116        ];
117        $result = $this->call->startBatch($batch);
118    }
119
120    /**
121     * @expectedException InvalidArgumentException
122     */
123    public function testInvalidMetadataStrKey()
124    {
125        $batch = [
126            Grpc\OP_SEND_INITIAL_METADATA => ['Key' => ['value1', 'value2']],
127        ];
128        $result = $this->call->startBatch($batch);
129    }
130
131    /**
132     * @expectedException InvalidArgumentException
133     */
134    public function testInvalidMetadataIntKey()
135    {
136        $batch = [
137            Grpc\OP_SEND_INITIAL_METADATA => [1 => ['value1', 'value2']],
138        ];
139        $result = $this->call->startBatch($batch);
140    }
141
142    /**
143     * @expectedException InvalidArgumentException
144     */
145    public function testInvalidMetadataInnerValue()
146    {
147        $batch = [
148            Grpc\OP_SEND_INITIAL_METADATA => ['key1' => 'value1'],
149        ];
150        $result = $this->call->startBatch($batch);
151    }
152
153    /**
154     * @expectedException InvalidArgumentException
155     */
156    public function testInvalidConstuctor()
157    {
158        $this->call = new Grpc\Call();
159        $this->assertNull($this->call);
160    }
161
162    /**
163     * @expectedException InvalidArgumentException
164     */
165    public function testInvalidConstuctor2()
166    {
167        $this->call = new Grpc\Call('hi', 'hi', 'hi');
168        $this->assertNull($this->call);
169    }
170
171    /**
172     * @expectedException InvalidArgumentException
173     */
174    public function testInvalidSetCredentials()
175    {
176        $this->call->setCredentials('hi');
177    }
178
179    /**
180     * @expectedException InvalidArgumentException
181     */
182    public function testInvalidSetCredentials2()
183    {
184        $this->call->setCredentials([]);
185    }
186}
187