AbstractGeneratedCodeTest.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
  35. require 'DrSlump/Protobuf.php';
  36. \DrSlump\Protobuf::autoload();
  37. require 'math.php';
  38. abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase {
  39. /* These tests require that a server exporting the math service must be
  40. * running on $GRPC_TEST_HOST */
  41. protected static $client;
  42. protected static $timeout;
  43. public function testSimpleRequest() {
  44. $div_arg = new math\DivArgs();
  45. $div_arg->setDividend(7);
  46. $div_arg->setDivisor(4);
  47. list($response, $status) = self::$client->Div($div_arg)->wait();
  48. $this->assertSame(1, $response->getQuotient());
  49. $this->assertSame(3, $response->getRemainder());
  50. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  51. }
  52. public function testServerStreaming() {
  53. $fib_arg = new math\FibArgs();
  54. $fib_arg->setLimit(7);
  55. $call = self::$client->Fib($fib_arg);
  56. $result_array = iterator_to_array($call->responses());
  57. $extract_num = function($num){
  58. return $num->getNum();
  59. };
  60. $values = array_map($extract_num, $result_array);
  61. $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values);
  62. $status = $call->getStatus();
  63. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  64. }
  65. public function testClientStreaming() {
  66. $num_iter = function() {
  67. for ($i = 0; $i < 7; $i++) {
  68. $num = new math\Num();
  69. $num->setNum($i);
  70. yield $num;
  71. }
  72. };
  73. $call = self::$client->Sum($num_iter());
  74. list($response, $status) = $call->wait();
  75. $this->assertSame(21, $response->getNum());
  76. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  77. }
  78. public function testBidiStreaming() {
  79. $call = self::$client->DivMany();
  80. for ($i = 0; $i < 7; $i++) {
  81. $div_arg = new math\DivArgs();
  82. $div_arg->setDividend(2 * $i + 1);
  83. $div_arg->setDivisor(2);
  84. $call->write($div_arg);
  85. $response = $call->read();
  86. $this->assertSame($i, $response->getQuotient());
  87. $this->assertSame(1, $response->getRemainder());
  88. }
  89. $call->writesDone();
  90. $status = $call->getStatus();
  91. $this->assertSame(\Grpc\STATUS_OK, $status->code);
  92. }
  93. }