Skip to content

Commit 4357726

Browse files
committed
THRIFT-5951: Add PHP runtime coverage tests
Client: php Generated-by: Codex GPT-5.4
1 parent 2176204 commit 4357726

21 files changed

Lines changed: 1605 additions & 0 deletions
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
namespace Test\Thrift\Unit\Lib\Base\Fixture;
23+
24+
use Thrift\Base\TBase;
25+
use Thrift\Type\TType;
26+
27+
class ComplexStruct extends TBase
28+
{
29+
public static $_TSPEC = [
30+
1 => [
31+
'var' => 'flag',
32+
'type' => TType::BOOL,
33+
],
34+
2 => [
35+
'var' => 'name',
36+
'type' => TType::STRING,
37+
],
38+
3 => [
39+
'var' => 'child',
40+
'type' => TType::STRUCT,
41+
'class' => NestedStruct::class,
42+
],
43+
4 => [
44+
'var' => 'mapField',
45+
'type' => TType::MAP,
46+
'ktype' => TType::STRING,
47+
'vtype' => TType::I32,
48+
'key' => [
49+
'type' => TType::STRING,
50+
],
51+
'val' => [
52+
'type' => TType::I32,
53+
],
54+
],
55+
5 => [
56+
'var' => 'listField',
57+
'type' => TType::LST,
58+
'etype' => TType::STRUCT,
59+
'elem' => [
60+
'type' => TType::STRUCT,
61+
'class' => NestedStruct::class,
62+
],
63+
],
64+
6 => [
65+
'var' => 'setField',
66+
'type' => TType::SET,
67+
'etype' => TType::I16,
68+
'elem' => [
69+
'type' => TType::I16,
70+
],
71+
],
72+
7 => [
73+
'var' => 'mapOfLists',
74+
'type' => TType::MAP,
75+
'ktype' => TType::I32,
76+
'vtype' => TType::LST,
77+
'key' => [
78+
'type' => TType::I32,
79+
],
80+
'val' => [
81+
'type' => TType::LST,
82+
'etype' => TType::I32,
83+
'elem' => [
84+
'type' => TType::I32,
85+
],
86+
],
87+
],
88+
8 => [
89+
'var' => 'optionalField',
90+
'type' => TType::STRING,
91+
],
92+
];
93+
94+
public $flag = null;
95+
public $name = null;
96+
public $child = null;
97+
public $mapField = null;
98+
public $listField = null;
99+
public $setField = null;
100+
public $mapOfLists = null;
101+
public $optionalField = null;
102+
103+
public function read($input)
104+
{
105+
return $this->_read(self::class, self::$_TSPEC, $input);
106+
}
107+
108+
public function write($output)
109+
{
110+
return $this->_write('ComplexStruct', self::$_TSPEC, $output);
111+
}
112+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
namespace Test\Thrift\Unit\Lib\Base\Fixture;
23+
24+
use Thrift\Base\TBase;
25+
use Thrift\Type\TType;
26+
27+
class NestedStruct extends TBase
28+
{
29+
public static $_TSPEC = [
30+
1 => [
31+
'var' => 'value',
32+
'type' => TType::STRING,
33+
],
34+
];
35+
36+
public $value = null;
37+
38+
public function read($input)
39+
{
40+
return $this->_read(self::class, self::$_TSPEC, $input);
41+
}
42+
43+
public function write($output)
44+
{
45+
return $this->_write('NestedStruct', self::$_TSPEC, $output);
46+
}
47+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
namespace Test\Thrift\Unit\Lib\Base;
23+
24+
use PHPUnit\Framework\TestCase;
25+
use Test\Thrift\Unit\Lib\Base\Fixture\ComplexStruct;
26+
use Test\Thrift\Unit\Lib\Base\Fixture\NestedStruct;
27+
use Thrift\Protocol\TBinaryProtocol;
28+
use Thrift\Transport\TMemoryBuffer;
29+
use Thrift\Type\TType;
30+
31+
class TBaseTest extends TestCase
32+
{
33+
public function testConstructorHydratesKnownFieldsFromSpec(): void
34+
{
35+
$struct = new ComplexStruct(
36+
ComplexStruct::$_TSPEC,
37+
[
38+
'flag' => true,
39+
'name' => 'hydrated',
40+
]
41+
);
42+
43+
$this->assertTrue($struct->flag);
44+
$this->assertSame('hydrated', $struct->name);
45+
$this->assertNull($struct->child);
46+
}
47+
48+
public function testWakeupPreservesExistingState(): void
49+
{
50+
$struct = $this->createComplexStruct();
51+
52+
/** @var ComplexStruct $restored */
53+
$restored = unserialize(serialize($struct));
54+
55+
$this->assertEquals($struct, $restored);
56+
}
57+
58+
public function testReadAndWriteRoundTripNestedContainers(): void
59+
{
60+
$restored = $this->roundTrip($this->createComplexStruct());
61+
62+
$this->assertTrue($restored->flag);
63+
$this->assertSame('root', $restored->name);
64+
$this->assertInstanceOf(NestedStruct::class, $restored->child);
65+
$this->assertSame('child', $restored->child->value);
66+
$this->assertSame(['alpha' => 1, 'beta' => 2], $restored->mapField);
67+
$this->assertCount(2, $restored->listField);
68+
$this->assertSame('first', $restored->listField[0]->value);
69+
$this->assertSame('second', $restored->listField[1]->value);
70+
$this->assertSame([10 => true, 20 => true], $restored->setField);
71+
$this->assertSame([1 => [3, 4], 2 => [5]], $restored->mapOfLists);
72+
$this->assertNull($restored->optionalField);
73+
}
74+
75+
public function testReadSkipsUnknownAndUnexpectedFields(): void
76+
{
77+
$transport = new TMemoryBuffer();
78+
$protocol = new TBinaryProtocol($transport);
79+
80+
$protocol->writeStructBegin('ComplexStruct');
81+
82+
$protocol->writeFieldBegin('flag', TType::STRING, 1);
83+
$protocol->writeString('ignored');
84+
$protocol->writeFieldEnd();
85+
86+
$protocol->writeFieldBegin('unknown', TType::I32, 99);
87+
$protocol->writeI32(123);
88+
$protocol->writeFieldEnd();
89+
90+
$protocol->writeFieldBegin('name', TType::STRING, 2);
91+
$protocol->writeString('kept');
92+
$protocol->writeFieldEnd();
93+
94+
$protocol->writeFieldStop();
95+
$protocol->writeStructEnd();
96+
97+
$struct = new ComplexStruct();
98+
$struct->read(new TBinaryProtocol($transport));
99+
100+
$this->assertNull($struct->flag);
101+
$this->assertSame('kept', $struct->name);
102+
$this->assertNull($struct->child);
103+
}
104+
105+
private function roundTrip(ComplexStruct $struct): ComplexStruct
106+
{
107+
$transport = new TMemoryBuffer();
108+
$writer = new TBinaryProtocol($transport);
109+
$struct->write($writer);
110+
111+
$copy = new ComplexStruct();
112+
$copy->read(new TBinaryProtocol($transport));
113+
114+
return $copy;
115+
}
116+
117+
private function createComplexStruct(): ComplexStruct
118+
{
119+
$child = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'child']);
120+
$first = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'first']);
121+
$second = new NestedStruct(NestedStruct::$_TSPEC, ['value' => 'second']);
122+
123+
return new ComplexStruct(
124+
ComplexStruct::$_TSPEC,
125+
[
126+
'flag' => true,
127+
'name' => 'root',
128+
'child' => $child,
129+
'mapField' => ['alpha' => 1, 'beta' => 2],
130+
'listField' => [$first, $second],
131+
'setField' => [10 => true, 20 => true],
132+
'mapOfLists' => [1 => [3, 4], 2 => [5]],
133+
]
134+
);
135+
}
136+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
namespace Test\Thrift\Unit\Lib\Exception;
23+
24+
use PHPUnit\Framework\TestCase;
25+
use Thrift\Exception\TProtocolException;
26+
use Thrift\Exception\TTransportException;
27+
28+
class ExceptionTypesTest extends TestCase
29+
{
30+
public function testProtocolExceptionPreservesMessageAndCode(): void
31+
{
32+
$exception = new TProtocolException(
33+
'invalid payload',
34+
TProtocolException::BAD_VERSION
35+
);
36+
37+
$this->assertSame('invalid payload', $exception->getMessage());
38+
$this->assertSame(TProtocolException::BAD_VERSION, $exception->getCode());
39+
}
40+
41+
public function testTransportExceptionPreservesMessageAndCode(): void
42+
{
43+
$exception = new TTransportException(
44+
'timed out',
45+
TTransportException::TIMED_OUT
46+
);
47+
48+
$this->assertSame('timed out', $exception->getMessage());
49+
$this->assertSame(TTransportException::TIMED_OUT, $exception->getCode());
50+
}
51+
}

0 commit comments

Comments
 (0)