| 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 PHPUnit\Framework\TestCase; |
| 25 |
use Test\Thrift\Unit\Lib\ReflectionHelper; |
| 26 |
use Thrift\Transport\TFramedTransport; |
| 27 |
use Thrift\Transport\TTransport; |
| 28 |
|
| 29 |
class TFramedTransportTest extends TestCase |
| 30 |
{ |
| 31 |
use ReflectionHelper; |
| 32 |
|
| 33 |
public function testIsOpen() |
| 34 |
{ |
| 35 |
$transport = $this->createMock(TTransport::class); |
| 36 |
$framedTransport = new TFramedTransport($transport); |
| 37 |
|
| 38 |
$transport |
| 39 |
->expects($this->once()) |
| 40 |
->method('isOpen') |
| 41 |
->willReturn(true); |
| 42 |
|
| 43 |
$this->assertTrue($framedTransport->isOpen()); |
| 44 |
} |
| 45 |
|
| 46 |
public function testOpen() |
| 47 |
{ |
| 48 |
$transport = $this->createMock(TTransport::class); |
| 49 |
$framedTransport = new TFramedTransport($transport); |
| 50 |
|
| 51 |
$transport |
| 52 |
->expects($this->once()) |
| 53 |
->method('open') |
| 54 |
->willReturn(null); |
| 55 |
|
| 56 |
$this->assertNull($framedTransport->open()); |
| 57 |
} |
| 58 |
|
| 59 |
public function testClose() |
| 60 |
{ |
| 61 |
$transport = $this->createMock(TTransport::class); |
| 62 |
$framedTransport = new TFramedTransport($transport); |
| 63 |
|
| 64 |
$transport |
| 65 |
->expects($this->once()) |
| 66 |
->method('close') |
| 67 |
->willReturn(null); |
| 68 |
|
| 69 |
$this->assertNull($framedTransport->close()); |
| 70 |
} |
| 71 |
|
| 72 |
public function testPutBack() |
| 73 |
{ |
| 74 |
$transport = $this->createMock(TTransport::class); |
| 75 |
$framedTransport = new TFramedTransport($transport); |
| 76 |
$framedTransport->putBack('test'); |
| 77 |
|
| 78 |
$this->assertEquals('test', $this->getPropertyValue($framedTransport, 'rBuf_')); |
| 79 |
|
| 80 |
$framedTransport->putBack('abcde'); |
| 81 |
$this->assertEquals('abcdetest', $this->getPropertyValue($framedTransport, 'rBuf_')); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @dataProvider readDataProvider |
| 86 |
*/ |
| 87 |
public function testRead( |
| 88 |
$readAllowed, |
| 89 |
$readBuffer, |
| 90 |
$lowLevelTransportReadResult, |
| 91 |
$lowLevelTransportReadAllParams, |
| 92 |
$lowLevelTransportReadAllResult, |
| 93 |
$readLength, |
| 94 |
$expectedReadResult |
| 95 |
) { |
| 96 |
$transport = $this->createMock(TTransport::class); |
| 97 |
$framedTransport = new TFramedTransport($transport, $readAllowed); |
| 98 |
$framedTransport->putBack($readBuffer); |
| 99 |
|
| 100 |
$transport |
| 101 |
->expects($readAllowed ? $this->never() : $this->once()) |
| 102 |
->method('read') |
| 103 |
->with($readLength) |
| 104 |
->willReturn($lowLevelTransportReadResult); |
| 105 |
|
| 106 |
$transport |
| 107 |
->expects($this->exactly(count($lowLevelTransportReadAllParams))) |
| 108 |
->method('readAll') |
| 109 |
->withConsecutive(...$lowLevelTransportReadAllParams) |
| 110 |
->willReturnOnConsecutiveCalls(...$lowLevelTransportReadAllResult); |
| 111 |
|
| 112 |
$this->assertEquals($expectedReadResult, $framedTransport->read($readLength)); |
| 113 |
} |
| 114 |
|
| 115 |
public function readDataProvider() |
| 116 |
{ |
| 117 |
yield 'read not allowed' => [ |
| 118 |
'readAllowed' => false, |
| 119 |
'readBuffer' => '', |
| 120 |
'lowLevelTransportReadResult' => '12345', |
| 121 |
'lowLevelTransportReadAllParams' => [], |
| 122 |
'lowLevelTransportReadAllResult' => [], |
| 123 |
'readLength' => 5, |
| 124 |
'expectedReadResult' => '12345', |
| 125 |
]; |
| 126 |
yield 'read fully buffered item' => [ |
| 127 |
'readAllowed' => true, |
| 128 |
'readBuffer' => '', |
| 129 |
'lowLevelTransportReadResult' => '', |
| 130 |
'lowLevelTransportReadAllParams' => [[4], [5]], |
| 131 |
'lowLevelTransportReadAllResult' => [pack('N', '5'), '12345'], |
| 132 |
'readLength' => 5, |
| 133 |
'expectedReadResult' => '12345', |
| 134 |
]; |
| 135 |
yield 'read partly buffered item' => [ |
| 136 |
'readAllowed' => true, |
| 137 |
'readBuffer' => '', |
| 138 |
'lowLevelTransportReadResult' => '', |
| 139 |
'lowLevelTransportReadAllParams' => [[4], [10]], |
| 140 |
'lowLevelTransportReadAllResult' => [pack('N', '10'), '1234567890'], |
| 141 |
'readLength' => 5, |
| 142 |
'expectedReadResult' => '12345', |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @dataProvider writeDataProvider |
| 148 |
*/ |
| 149 |
public function testWrite( |
| 150 |
$writeAllowed, |
| 151 |
$writeData, |
| 152 |
$writeLength, |
| 153 |
$expectedWriteBufferValue |
| 154 |
) { |
| 155 |
$transport = $this->createMock(TTransport::class); |
| 156 |
$framedTransport = new TFramedTransport($transport, true, $writeAllowed); |
| 157 |
|
| 158 |
$transport |
| 159 |
->expects($writeAllowed ? $this->never() : $this->once()) |
| 160 |
->method('write') |
| 161 |
->with('12345', 5) |
| 162 |
->willReturn(5); |
| 163 |
|
| 164 |
$framedTransport->write($writeData, $writeLength); |
| 165 |
|
| 166 |
$this->assertEquals($expectedWriteBufferValue, $this->getPropertyValue($framedTransport, 'wBuf_')); |
| 167 |
} |
| 168 |
|
| 169 |
public function writeDataProvider() |
| 170 |
{ |
| 171 |
yield 'write not allowed' => [ |
| 172 |
'writeAllowed' => false, |
| 173 |
'writeData' => '12345', |
| 174 |
'writeLength' => 5, |
| 175 |
'expectedWriteBufferValue' => '', |
| 176 |
]; |
| 177 |
yield 'write full' => [ |
| 178 |
'writeAllowed' => true, |
| 179 |
'writeData' => '12345', |
| 180 |
'writeLength' => 5, |
| 181 |
'expectedWriteBufferValue' => '12345', |
| 182 |
]; |
| 183 |
yield 'write partly' => [ |
| 184 |
'writeAllowed' => true, |
| 185 |
'writeData' => '1234567890', |
| 186 |
'writeLength' => 5, |
| 187 |
'expectedWriteBufferValue' => '12345', |
| 188 |
]; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @dataProvider flushDataProvider |
| 193 |
*/ |
| 194 |
public function testFlush( |
| 195 |
$writeAllowed, |
| 196 |
$writeBuffer, |
| 197 |
$lowLevelTransportWrite |
| 198 |
) { |
| 199 |
$transport = $this->createMock(TTransport::class); |
| 200 |
$framedTransport = new TFramedTransport($transport, true, $writeAllowed); |
| 201 |
$this->setPropertyValue($framedTransport, 'wBuf_', $writeBuffer); |
| 202 |
|
| 203 |
$transport |
| 204 |
->expects($this->once()) |
| 205 |
->method('flush'); |
| 206 |
|
| 207 |
$transport |
| 208 |
->expects($writeAllowed && !empty($writeBuffer) ? $this->once() : $this->never()) |
| 209 |
->method('write') |
| 210 |
->with($lowLevelTransportWrite) |
| 211 |
->willReturn(null); |
| 212 |
|
| 213 |
$this->assertNull($framedTransport->flush()); |
| 214 |
} |
| 215 |
|
| 216 |
public function flushDataProvider() |
| 217 |
{ |
| 218 |
yield 'write not allowed' => [ |
| 219 |
'writeAllowed' => false, |
| 220 |
'writeBuffer' => '12345', |
| 221 |
'lowLevelTransportWrite' => '', |
| 222 |
]; |
| 223 |
yield 'empty buffer' => [ |
| 224 |
'writeAllowed' => true, |
| 225 |
'writeBuffer' => '', |
| 226 |
'lowLevelTransportWrite' => '', |
| 227 |
]; |
| 228 |
yield 'write full' => [ |
| 229 |
'writeAllowed' => true, |
| 230 |
'writeBuffer' => '12345', |
| 231 |
'lowLevelTransportWrite' => pack('N', strlen('12345')) . '12345', |
| 232 |
]; |
| 233 |
} |
| 234 |
} |