blob: be9234c1ab0d28444c2ddf585880901186886065 [file] [log] [blame]
Matt A9e745f72017-07-18 10:45:18 -04001<?php
2
3require_once('test_base.php');
4require_once('test_util.php');
5
6use Google\Protobuf\Internal\RepeatedField;
7use Google\Protobuf\Internal\MapField;
8use Google\Protobuf\Internal\GPBType;
9use Foo\Greeter;
10use Foo\HelloRequest;
11use Foo\HelloReply;
12
13class 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 Yang823f3512020-12-04 13:46:34 -080033 /**
34 * Avoid calling setUp, which has void return type (not avalialbe in php7.0).
35 *
36 * @before
37 */
38 public function setUpTest()
Matt A9e745f72017-07-18 10:45:18 -040039 {
Matt A9e745f72017-07-18 10:45:18 -040040 $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 Yang823f3512020-12-04 13:46:34 -080052 $this->assertStringContains(
53 'foo.Greeter', $this->serviceClass->getDocComment());
Matt A9e745f72017-07-18 10:45:18 -040054 }
55
56 public function testPhpDocForNamespacedClass()
57 {
Paul Yang823f3512020-12-04 13:46:34 -080058 $this->assertStringContains(
59 'foo.OtherGreeter', $this->namespacedServiceClass->getDocComment());
Matt A9e745f72017-07-18 10:45:18 -040060 }
61
62 public function testServiceMethodsAreGenerated()
63 {
Paul Yang823f3512020-12-04 13:46:34 -080064 $this->assertCount(
65 count($this->methodNames), $this->serviceClass->getMethods());
Matt A9e745f72017-07-18 10:45:18 -040066 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 Yang823f3512020-12-04 13:46:34 -080074 $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 A9e745f72017-07-18 10:45:18 -040081 }
82 }
83
84 public function testPhpDocForServiceMethodInNamespacedClass()
85 {
86 foreach ($this->methodNames as $methodName) {
Paul Yang823f3512020-12-04 13:46:34 -080087 $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 A9e745f72017-07-18 10:45:18 -040095 }
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 Yang823f3512020-12-04 13:46:34 -0800106 // ReflectionParameter::getType only exists in PHP 7+, so get the
107 // type from __toString
108 $this->assertStringContains(
109 'Foo\HelloRequest $request', (string) $param);
Matt A9e745f72017-07-18 10:45:18 -0400110 }
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 Yang823f3512020-12-04 13:46:34 -0800121 // ReflectionParameter::getType only exists in PHP 7+, so get the
122 // type from __toString
123 $this->assertStringContains(
124 'Foo\HelloRequest $request', (string) $param);
Matt A9e745f72017-07-18 10:45:18 -0400125 }
126 }
127}