| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Test\Thrift\Unit\Lib\Server; |
| 23 |
|
| 24 |
use PHPUnit\Framework\MockObject\MockObject; |
| 25 |
use PHPUnit\Framework\TestCase; |
| 26 |
use Test\Thrift\Unit\Lib\Server\Fixture\TestProcessor; |
| 27 |
use Thrift\Factory\TProtocolFactory; |
| 28 |
use Thrift\Factory\TTransportFactoryInterface; |
| 29 |
use Thrift\Server\TServerTransport; |
| 30 |
use Thrift\Server\TSimpleServer; |
| 31 |
use Thrift\Transport\TTransport; |
| 32 |
|
| 33 |
class TSimpleServerTest extends TestCase |
| 34 |
{ |
| 35 |
/** |
| 36 |
* @var object |
| 37 |
*/ |
| 38 |
private $processor; |
| 39 |
/** |
| 40 |
* @var MockObject|TServerTransport |
| 41 |
*/ |
| 42 |
private $transport; |
| 43 |
/** |
| 44 |
* @var MockObject|TTransportFactoryInterface |
| 45 |
*/ |
| 46 |
private $inputTransportFactory; |
| 47 |
/** |
| 48 |
* @var MockObject|TTransportFactoryInterface |
| 49 |
*/ |
| 50 |
private $outputTransportFactory; |
| 51 |
/** |
| 52 |
* @var MockObject|TProtocolFactory |
| 53 |
*/ |
| 54 |
private $inputProtocolFactory; |
| 55 |
/** |
| 56 |
* @var MockObject|TProtocolFactory |
| 57 |
*/ |
| 58 |
private $outputProtocolFactory; |
| 59 |
/** |
| 60 |
* @var TSimpleServer |
| 61 |
*/ |
| 62 |
private $server; |
| 63 |
|
| 64 |
protected function setUp(): void |
| 65 |
{ |
| 66 |
$this->processor = $this->createMock(TestProcessor::class); |
| 67 |
$this->transport = $this->createMock(TServerTransport::class); |
| 68 |
$this->inputTransportFactory = $this->createMock(TTransportFactoryInterface::class); |
| 69 |
$this->outputTransportFactory = $this->createMock(TTransportFactoryInterface::class); |
| 70 |
$this->inputProtocolFactory = $this->createMock(TProtocolFactory::class); |
| 71 |
$this->outputProtocolFactory = $this->createMock(TProtocolFactory::class); |
| 72 |
|
| 73 |
$this->server = new TSimpleServer( |
| 74 |
$this->processor, |
| 75 |
$this->transport, |
| 76 |
$this->inputTransportFactory, |
| 77 |
$this->outputTransportFactory, |
| 78 |
$this->inputProtocolFactory, |
| 79 |
$this->outputProtocolFactory |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
protected function tearDown(): void |
| 84 |
{ |
| 85 |
unset( |
| 86 |
$this->processor, |
| 87 |
$this->transport, |
| 88 |
$this->inputTransportFactory, |
| 89 |
$this->outputTransportFactory, |
| 90 |
$this->inputProtocolFactory, |
| 91 |
$this->outputProtocolFactory, |
| 92 |
$this->server |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @dataProvider serveDataProvider |
| 98 |
*/ |
| 99 |
public function testServe( |
| 100 |
$serveLoopCount, |
| 101 |
array $processLoopResult |
| 102 |
): void { |
| 103 |
$transport = $this->createMock(TTransport::class); |
| 104 |
|
| 105 |
$this->transport->expects($this->once()) |
| 106 |
->method('listen'); |
| 107 |
$this->transport->expects($this->exactly($serveLoopCount)) |
| 108 |
->method('accept') |
| 109 |
->willReturn($transport); |
| 110 |
|
| 111 |
$this->inputTransportFactory->expects($this->exactly($serveLoopCount)) |
| 112 |
->method('getTransport') |
| 113 |
->willReturn($this->createMock(TServerTransport::class)); |
| 114 |
$this->outputTransportFactory->expects($this->exactly($serveLoopCount)) |
| 115 |
->method('getTransport') |
| 116 |
->willReturn($this->createMock(TServerTransport::class)); |
| 117 |
|
| 118 |
$inputProtocol = $this->createMock(TServerTransport::class); |
| 119 |
$this->inputProtocolFactory->expects($this->exactly($serveLoopCount)) |
| 120 |
->method('getProtocol') |
| 121 |
->willReturn($inputProtocol); |
| 122 |
|
| 123 |
$outputProtocol = $this->createMock(TServerTransport::class); |
| 124 |
$this->outputProtocolFactory->expects($this->exactly($serveLoopCount)) |
| 125 |
->method('getProtocol') |
| 126 |
->willReturn($outputProtocol); |
| 127 |
|
| 128 |
/** |
| 129 |
* ATTENTION! |
| 130 |
* it is a hack to stop the server loop in unit test |
| 131 |
* last call of process can return any value, but should stop server for removing infinite loop |
| 132 |
**/ |
| 133 |
$processLoopResult[] = $this->returnCallback(function () { |
| 134 |
$this->server->stop(); |
| 135 |
|
| 136 |
return false; |
| 137 |
}); |
| 138 |
|
| 139 |
$this->processor->expects($this->exactly(count($processLoopResult))) |
| 140 |
->method('process') |
| 141 |
->with( |
| 142 |
$this->equalTo($inputProtocol), |
| 143 |
$this->equalTo($outputProtocol) |
| 144 |
) |
| 145 |
->willReturnOnConsecutiveCalls(...$processLoopResult); |
| 146 |
|
| 147 |
$this->server->serve(); |
| 148 |
} |
| 149 |
|
| 150 |
public function serveDataProvider() |
| 151 |
{ |
| 152 |
yield 'one serve loop' => [ |
| 153 |
'serveLoopCount' => 1, |
| 154 |
'processLoopResult' => [ |
| 155 |
true, |
| 156 |
] |
| 157 |
]; |
| 158 |
yield 'two serve loop' => [ |
| 159 |
'serveLoopCount' => 2, |
| 160 |
'processLoopResult' => [ |
| 161 |
true, |
| 162 |
false, |
| 163 |
] |
| 164 |
]; |
| 165 |
} |
| 166 |
} |