GeneratedServiceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. public function setUp()
  28. {
  29. parent::setUp();
  30. $this->serviceClass = new ReflectionClass('Foo\GreeterInterface');
  31. $this->namespacedServiceClass = new ReflectionClass('Bar\OtherGreeterInterface');
  32. }
  33. public function testIsInterface()
  34. {
  35. $this->assertTrue($this->serviceClass->isInterface());
  36. }
  37. public function testPhpDocForClass()
  38. {
  39. $this->assertContains('foo.Greeter', $this->serviceClass->getDocComment());
  40. }
  41. public function testPhpDocForNamespacedClass()
  42. {
  43. $this->assertContains('foo.OtherGreeter', $this->namespacedServiceClass->getDocComment());
  44. }
  45. public function testServiceMethodsAreGenerated()
  46. {
  47. $this->assertCount(count($this->methodNames), $this->serviceClass->getMethods());
  48. foreach ($this->methodNames as $methodName) {
  49. $this->assertTrue($this->serviceClass->hasMethod($methodName));
  50. }
  51. }
  52. public function testPhpDocForServiceMethod()
  53. {
  54. foreach ($this->methodNames as $methodName) {
  55. $docComment = $this->serviceClass->getMethod($methodName)->getDocComment();
  56. $this->assertContains($methodName, $docComment);
  57. $this->assertContains('@param \Foo\HelloRequest $request', $docComment);
  58. $this->assertContains('@return \Foo\HelloReply', $docComment);
  59. }
  60. }
  61. public function testPhpDocForServiceMethodInNamespacedClass()
  62. {
  63. foreach ($this->methodNames as $methodName) {
  64. $docComment = $this->namespacedServiceClass->getMethod($methodName)->getDocComment();
  65. $this->assertContains($methodName, $docComment);
  66. $this->assertContains('@param \Foo\HelloRequest $request', $docComment);
  67. $this->assertContains('@return \Foo\HelloReply', $docComment);
  68. }
  69. }
  70. public function testParamForServiceMethod()
  71. {
  72. foreach ($this->methodNames as $methodName) {
  73. $method = $this->serviceClass->getMethod($methodName);
  74. $this->assertCount(1, $method->getParameters());
  75. $param = $method->getParameters()[0];
  76. $this->assertFalse($param->isOptional());
  77. $this->assertSame('request', $param->getName());
  78. // ReflectionParameter::getType only exists in PHP 7+, so get the type from __toString
  79. $this->assertContains('Foo\HelloRequest $request', (string) $param);
  80. }
  81. }
  82. public function testParamForServiceMethodInNamespacedClass()
  83. {
  84. foreach ($this->methodNames as $methodName) {
  85. $method = $this->serviceClass->getMethod($methodName);
  86. $this->assertCount(1, $method->getParameters());
  87. $param = $method->getParameters()[0];
  88. $this->assertFalse($param->isOptional());
  89. $this->assertSame('request', $param->getName());
  90. // ReflectionParameter::getType only exists in PHP 7+, so get the type from __toString
  91. $this->assertContains('Foo\HelloRequest $request', (string) $param);
  92. }
  93. }
  94. }