GeneratedServiceTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require_once('test_base.php');
  3. require_once('test_util.php');
  4. use Google\Protobuf\Internal\RepeatedField;
  5. use Google\Protobuf\Internal\MapField;
  6. use Google\Protobuf\Internal\GPBType;
  7. use Foo\Greeter;
  8. use Foo\HelloRequest;
  9. use Foo\HelloReply;
  10. class GeneratedServiceTest extends TestBase
  11. {
  12. /**
  13. * @var \ReflectionClass
  14. */
  15. private $serviceClass;
  16. /**
  17. * @var \ReflectionClass
  18. */
  19. private $namespacedServiceClass;
  20. /**
  21. * @var array
  22. */
  23. private $methodNames = [
  24. 'sayHello',
  25. 'sayHelloAgain'
  26. ];
  27. /**
  28. * Avoid calling setUp, which has void return type (not avalialbe in php7.0).
  29. *
  30. * @before
  31. */
  32. public function setUpTest()
  33. {
  34. $this->serviceClass = new ReflectionClass('Foo\GreeterInterface');
  35. $this->namespacedServiceClass = new ReflectionClass('Bar\OtherGreeterInterface');
  36. }
  37. public function testIsInterface()
  38. {
  39. $this->assertTrue($this->serviceClass->isInterface());
  40. }
  41. public function testPhpDocForClass()
  42. {
  43. $this->assertStringContains(
  44. 'foo.Greeter', $this->serviceClass->getDocComment());
  45. }
  46. public function testPhpDocForNamespacedClass()
  47. {
  48. $this->assertStringContains(
  49. 'foo.OtherGreeter', $this->namespacedServiceClass->getDocComment());
  50. }
  51. public function testServiceMethodsAreGenerated()
  52. {
  53. $this->assertCount(
  54. count($this->methodNames), $this->serviceClass->getMethods());
  55. foreach ($this->methodNames as $methodName) {
  56. $this->assertTrue($this->serviceClass->hasMethod($methodName));
  57. }
  58. }
  59. public function testPhpDocForServiceMethod()
  60. {
  61. foreach ($this->methodNames as $methodName) {
  62. $docComment =
  63. $this->serviceClass->getMethod($methodName)->getDocComment();
  64. $this->assertStringContains($methodName, $docComment);
  65. $this->assertStringContains(
  66. '@param \Foo\HelloRequest $request', $docComment);
  67. $this->assertStringContains(
  68. '@return \Foo\HelloReply', $docComment);
  69. }
  70. }
  71. public function testPhpDocForServiceMethodInNamespacedClass()
  72. {
  73. foreach ($this->methodNames as $methodName) {
  74. $docComment =
  75. $this->namespacedServiceClass->getMethod(
  76. $methodName)->getDocComment();
  77. $this->assertStringContains($methodName, $docComment);
  78. $this->assertStringContains(
  79. '@param \Foo\HelloRequest $request', $docComment);
  80. $this->assertStringContains(
  81. '@return \Foo\HelloReply', $docComment);
  82. }
  83. }
  84. public function testParamForServiceMethod()
  85. {
  86. foreach ($this->methodNames as $methodName) {
  87. $method = $this->serviceClass->getMethod($methodName);
  88. $this->assertCount(1, $method->getParameters());
  89. $param = $method->getParameters()[0];
  90. $this->assertFalse($param->isOptional());
  91. $this->assertSame('request', $param->getName());
  92. // ReflectionParameter::getType only exists in PHP 7+, so get the
  93. // type from __toString
  94. $this->assertStringContains(
  95. 'Foo\HelloRequest $request', (string) $param);
  96. }
  97. }
  98. public function testParamForServiceMethodInNamespacedClass()
  99. {
  100. foreach ($this->methodNames as $methodName) {
  101. $method = $this->serviceClass->getMethod($methodName);
  102. $this->assertCount(1, $method->getParameters());
  103. $param = $method->getParameters()[0];
  104. $this->assertFalse($param->isOptional());
  105. $this->assertSame('request', $param->getName());
  106. // ReflectionParameter::getType only exists in PHP 7+, so get the
  107. // type from __toString
  108. $this->assertStringContains(
  109. 'Foo\HelloRequest $request', (string) $param);
  110. }
  111. }
  112. }