2323namespace Test \Thrift \Unit \Lib \Protocol ;
2424
2525use PHPUnit \Framework \TestCase ;
26+ use Test \Thrift \Unit \Lib \ReflectionHelper ;
2627use Thrift \Exception \TProtocolException ;
2728use Thrift \Protocol \TCompactProtocol ;
2829use Thrift \Transport \TTransport ;
2930use Thrift \Type \TType ;
3031
3132class 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