xref: /aosp_15_r20/external/cronet/third_party/protobuf/php/tests/GeneratedServiceTest.php (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1<?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
33    /**
34     * Avoid calling setUp, which has void return type (not avalialbe in php7.0).
35     *
36     * @before
37     */
38    public function setUpTest()
39    {
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    {
52        $this->assertStringContains(
53            'foo.Greeter', $this->serviceClass->getDocComment());
54    }
55
56    public function testPhpDocForNamespacedClass()
57    {
58        $this->assertStringContains(
59            'foo.OtherGreeter', $this->namespacedServiceClass->getDocComment());
60    }
61
62    public function testServiceMethodsAreGenerated()
63    {
64        $this->assertCount(
65            count($this->methodNames), $this->serviceClass->getMethods());
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) {
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);
81        }
82    }
83
84    public function testPhpDocForServiceMethodInNamespacedClass()
85    {
86        foreach ($this->methodNames as $methodName) {
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);
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());
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
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());
121	    // ReflectionParameter::getType only exists in PHP 7+, so get the
122	    // type from __toString
123            $this->assertStringContains(
124                'Foo\HelloRequest $request', (string) $param);
125        }
126    }
127}
128