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