Upgrading dependency to Thrift 0.12.0

This commit is contained in:
Renan DelValle 2018-11-27 18:03:50 -08:00
parent 3e4590dcc0
commit 356978cb42
No known key found for this signature in database
GPG key ID: C240AD6D6F443EC9
1302 changed files with 101701 additions and 26784 deletions

View file

@ -0,0 +1,194 @@
<?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.
*
* @package thrift.test
*/
namespace Test\Thrift;
use ThriftTest\Xtruct;
use ThriftTest\Xtruct2;
use ThriftTest\Numberz;
use ThriftTest\Insanity;
class Fixtures
{
public static $bufsize = 8192; //big enough to read biggest serialized Fixture arg.
public static $testArgs = array();
public static function populateTestArgs()
{
self::$testArgs['testString1'] = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, Bahasa Melayu, مازِرونی, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, Occitan, Иронау, Papiamentu, Deitsch, Norfuk / Pitkern, Polski, پنجابی, پښتو, Português, Runa Simi, Rumantsch, Romani, Română, Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語";
self::$testArgs['testString2'] =
"quote: \\\" backslash:" .
" forwardslash-escaped: \\/ " .
" backspace: \b formfeed: \f newline: \n return: \r tab: " .
" now-all-of-them-together: \"\\\/\b\n\r\t" .
" now-a-bunch-of-junk: !@#\$%&()(&%$#{}{}<><><";
self::$testArgs['testString3'] =
"string that ends in double-backslash \\\\";
self::$testArgs['testUnicodeStringWithNonBMP'] =
"สวัสดี/𝒯";
self::$testArgs['testDouble'] = 3.1415926535898;
// TODO: add testBinary() call
self::$testArgs['testByte'] = 0x01;
self::$testArgs['testI32'] = pow(2, 30);
if (PHP_INT_SIZE == 8) {
self::$testArgs['testI64'] = pow(2, 60);
} else {
self::$testArgs['testI64'] = "1152921504606847000";
}
self::$testArgs['testStruct'] =
new Xtruct(
array(
'string_thing' => 'worked',
'byte_thing' => 0x01,
'i32_thing' => pow(2, 30),
'i64_thing' => self::$testArgs['testI64']
)
);
self::$testArgs['testNestNested'] =
new Xtruct(
array(
'string_thing' => 'worked',
'byte_thing' => 0x01,
'i32_thing' => pow(2, 30),
'i64_thing' => self::$testArgs['testI64']
)
);
self::$testArgs['testNest'] =
new Xtruct2(
array(
'byte_thing' => 0x01,
'struct_thing' => self::$testArgs['testNestNested'],
'i32_thing' => pow(2, 15)
)
);
self::$testArgs['testMap'] =
array(
7 => 77,
8 => 88,
9 => 99
);
self::$testArgs['testStringMap'] =
array(
"a" => "123",
"a b" => "with spaces ",
"same" => "same",
"0" => "numeric key",
"longValue" => self::$testArgs['testString1'],
self::$testArgs['testString1'] => "long key"
);
self::$testArgs['testSet'] = array(1 => true, 5 => true, 6 => true);
self::$testArgs['testList'] = array(1, 2, 3);
self::$testArgs['testEnum'] = Numberz::ONE;
self::$testArgs['testTypedef'] = 69;
self::$testArgs['testMapMapExpectedResult'] =
array(
4 => array(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
),
-4 => array(
-4 => -4,
-3 => -3,
-2 => -2,
-1 => -1
)
);
// testInsanity ... takes a few steps to set up!
$xtruct1 =
new Xtruct(
array(
'string_thing' => 'Goodbye4',
'byte_thing' => 4,
'i32_thing' => 4,
'i64_thing' => 4
)
);
$xtruct2 =
new Xtruct(
array(
'string_thing' => 'Hello2',
'byte_thing' => 2,
'i32_thing' => 2,
'i64_thing' => 2
)
);
$userMap =
array(
Numberz::FIVE => 5,
Numberz::EIGHT => 8
);
$insanity2 =
new Insanity(
array(
'userMap' => $userMap,
'xtructs' => array($xtruct1, $xtruct2)
)
);
$insanity3 = $insanity2;
$insanity6 =
new Insanity(
array(
'userMap' => null,
'xtructs' => null
)
);
self::$testArgs['testInsanityExpectedResult'] =
array(
"1" => array(
Numberz::TWO => $insanity2,
Numberz::THREE => $insanity3
),
"2" => array(
Numberz::SIX => $insanity6
)
);
}
}

View file

@ -0,0 +1,116 @@
<?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\JsonSerialize;
use PHPUnit\Framework\TestCase;
use stdClass;
require __DIR__ . '/../../../../vendor/autoload.php';
/**
* @runTestsInSeparateProcesses
*/
class JsonSerializeTest extends TestCase
{
protected function setUp()
{
if (version_compare(phpversion(), '5.4', '<')) {
$this->markTestSkipped('Requires PHP 5.4 or newer!');
}
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('', __DIR__ . '/../packages/phpjs');
}
public function testEmptyStruct()
{
$empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
$this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
}
public function testStringsAndInts()
{
$input = array(
'string_thing' => 'foo',
'i64_thing' => 1234567890,
);
$xtruct = new \ThriftTest\Xtruct($input);
// Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
$expected = new stdClass();
$expected->string_thing = $input['string_thing'];
$expected->i64_thing = $input['i64_thing'];
$this->assertEquals($expected, json_decode(json_encode($xtruct)));
}
public function testNestedStructs()
{
$xtruct2 = new \ThriftTest\Xtruct2(array(
'byte_thing' => 42,
'struct_thing' => new \ThriftTest\Xtruct(array(
'i32_thing' => 123456,
)),
));
$expected = new stdClass();
$expected->byte_thing = $xtruct2->byte_thing;
$expected->struct_thing = new stdClass();
$expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
$this->assertEquals($expected, json_decode(json_encode($xtruct2)));
}
public function testInsanity()
{
$xinput = array('string_thing' => 'foo');
$xtruct = new \ThriftTest\Xtruct($xinput);
$insanity = new \ThriftTest\Insanity(array(
'xtructs' => array($xtruct, $xtruct, $xtruct)
));
$expected = new stdClass();
$expected->xtructs = array((object)$xinput, (object)$xinput, (object)$xinput);
$this->assertEquals($expected, json_decode(json_encode($insanity)));
}
public function testNestedLists()
{
$bonk = new \ThriftTest\Bonk(array('message' => 'foo'));
$nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
$expected = new stdClass();
$expected->bonk = array(array(array((object)array('message' => 'foo'))));
$this->assertEquals($expected, json_decode(json_encode($nested)));
}
public function testMaps()
{
$intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
$emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
$this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
$this->assertEquals('{}', json_encode($emptymap));
}
public function testScalarTypes()
{
$b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
$this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
$s = new \ThriftTest\StructA(['s' => 42]);
$this->assertEquals('{"s":"42"}', json_encode($s));
}
}

View file

@ -17,11 +17,11 @@
# under the License.
#
THRIFT = $(top_builddir)/compiler/cpp/thrift
PHPUNIT=php $(top_srcdir)/vendor/bin/phpunit
stubs: ../../../test/ThriftTest.thrift TestValidators.thrift
mkdir -p ./packages
$(THRIFT) --gen php -r --out ./packages ../../../test/ThriftTest.thrift
mkdir -p ./packages/php
$(THRIFT) --gen php -r --out ./packages/php ../../../test/ThriftTest.thrift
mkdir -p ./packages/phpv
mkdir -p ./packages/phpvo
mkdir -p ./packages/phpjs
@ -29,23 +29,21 @@ stubs: ../../../test/ThriftTest.thrift TestValidators.thrift
$(THRIFT) --gen php:validate,oop -r --out ./packages/phpvo TestValidators.thrift
$(THRIFT) --gen php:json -r --out ./packages/phpjs TestValidators.thrift
check-json-serializer: stubs
if HAVE_PHPUNIT
$(PHPUNIT) --log-junit=TEST-json-serializer.xml Test/Thrift/JsonSerialize/
endif
deps: $(top_srcdir)/composer.json
composer install --working-dir=$(top_srcdir)
check-validator: stubs
php Test/Thrift/TestValidators.php
php Test/Thrift/TestValidators.php -oop
all-local: deps
check-protocol: stubs
if HAVE_PHPUNIT
$(PHPUNIT) --log-junit=TEST-log-json-protocol.xml Test/Thrift/Protocol/TestTJSONProtocol.php
$(PHPUNIT) --log-junit=TEST-binary-serializer.xml Test/Thrift/Protocol/TestBinarySerializer.php
$(PHPUNIT) --log-junit=TEST-log-simple-json-protocol.xml Test/Thrift/Protocol/TestTSimpleJSONProtocol.php
endif
check-json-serializer: deps stubs
$(PHPUNIT) --log-junit=TEST-log-json-serializer.xml JsonSerialize/
check: stubs \
check-validator: deps stubs
$(PHPUNIT) --log-junit=TEST-log-validator.xml Validator/
check-protocol: deps stubs
$(PHPUNIT) --log-junit=TEST-log-protocol.xml Protocol/
check: deps stubs \
check-protocol \
check-validator \
check-json-serializer
@ -53,9 +51,3 @@ check: stubs \
clean-local:
$(RM) -r ./packages
$(RM) TEST-*.xml
EXTRA_DIST = \
Test \
TestValidators.thrift

View file

@ -23,16 +23,10 @@
namespace Test\Thrift\Protocol;
use Thrift\ClassLoader\ThriftClassLoader;
use PHPUnit\Framework\TestCase;
use Thrift\Serializer\TBinarySerializer;
require_once __DIR__.'/../../../../lib/Thrift/ClassLoader/ThriftClassLoader.php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../../../../lib');
$loader->registerNamespace('Test', __DIR__ . '/../../..');
$loader->registerDefinition('ThriftTest', __DIR__ . '/../../../packages');
$loader->register();
require __DIR__ . '/../../../../vendor/autoload.php';
/***
* This test suite depends on running the compiler against the
@ -40,25 +34,27 @@ $loader->register();
*
* lib/php/test$ ../../../compiler/cpp/thrift --gen php -r \
* --out ./packages ../../../test/ThriftTest.thrift
*
* @runTestsInSeparateProcesses
*/
class TestBinarySerializer extends \PHPUnit_Framework_TestCase
class BinarySerializerTest extends TestCase
{
public function setUp()
{
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('', __DIR__ . '/../packages/php');
}
public function setUp()
{
}
/**
* We try to serialize and deserialize a random object to make sure no exceptions are thrown.
* @see THRIFT-1579
*/
public function testBinarySerializer()
{
$struct = new \ThriftTest\Xtruct(array('string_thing' => 'abc'));
$serialized = TBinarySerializer::serialize($struct, 'ThriftTest\\Xtruct');
$deserialized = TBinarySerializer::deserialize($serialized, 'ThriftTest\\Xtruct');
$this->assertEquals($struct, $deserialized);
}
/**
* We try to serialize and deserialize a random object to make sure no exceptions are thrown.
* @see THRIFT-1579
*/
public function testBinarySerializer()
{
$struct = new \ThriftTest\Xtruct(array('string_thing' => 'abc'));
$serialized = TBinarySerializer::serialize($struct, 'ThriftTest\\Xtruct');
$deserialized = TBinarySerializer::deserialize($serialized, 'ThriftTest\\Xtruct');
$this->assertEquals($struct, $deserialized);
}
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,518 @@
<?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.
*
* @package thrift.test
*/
namespace Test\Thrift\Protocol;
use PHPUnit\Framework\TestCase;
use Test\Thrift\Fixtures;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TMemoryBuffer;
require __DIR__ . '/../../../../vendor/autoload.php';
/***
* This test suite depends on running the compiler against the
* standard ThriftTest.thrift file:
*
* lib/php/test$ ../../../compiler/cpp/thrift --gen php -r \
* --out ./packages ../../../test/ThriftTest.thrift
*
* @runTestsInSeparateProcesses
*/
class TJSONProtocolTest extends TestCase
{
private $transport;
private $protocol;
public static function setUpBeforeClass()
{
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('', __DIR__ . '/../packages/php');
Fixtures::populateTestArgs();
TJSONProtocolFixtures::populateTestArgsJSON();
}
public function setUp()
{
$this->transport = new TMemoryBuffer();
$this->protocol = new TJSONProtocol($this->transport);
$this->transport->open();
}
/**
* WRITE TESTS
*/
public function testVoidWrite()
{
$args = new \ThriftTest\ThriftTest_testVoid_args();
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testVoid'];
$this->assertEquals($expected, $actual);
}
public function testString1Write()
{
$args = new \ThriftTest\ThriftTest_testString_args();
$args->thing = Fixtures::$testArgs['testString1'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testString1'];
$this->assertEquals($expected, $actual);
}
public function testString2Write()
{
$args = new \ThriftTest\ThriftTest_testString_args();
$args->thing = Fixtures::$testArgs['testString2'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testString2'];
$this->assertEquals($expected, $actual);
}
public function testDoubleWrite()
{
$args = new \ThriftTest\ThriftTest_testDouble_args();
$args->thing = Fixtures::$testArgs['testDouble'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testDouble'];
$this->assertEquals($expected, $actual);
}
public function testByteWrite()
{
$args = new \ThriftTest\ThriftTest_testByte_args();
$args->thing = Fixtures::$testArgs['testByte'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testByte'];
$this->assertEquals($expected, $actual);
}
public function testI32Write()
{
$args = new \ThriftTest\ThriftTest_testI32_args();
$args->thing = Fixtures::$testArgs['testI32'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testI32'];
$this->assertEquals($expected, $actual);
}
public function testI64Write()
{
$args = new \ThriftTest\ThriftTest_testI64_args();
$args->thing = Fixtures::$testArgs['testI64'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testI64'];
$this->assertEquals($expected, $actual);
}
public function testStructWrite()
{
$args = new \ThriftTest\ThriftTest_testStruct_args();
$args->thing = Fixtures::$testArgs['testStruct'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testStruct'];
$this->assertEquals($expected, $actual);
}
public function testNestWrite()
{
$args = new \ThriftTest\ThriftTest_testNest_args();
$args->thing = Fixtures::$testArgs['testNest'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testNest'];
$this->assertEquals($expected, $actual);
}
public function testMapWrite()
{
$args = new \ThriftTest\ThriftTest_testMap_args();
$args->thing = Fixtures::$testArgs['testMap'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testMap'];
$this->assertEquals($expected, $actual);
}
public function testStringMapWrite()
{
$args = new \ThriftTest\ThriftTest_testStringMap_args();
$args->thing = Fixtures::$testArgs['testStringMap'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testStringMap'];
/*
* The $actual returns unescaped string.
* It is required to to decode then encode it again
* to get the expected escaped unicode.
*/
$this->assertEquals($expected, json_encode(json_decode($actual)));
}
public function testSetWrite()
{
$args = new \ThriftTest\ThriftTest_testSet_args();
$args->thing = Fixtures::$testArgs['testSet'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testSet'];
$this->assertEquals($expected, $actual);
}
public function testListWrite()
{
$args = new \ThriftTest\ThriftTest_testList_args();
$args->thing = Fixtures::$testArgs['testList'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testList'];
$this->assertEquals($expected, $actual);
}
public function testEnumWrite()
{
$args = new \ThriftTest\ThriftTest_testEnum_args();
$args->thing = Fixtures::$testArgs['testEnum'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testEnum'];
$this->assertEquals($expected, $actual);
}
public function testTypedefWrite()
{
$args = new \ThriftTest\ThriftTest_testTypedef_args();
$args->thing = Fixtures::$testArgs['testTypedef'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testTypedef'];
$this->assertEquals($expected, $actual);
}
/**
* READ TESTS
*/
public function testVoidRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testVoid']
);
$args = new \ThriftTest\ThriftTest_testVoid_args();
$args->read($this->protocol);
}
public function testString1Read()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testString1']
);
$args = new \ThriftTest\ThriftTest_testString_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testString1'];
$this->assertEquals($expected, $actual);
}
public function testString2Read()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testString2']
);
$args = new \ThriftTest\ThriftTest_testString_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testString2'];
$this->assertEquals($expected, $actual);
}
public function testString3Write()
{
$args = new \ThriftTest\ThriftTest_testString_args();
$args->thing = Fixtures::$testArgs['testString3'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testString3'];
$this->assertEquals($expected, $actual);
}
public function testString4Write()
{
$args = new \ThriftTest\ThriftTest_testString_args();
$args->thing = Fixtures::$testArgs['testUnicodeStringWithNonBMP'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TJSONProtocolFixtures::$testArgsJSON['testUnicodeStringWithNonBMP'];
$this->assertEquals($expected, $actual);
}
public function testDoubleRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testDouble']
);
$args = new \ThriftTest\ThriftTest_testDouble_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testDouble'];
$this->assertEquals($expected, $actual);
}
public function testByteRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testByte']
);
$args = new \ThriftTest\ThriftTest_testByte_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testByte'];
$this->assertEquals($expected, $actual);
}
public function testI32Read()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testI32']
);
$args = new \ThriftTest\ThriftTest_testI32_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testI32'];
$this->assertEquals($expected, $actual);
}
public function testI64Read()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testI64']
);
$args = new \ThriftTest\ThriftTest_testI64_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testI64'];
$this->assertEquals($expected, $actual);
}
public function testStructRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testStruct']
);
$args = new \ThriftTest\ThriftTest_testStruct_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testStruct'];
$this->assertEquals($expected, $actual);
}
public function testNestRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testNest']
);
$args = new \ThriftTest\ThriftTest_testNest_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testNest'];
$this->assertEquals($expected, $actual);
}
public function testMapRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testMap']
);
$args = new \ThriftTest\ThriftTest_testMap_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testMap'];
$this->assertEquals($expected, $actual);
}
public function testStringMapRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testStringMap']
);
$args = new \ThriftTest\ThriftTest_testStringMap_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testStringMap'];
$this->assertEquals($expected, $actual);
}
public function testSetRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testSet']
);
$args = new \ThriftTest\ThriftTest_testSet_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testSet'];
$this->assertEquals($expected, $actual);
}
public function testListRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testList']
);
$args = new \ThriftTest\ThriftTest_testList_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testList'];
$this->assertEquals($expected, $actual);
}
public function testEnumRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testEnum']
);
$args = new \ThriftTest\ThriftTest_testEnum_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testEnum'];
$this->assertEquals($expected, $actual);
}
public function testTypedefRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testTypedef']
);
$args = new \ThriftTest\ThriftTest_testTypedef_args();
$args->read($this->protocol);
$actual = $args->thing;
$expected = Fixtures::$testArgs['testTypedef'];
$this->assertEquals($expected, $actual);
}
public function testMapMapRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testMapMap']
);
$result = new \ThriftTest\ThriftTest_testMapMap_result();
$result->read($this->protocol);
$actual = $result->success;
$expected = Fixtures::$testArgs['testMapMapExpectedResult'];
$this->assertEquals($expected, $actual);
}
public function testInsanityRead()
{
$this->transport->write(
TJSONProtocolFixtures::$testArgsJSON['testInsanity']
);
$result = new \ThriftTest\ThriftTest_testInsanity_result();
$result->read($this->protocol);
$actual = $result->success;
$expected = Fixtures::$testArgs['testInsanityExpectedResult'];
$this->assertEquals($expected, $actual);
}
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,254 @@
<?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.
*
* @package thrift.test
*/
namespace Test\Thrift\Protocol;
use PHPUnit\Framework\TestCase;
use Test\Thrift\Fixtures;
use Thrift\Protocol\TSimpleJSONProtocol;
use Thrift\Transport\TMemoryBuffer;
require __DIR__ . '/../../../../vendor/autoload.php';
/***
* This test suite depends on running the compiler against the
* standard ThriftTest.thrift file:
*
* lib/php/test$ ../../../compiler/cpp/thrift --gen php -r \
* --out ./packages ../../../test/ThriftTest.thrift
*
* @runTestsInSeparateProcesses
*/
class TSimpleJSONProtocolTest extends TestCase
{
private $transport;
private $protocol;
public static function setUpBeforeClass()
{
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('', __DIR__ . '/../packages/php');
Fixtures::populateTestArgs();
TSimpleJSONProtocolFixtures::populateTestArgsSimpleJSON();
}
public function setUp()
{
$this->transport = new TMemoryBuffer();
$this->protocol = new TSimpleJSONProtocol($this->transport);
$this->transport->open();
}
/**
* WRITE TESTS
*/
public function testVoidWrite()
{
$args = new \ThriftTest\ThriftTest_testVoid_args();
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testVoid'];
$this->assertEquals($expected, $actual);
}
public function testString1Write()
{
$args = new \ThriftTest\ThriftTest_testString_args();
$args->thing = Fixtures::$testArgs['testString1'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testString1'];
$this->assertEquals($expected, $actual);
}
public function testString2Write()
{
$args = new \ThriftTest\ThriftTest_testString_args();
$args->thing = Fixtures::$testArgs['testString2'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testString2'];
$this->assertEquals($expected, $actual);
}
public function testDoubleWrite()
{
$args = new \ThriftTest\ThriftTest_testDouble_args();
$args->thing = Fixtures::$testArgs['testDouble'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testDouble'];
$this->assertEquals($expected, $actual);
}
public function testByteWrite()
{
$args = new \ThriftTest\ThriftTest_testByte_args();
$args->thing = Fixtures::$testArgs['testByte'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testByte'];
$this->assertEquals($expected, $actual);
}
public function testI32Write()
{
$args = new \ThriftTest\ThriftTest_testI32_args();
$args->thing = Fixtures::$testArgs['testI32'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testI32'];
$this->assertEquals($expected, $actual);
}
public function testI64Write()
{
$args = new \ThriftTest\ThriftTest_testI64_args();
$args->thing = Fixtures::$testArgs['testI64'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testI64'];
$this->assertEquals($expected, $actual);
}
public function testStructWrite()
{
$args = new \ThriftTest\ThriftTest_testStruct_args();
$args->thing = Fixtures::$testArgs['testStruct'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testStruct'];
$this->assertEquals($expected, $actual);
}
public function testNestWrite()
{
$args = new \ThriftTest\ThriftTest_testNest_args();
$args->thing = Fixtures::$testArgs['testNest'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testNest'];
$this->assertEquals($expected, $actual);
}
public function testMapWrite()
{
$args = new \ThriftTest\ThriftTest_testMap_args();
$args->thing = Fixtures::$testArgs['testMap'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testMap'];
$this->assertEquals($expected, $actual);
}
public function testStringMapWrite()
{
$args = new \ThriftTest\ThriftTest_testStringMap_args();
$args->thing = Fixtures::$testArgs['testStringMap'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testStringMap'];
$this->assertEquals($expected, $actual);
}
public function testSetWrite()
{
$args = new \ThriftTest\ThriftTest_testSet_args();
$args->thing = Fixtures::$testArgs['testSet'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testSet'];
$this->assertEquals($expected, $actual);
}
public function testListWrite()
{
$args = new \ThriftTest\ThriftTest_testList_args();
$args->thing = Fixtures::$testArgs['testList'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testList'];
$this->assertEquals($expected, $actual);
}
public function testEnumWrite()
{
$args = new \ThriftTest\ThriftTest_testEnum_args();
$args->thing = Fixtures::$testArgs['testEnum'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testEnum'];
$this->assertEquals($expected, $actual);
}
public function testTypedefWrite()
{
$args = new \ThriftTest\ThriftTest_testTypedef_args();
$args->thing = Fixtures::$testArgs['testTypedef'];
$args->write($this->protocol);
$actual = $this->transport->read(Fixtures::$bufsize);
$expected = TSimpleJSONProtocolFixtures::$testArgsJSON['testTypedef'];
$this->assertEquals($expected, $actual);
}
}

View file

@ -1,194 +0,0 @@
<?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.
*
* @package thrift.test
*/
namespace Test\Thrift;
use ThriftTest\Xtruct;
use ThriftTest\Xtruct2;
use ThriftTest\Numberz;
use ThriftTest\Insanity;
class Fixtures
{
public static $testArgs = array();
public static function populateTestArgs()
{
self::$testArgs['testString1'] = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, Bahasa Melayu, مازِرونی, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, Occitan, Иронау, Papiamentu, Deitsch, Norfuk / Pitkern, Polski, پنجابی, پښتو, Português, Runa Simi, Rumantsch, Romani, Română, Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語";
self::$testArgs['testString2'] =
"quote: \\\" backslash:" .
" forwardslash-escaped: \\/ " .
" backspace: \b formfeed: \f newline: \n return: \r tab: " .
" now-all-of-them-together: \"\\\/\b\n\r\t" .
" now-a-bunch-of-junk: !@#\$%&()(&%$#{}{}<><><";
self::$testArgs['testString3'] =
"string that ends in double-backslash \\\\";
self::$testArgs['testUnicodeStringWithNonBMP'] =
"สวัสดี/𝒯";
self::$testArgs['testDouble'] = 3.1415926535898;
// TODO: add testBinary() call
self::$testArgs['testByte'] = 0x01;
self::$testArgs['testI32'] = pow( 2, 30 );
if (PHP_INT_SIZE == 8) {
self::$testArgs['testI64'] = pow( 2, 60 );
} else {
self::$testArgs['testI64'] = "1152921504606847000";
}
self::$testArgs['testStruct'] =
new Xtruct(
array(
'string_thing' => 'worked',
'byte_thing' => 0x01,
'i32_thing' => pow( 2, 30 ),
'i64_thing' => self::$testArgs['testI64']
)
);
self::$testArgs['testNestNested'] =
new Xtruct(
array(
'string_thing' => 'worked',
'byte_thing' => 0x01,
'i32_thing' => pow( 2, 30 ),
'i64_thing' => self::$testArgs['testI64']
)
);
self::$testArgs['testNest'] =
new Xtruct2(
array(
'byte_thing' => 0x01,
'struct_thing' => self::$testArgs['testNestNested'],
'i32_thing' => pow( 2, 15 )
)
);
self::$testArgs['testMap'] =
array(
7 => 77,
8 => 88,
9 => 99
);
self::$testArgs['testStringMap'] =
array(
"a" => "123",
"a b" => "with spaces ",
"same" => "same",
"0" => "numeric key",
"longValue" => self::$testArgs['testString1'],
self::$testArgs['testString1'] => "long key"
);
self::$testArgs['testSet'] = array( 1 => true, 5 => true, 6 => true );
self::$testArgs['testList'] = array( 1, 2, 3 );
self::$testArgs['testEnum'] = Numberz::ONE;
self::$testArgs['testTypedef'] = 69;
self::$testArgs['testMapMapExpectedResult'] =
array(
4 => array(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
),
-4 => array(
-4 => -4,
-3 => -3,
-2 => -2,
-1 => -1
)
);
// testInsanity ... takes a few steps to set up!
$xtruct1 =
new Xtruct(
array(
'string_thing' => 'Goodbye4',
'byte_thing' => 4,
'i32_thing' => 4,
'i64_thing' => 4
)
);
$xtruct2 =
new Xtruct(
array(
'string_thing' => 'Hello2',
'byte_thing' =>2,
'i32_thing' => 2,
'i64_thing' => 2
)
);
$userMap =
array(
Numberz::FIVE => 5,
Numberz::EIGHT => 8
);
$insanity2 =
new Insanity(
array(
'userMap' => $userMap,
'xtructs' => array($xtruct1,$xtruct2)
)
);
$insanity3 = $insanity2;
$insanity6 =
new Insanity(
array(
'userMap' => null,
'xtructs' => null
)
);
self::$testArgs['testInsanityExpectedResult'] =
array(
"1" => array(
Numberz::TWO => $insanity2,
Numberz::THREE => $insanity3
),
"2" => array(
Numberz::SIX => $insanity6
)
);
}
}

View file

@ -1,116 +0,0 @@
<?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\JsonSerialize;
use stdClass;
use Thrift\ClassLoader\ThriftClassLoader;
require_once __DIR__.'/../../../../lib/Thrift/ClassLoader/ThriftClassLoader.php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../../../../lib');
$loader->registerNamespace('Test', __DIR__ . '/../../..');
$loader->registerDefinition('ThriftTest', __DIR__ . '/../../../packages/phpjs');
$loader->register();
class JsonSerializeTest extends \PHPUnit_Framework_TestCase
{
protected function setUp() {
if (version_compare(phpversion(), '5.4', '<')) {
$this->markTestSkipped('Requires PHP 5.4 or newer!');
}
}
public function testEmptyStruct()
{
$empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
$this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
}
public function testStringsAndInts()
{
$input = array(
'string_thing' => 'foo',
'i64_thing' => 1234567890,
);
$xtruct = new \ThriftTest\Xtruct($input);
// Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
$expected = new stdClass();
$expected->string_thing = $input['string_thing'];
$expected->i64_thing = $input['i64_thing'];
$this->assertEquals($expected, json_decode(json_encode($xtruct)));
}
public function testNestedStructs()
{
$xtruct2 = new \ThriftTest\Xtruct2(array(
'byte_thing' => 42,
'struct_thing' => new \ThriftTest\Xtruct(array(
'i32_thing' => 123456,
)),
));
$expected = new stdClass();
$expected->byte_thing = $xtruct2->byte_thing;
$expected->struct_thing = new stdClass();
$expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
$this->assertEquals($expected, json_decode(json_encode($xtruct2)));
}
public function testInsanity()
{
$xinput = array('string_thing' => 'foo');
$xtruct = new \ThriftTest\Xtruct($xinput);
$insanity = new \ThriftTest\Insanity(array(
'xtructs' => array($xtruct, $xtruct, $xtruct)
));
$expected = new stdClass();
$expected->xtructs = array((object) $xinput, (object) $xinput, (object) $xinput);
$this->assertEquals($expected, json_decode(json_encode($insanity)));
}
public function testNestedLists()
{
$bonk = new \ThriftTest\Bonk(array('message' => 'foo'));
$nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
$expected = new stdClass();
$expected->bonk = array(array(array((object) array('message' => 'foo'))));
$this->assertEquals($expected, json_decode(json_encode($nested)));
}
public function testMaps()
{
$intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
$emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
$this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
$this->assertEquals('{}', json_encode($emptymap));
}
public function testScalarTypes()
{
$b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
$this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
$s = new \ThriftTest\StructA(['s' => 42]);
$this->assertEquals('{"s":"42"}', json_encode($s));
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,156 +0,0 @@
<?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\php;
require_once __DIR__.'/../../../lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Exception\TProtocolException;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TMemoryBuffer;
$oop_mode = (isset($argv[1]) && $argv[1] === '-oop');
$GEN_DIR = $oop_mode ? 'phpvo' : 'phpv';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../../../lib');
$loader->registerDefinition('ThriftTest', __DIR__ . '/../../packages/' . $GEN_DIR);
$loader->registerDefinition('TestValidators', __DIR__ . '/../../packages/' . $GEN_DIR);
$loader->register();
// Would be nice to have PHPUnit here, but for now just hack it.
set_exception_handler(function ($e) {
my_assert(false, "Unexpected exception caught: " . $e->getMessage());
});
set_error_handler(function ($errno, $errmsg) {
my_assert(false, "Unexpected PHP error: " . $errmsg);
});
// Empty structs should not have validators
assert_has_no_read_validator('ThriftTest\EmptyStruct');
assert_has_no_write_validator('ThriftTest\EmptyStruct');
// Bonk has only opt_in_req_out fields
{
assert_has_no_read_validator('ThriftTest\Bonk');
assert_has_a_write_validator('ThriftTest\Bonk');
{
// Check that we can read an empty object
$bonk = new \ThriftTest\Bonk();
$transport = new TMemoryBuffer("\000");
$protocol = new TBinaryProtocol($transport);
$bonk->read($protocol);
}
{
// ...but not write an empty object
$bonk = new \ThriftTest\Bonk();
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocol($transport);
assert_protocol_exception_thrown(function () use ($bonk, $protocol) { $bonk->write($protocol); },
'Bonk was able to write an empty object');
}
}
// StructA has a single required field
{
assert_has_a_read_validator('ThriftTest\StructA');
assert_has_a_write_validator('ThriftTest\StructA');
{
// Check that we are not able to write StructA with a missing required field
$structa = new \ThriftTest\StructA();
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocol($transport);
assert_protocol_exception_thrown(function () use ($structa, $protocol) { $structa->write($protocol); },
'StructA was able to write an empty object');
}
{
// Check that we are able to read and write a message with a good StructA
$transport = new TMemoryBuffer(base64_decode('CwABAAAAA2FiYwA='));
$protocol = new TBinaryProtocol($transport);
$structa = new \ThriftTest\StructA();
$structa->read($protocol);
$structa->write($protocol);
}
}
// Unions should not get write validators
assert_has_no_write_validator('TestValidators\UnionOfStrings');
// Service _result classes should not get any validators
assert_has_no_read_validator('TestValidators\TestService_test_result');
assert_has_no_write_validator('TestValidators\TestService_test_result');
function assert_has_a_read_validator($class)
{
my_assert(has_read_validator_method($class),
$class . ' class should have a read validator');
}
function assert_has_no_read_validator($class)
{
my_assert(!has_read_validator_method($class),
$class . ' class should not have a read validator');
}
function assert_has_a_write_validator($class)
{
my_assert(has_write_validator_method($class),
$class . ' class should have a write validator');
}
function assert_has_no_write_validator($class)
{
my_assert(!has_write_validator_method($class),
$class . ' class should not have a write validator');
}
function assert_protocol_exception_thrown($callable, $message)
{
try {
call_user_func($callable);
my_assert(false, $message);
} catch (TProtocolException $e) {
}
}
function has_write_validator_method($class)
{
$rc = new \ReflectionClass($class);
return $rc->hasMethod('_validateForWrite');
}
function has_read_validator_method($class)
{
$rc = new \ReflectionClass($class);
return $rc->hasMethod('_validateForRead');
}
function my_assert($something, $message)
{
if (!$something) {
fwrite(STDERR, basename(__FILE__) . " FAILED: $message\n");
exit(1);
}
}

View file

@ -0,0 +1,154 @@
<?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;
use PHPUnit\Framework\TestCase;
use Thrift\Exception\TProtocolException;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TMemoryBuffer;
abstract class BaseValidatorTest extends TestCase
{
public function testEmptyStructValidator()
{
$this->assertNoReadValidator('ThriftTest\EmptyStruct');
$this->assertNoWriteValidator('ThriftTest\EmptyStruct');
}
public function testBonkValidator()
{
$this->assertNoReadValidator('ThriftTest\Bonk');
$this->assertHasWriteValidator('ThriftTest\Bonk');
}
public function testStructAValidator()
{
$this->assertHasReadValidator('ThriftTest\StructA');
$this->assertHasWriteValidator('ThriftTest\StructA');
}
public function testUnionOfStringsValidator()
{
$this->assertNoWriteValidator('TestValidators\UnionOfStrings');
}
public function testServiceResultValidator()
{
$this->assertNoReadValidator('TestValidators\TestService_test_result');
$this->assertNoWriteValidator('TestValidators\TestService_test_result');
}
public function testReadEmpty()
{
$bonk = new \ThriftTest\Bonk();
$transport = new TMemoryBuffer("\000");
$protocol = new TBinaryProtocol($transport);
$bonk->read($protocol);
}
public function testWriteEmpty()
{
$bonk = new \ThriftTest\Bonk();
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocol($transport);
try {
$bonk->write($protocol);
$this->fail('Bonk was able to write an empty object');
} catch (TProtocolException $e) {
}
}
public function testWriteWithMissingRequired()
{
// Check that we are not able to write StructA with a missing required field
$structa = new \ThriftTest\StructA();
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocol($transport);
try {
$structa->write($protocol);
$this->fail('StructA was able to write an empty object');
} catch (TProtocolException $e) {
}
}
public function testReadStructA()
{
$transport = new TMemoryBuffer(base64_decode('CwABAAAAA2FiYwA='));
$protocol = new TBinaryProtocol($transport);
$structa = new \ThriftTest\StructA();
$structa->read($protocol);
$this->assertEquals("abc", $structa->s);
}
public function testWriteStructA()
{
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocol($transport);
$structa = new \ThriftTest\StructA();
$structa->s = "abc";
$structa->write($protocol);
$writeResult = base64_encode($transport->getBuffer());
$this->assertEquals('CwABAAAAA2FiYwA=', $writeResult);
}
protected static function assertHasReadValidator($class)
{
if (!static::hasReadValidator($class)) {
static::fail($class . ' class should have a read validator');
}
}
protected static function assertNoReadValidator($class)
{
if (static::hasReadValidator($class)) {
static::fail($class . ' class should not have a write validator');
}
}
protected static function assertHasWriteValidator($class)
{
if (!static::hasWriteValidator($class)) {
static::fail($class . ' class should have a write validator');
}
}
protected static function assertNoWriteValidator($class)
{
if (static::hasWriteValidator($class)) {
static::fail($class . ' class should not have a write validator');
}
}
private static function hasReadValidator($class)
{
$rc = new \ReflectionClass($class);
return $rc->hasMethod('_validateForRead');
}
private static function hasWriteValidator($class)
{
$rc = new \ReflectionClass($class);
return $rc->hasMethod('_validateForWrite');
}
}

View file

@ -0,0 +1,41 @@
<?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;
require __DIR__ . '/../../../../vendor/autoload.php';
use Thrift\ClassLoader\ThriftClassLoader;
/**
* Class TestValidators
* @package Test\Thrift
*
* @runTestsInSeparateProcesses
*/
class ValidatorTest extends BaseValidatorTest
{
public function setUp()
{
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('', __DIR__ . '/../packages/phpv');
}
}

View file

@ -0,0 +1,41 @@
<?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;
require_once __DIR__ . '/../../../../vendor/autoload.php';
use Thrift\ClassLoader\ThriftClassLoader;
/**
* Class TestValidatorsOop
* @package Test\Thrift
*
* @runTestsInSeparateProcesses
*/
class ValidatorTestOop extends BaseValidatorTest
{
public function setUp()
{
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('', __DIR__ . '/../packages/phpvo');
}
}