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
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ jobs:
- name: Build Thrift Classes
run: |
mkdir -p ./lib/php/test/Resources/packages/php
mkdir -p ./lib/php/test/Resources/packages/phpi
mkdir -p ./lib/php/test/Resources/packages/phpv
mkdir -p ./lib/php/test/Resources/packages/phpvo
mkdir -p ./lib/php/test/Resources/packages/phpjs
mkdir -p ./lib/php/test/Resources/packages/phpcm
compiler/cpp/thrift --gen php:nsglobal="Basic" -r --out ./lib/php/test/Resources/packages/php lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:inlined,nsglobal="BasicInline" -r --out ./lib/php/test/Resources/packages/phpi lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:validate,nsglobal="Validate" -r --out ./lib/php/test/Resources/packages/phpv lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:validate,oop,nsglobal="ValidateOop" -r --out ./lib/php/test/Resources/packages/phpvo lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:json,nsglobal="Json" -r --out ./lib/php/test/Resources/packages/phpjs lib/php/test/Resources/ThriftTest.thrift
Expand Down
36 changes: 27 additions & 9 deletions lib/php/lib/Protocol/TProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,41 +266,59 @@ public static function skipBinary($itrans, $type)
{
switch ($type) {
case TType::BOOL:
return $itrans->readAll(1);
$itrans->readAll(1);

return 1;
case TType::BYTE:
return $itrans->readAll(1);
$itrans->readAll(1);

return 1;
case TType::I16:
return $itrans->readAll(2);
$itrans->readAll(2);

return 2;
case TType::I32:
return $itrans->readAll(4);
$itrans->readAll(4);

return 4;
case TType::I64:
return $itrans->readAll(8);
$itrans->readAll(8);

return 8;
case TType::DOUBLE:
return $itrans->readAll(8);
$itrans->readAll(8);

return 8;
case TType::UUID:
return $itrans->readAll(16);
$itrans->readAll(16);

return 16;
case TType::STRING:
$len = unpack('N', $itrans->readAll(4));
$len = $len[1];
if ($len > 0x7fffffff) {
$len = 0 - (($len - 1) ^ 0xffffffff);
}

return 4 + $itrans->readAll($len);
$itrans->readAll($len);

return 4 + $len;

case TType::STRUCT:
$result = 0;
while (true) {
$ftype = 0;
$fid = 0;
$data = $itrans->readAll(1);
$result += 1;
$arr = unpack('c', $data);
$ftype = $arr[1];
if ($ftype == TType::STOP) {
break;
}
// I16 field id
$result += $itrans->readAll(2);
$itrans->readAll(2);
$result += 2;
$result += self::skipBinary($itrans, $ftype);
}

Expand Down
77 changes: 77 additions & 0 deletions lib/php/test/Integration/Lib/Protocol/TProtocolInlinedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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\Integration\Lib\Protocol;

use BasicInline\ThriftTest\Bonk;
use BasicInline\ThriftTest\VersioningTestV1;
use BasicInline\ThriftTest\VersioningTestV2;
use PHPUnit\Framework\TestCase;
use Thrift\Transport\TMemoryBuffer;

/***
* This test suite depends on running the compiler against the ./Resources/ThriftTest.thrift file:
* lib/php/test$ ../../../compiler/cpp/thrift --gen php:inlined,nsglobal="BasicInline" -r --out ./Resources/packages/phpi ./Resources/ThriftTest.thrift
*/
class TProtocolInlinedTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!is_dir(__DIR__ . '/../../../Resources/packages/phpi')) {
self::fail(
'Before running Integration test suite, you must run the Thrift compiler against the ThriftTest.thrift file in the ./Resources directory with the php:inlined generator.'
);
}
}

public function testInlinedReaderSkipsUnknownAndMismatchedFields(): void
{
$writer = new VersioningTestV2([
'begin_in_both' => 1,
'newint' => 2,
'newbyte' => 3,
'newshort' => 4,
'newlong' => 5,
'newdouble' => 6.25,
'newstruct' => new Bonk([
'message' => 'skip me',
'type' => 7,
]),
'newlist' => [8, 9],
'newset' => [10, 11],
'newmap' => [12 => 13],
'newstring' => 'skip me too',
'end_in_both' => 14,
]);

$buffer = '';
$writer->write($buffer);

$transport = new TMemoryBuffer($buffer);
$reader = new VersioningTestV1();
$reader->read($transport);

$this->assertSame(1, $reader->begin_in_both);
$this->assertNull($reader->old_string);
$this->assertSame(14, $reader->end_in_both);
$this->assertSame(0, (int)$transport->available());
}
}
2 changes: 2 additions & 0 deletions lib/php/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ PHPUNIT=php $(top_srcdir)/vendor/bin/phpunit

stubs: Resources/ThriftTest.thrift
mkdir -p ./Resources/packages/php
mkdir -p ./Resources/packages/phpi
mkdir -p ./Resources/packages/phpv
mkdir -p ./Resources/packages/phpvo
mkdir -p ./Resources/packages/phpjs
mkdir -p ./Resources/packages/phpcm
$(THRIFT) --gen php:nsglobal="Basic" -r --out ./Resources/packages/php Resources/ThriftTest.thrift
$(THRIFT) --gen php:inlined,nsglobal="BasicInline" -r --out ./Resources/packages/phpi Resources/ThriftTest.thrift
$(THRIFT) --gen php:validate,nsglobal="Validate" -r --out ./Resources/packages/phpv Resources/ThriftTest.thrift
$(THRIFT) --gen php:validate,oop,nsglobal="ValidateOop" -r --out ./Resources/packages/phpvo Resources/ThriftTest.thrift
$(THRIFT) --gen php:json,nsglobal="Json" -r --out ./Resources/packages/phpjs Resources/ThriftTest.thrift
Expand Down
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);
}
}
Loading
Loading