pkvach commented on code in PR #2942: URL: https://github.com/apache/thrift/pull/2942#discussion_r1510414402
########## lib/php/test/Unit/Lib/Transport/TBufferedTransportTest.php: ########## @@ -0,0 +1,288 @@ +<?php + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.transport + */ + +namespace Test\Thrift\Unit\Lib\Transport; + +use PHPUnit\Framework\TestCase; +use Thrift\Transport\TBufferedTransport; +use Thrift\Transport\TTransport; + +class TBufferedTransportTest extends TestCase +{ + public function testIsOpen() + { + $transport = $this->createMock(TTransport::class); + $bufferedTransport = new TBufferedTransport($transport); + + $transport + ->expects($this->once()) + ->method('isOpen') + ->willReturn(true); + + $this->assertTrue($bufferedTransport->isOpen()); + } + + public function testOpen() + { + $transport = $this->createMock(TTransport::class); + $bufferedTransport = new TBufferedTransport($transport); + + $transport + ->expects($this->once()) + ->method('open') + ->willReturn(null); + + $this->assertNull($bufferedTransport->open()); + } + + public function testClose() + { + $transport = $this->createMock(TTransport::class); + $bufferedTransport = new TBufferedTransport($transport); + + $transport + ->expects($this->once()) + ->method('close') + ->willReturn(null); + + $this->assertNull($bufferedTransport->close()); + } + + public function testPutBack() + { + $transport = $this->createMock(TTransport::class); + $bufferedTransport = new TBufferedTransport($transport); + $bufferedTransport->putBack('test'); + + $ref = new \ReflectionClass($bufferedTransport); + $property = $ref->getProperty('rBuf_'); + $property->setAccessible(true); + $this->assertEquals('test', $property->getValue($bufferedTransport)); + + $bufferedTransport->putBack('abcde'); + $this->assertEquals('abcdetest', $property->getValue($bufferedTransport)); + } + + /** + * @dataProvider readAllDataProvider + */ + public function testReadAll( + $startBuffer, + $readLength, + $bufferReadLength, + $bufferReadResult, + $expectedBufferValue, + $expectedRead + ) { + $transport = $this->createMock(TTransport::class); + $bufferedTransport = new TBufferedTransport($transport); + $bufferedTransport->putBack($startBuffer); + + $transport + ->expects($bufferReadLength > 0 ? $this->once() : $this->never()) + ->method('readAll') + ->with($bufferReadLength) + ->willReturn($bufferReadResult); + + $this->assertEquals($expectedRead, $bufferedTransport->readAll($readLength)); + + $ref = new \ReflectionClass($bufferedTransport); + $property = $ref->getProperty('rBuf_'); + $property->setAccessible(true); + $this->assertEquals($expectedBufferValue, $property->getValue($bufferedTransport)); + } + + public function readAllDataProvider() + { + yield 'buffer empty' => [ + 'startBuffer' => '', + 'readLength' => 5, + 'bufferReadLength' => 5, + 'bufferReadResult' => '12345', + 'expectedBufferValue' => '', + 'expectedRead' => '12345', + ]; + yield 'buffer have partly loaded data' => [ + 'startBuffer' => '12345', + 'readLength' => 10, + 'bufferReadLength' => 5, + 'bufferReadResult' => '67890', + 'expectedBufferValue' => '', + 'expectedRead' => '1234567890', + ]; + yield 'buffer fully readed' => [ Review Comment: ```suggestion yield 'buffer fully read' => [ ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
