Skip to content

Commit c9be26c

Browse files
committed
THRIFT-5934: Fix deprecated usage of ReflectionProperty::setAccessible() in PHP tests
1 parent caa4e78 commit c9be26c

15 files changed

+199
-285
lines changed

lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
namespace Test\Thrift\Unit\Lib\Factory;
2323

2424
use PHPUnit\Framework\TestCase;
25+
use Test\Thrift\Unit\Lib\ReflectionHelper;
2526
use Thrift\Factory\TBinaryProtocolFactory;
2627
use Thrift\Protocol\TBinaryProtocol;
2728
use Thrift\Transport\TTransport;
2829

2930
class TBinaryProtocolFactoryTest extends TestCase
3031
{
32+
use ReflectionHelper;
33+
3134
/**
3235
* @dataProvider getProtocolDataProvider
3336
* @param bool $strictRead
@@ -44,17 +47,9 @@ public function testGetProtocol(
4447

4548
$this->assertInstanceOf(TBinaryProtocol::class, $protocol);
4649

47-
$ref = new \ReflectionClass($protocol);
48-
$refStrictRead = $ref->getProperty('strictRead_');
49-
$refStrictRead->setAccessible(true);
50-
$refStrictWrite = $ref->getProperty('strictWrite_');
51-
$refStrictWrite->setAccessible(true);
52-
$refTrans = $ref->getProperty('trans_');
53-
$refTrans->setAccessible(true);
54-
55-
$this->assertEquals($strictRead, $refStrictRead->getValue($protocol));
56-
$this->assertEquals($strictWrite, $refStrictWrite->getValue($protocol));
57-
$this->assertSame($transport, $refTrans->getValue($protocol));
50+
$this->assertEquals($strictRead, $this->getPropertyValue($protocol, 'strictRead_'));
51+
$this->assertEquals($strictWrite, $this->getPropertyValue($protocol, 'strictWrite_'));
52+
$this->assertSame($transport, $this->getPropertyValue($protocol, 'trans_'));
5853
}
5954

6055
public function getProtocolDataProvider()

lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
namespace Test\Thrift\Unit\Lib\Factory;
2323

2424
use PHPUnit\Framework\TestCase;
25+
use Test\Thrift\Unit\Lib\ReflectionHelper;
2526
use Thrift\Factory\TCompactProtocolFactory;
2627
use Thrift\Protocol\TCompactProtocol;
2728
use Thrift\Transport\TTransport;
2829

2930
class TCompactProtocolFactoryTest extends TestCase
3031
{
32+
use ReflectionHelper;
33+
3134
/**
3235
* @return void
3336
*/
@@ -39,10 +42,6 @@ public function testGetProtocol()
3942

4043
$this->assertInstanceOf(TCompactProtocol::class, $protocol);
4144

42-
$ref = new \ReflectionClass($protocol);
43-
$refTrans = $ref->getProperty('trans_');
44-
$refTrans->setAccessible(true);
45-
46-
$this->assertSame($transport, $refTrans->getValue($protocol));
45+
$this->assertSame($transport, $this->getPropertyValue($protocol, 'trans_'));
4746
}
4847
}

lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
namespace Test\Thrift\Unit\Lib\Factory;
2323

2424
use PHPUnit\Framework\TestCase;
25+
use Test\Thrift\Unit\Lib\ReflectionHelper;
2526
use Thrift\Factory\TFramedTransportFactory;
2627
use Thrift\Transport\TFramedTransport;
2728
use Thrift\Transport\TTransport;
2829

2930
class TFramedTransportFactoryTest extends TestCase
3031
{
32+
use ReflectionHelper;
33+
3134
/**
3235
* @return void
3336
*/
@@ -39,16 +42,8 @@ public function testGetTransport()
3942

4043
$this->assertInstanceOf(TFramedTransport::class, $framedTransport);
4144

42-
$ref = new \ReflectionClass($framedTransport);
43-
$refRead = $ref->getProperty('read_');
44-
$refRead->setAccessible(true);
45-
$refWrite = $ref->getProperty('write_');
46-
$refWrite->setAccessible(true);
47-
$refTrans = $ref->getProperty('transport_');
48-
$refTrans->setAccessible(true);
49-
50-
$this->assertTrue($refRead->getValue($framedTransport));
51-
$this->assertTrue($refWrite->getValue($framedTransport));
52-
$this->assertSame($transport, $refTrans->getValue($framedTransport));
45+
$this->assertTrue($this->getPropertyValue($framedTransport, 'read_'));
46+
$this->assertTrue($this->getPropertyValue($framedTransport, 'write_'));
47+
$this->assertSame($transport, $this->getPropertyValue($framedTransport, 'transport_'));
5348
}
5449
}

lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
namespace Test\Thrift\Unit\Lib\Factory;
2323

2424
use PHPUnit\Framework\TestCase;
25+
use Test\Thrift\Unit\Lib\ReflectionHelper;
2526
use Thrift\Factory\TJSONProtocolFactory;
2627
use Thrift\Protocol\TJSONProtocol;
2728
use Thrift\Transport\TTransport;
2829

2930
class TJSONProtocolFactoryTest extends TestCase
3031
{
32+
use ReflectionHelper;
33+
3134
/**
3235
* @return void
3336
*/
@@ -39,10 +42,6 @@ public function testGetProtocol()
3942

4043
$this->assertInstanceOf(TJSONProtocol::class, $protocol);
4144

42-
$ref = new \ReflectionClass($protocol);
43-
$refTrans = $ref->getProperty('trans_');
44-
$refTrans->setAccessible(true);
45-
46-
$this->assertSame($transport, $refTrans->getValue($protocol));
45+
$this->assertSame($transport, $this->getPropertyValue($protocol, 'trans_'));
4746
}
4847
}

lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use phpmock\phpunit\PHPMock;
2525
use PHPUnit\Framework\TestCase;
26+
use Test\Thrift\Unit\Lib\ReflectionHelper;
2627
use Thrift\Factory\TStringFuncFactory;
2728
use Thrift\StringFunc\Core;
2829
use Thrift\StringFunc\Mbstring;
@@ -31,6 +32,7 @@
3132
class TStringFuncFactoryTest extends TestCase
3233
{
3334
use PHPMock;
35+
use ReflectionHelper;
3436

3537
/**
3638
* @dataProvider createDataProvider
@@ -48,10 +50,7 @@ public function testCreate(
4850
/**
4951
* it is a hack to nullable the instance of TStringFuncFactory, and get a new instance based on the new ini_get value
5052
*/
51-
$ref = new \ReflectionClass($factory);
52-
$refInstance = $ref->getProperty('_instance');
53-
$refInstance->setAccessible(true);
54-
$refInstance->setValue($factory, null);
53+
$this->setPropertyValue($factory, '_instance', null);
5554

5655
$stringFunc = $factory::create();
5756

lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
namespace Test\Thrift\Unit\Lib\Protocol;
2424

2525
use PHPUnit\Framework\TestCase;
26+
use Test\Thrift\Unit\Lib\ReflectionHelper;
2627
use Thrift\Exception\TProtocolException;
2728
use Thrift\Protocol\TCompactProtocol;
2829
use Thrift\Transport\TTransport;
2930
use Thrift\Type\TType;
3031

3132
class TCompactProtocolTest extends TestCase
3233
{
34+
use ReflectionHelper;
35+
3336
private const COMPACT_STOP = 0x00;
3437
private const COMPACT_TRUE = 0x01;
3538
private const COMPACT_FALSE = 0x02;
@@ -192,10 +195,7 @@ public function testWriteMessageBegin()
192195
$result = $protocol->writeMessageBegin($name, $type, $seqid);
193196
$this->assertSame(12, $result);
194197

195-
$ref = new \ReflectionClass($protocol);
196-
$state = $ref->getProperty('state');
197-
$state->setAccessible(true);
198-
$this->assertSame(self::STATE_VALUE_WRITE, $state->getValue($protocol));
198+
$this->assertSame(self::STATE_VALUE_WRITE, $this->getPropertyValue($protocol, 'state'));
199199
}
200200

201201
public function testWriteMessageEnd()
@@ -204,10 +204,7 @@ public function testWriteMessageEnd()
204204
$protocol = new TCompactProtocol($transport);
205205

206206
$this->assertSame(0, $protocol->writeMessageEnd());
207-
$ref = new \ReflectionClass($protocol);
208-
$state = $ref->getProperty('state');
209-
$state->setAccessible(true);
210-
$this->assertSame(self::STATE_CLEAR, $state->getValue($protocol));
207+
$this->assertSame(self::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
211208
}
212209

213210
public function testWriteStruct()
@@ -216,33 +213,25 @@ public function testWriteStruct()
216213

217214
$transport = $this->createMock(TTransport::class);
218215
$protocol = new TCompactProtocol($transport);
219-
$ref = new \ReflectionClass($protocol);
220-
$state = $ref->getProperty('state');
221-
$state->setAccessible(true);
222-
$lastFid = $ref->getProperty('lastFid');
223-
$lastFid->setAccessible(true);
224-
$structs = $ref->getProperty('structs');
225-
$structs->setAccessible(true);
226-
227216
$this->assertSame(0, $protocol->writeStructBegin($name));
228-
$this->assertSame([[self::STATE_CLEAR, 0]], $structs->getValue($protocol));
229-
$this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
230-
$this->assertSame(0, $lastFid->getValue($protocol));
217+
$this->assertSame([[self::STATE_CLEAR, 0]], $this->getPropertyValue($protocol, 'structs'));
218+
$this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
219+
$this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
231220

232221
$this->assertSame(0, $protocol->writeStructBegin($name));
233-
$this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
234-
$this->assertSame(0, $lastFid->getValue($protocol));
235-
$this->assertSame([[self::STATE_CLEAR, 0], [self::STATE_FIELD_WRITE, 0]], $structs->getValue($protocol));
222+
$this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
223+
$this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
224+
$this->assertSame([[self::STATE_CLEAR, 0], [self::STATE_FIELD_WRITE, 0]], $this->getPropertyValue($protocol, 'structs'));
236225

237226
$this->assertSame(0, $protocol->writeStructEnd());
238-
$this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
239-
$this->assertSame(0, $lastFid->getValue($protocol));
240-
$this->assertSame([[self::STATE_CLEAR, 0]], $structs->getValue($protocol));
227+
$this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
228+
$this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
229+
$this->assertSame([[self::STATE_CLEAR, 0]], $this->getPropertyValue($protocol, 'structs'));
241230

242231
$this->assertSame(0, $protocol->writeStructEnd());
243-
$this->assertSame(self::STATE_CLEAR, $state->getValue($protocol));
244-
$this->assertSame(0, $lastFid->getValue($protocol));
245-
$this->assertSame([], $structs->getValue($protocol));
232+
$this->assertSame(self::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
233+
$this->assertSame(0, $this->getPropertyValue($protocol, 'lastFid'));
234+
$this->assertSame([], $this->getPropertyValue($protocol, 'structs'));
246235
}
247236

248237
public function testWriteFieldStop()
@@ -331,16 +320,9 @@ public function testWriteFieldBegin(
331320

332321
$this->assertSame($expectedResult, $protocol->writeFieldBegin($fieldName, $fieldType, $fieldId));
333322

334-
$ref = new \ReflectionClass($protocol);
335-
$state = $ref->getProperty('state');
336-
$state->setAccessible(true);
337-
$boolFid = $ref->getProperty('boolFid');
338-
$boolFid->setAccessible(true);
339-
$lastFid = $ref->getProperty('lastFid');
340-
$lastFid->setAccessible(true);
341-
$this->assertSame($expectedState, $state->getValue($protocol));
342-
$this->assertSame($expectedBoolFid, $boolFid->getValue($protocol));
343-
$this->assertSame($expectedLastFid, $lastFid->getValue($protocol));
323+
$this->assertSame($expectedState, $this->getPropertyValue($protocol, 'state'));
324+
$this->assertSame($expectedBoolFid, $this->getPropertyValue($protocol, 'boolFid'));
325+
$this->assertSame($expectedLastFid, $this->getPropertyValue($protocol, 'lastFid'));
344326
}
345327

346328
public function writeFieldBeginDataProvider()
@@ -380,10 +362,7 @@ public function testWriteFieldEnd()
380362

381363
$this->assertSame(0, $protocol->writeFieldEnd());
382364

383-
$ref = new \ReflectionClass($protocol);
384-
$state = $ref->getProperty('state');
385-
$state->setAccessible(true);
386-
$this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
365+
$this->assertSame(self::STATE_FIELD_WRITE, $this->getPropertyValue($protocol, 'state'));
387366
}
388367

389368
/**
@@ -409,16 +388,11 @@ public function testWriteCollection(
409388

410389
$this->assertSame($expectedResult, $protocol->writeCollectionBegin($etype, $size));
411390

412-
$ref = new \ReflectionClass($protocol);
413-
$state = $ref->getProperty('state');
414-
$state->setAccessible(true);
415-
$containers = $ref->getProperty('containers');
416-
$containers->setAccessible(true);
417-
$this->assertSame($expectedState, $state->getValue($protocol));
418-
$this->assertSame($expectedContainers, $containers->getValue($protocol));
391+
$this->assertSame($expectedState, $this->getPropertyValue($protocol, 'state'));
392+
$this->assertSame($expectedContainers, $this->getPropertyValue($protocol, 'containers'));
419393

420394
$this->assertSame(0, $protocol->writeCollectionEnd());
421-
$this->assertSame(TCompactProtocol::STATE_CLEAR, $state->getValue($protocol));
395+
$this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
422396
}
423397

424398
public function writeCollectionDataProvider()
@@ -480,17 +454,12 @@ public function testWriteMap(
480454

481455
$this->assertSame($expectedResult, $protocol->writeMapBegin($keyType, $valType, $size));
482456

483-
$ref = new \ReflectionClass($protocol);
484-
$containers = $ref->getProperty('containers');
485-
$containers->setAccessible(true);
486-
$state = $ref->getProperty('state');
487-
$state->setAccessible(true);
488-
$this->assertSame($expectedContainers, $containers->getValue($protocol));
489-
$this->assertSame(TCompactProtocol::STATE_CLEAR, $state->getValue($protocol));
457+
$this->assertSame($expectedContainers, $this->getPropertyValue($protocol, 'containers'));
458+
$this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
490459

491460
$this->assertSame(0, $protocol->writeMapEnd());
492-
$this->assertSame(TCompactProtocol::STATE_CLEAR, $state->getValue($protocol));
493-
$this->assertSame([], $containers->getValue($protocol));
461+
$this->assertSame(TCompactProtocol::STATE_CLEAR, $this->getPropertyValue($protocol, 'state'));
462+
$this->assertSame([], $this->getPropertyValue($protocol, 'containers'));
494463
}
495464

496465
public function writeMapDataProvider()
@@ -595,10 +564,7 @@ public function testWriteBool(
595564
$transport = $this->createMock(TTransport::class);
596565
$protocol = new TCompactProtocol($transport);
597566
if (!is_null($startState)) {
598-
$ref = new \ReflectionClass($protocol);
599-
$state = $ref->getProperty('state');
600-
$state->setAccessible(true);
601-
$state->setValue($protocol, $startState);
567+
$this->setPropertyValue($protocol, 'state', $startState);
602568
}
603569

604570
$transport

0 commit comments

Comments
 (0)