Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,17 +47,9 @@ public function testGetProtocol(

$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()
Expand Down
9 changes: 4 additions & 5 deletions lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -39,10 +42,6 @@ public function testGetProtocol()

$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_'));
}
}
17 changes: 6 additions & 11 deletions lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -39,16 +42,8 @@ public function testGetTransport()

$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_'));
}
}
9 changes: 4 additions & 5 deletions lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -39,10 +42,6 @@ public function testGetProtocol()

$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_'));
}
}
7 changes: 3 additions & 4 deletions lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@
class TStringFuncFactoryTest extends TestCase
{
use PHPMock;
use ReflectionHelper;

/**
* @dataProvider createDataProvider
Expand All @@ -48,10 +50,7 @@ public function testCreate(
/**
* 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();

Expand Down
92 changes: 29 additions & 63 deletions lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
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;
use Thrift\Type\TType;

class TCompactProtocolTest extends TestCase
{
use ReflectionHelper;

private const COMPACT_STOP = 0x00;
private const COMPACT_TRUE = 0x01;
private const COMPACT_FALSE = 0x02;
Expand Down Expand Up @@ -192,10 +195,7 @@ public function testWriteMessageBegin()
$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()
Expand All @@ -204,10 +204,7 @@ public function testWriteMessageEnd()
$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()
Expand All @@ -216,33 +213,25 @@ public function testWriteStruct()

$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]], $structs->getValue($protocol));
$this->assertSame(self::STATE_FIELD_WRITE, $state->getValue($protocol));
$this->assertSame(0, $lastFid->getValue($protocol));
$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_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()
Expand Down Expand Up @@ -331,16 +320,9 @@ public function testWriteFieldBegin(

$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()
Expand Down Expand Up @@ -380,10 +362,7 @@ public function testWriteFieldEnd()

$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'));
}

/**
Expand All @@ -409,16 +388,11 @@ public function testWriteCollection(

$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()
Expand Down Expand Up @@ -480,17 +454,12 @@ public function testWriteMap(

$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()
Expand Down Expand Up @@ -595,10 +564,7 @@ public function testWriteBool(
$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
Expand Down
Loading