Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
112 changes: 112 additions & 0 deletions lib/php/test/Unit/Lib/Base/Fixture/ComplexStruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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\Base\Fixture;

use Thrift\Base\TBase;
use Thrift\Type\TType;

class ComplexStruct extends TBase
{
public static $_TSPEC = [
1 => [
'var' => 'flag',
'type' => TType::BOOL,
],
2 => [
'var' => 'name',
'type' => TType::STRING,
],
3 => [
'var' => 'child',
'type' => TType::STRUCT,
'class' => NestedStruct::class,
],
4 => [
'var' => 'mapField',
'type' => TType::MAP,
'ktype' => TType::STRING,
'vtype' => TType::I32,
'key' => [
'type' => TType::STRING,
],
'val' => [
'type' => TType::I32,
],
],
5 => [
'var' => 'listField',
'type' => TType::LST,
'etype' => TType::STRUCT,
'elem' => [
'type' => TType::STRUCT,
'class' => NestedStruct::class,
],
],
6 => [
'var' => 'setField',
'type' => TType::SET,
'etype' => TType::I16,
'elem' => [
'type' => TType::I16,
],
],
7 => [
'var' => 'mapOfLists',
'type' => TType::MAP,
'ktype' => TType::I32,
'vtype' => TType::LST,
'key' => [
'type' => TType::I32,
],
'val' => [
'type' => TType::LST,
'etype' => TType::I32,
'elem' => [
'type' => TType::I32,
],
],
],
8 => [
'var' => 'optionalField',
'type' => TType::STRING,
],
];

public $flag = null;
public $name = null;
public $child = null;
public $mapField = null;
public $listField = null;
public $setField = null;
public $mapOfLists = null;
public $optionalField = null;

public function read($input)
{
return $this->_read(self::class, self::$_TSPEC, $input);
}

public function write($output)
{
return $this->_write('ComplexStruct', self::$_TSPEC, $output);
}
}
47 changes: 47 additions & 0 deletions lib/php/test/Unit/Lib/Base/Fixture/NestedStruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Base\Fixture;

use Thrift\Base\TBase;
use Thrift\Type\TType;

class NestedStruct extends TBase
{
public static $_TSPEC = [
1 => [
'var' => 'value',
'type' => TType::STRING,
],
];

public $value = null;

public function read($input)
{
return $this->_read(self::class, self::$_TSPEC, $input);
}

public function write($output)
{
return $this->_write('NestedStruct', self::$_TSPEC, $output);
}
}
136 changes: 136 additions & 0 deletions lib/php/test/Unit/Lib/Base/TBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?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\Base;

use PHPUnit\Framework\TestCase;
use Test\Thrift\Unit\Lib\Base\Fixture\ComplexStruct;
use Test\Thrift\Unit\Lib\Base\Fixture\NestedStruct;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TMemoryBuffer;
use Thrift\Type\TType;

class TBaseTest extends TestCase
{
public function testConstructorHydratesKnownFieldsFromSpec(): void
{
$struct = new ComplexStruct(
ComplexStruct::$_TSPEC,
[
'flag' => true,
'name' => 'hydrated',
]
);

$this->assertTrue($struct->flag);
$this->assertSame('hydrated', $struct->name);
$this->assertNull($struct->child);
}

public function testWakeupPreservesExistingState(): void
{
$struct = $this->createComplexStruct();

/** @var ComplexStruct $restored */
$restored = unserialize(serialize($struct));

$this->assertEquals($struct, $restored);
}

public function testReadAndWriteRoundTripNestedContainers(): void
{
$restored = $this->roundTrip($this->createComplexStruct());

$this->assertTrue($restored->flag);
$this->assertSame('root', $restored->name);
$this->assertInstanceOf(NestedStruct::class, $restored->child);
$this->assertSame('child', $restored->child->value);
$this->assertSame(['alpha' => 1, 'beta' => 2], $restored->mapField);
$this->assertCount(2, $restored->listField);
$this->assertSame('first', $restored->listField[0]->value);
$this->assertSame('second', $restored->listField[1]->value);
$this->assertSame([10 => true, 20 => true], $restored->setField);
$this->assertSame([1 => [3, 4], 2 => [5]], $restored->mapOfLists);
$this->assertNull($restored->optionalField);
}

public function testReadSkipsUnknownAndUnexpectedFields(): void
{
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocol($transport);

$protocol->writeStructBegin('ComplexStruct');

$protocol->writeFieldBegin('flag', TType::STRING, 1);
$protocol->writeString('ignored');
$protocol->writeFieldEnd();

$protocol->writeFieldBegin('unknown', TType::I32, 99);
$protocol->writeI32(123);
$protocol->writeFieldEnd();

$protocol->writeFieldBegin('name', TType::STRING, 2);
$protocol->writeString('kept');
$protocol->writeFieldEnd();

$protocol->writeFieldStop();
$protocol->writeStructEnd();

$struct = new ComplexStruct();
$struct->read(new TBinaryProtocol($transport));

$this->assertNull($struct->flag);
$this->assertSame('kept', $struct->name);
$this->assertNull($struct->child);
}

private function roundTrip(ComplexStruct $struct): ComplexStruct
{
$transport = new TMemoryBuffer();
$writer = new TBinaryProtocol($transport);
$struct->write($writer);

$copy = new ComplexStruct();
$copy->read(new TBinaryProtocol($transport));

return $copy;
}

private function createComplexStruct(): ComplexStruct
{
$child = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'child']);
$first = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'first']);
$second = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'second']);

return new ComplexStruct(
ComplexStruct::$_TSPEC,
[
'flag' => true,
'name' => 'root',
'child' => $child,
'mapField' => ['alpha' => 1, 'beta' => 2],
'listField' => [$first, $second],
'setField' => [10 => true, 20 => true],
'mapOfLists' => [1 => [3, 4], 2 => [5]],
]
);
}
}
51 changes: 51 additions & 0 deletions lib/php/test/Unit/Lib/Exception/ExceptionTypesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Exception;

use PHPUnit\Framework\TestCase;
use Thrift\Exception\TProtocolException;
use Thrift\Exception\TTransportException;

class ExceptionTypesTest extends TestCase
{
public function testProtocolExceptionPreservesMessageAndCode(): void
{
$exception = new TProtocolException(
'invalid payload',
TProtocolException::BAD_VERSION
);

$this->assertSame('invalid payload', $exception->getMessage());
$this->assertSame(TProtocolException::BAD_VERSION, $exception->getCode());
}

public function testTransportExceptionPreservesMessageAndCode(): void
{
$exception = new TTransportException(
'timed out',
TTransportException::TIMED_OUT
);

$this->assertSame('timed out', $exception->getMessage());
$this->assertSame(TTransportException::TIMED_OUT, $exception->getCode());
}
}
Loading