blob: 1205f0cd8eb7b7fca3b9ad8b891dd23b84d7b025 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
3 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07004 * Copyright 2015, Google Inc.
Craig Tiller2e498aa2015-02-16 12:09:31 -08005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070034class CallTest extends PHPUnit_Framework_TestCase
35{
36 public static $server;
37 public static $port;
mlumishb892a272014-12-09 16:28:23 -080038
Stanley Cheungd5b20562015-10-27 13:27:05 -070039 public static function setUpBeforeClass()
40 {
41 self::$server = new Grpc\Server([]);
42 self::$port = self::$server->addHttp2Port('0.0.0.0:0');
43 }
mlumishb892a272014-12-09 16:28:23 -080044
Stanley Cheungd5b20562015-10-27 13:27:05 -070045 public function setUp()
46 {
47 $this->channel = new Grpc\Channel('localhost:'.self::$port, []);
48 $this->call = new Grpc\Call($this->channel,
49 '/foo',
50 Grpc\Timeval::infFuture());
51 }
mlumishb892a272014-12-09 16:28:23 -080052
thinkeroua3730b72016-07-20 16:59:54 +080053 public function tearDown()
54 {
55 unset($this->call);
56 unset($this->channel);
57 }
58
59 public function testConstructor()
60 {
61 $this->assertSame('Grpc\Call', get_class($this->call));
62 $this->assertObjectHasAttribute('channel', $this->call);
63 }
64
Stanley Cheungd5b20562015-10-27 13:27:05 -070065 public function testAddEmptyMetadata()
66 {
67 $batch = [
68 Grpc\OP_SEND_INITIAL_METADATA => [],
69 ];
70 $result = $this->call->startBatch($batch);
71 $this->assertTrue($result->send_metadata);
72 }
mlumishb892a272014-12-09 16:28:23 -080073
Stanley Cheungd5b20562015-10-27 13:27:05 -070074 public function testAddSingleMetadata()
75 {
76 $batch = [
77 Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value']],
78 ];
79 $result = $this->call->startBatch($batch);
80 $this->assertTrue($result->send_metadata);
81 }
mlumishb892a272014-12-09 16:28:23 -080082
Stanley Cheungd5b20562015-10-27 13:27:05 -070083 public function testAddMultiValueMetadata()
84 {
85 $batch = [
86 Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value1', 'value2']],
87 ];
88 $result = $this->call->startBatch($batch);
89 $this->assertTrue($result->send_metadata);
90 }
mlumishb892a272014-12-09 16:28:23 -080091
Stanley Cheungd5b20562015-10-27 13:27:05 -070092 public function testAddSingleAndMultiValueMetadata()
93 {
94 $batch = [
95 Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1'],
Stanley Cheung69c565b2016-07-20 14:21:35 -070096 'key2' => ['value2',
97 'value3'], ],
Stanley Cheungd5b20562015-10-27 13:27:05 -070098 ];
99 $result = $this->call->startBatch($batch);
100 $this->assertTrue($result->send_metadata);
101 }
Stanley Cheungdb98e082015-07-27 10:19:45 -0700102
Stanley Cheungd5b20562015-10-27 13:27:05 -0700103 public function testGetPeer()
104 {
105 $this->assertTrue(is_string($this->call->getPeer()));
106 }
Stanley Cheungcccf9292016-02-12 16:37:19 -0800107
108 public function testCancel()
109 {
Stanley Cheung7f05da62016-04-19 09:31:22 -0700110 $this->assertNull($this->call->cancel());
Stanley Cheungcccf9292016-02-12 16:37:19 -0800111 }
112
113 /**
114 * @expectedException InvalidArgumentException
115 */
Stanley Cheung129bca62016-08-26 19:54:57 -0700116 public function testInvalidStartBatchKey()
Stanley Cheungcccf9292016-02-12 16:37:19 -0800117 {
118 $batch = [
119 'invalid' => ['key1' => 'value1'],
120 ];
121 $result = $this->call->startBatch($batch);
122 }
123
124 /**
125 * @expectedException InvalidArgumentException
126 */
Stanley Cheung129bca62016-08-26 19:54:57 -0700127 public function testInvalidMetadataStrKey()
128 {
129 $batch = [
130 Grpc\OP_SEND_INITIAL_METADATA => ['Key' => ['value1', 'value2']],
131 ];
132 $result = $this->call->startBatch($batch);
133 }
134
135 /**
136 * @expectedException InvalidArgumentException
137 */
138 public function testInvalidMetadataIntKey()
139 {
140 $batch = [
141 Grpc\OP_SEND_INITIAL_METADATA => [1 => ['value1', 'value2']],
142 ];
143 $result = $this->call->startBatch($batch);
144 }
145
146 /**
147 * @expectedException InvalidArgumentException
148 */
Stanley Cheungcccf9292016-02-12 16:37:19 -0800149 public function testInvalidMetadataInnerValue()
150 {
151 $batch = [
152 Grpc\OP_SEND_INITIAL_METADATA => ['key1' => 'value1'],
153 ];
154 $result = $this->call->startBatch($batch);
155 }
thinkeroua3730b72016-07-20 16:59:54 +0800156
157 /**
158 * @expectedException InvalidArgumentException
159 */
160 public function testInvalidConstuctor()
161 {
162 $this->call = new Grpc\Call();
163 $this->assertNull($this->call);
164 }
165
166 /**
167 * @expectedException InvalidArgumentException
168 */
169 public function testInvalidConstuctor2()
170 {
171 $this->call = new Grpc\Call('hi', 'hi', 'hi');
172 $this->assertNull($this->call);
173 }
174
175 /**
176 * @expectedException InvalidArgumentException
177 */
178 public function testInvalidSetCredentials()
179 {
180 $this->call->setCredentials('hi');
181 }
182
183 /**
184 * @expectedException InvalidArgumentException
185 */
186 public function testInvalidSetCredentials2()
187 {
188 $this->call->setCredentials([]);
189 }
mlumishb892a272014-12-09 16:28:23 -0800190}