| 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\Transport; |
| 23 |
|
| 24 |
use phpmock\phpunit\PHPMock; |
| 25 |
use PHPUnit\Framework\TestCase; |
| 26 |
use Thrift\Exception\TException; |
| 27 |
use Thrift\Transport\TPhpStream; |
| 28 |
|
| 29 |
class TPhpStreamTest extends TestCase |
| 30 |
{ |
| 31 |
use PHPMock; |
| 32 |
|
| 33 |
/** |
| 34 |
* @dataProvider fopenDataProvider |
| 35 |
*/ |
| 36 |
public function testOpen( |
| 37 |
$mode, |
| 38 |
$sapiName, |
| 39 |
$fopenParams, |
| 40 |
$fopenResult, |
| 41 |
$expectedException, |
| 42 |
$expectedExceptionMessage, |
| 43 |
$expectedExceptionCode |
| 44 |
) { |
| 45 |
#due to the running tests in separate process we could not open stream in data provider, so we need to do it here |
| 46 |
foreach ($fopenResult as $num => $result) { |
| 47 |
$fopenResult[$num] = $result ? fopen(...$result) : $result; |
| 48 |
} |
| 49 |
|
| 50 |
$this->getFunctionMock('Thrift\Transport', 'php_sapi_name') |
| 51 |
->expects(!empty($sapiName) ? $this->once() : $this->never()) |
| 52 |
->willReturn($sapiName); |
| 53 |
|
| 54 |
$this->getFunctionMock('Thrift\Transport', 'fopen') |
| 55 |
->expects($this->exactly(count($fopenResult))) |
| 56 |
->withConsecutive(...$fopenParams) |
| 57 |
->willReturnOnConsecutiveCalls(...$fopenResult); |
| 58 |
|
| 59 |
if ($expectedException) { |
| 60 |
$this->expectException($expectedException); |
| 61 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 62 |
$this->expectExceptionCode($expectedExceptionCode); |
| 63 |
} |
| 64 |
|
| 65 |
$transport = new TPhpStream($mode); |
| 66 |
$transport->open(); |
| 67 |
} |
| 68 |
|
| 69 |
public function fopenDataProvider() |
| 70 |
{ |
| 71 |
yield 'readCli' => [ |
| 72 |
'mode' => TPhpStream::MODE_R, |
| 73 |
'sapiName' => 'cli', |
| 74 |
'fopenParams' => [['php://stdin', 'r']], |
| 75 |
'fopenResult' => [['php://temp', 'r']], |
| 76 |
'expectedException' => null, |
| 77 |
'expectedExceptionMessage' => '', |
| 78 |
'expectedExceptionCode' => 0, |
| 79 |
]; |
| 80 |
yield 'readNotCli' => [ |
| 81 |
'mode' => TPhpStream::MODE_R, |
| 82 |
'sapiName' => 'apache', |
| 83 |
'fopenParams' => [['php://input', 'r']], |
| 84 |
'fopenResult' => [['php://temp', 'r']], |
| 85 |
'expectedException' => null, |
| 86 |
'expectedExceptionMessage' => '', |
| 87 |
'expectedExceptionCode' => 0, |
| 88 |
]; |
| 89 |
yield 'write' => [ |
| 90 |
'mode' => TPhpStream::MODE_W, |
| 91 |
'sapiName' => '', |
| 92 |
'fopenParams' => [['php://output', 'w']], |
| 93 |
'fopenResult' => [['php://temp', 'w']], |
| 94 |
'expectedException' => null, |
| 95 |
'expectedExceptionMessage' => '', |
| 96 |
'expectedExceptionCode' => 0, |
| 97 |
]; |
| 98 |
yield 'read and write' => [ |
| 99 |
'mode' => TPhpStream::MODE_R | TPhpStream::MODE_W, |
| 100 |
'sapiName' => 'cli', |
| 101 |
'fopenParams' => [['php://stdin', 'r'], ['php://output', 'w']], |
| 102 |
'fopenResult' => [['php://temp', 'r'], ['php://temp', 'w']], |
| 103 |
'expectedException' => null, |
| 104 |
'expectedExceptionMessage' => '', |
| 105 |
'expectedExceptionCode' => 0, |
| 106 |
]; |
| 107 |
yield 'read exception' => [ |
| 108 |
'mode' => TPhpStream::MODE_R, |
| 109 |
'sapiName' => 'cli', |
| 110 |
'fopenParams' => [['php://stdin', 'r']], |
| 111 |
'fopenResult' => [false], |
| 112 |
'expectedException' => TException::class, |
| 113 |
'expectedExceptionMessage' => 'TPhpStream: Could not open php://input', |
| 114 |
#should depend on php_sapi_name result |
| 115 |
'expectedExceptionCode' => 0, |
| 116 |
]; |
| 117 |
yield 'write exception' => [ |
| 118 |
'mode' => TPhpStream::MODE_W, |
| 119 |
'sapiName' => '', |
| 120 |
'fopenParams' => [['php://output', 'w']], |
| 121 |
'fopenResult' => [false], |
| 122 |
'expectedException' => TException::class, |
| 123 |
'expectedExceptionMessage' => 'TPhpStream: Could not open php://output', |
| 124 |
'expectedExceptionCode' => 0, |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @dataProvider closeDataProvider |
| 130 |
*/ |
| 131 |
public function testClose( |
| 132 |
$mode, |
| 133 |
$fopenParams, |
| 134 |
$fopenResult |
| 135 |
) { |
| 136 |
#due to the running tests in separate process we could not open stream in data provider, so we need to do it here |
| 137 |
foreach ($fopenResult as $num => $result) { |
| 138 |
$fopenResult[$num] = $result ? fopen(...$result) : $result; |
| 139 |
} |
| 140 |
|
| 141 |
$this->getFunctionMock('Thrift\Transport', 'fopen') |
| 142 |
->expects($this->exactly(count($fopenParams))) |
| 143 |
->withConsecutive(...$fopenParams) |
| 144 |
->willReturnOnConsecutiveCalls(...$fopenResult); |
| 145 |
|
| 146 |
$this->getFunctionMock('Thrift\Transport', 'fclose') |
| 147 |
->expects($this->exactly(count($fopenParams))) |
| 148 |
->with( |
| 149 |
$this->callback(function ($stream) { |
| 150 |
return is_resource($stream); |
| 151 |
}) |
| 152 |
) |
| 153 |
->willReturn(true); |
| 154 |
|
| 155 |
$transport = new TPhpStream($mode); |
| 156 |
$transport->open(); |
| 157 |
$this->assertTrue($transport->isOpen()); |
| 158 |
|
| 159 |
$transport->close(); |
| 160 |
$this->assertFalse($transport->isOpen()); |
| 161 |
} |
| 162 |
|
| 163 |
public function closeDataProvider() |
| 164 |
{ |
| 165 |
$read = ['php://temp', 'r']; |
| 166 |
$write = ['php://temp', 'w']; |
| 167 |
yield 'read' => [ |
| 168 |
'mode' => TPhpStream::MODE_R, |
| 169 |
'fopenParams' => [['php://stdin', 'r']], |
| 170 |
'fopenResult' => [$read], |
| 171 |
]; |
| 172 |
yield 'write' => [ |
| 173 |
'mode' => TPhpStream::MODE_W, |
| 174 |
'fopenParams' => [['php://output', 'w']], |
| 175 |
'fopenResult' => [$write], |
| 176 |
]; |
| 177 |
yield 'read and write' => [ |
| 178 |
'mode' => TPhpStream::MODE_R | TPhpStream::MODE_W, |
| 179 |
'fopenParams' => [['php://stdin', 'r'], ['php://output', 'w']], |
| 180 |
'fopenResult' => [$read, $write], |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @dataProvider readDataProvider |
| 186 |
*/ |
| 187 |
public function testRead( |
| 188 |
$freadResult, |
| 189 |
$expectedResult, |
| 190 |
$expectedException, |
| 191 |
$expectedExceptionMessage, |
| 192 |
$expectedExceptionCode |
| 193 |
) { |
| 194 |
$this->getFunctionMock('Thrift\Transport', 'fread') |
| 195 |
->expects($this->once()) |
| 196 |
->with($this->anything(), 5) |
| 197 |
->willReturn($freadResult); |
| 198 |
|
| 199 |
if ($expectedException) { |
| 200 |
$this->expectException($expectedException); |
| 201 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 202 |
$this->expectExceptionCode($expectedExceptionCode); |
| 203 |
} |
| 204 |
|
| 205 |
$transport = new TPhpStream(TPhpStream::MODE_R); |
| 206 |
$this->assertEquals($expectedResult, $transport->read(5)); |
| 207 |
} |
| 208 |
|
| 209 |
public function readDataProvider() |
| 210 |
{ |
| 211 |
yield 'success' => [ |
| 212 |
'freadResult' => '12345', |
| 213 |
'expectedResult' => '12345', |
| 214 |
'expectedException' => null, |
| 215 |
'expectedExceptionMessage' => '', |
| 216 |
'expectedExceptionCode' => 0, |
| 217 |
]; |
| 218 |
yield 'empty' => [ |
| 219 |
'freadResult' => '', |
| 220 |
'expectedResult' => '', |
| 221 |
'expectedException' => TException::class, |
| 222 |
'expectedExceptionMessage' => 'TPhpStream: Could not read 5 bytes', |
| 223 |
'expectedExceptionCode' => 0, |
| 224 |
]; |
| 225 |
yield 'false' => [ |
| 226 |
'freadResult' => false, |
| 227 |
'expectedResult' => false, |
| 228 |
'expectedException' => TException::class, |
| 229 |
'expectedExceptionMessage' => 'TPhpStream: Could not read 5 bytes', |
| 230 |
'expectedExceptionCode' => 0, |
| 231 |
]; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @dataProvider writeDataProvider |
| 236 |
*/ |
| 237 |
public function testWrite( |
| 238 |
$buf, |
| 239 |
$fwriteParams, |
| 240 |
$fwriteResult, |
| 241 |
$expectedException, |
| 242 |
$expectedExceptionMessage, |
| 243 |
$expectedExceptionCode |
| 244 |
) { |
| 245 |
$this->getFunctionMock('Thrift\Transport', 'fwrite') |
| 246 |
->expects($this->exactly(count($fwriteParams))) |
| 247 |
->withConsecutive(...$fwriteParams) |
| 248 |
->willReturnOnConsecutiveCalls(...$fwriteResult); |
| 249 |
|
| 250 |
if ($expectedException) { |
| 251 |
$this->expectException($expectedException); |
| 252 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 253 |
$this->expectExceptionCode($expectedExceptionCode); |
| 254 |
} |
| 255 |
|
| 256 |
$transport = new TPhpStream(TPhpStream::MODE_W); |
| 257 |
$transport->write($buf); |
| 258 |
} |
| 259 |
|
| 260 |
public function writeDataProvider() |
| 261 |
{ |
| 262 |
yield 'success' => [ |
| 263 |
'buf' => '12345', |
| 264 |
'fwriteParams' => [[$this->anything(), '12345']], |
| 265 |
'fwriteResult' => [5], |
| 266 |
'expectedException' => null, |
| 267 |
'expectedExceptionMessage' => '', |
| 268 |
'expectedExceptionCode' => 0, |
| 269 |
]; |
| 270 |
yield 'several iteration' => [ |
| 271 |
'buf' => '1234567890', |
| 272 |
'fwriteParams' => [[$this->anything(), '1234567890'], [$this->anything(), '67890']], |
| 273 |
'fwriteResult' => [5, 5], |
| 274 |
'expectedException' => null, |
| 275 |
'expectedExceptionMessage' => '', |
| 276 |
'expectedExceptionCode' => 0, |
| 277 |
]; |
| 278 |
yield 'fail' => [ |
| 279 |
'buf' => '1234567890', |
| 280 |
'fwriteParams' => [[$this->anything(), '1234567890']], |
| 281 |
'fwriteResult' => [false], |
| 282 |
'expectedException' => TException::class, |
| 283 |
'expectedExceptionMessage' => 'TPhpStream: Could not write 10 bytes', |
| 284 |
'expectedExceptionCode' => 0, |
| 285 |
]; |
| 286 |
} |
| 287 |
|
| 288 |
public function testFlush() |
| 289 |
{ |
| 290 |
$this->getFunctionMock('Thrift\Transport', 'fflush') |
| 291 |
->expects($this->once()); |
| 292 |
|
| 293 |
$transport = new TPhpStream(TPhpStream::MODE_R); |
| 294 |
$transport->flush(); |
| 295 |
} |
| 296 |
} |