THRIFT-5934: Fix deprecated usage of ReflectionProperty::setAccessible() in PHP tests
diff --git a/lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php b/lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php
index 76ff187..04b1159 100644
--- a/lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php
+++ b/lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php
@@ -22,12 +22,15 @@
namespace Test\Thrift\Unit\Lib\Factory;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Factory\TBinaryProtocolFactory;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TTransport;
class TBinaryProtocolFactoryTest extends TestCase
{
+ use ReflectionHelper;
+
/**
* @dataProvider getProtocolDataProvider
* @param bool $strictRead
@@ -44,17 +47,9 @@
$this->assertInstanceOf(TBinaryProtocol::class, $protocol);
- $ref = new \ReflectionClass($protocol);
- $refStrictRead = $ref->getProperty('strictRead_');
- $refStrictRead->setAccessible(true);
- $refStrictWrite = $ref->getProperty('strictWrite_');
- $refStrictWrite->setAccessible(true);
- $refTrans = $ref->getProperty('trans_');
- $refTrans->setAccessible(true);
-
- $this->assertEquals($strictRead, $refStrictRead->getValue($protocol));
- $this->assertEquals($strictWrite, $refStrictWrite->getValue($protocol));
- $this->assertSame($transport, $refTrans->getValue($protocol));
+ $this->assertEquals($strictRead, $this->getPropertyValue($protocol, 'strictRead_'));
+ $this->assertEquals($strictWrite, $this->getPropertyValue($protocol, 'strictWrite_'));
+ $this->assertSame($transport, $this->getPropertyValue($protocol, 'trans_'));
}
public function getProtocolDataProvider()
diff --git a/lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php b/lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php
index 1483c6a..77fb2ce 100644
--- a/lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php
+++ b/lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php
@@ -22,12 +22,15 @@
namespace Test\Thrift\Unit\Lib\Factory;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Factory\TCompactProtocolFactory;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Transport\TTransport;
class TCompactProtocolFactoryTest extends TestCase
{
+ use ReflectionHelper;
+
/**
* @return void
*/
@@ -39,10 +42,6 @@
$this->assertInstanceOf(TCompactProtocol::class, $protocol);
- $ref = new \ReflectionClass($protocol);
- $refTrans = $ref->getProperty('trans_');
- $refTrans->setAccessible(true);
-
- $this->assertSame($transport, $refTrans->getValue($protocol));
+ $this->assertSame($transport, $this->getPropertyValue($protocol, 'trans_'));
}
}
diff --git a/lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php b/lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php
index f8a860c..18cdc3e 100644
--- a/lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php
+++ b/lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php
@@ -22,12 +22,15 @@
namespace Test\Thrift\Unit\Lib\Factory;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Factory\TFramedTransportFactory;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TTransport;
class TFramedTransportFactoryTest extends TestCase
{
+ use ReflectionHelper;
+
/**
* @return void
*/
@@ -39,16 +42,8 @@
$this->assertInstanceOf(TFramedTransport::class, $framedTransport);
- $ref = new \ReflectionClass($framedTransport);
- $refRead = $ref->getProperty('read_');
- $refRead->setAccessible(true);
- $refWrite = $ref->getProperty('write_');
- $refWrite->setAccessible(true);
- $refTrans = $ref->getProperty('transport_');
- $refTrans->setAccessible(true);
-
- $this->assertTrue($refRead->getValue($framedTransport));
- $this->assertTrue($refWrite->getValue($framedTransport));
- $this->assertSame($transport, $refTrans->getValue($framedTransport));
+ $this->assertTrue($this->getPropertyValue($framedTransport, 'read_'));
+ $this->assertTrue($this->getPropertyValue($framedTransport, 'write_'));
+ $this->assertSame($transport, $this->getPropertyValue($framedTransport, 'transport_'));
}
}
diff --git a/lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php b/lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php
index 9c7055d..9a0f3c0 100644
--- a/lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php
+++ b/lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php
@@ -22,12 +22,15 @@
namespace Test\Thrift\Unit\Lib\Factory;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Factory\TJSONProtocolFactory;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TTransport;
class TJSONProtocolFactoryTest extends TestCase
{
+ use ReflectionHelper;
+
/**
* @return void
*/
@@ -39,10 +42,6 @@
$this->assertInstanceOf(TJSONProtocol::class, $protocol);
- $ref = new \ReflectionClass($protocol);
- $refTrans = $ref->getProperty('trans_');
- $refTrans->setAccessible(true);
-
- $this->assertSame($transport, $refTrans->getValue($protocol));
+ $this->assertSame($transport, $this->getPropertyValue($protocol, 'trans_'));
}
}
diff --git a/lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php b/lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php
index c6feb2c..ad37a99 100644
--- a/lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php
+++ b/lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php
@@ -23,6 +23,7 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Factory\TStringFuncFactory;
use Thrift\StringFunc\Core;
use Thrift\StringFunc\Mbstring;
@@ -31,6 +32,7 @@
class TStringFuncFactoryTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
/**
* @dataProvider createDataProvider
@@ -48,10 +50,7 @@
/**
* it is a hack to nullable the instance of TStringFuncFactory, and get a new instance based on the new ini_get value
*/
- $ref = new \ReflectionClass($factory);
- $refInstance = $ref->getProperty('_instance');
- $refInstance->setAccessible(true);
- $refInstance->setValue($factory, null);
+ $this->setPropertyValue($factory, '_instance', null);
$stringFunc = $factory::create();
diff --git a/lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php b/lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php
index 2474ec0..04e38fa 100644
--- a/lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php
+++ b/lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php
@@ -23,6 +23,7 @@
namespace Test\Thrift\Unit\Lib\Protocol;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TProtocolException;
use Thrift\Protocol\TCompactProtocol;
use Thrift\Transport\TTransport;
@@ -30,6 +31,8 @@
class TCompactProtocolTest extends TestCase
{
+ use ReflectionHelper;
+
private const COMPACT_STOP = 0x00;
private const COMPACT_TRUE = 0x01;
private const COMPACT_FALSE = 0x02;
@@ -192,10 +195,7 @@
$result = $protocol->writeMessageBegin($name, $type, $seqid);
$this->assertSame(12, $result);
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $this->assertSame(self::STATE_VALUE_WRITE, $state->getValue($protocol));
+ $this->assertSame(self::STATE_VALUE_WRITE, $this->getPropertyValue($protocol, 'state'));
}
public function testWriteMessageEnd()
@@ -204,10 +204,7 @@
$protocol = new TCompactProtocol($transport);
$this->assertSame(0, $protocol->writeMessageEnd());
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $this->assertSame(self::STATE_CLEAR, $state->getValue($protocol));
+ $this->assertSame(self::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
}
public function testWriteStruct()
@@ -216,33 +213,25 @@
$transport = $this->createMock(TTransport::class);
$protocol = new TCompactProtocol($transport);
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $lastFid = $ref->getProperty('lastFid');
- $lastFid->setAccessible(true);
- $structs = $ref->getProperty('structs');
- $structs->setAccessible(true);
+ $this->assertSame(0, $protocol->writeStructBegin($name));
+ $this->assertSame([[self::STATE_CLEAR, 0]], $this->getPropertyValue($protocol, 'structs'));
+ $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
$this->assertSame(0, $protocol->writeStructBegin($name));
- $this->assertSame([[self::STATE_CLEAR, 0]], $structs->getValue($protocol));
- $this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
- $this->assertSame(0, $lastFid->getValue($protocol));
-
- $this->assertSame(0, $protocol->writeStructBegin($name));
- $this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
- $this->assertSame(0, $lastFid->getValue($protocol));
- $this->assertSame([[self::STATE_CLEAR, 0], [self::STATE_FIELD_WRITE, 0]], $structs->getValue($protocol));
+ $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
+ $this->assertSame([[self::STATE_CLEAR, 0], [self::STATE_FIELD_WRITE, 0]], $this->getPropertyValue($protocol, 'structs'));
$this->assertSame(0, $protocol->writeStructEnd());
- $this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
- $this->assertSame(0, $lastFid->getValue($protocol));
- $this->assertSame([[self::STATE_CLEAR, 0]], $structs->getValue($protocol));
+ $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
+ $this->assertSame([[self::STATE_CLEAR, 0]], $this->getPropertyValue($protocol, 'structs'));
$this->assertSame(0, $protocol->writeStructEnd());
- $this->assertSame(self::STATE_CLEAR, $state->getValue($protocol));
- $this->assertSame(0, $lastFid->getValue($protocol));
- $this->assertSame([], $structs->getValue($protocol));
+ $this->assertSame(self::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
+ $this->assertSame([], $this->getPropertyValue($protocol, 'structs'));
}
public function testWriteFieldStop()
@@ -331,16 +320,9 @@
$this->assertSame($expectedResult, $protocol->writeFieldBegin($fieldName, $fieldType, $fieldId));
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $boolFid = $ref->getProperty('boolFid');
- $boolFid->setAccessible(true);
- $lastFid = $ref->getProperty('lastFid');
- $lastFid->setAccessible(true);
- $this->assertSame($expectedState, $state->getValue($protocol));
- $this->assertSame($expectedBoolFid, $boolFid->getValue($protocol));
- $this->assertSame($expectedLastFid, $lastFid->getValue($protocol));
+ $this->assertSame($expectedState, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame($expectedBoolFid, $this->getPropertyValue($protocol, 'boolFid'));
+ $this->assertSame($expectedLastFid, $this->getPropertyValue($protocol, 'lastFid'));
}
public function writeFieldBeginDataProvider()
@@ -380,10 +362,7 @@
$this->assertSame(0, $protocol->writeFieldEnd());
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
+ $this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
}
/**
@@ -409,16 +388,11 @@
$this->assertSame($expectedResult, $protocol->writeCollectionBegin($etype, $size));
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $containers = $ref->getProperty('containers');
- $containers->setAccessible(true);
- $this->assertSame($expectedState, $state->getValue($protocol));
- $this->assertSame($expectedContainers, $containers->getValue($protocol));
+ $this->assertSame($expectedState, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame($expectedContainers, $this->getPropertyValue($protocol, 'containers'));
$this->assertSame(0, $protocol->writeCollectionEnd());
- $this->assertSame(TCompactProtocol::STATE_CLEAR, $state->getValue($protocol));
+ $this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
}
public function writeCollectionDataProvider()
@@ -480,17 +454,12 @@
$this->assertSame($expectedResult, $protocol->writeMapBegin($keyType, $valType, $size));
- $ref = new \ReflectionClass($protocol);
- $containers = $ref->getProperty('containers');
- $containers->setAccessible(true);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $this->assertSame($expectedContainers, $containers->getValue($protocol));
- $this->assertSame(TCompactProtocol::STATE_CLEAR, $state->getValue($protocol));
+ $this->assertSame($expectedContainers, $this->getPropertyValue($protocol, 'containers'));
+ $this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
$this->assertSame(0, $protocol->writeMapEnd());
- $this->assertSame(TCompactProtocol::STATE_CLEAR, $state->getValue($protocol));
- $this->assertSame([], $containers->getValue($protocol));
+ $this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
+ $this->assertSame([], $this->getPropertyValue($protocol, 'containers'));
}
public function writeMapDataProvider()
@@ -595,10 +564,7 @@
$transport = $this->createMock(TTransport::class);
$protocol = new TCompactProtocol($transport);
if (!is_null($startState)) {
- $ref = new \ReflectionClass($protocol);
- $state = $ref->getProperty('state');
- $state->setAccessible(true);
- $state->setValue($protocol, $startState);
+ $this->setPropertyValue($protocol, 'state', $startState);
}
$transport
diff --git a/lib/php/test/Unit/Lib/ReflectionHelper.php b/lib/php/test/Unit/Lib/ReflectionHelper.php
new file mode 100644
index 0000000..5db8741
--- /dev/null
+++ b/lib/php/test/Unit/Lib/ReflectionHelper.php
@@ -0,0 +1,73 @@
+<?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.
+ */
+
+namespace Test\Thrift\Unit\Lib;
+
+trait ReflectionHelper
+{
+ /**
+ * Get a reflection property and make it accessible if needed
+ *
+ * @param object|string $objectOrClass
+ * @param string $propertyName
+ * @return \ReflectionProperty
+ */
+ protected function getAccessibleProperty($objectOrClass, string $propertyName): \ReflectionProperty
+ {
+ $ref = new \ReflectionClass($objectOrClass);
+ $property = $ref->getProperty($propertyName);
+
+ // Only call setAccessible for PHP < 8.1.0
+ if (PHP_VERSION_ID < 80100) {
+ $property->setAccessible(true);
+ }
+
+ return $property;
+ }
+
+ /**
+ * Get the value of a private/protected property
+ *
+ * @param object $object
+ * @param string $propertyName
+ * @return mixed
+ */
+ protected function getPropertyValue($object, string $propertyName)
+ {
+ $property = $this->getAccessibleProperty($object, $propertyName);
+
+ return $property->getValue($object);
+ }
+
+ /**
+ * Set the value of a private/protected property
+ *
+ * @param object $object
+ * @param string $propertyName
+ * @param mixed $value
+ * @return void
+ */
+ protected function setPropertyValue($object, string $propertyName, $value): void
+ {
+ $property = $this->getAccessibleProperty($object, $propertyName);
+ $property->setValue($object, $value);
+ }
+}
diff --git a/lib/php/test/Unit/Lib/Server/TSSLServerSocketTest.php b/lib/php/test/Unit/Lib/Server/TSSLServerSocketTest.php
index 02ae584..1aa80a4 100644
--- a/lib/php/test/Unit/Lib/Server/TSSLServerSocketTest.php
+++ b/lib/php/test/Unit/Lib/Server/TSSLServerSocketTest.php
@@ -23,6 +23,7 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TTransportException;
use Thrift\Server\TSSLServerSocket;
use Thrift\Transport\TSocket;
@@ -30,6 +31,8 @@
class TSSLServerSocketTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
+
public function testGetSSLHost()
{
@@ -69,11 +72,7 @@
$socket->listen();
- $reflection = new \ReflectionClass($socket);
- $property = $reflection->getProperty('listener_');
- $property->setAccessible(true);
-
- $this->assertIsResource($property->getValue($socket));
+ $this->assertIsResource($this->getPropertyValue($socket, 'listener_'));
$this->getFunctionMock('Thrift\Server', 'fclose')
->expects($this->once())
@@ -81,7 +80,7 @@
->willReturn(true);
$socket->close();
- $this->assertNull($property->getValue($socket));
+ $this->assertNull($this->getPropertyValue($socket, 'listener_'));
}
public function testAccept()
@@ -116,10 +115,7 @@
$result = $socket->accept();
$this->assertInstanceOf(TSocket::class, $result);
- $reflection = new \ReflectionClass($result);
- $property = $reflection->getProperty('handle_');
- $property->setAccessible(true);
- $this->assertEquals($transportHandle, $property->getValue($result));
+ $this->assertEquals($transportHandle, $this->getPropertyValue($result, 'handle_'));
}
public function testAcceptFailed()
diff --git a/lib/php/test/Unit/Lib/Server/TServerSocketTest.php b/lib/php/test/Unit/Lib/Server/TServerSocketTest.php
index e9d3b96..35b13e4 100644
--- a/lib/php/test/Unit/Lib/Server/TServerSocketTest.php
+++ b/lib/php/test/Unit/Lib/Server/TServerSocketTest.php
@@ -23,6 +23,7 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TTransportException;
use Thrift\Server\TServerSocket;
use Thrift\Transport\TSocket;
@@ -30,17 +31,14 @@
class TServerSocketTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
public function testSetAcceptTimeout(): void
{
$socket = new TServerSocket();
$socket->setAcceptTimeout(1000);
- $reflection = new \ReflectionClass($socket);
- $property = $reflection->getProperty('acceptTimeout_');
- $property->setAccessible(true);
-
- $this->assertEquals(1000, $property->getValue($socket));
+ $this->assertEquals(1000, $this->getPropertyValue($socket, 'acceptTimeout_'));
}
public function testListenAndClose(): void
@@ -55,11 +53,7 @@
$socket->listen();
- $reflection = new \ReflectionClass($socket);
- $property = $reflection->getProperty('listener_');
- $property->setAccessible(true);
-
- $this->assertIsResource($property->getValue($socket));
+ $this->assertIsResource($this->getPropertyValue($socket, 'listener_'));
$this->getFunctionMock('Thrift\Server', 'fclose')
->expects($this->once())
@@ -67,7 +61,7 @@
->willReturn(true);
$socket->close();
- $this->assertNull($property->getValue($socket));
+ $this->assertNull($this->getPropertyValue($socket, 'listener_'));
}
public function testAccept()
@@ -94,10 +88,7 @@
$result = $socket->accept();
$this->assertInstanceOf(TSocket::class, $result);
- $reflection = new \ReflectionClass($result);
- $property = $reflection->getProperty('handle_');
- $property->setAccessible(true);
- $this->assertEquals($transportHandle, $property->getValue($result));
+ $this->assertEquals($transportHandle, $this->getPropertyValue($result, 'handle_'));
}
public function testAcceptFailed()
diff --git a/lib/php/test/Unit/Lib/Transport/TBufferedTransportTest.php b/lib/php/test/Unit/Lib/Transport/TBufferedTransportTest.php
index dd6003a..ab9b9ad 100644
--- a/lib/php/test/Unit/Lib/Transport/TBufferedTransportTest.php
+++ b/lib/php/test/Unit/Lib/Transport/TBufferedTransportTest.php
@@ -22,11 +22,14 @@
namespace Test\Thrift\Unit\Lib\Transport;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Transport\TBufferedTransport;
use Thrift\Transport\TTransport;
class TBufferedTransportTest extends TestCase
{
+ use ReflectionHelper;
+
public function testIsOpen()
{
$transport = $this->createMock(TTransport::class);
@@ -72,13 +75,10 @@
$bufferedTransport = new TBufferedTransport($transport);
$bufferedTransport->putBack('test');
- $ref = new \ReflectionClass($bufferedTransport);
- $property = $ref->getProperty('rBuf_');
- $property->setAccessible(true);
- $this->assertEquals('test', $property->getValue($bufferedTransport));
+ $this->assertEquals('test', $this->getPropertyValue($bufferedTransport, 'rBuf_'));
$bufferedTransport->putBack('abcde');
- $this->assertEquals('abcdetest', $property->getValue($bufferedTransport));
+ $this->assertEquals('abcdetest', $this->getPropertyValue($bufferedTransport, 'rBuf_'));
}
/**
@@ -104,10 +104,7 @@
$this->assertEquals($expectedRead, $bufferedTransport->readAll($readLength));
- $ref = new \ReflectionClass($bufferedTransport);
- $property = $ref->getProperty('rBuf_');
- $property->setAccessible(true);
- $this->assertEquals($expectedBufferValue, $property->getValue($bufferedTransport));
+ $this->assertEquals($expectedBufferValue, $this->getPropertyValue($bufferedTransport, 'rBuf_'));
}
public function readAllDataProvider()
@@ -169,10 +166,7 @@
$this->assertEquals($expectedRead, $bufferedTransport->read($readLength));
- $ref = new \ReflectionClass($bufferedTransport);
- $property = $ref->getProperty('rBuf_');
- $property->setAccessible(true);
- $this->assertEquals($expectedBufferValue, $property->getValue($bufferedTransport));
+ $this->assertEquals($expectedBufferValue, $this->getPropertyValue($bufferedTransport, 'rBuf_'));
}
public function readDataProvider()
@@ -223,10 +217,7 @@
$this->assertNull($bufferedTransport->write($writeData));
- $ref = new \ReflectionClass($bufferedTransport);
- $property = $ref->getProperty('wBuf_');
- $property->setAccessible(true);
- $this->assertEquals($expectedWriteBufferValue, $property->getValue($bufferedTransport));
+ $this->assertEquals($expectedWriteBufferValue, $this->getPropertyValue($bufferedTransport, 'wBuf_'));
}
public function writeDataProvider()
@@ -253,10 +244,7 @@
) {
$transport = $this->createMock(TTransport::class);
$bufferedTransport = new TBufferedTransport($transport, 512, 512);
- $ref = new \ReflectionClass($bufferedTransport);
- $property = $ref->getProperty('wBuf_');
- $property->setAccessible(true);
- $property->setValue($bufferedTransport, $writeBuffer);
+ $this->setPropertyValue($bufferedTransport, 'wBuf_', $writeBuffer);
$transport
->expects(!empty($writeBuffer) ? $this->once() : $this->never())
@@ -271,7 +259,7 @@
$this->assertNull($bufferedTransport->flush());
- $this->assertEquals('', $property->getValue($bufferedTransport));
+ $this->assertEquals('', $this->getPropertyValue($bufferedTransport, 'wBuf_'));
}
public function flushDataProvider()
diff --git a/lib/php/test/Unit/Lib/Transport/TCurlClientTest.php b/lib/php/test/Unit/Lib/Transport/TCurlClientTest.php
index 6deed5a..499f459 100644
--- a/lib/php/test/Unit/Lib/Transport/TCurlClientTest.php
+++ b/lib/php/test/Unit/Lib/Transport/TCurlClientTest.php
@@ -23,12 +23,14 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TTransportException;
use Thrift\Transport\TCurlClient;
class TCurlClientTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
public function testSetTimeoutSecs()
{
@@ -36,10 +38,7 @@
$transport = new TCurlClient($host);
$transport->setTimeoutSecs(1000);
- $ref = new \ReflectionClass($transport);
- $prop = $ref->getProperty('timeout_');
- $prop->setAccessible(true);
- $this->assertEquals(1000, $prop->getValue($transport));
+ $this->assertEquals(1000, $this->getPropertyValue($transport, 'timeout_'));
}
public function testSetConnectionTimeoutSecs()
@@ -48,10 +47,7 @@
$transport = new TCurlClient($host);
$transport->setConnectionTimeoutSecs(1000);
- $ref = new \ReflectionClass($transport);
- $prop = $ref->getProperty('connectionTimeout_');
- $prop->setAccessible(true);
- $this->assertEquals(1000, $prop->getValue($transport));
+ $this->assertEquals(1000, $this->getPropertyValue($transport, 'connectionTimeout_'));
}
public function testIsOpen()
@@ -73,17 +69,12 @@
$host = 'localhost';
$transport = new TCurlClient($host);
- $ref = new \ReflectionClass($transport);
- $propRequest = $ref->getProperty('request_');
- $propRequest->setAccessible(true);
- $propRequest->setValue($transport, 'testRequest');
- $propResponse = $ref->getProperty('response_');
- $propResponse->setAccessible(true);
- $propResponse->setValue($transport, 'testResponse');
+ $this->setPropertyValue($transport, 'request_', 'testRequest');
+ $this->setPropertyValue($transport, 'response_', 'testResponse');
$this->assertNull($transport->close());
- $this->assertEmpty($propRequest->getValue($transport));
- $this->assertEmpty($propResponse->getValue($transport));
+ $this->assertEmpty($this->getPropertyValue($transport, 'request_'));
+ $this->assertEmpty($this->getPropertyValue($transport, 'response_'));
}
public function testRead()
@@ -91,19 +82,16 @@
$host = 'localhost';
$transport = new TCurlClient($host);
- $ref = new \ReflectionClass($transport);
- $propResponse = $ref->getProperty('response_');
- $propResponse->setAccessible(true);
- $propResponse->setValue($transport, '1234567890');
+ $this->setPropertyValue($transport, 'response_', '1234567890');
$response = $transport->read(5);
$this->assertEquals('12345', $response);
- $this->assertEquals('67890', $propResponse->getValue($transport));
+ $this->assertEquals('67890', $this->getPropertyValue($transport, 'response_'));
$response = $transport->read(5);
$this->assertEquals('67890', $response);
# The response does not cleaned after reading full answer, maybe it should be fixed
- $this->assertEquals('67890', $propResponse->getValue($transport));
+ $this->assertEquals('67890', $this->getPropertyValue($transport, 'response_'));
}
public function testReadAll()
@@ -111,14 +99,11 @@
$host = 'localhost';
$transport = new TCurlClient($host);
- $ref = new \ReflectionClass($transport);
- $propResponse = $ref->getProperty('response_');
- $propResponse->setAccessible(true);
- $propResponse->setValue($transport, '1234567890');
+ $this->setPropertyValue($transport, 'response_', '1234567890');
$response = $transport->readAll(5);
$this->assertEquals('12345', $response);
- $this->assertEquals('67890', $propResponse->getValue($transport));
+ $this->assertEquals('67890', $this->getPropertyValue($transport, 'response_'));
}
public function testReadAllThrift4656()
@@ -126,10 +111,7 @@
$host = 'localhost';
$transport = new TCurlClient($host);
- $ref = new \ReflectionClass($transport);
- $propResponse = $ref->getProperty('response_');
- $propResponse->setAccessible(true);
- $propResponse->setValue($transport, '');
+ $this->setPropertyValue($transport, 'response_', '');
$this->expectException(TTransportException::class);
$this->expectExceptionMessage('TCurlClient could not read 5 bytes');
@@ -143,13 +125,10 @@
$host = 'localhost';
$transport = new TCurlClient($host);
- $ref = new \ReflectionClass($transport);
- $propRequest = $ref->getProperty('request_');
- $propRequest->setAccessible(true);
- $propRequest->setValue($transport, '1234567890');
+ $this->setPropertyValue($transport, 'request_', '1234567890');
$transport->write('12345');
- $this->assertEquals('123456789012345', $propRequest->getValue($transport));
+ $this->assertEquals('123456789012345', $this->getPropertyValue($transport, 'request_'));
}
public function testAddHeaders()
@@ -157,13 +136,10 @@
$host = 'localhost';
$transport = new TCurlClient($host);
- $ref = new \ReflectionClass($transport);
- $propRequest = $ref->getProperty('headers_');
- $propRequest->setAccessible(true);
- $propRequest->setValue($transport, ['test' => '1234567890']);
+ $this->setPropertyValue($transport, 'headers_', ['test' => '1234567890']);
$transport->addHeaders(['test2' => '12345']);
- $this->assertEquals(['test' => '1234567890', 'test2' => '12345'], $propRequest->getValue($transport));
+ $this->assertEquals(['test' => '1234567890', 'test2' => '12345'], $this->getPropertyValue($transport, 'headers_'));
}
/**
@@ -413,10 +389,7 @@
->with('testHandle');
$transport = new TCurlClient('localhost');
- $ref = new \ReflectionClass($transport);
- $prop = $ref->getProperty('curlHandle');
- $prop->setAccessible(true);
- $prop->setValue($transport, 'testHandle');
+ $this->setPropertyValue($transport, 'curlHandle', 'testHandle');
$transport::closeCurlHandle();
}
diff --git a/lib/php/test/Unit/Lib/Transport/TFramedTransportTest.php b/lib/php/test/Unit/Lib/Transport/TFramedTransportTest.php
index 2607ddb..33e8966 100644
--- a/lib/php/test/Unit/Lib/Transport/TFramedTransportTest.php
+++ b/lib/php/test/Unit/Lib/Transport/TFramedTransportTest.php
@@ -22,11 +22,14 @@
namespace Test\Thrift\Unit\Lib\Transport;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TTransport;
class TFramedTransportTest extends TestCase
{
+ use ReflectionHelper;
+
public function testIsOpen()
{
$transport = $this->createMock(TTransport::class);
@@ -72,13 +75,10 @@
$framedTransport = new TFramedTransport($transport);
$framedTransport->putBack('test');
- $ref = new \ReflectionClass($framedTransport);
- $property = $ref->getProperty('rBuf_');
- $property->setAccessible(true);
- $this->assertEquals('test', $property->getValue($framedTransport));
+ $this->assertEquals('test', $this->getPropertyValue($framedTransport, 'rBuf_'));
$framedTransport->putBack('abcde');
- $this->assertEquals('abcdetest', $property->getValue($framedTransport));
+ $this->assertEquals('abcdetest', $this->getPropertyValue($framedTransport, 'rBuf_'));
}
/**
@@ -163,10 +163,7 @@
$framedTransport->write($writeData, $writeLength);
- $ref = new \ReflectionClass($framedTransport);
- $property = $ref->getProperty('wBuf_');
- $property->setAccessible(true);
- $this->assertEquals($expectedWriteBufferValue, $property->getValue($framedTransport));
+ $this->assertEquals($expectedWriteBufferValue, $this->getPropertyValue($framedTransport, 'wBuf_'));
}
public function writeDataProvider()
@@ -201,10 +198,7 @@
) {
$transport = $this->createMock(TTransport::class);
$framedTransport = new TFramedTransport($transport, true, $writeAllowed);
- $ref = new \ReflectionClass($framedTransport);
- $property = $ref->getProperty('wBuf_');
- $property->setAccessible(true);
- $property->setValue($framedTransport, $writeBuffer);
+ $this->setPropertyValue($framedTransport, 'wBuf_', $writeBuffer);
$transport
->expects($this->once())
diff --git a/lib/php/test/Unit/Lib/Transport/THttpClientTest.php b/lib/php/test/Unit/Lib/Transport/THttpClientTest.php
index ce6813c..9cde8ee 100644
--- a/lib/php/test/Unit/Lib/Transport/THttpClientTest.php
+++ b/lib/php/test/Unit/Lib/Transport/THttpClientTest.php
@@ -23,12 +23,14 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TTransportException;
use Thrift\Transport\THttpClient;
class THttpClientTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
public function testSetTimeoutSecs()
{
@@ -36,10 +38,7 @@
$transport = new THttpClient($host);
$transport->setTimeoutSecs(1000);
- $ref = new \ReflectionClass($transport);
- $prop = $ref->getProperty('timeout_');
- $prop->setAccessible(true);
- $this->assertEquals(1000, $prop->getValue($transport));
+ $this->assertEquals(1000, $this->getPropertyValue($transport, 'timeout_'));
}
public function testIsOpen()
@@ -67,13 +66,10 @@
$host = 'localhost';
$transport = new THttpClient($host);
- $ref = new \ReflectionClass($transport);
- $propRequest = $ref->getProperty('handle_');
- $propRequest->setAccessible(true);
- $propRequest->setValue($transport, $handle);
+ $this->setPropertyValue($transport, 'handle_', $handle);
$this->assertNull($transport->close());
- $this->assertNull($propRequest->getValue($transport));
+ $this->assertNull($this->getPropertyValue($transport, 'handle_'));
}
/**
@@ -108,10 +104,7 @@
$host = 'localhost';
$transport = new THttpClient($host);
- $ref = new \ReflectionClass($transport);
- $propRequest = $ref->getProperty('handle_');
- $propRequest->setAccessible(true);
- $propRequest->setValue($transport, $handle);
+ $this->setPropertyValue($transport, 'handle_', $handle);
$this->assertEquals($expectedResult, $transport->read($readLen));
}
@@ -156,13 +149,9 @@
$host = 'localhost';
$transport = new THttpClient($host);
- $ref = new \ReflectionClass($transport);
- $prop = $ref->getProperty('buf_');
- $prop->setAccessible(true);
-
$transport->write('1234567890');
- $this->assertEquals('1234567890', $prop->getValue($transport));
+ $this->assertEquals('1234567890', $this->getPropertyValue($transport, 'buf_'));
}
/**
@@ -321,12 +310,9 @@
$host = 'localhost';
$transport = new THttpClient($host);
- $ref = new \ReflectionClass($transport);
- $propRequest = $ref->getProperty('headers_');
- $propRequest->setAccessible(true);
- $propRequest->setValue($transport, ['test' => '1234567890']);
+ $this->setPropertyValue($transport, 'headers_', ['test' => '1234567890']);
$transport->addHeaders(['test2' => '12345']);
- $this->assertEquals(['test' => '1234567890', 'test2' => '12345'], $propRequest->getValue($transport));
+ $this->assertEquals(['test' => '1234567890', 'test2' => '12345'], $this->getPropertyValue($transport, 'headers_'));
}
}
diff --git a/lib/php/test/Unit/Lib/Transport/TSocketPoolTest.php b/lib/php/test/Unit/Lib/Transport/TSocketPoolTest.php
index 01e4532..1c79990 100644
--- a/lib/php/test/Unit/Lib/Transport/TSocketPoolTest.php
+++ b/lib/php/test/Unit/Lib/Transport/TSocketPoolTest.php
@@ -23,12 +23,14 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TException;
use Thrift\Transport\TSocketPool;
class TSocketPoolTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
protected function setUp(): void
{
@@ -48,11 +50,7 @@
) {
$socketPool = new TSocketPool($hosts, $ports, $persist, $debugHandler);
- $ref = new \ReflectionObject($socketPool);
- $serversProp = $ref->getProperty('servers_');
- $serversProp->setAccessible(true);
-
- $this->assertEquals($expectedServers, $serversProp->getValue($socketPool));
+ $this->assertEquals($expectedServers, $this->getPropertyValue($socketPool, 'servers_'));
}
@@ -103,11 +101,7 @@
$socketPool = new TSocketPool([], []);
$socketPool->addServer('localhost', 9090);
- $ref = new \ReflectionObject($socketPool);
- $servers = $ref->getProperty('servers_');
- $servers->setAccessible(true);
-
- $this->assertEquals([['host' => 'localhost', 'port' => 9090]], $servers->getValue($socketPool));
+ $this->assertEquals([['host' => 'localhost', 'port' => 9090]], $this->getPropertyValue($socketPool, 'servers_'));
}
public function testSetNumRetries(): void
@@ -115,11 +109,7 @@
$socketPool = new TSocketPool([], []);
$socketPool->setNumRetries(5);
- $ref = new \ReflectionObject($socketPool);
- $numRetries = $ref->getProperty('numRetries_');
- $numRetries->setAccessible(true);
-
- $this->assertEquals(5, $numRetries->getValue($socketPool));
+ $this->assertEquals(5, $this->getPropertyValue($socketPool, 'numRetries_'));
}
public function testrSetRetryInterval(): void
@@ -127,11 +117,7 @@
$socketPool = new TSocketPool([], []);
$socketPool->setRetryInterval(5);
- $ref = new \ReflectionObject($socketPool);
- $retryInterval = $ref->getProperty('retryInterval_');
- $retryInterval->setAccessible(true);
-
- $this->assertEquals(5, $retryInterval->getValue($socketPool));
+ $this->assertEquals(5, $this->getPropertyValue($socketPool, 'retryInterval_'));
}
public function testrSetMaxConsecutiveFailures(): void
@@ -139,11 +125,7 @@
$socketPool = new TSocketPool([], []);
$socketPool->setMaxConsecutiveFailures(5);
- $ref = new \ReflectionObject($socketPool);
- $maxConsecutiveFailures = $ref->getProperty('maxConsecutiveFailures_');
- $maxConsecutiveFailures->setAccessible(true);
-
- $this->assertEquals(5, $maxConsecutiveFailures->getValue($socketPool));
+ $this->assertEquals(5, $this->getPropertyValue($socketPool, 'maxConsecutiveFailures_'));
}
public function testrSetRandomize(): void
@@ -151,11 +133,7 @@
$socketPool = new TSocketPool([], []);
$socketPool->setRandomize(false);
- $ref = new \ReflectionObject($socketPool);
- $randomize = $ref->getProperty('randomize_');
- $randomize->setAccessible(true);
-
- $this->assertEquals(false, $randomize->getValue($socketPool));
+ $this->assertEquals(false, $this->getPropertyValue($socketPool, 'randomize_'));
}
public function testrSetAlwaysTryLast(): void
@@ -163,11 +141,7 @@
$socketPool = new TSocketPool([], []);
$socketPool->setAlwaysTryLast(false);
- $ref = new \ReflectionObject($socketPool);
- $alwaysTryLast = $ref->getProperty('alwaysTryLast_');
- $alwaysTryLast->setAccessible(true);
-
- $this->assertEquals(false, $alwaysTryLast->getValue($socketPool));
+ $this->assertEquals(false, $this->getPropertyValue($socketPool, 'alwaysTryLast_'));
}
/**
diff --git a/lib/php/test/Unit/Lib/Transport/TSocketTest.php b/lib/php/test/Unit/Lib/Transport/TSocketTest.php
index 5dfa977..494f8ea 100644
--- a/lib/php/test/Unit/Lib/Transport/TSocketTest.php
+++ b/lib/php/test/Unit/Lib/Transport/TSocketTest.php
@@ -23,6 +23,7 @@
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;
+use Test\Thrift\Unit\Lib\ReflectionHelper;
use Thrift\Exception\TException;
use Thrift\Exception\TTransportException;
use Thrift\Transport\TSocket;
@@ -30,6 +31,7 @@
class TSocketTest extends TestCase
{
use PHPMock;
+ use ReflectionHelper;
/**
* @dataProvider openExceptionDataProvider
@@ -379,13 +381,8 @@
);
$transport->setSendTimeout(9999);
- $reflector = new \ReflectionClass($transport);
- $property = $reflector->getProperty('sendTimeoutSec_');
- $property->setAccessible(true);
- $this->assertEquals(9.0, $property->getValue($transport));
- $property = $reflector->getProperty('sendTimeoutUsec_');
- $property->setAccessible(true);
- $this->assertEquals(999000, $property->getValue($transport));
+ $this->assertEquals(9.0, $this->getPropertyValue($transport, 'sendTimeoutSec_'));
+ $this->assertEquals(999000, $this->getPropertyValue($transport, 'sendTimeoutUsec_'));
}
public function testSetRecvTimeout()
@@ -402,13 +399,8 @@
);
$transport->setRecvTimeout(9999);
- $reflector = new \ReflectionClass($transport);
- $property = $reflector->getProperty('recvTimeoutSec_');
- $property->setAccessible(true);
- $this->assertEquals(9.0, $property->getValue($transport));
- $property = $reflector->getProperty('recvTimeoutUsec_');
- $property->setAccessible(true);
- $this->assertEquals(999000, $property->getValue($transport));
+ $this->assertEquals(9.0, $this->getPropertyValue($transport, 'recvTimeoutSec_'));
+ $this->assertEquals(999000, $this->getPropertyValue($transport, 'recvTimeoutUsec_'));
}
/**
@@ -463,16 +455,10 @@
$debugHandler
);
$transport->setHandle(fopen('php://memory', 'r+'));
- $reflector = new \ReflectionClass($transport);
- $property = $reflector->getProperty('handle_');
- $property->setAccessible(true);
- $this->assertNotNull($property->getValue($transport));
+ $this->assertNotNull($this->getPropertyValue($transport, 'handle_'));
$transport->close();
- $reflector = new \ReflectionClass($transport);
- $property = $reflector->getProperty('handle_');
- $property->setAccessible(true);
- $this->assertNull($property->getValue($transport));
+ $this->assertNull($this->getPropertyValue($transport, 'handle_'));
}
/**