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