CallTest.php 5.5 KB

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