xref: /aosp_15_r20/external/grpc-grpc/src/php/tests/unit_tests/RpcServerTest.php (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1<?php
2/*
3 *
4 * Copyright 2015 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20require_once(dirname(__FILE__) . '/../../lib/Grpc/ServerCallReader.php');
21require_once(dirname(__FILE__) . '/../../lib/Grpc/ServerCallWriter.php');
22require_once(dirname(__FILE__) . '/../../lib/Grpc/Status.php');
23require_once(dirname(__FILE__) . '/../../lib/Grpc/MethodDescriptor.php');
24require_once(dirname(__FILE__) . '/../../lib/Grpc/RpcServer.php');
25
26class RpcServerTest extends \PHPUnit\Framework\TestCase
27{
28    private $server;
29    private $mockService;
30
31    public function setUp(): void
32    {
33        $this->server = new \Grpc\RpcServer();
34        $this->mockService = $this->getMockBuilder(stdClass::class)
35            ->setMethods(['getMethodDescriptors', 'hello'])
36            ->getMock();
37    }
38
39    public function testHandleServices()
40    {
41        $helloMethodDescriptor = new \Grpc\MethodDescriptor(
42            $this->mockService,
43            'hello',
44            'String',
45            \Grpc\MethodDescriptor::UNARY_CALL
46        );
47        $this->mockService->expects($this->once())
48            ->method('getMethodDescriptors')
49            ->with()
50            ->will($this->returnValue([
51                '/test/hello' => $helloMethodDescriptor
52            ]));
53
54        $pathMap = $this->server->handle($this->mockService);
55        $this->assertEquals($pathMap, [
56            '/test/hello' => $helloMethodDescriptor
57        ]);
58
59        $mockService2 = $this->getMockBuilder(stdClass::class)
60            ->setMethods(['getMethodDescriptors', 'hello', 'bye'])
61            ->getMock();
62        $helloMethodDescriptor2 = new \Grpc\MethodDescriptor(
63            $this->mockService,
64            'hello',
65            'Number',
66            \Grpc\MethodDescriptor::UNARY_CALL
67        );
68        $byeMethodDescritor = new \Grpc\MethodDescriptor(
69            $this->mockService,
70            'bye',
71            'String',
72            \Grpc\MethodDescriptor::UNARY_CALL
73        );
74        $mockService2->expects($this->once())
75            ->method('getMethodDescriptors')
76            ->with()
77            ->will($this->returnValue([
78                '/test/hello' => $helloMethodDescriptor2,
79                '/test/bye' => $byeMethodDescritor
80            ]));
81
82        $pathMap = $this->server->handle($mockService2);
83        $this->assertEquals($pathMap, [
84            '/test/hello' => $helloMethodDescriptor2,
85            '/test/bye' => $byeMethodDescritor
86        ]);
87    }
88}
89