mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | <?php |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 2 | /* |
| 3 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 4 | * Copyright 2015, Google Inc. |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 5 | * 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 Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 34 | class CallTest extends PHPUnit_Framework_TestCase |
| 35 | { |
| 36 | public static $server; |
| 37 | public static $port; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 38 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 39 | public static function setUpBeforeClass() |
| 40 | { |
| 41 | self::$server = new Grpc\Server([]); |
| 42 | self::$port = self::$server->addHttp2Port('0.0.0.0:0'); |
| 43 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 44 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 45 | 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 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 52 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 53 | 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 Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 65 | 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 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 73 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 74 | 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 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 82 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 83 | 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 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 91 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 92 | public function testAddSingleAndMultiValueMetadata() |
| 93 | { |
| 94 | $batch = [ |
| 95 | Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1'], |
Stanley Cheung | 69c565b | 2016-07-20 14:21:35 -0700 | [diff] [blame] | 96 | 'key2' => ['value2', |
| 97 | 'value3'], ], |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 98 | ]; |
| 99 | $result = $this->call->startBatch($batch); |
| 100 | $this->assertTrue($result->send_metadata); |
| 101 | } |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 102 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 103 | public function testGetPeer() |
| 104 | { |
| 105 | $this->assertTrue(is_string($this->call->getPeer())); |
| 106 | } |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 107 | |
| 108 | public function testCancel() |
| 109 | { |
Stanley Cheung | 7f05da6 | 2016-04-19 09:31:22 -0700 | [diff] [blame] | 110 | $this->assertNull($this->call->cancel()); |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @expectedException InvalidArgumentException |
| 115 | */ |
Stanley Cheung | 129bca6 | 2016-08-26 19:54:57 -0700 | [diff] [blame] | 116 | public function testInvalidStartBatchKey() |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 117 | { |
| 118 | $batch = [ |
| 119 | 'invalid' => ['key1' => 'value1'], |
| 120 | ]; |
| 121 | $result = $this->call->startBatch($batch); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @expectedException InvalidArgumentException |
| 126 | */ |
Stanley Cheung | 129bca6 | 2016-08-26 19:54:57 -0700 | [diff] [blame] | 127 | 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 Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 149 | public function testInvalidMetadataInnerValue() |
| 150 | { |
| 151 | $batch = [ |
| 152 | Grpc\OP_SEND_INITIAL_METADATA => ['key1' => 'value1'], |
| 153 | ]; |
| 154 | $result = $this->call->startBatch($batch); |
| 155 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 156 | |
| 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 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 190 | } |