Skip to content

Commit

Permalink
[THRIFT-5757] Unit tests for php lib
Browse files Browse the repository at this point in the history
Client: php
Patch: Volodymyr Panivko

This closes #2951
  • Loading branch information
sveneld authored and Jens-G committed Apr 7, 2024
1 parent 1d886ca commit 68139d1
Show file tree
Hide file tree
Showing 33 changed files with 3,722 additions and 1,372 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Expand Up @@ -120,11 +120,11 @@ jobs:
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 -r --out ./lib/php/test/Resources/packages/php lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:validate -r --out ./lib/php/test/Resources/packages/phpv lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:validate,oop -r --out ./lib/php/test/Resources/packages/phpvo lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:json -r --out ./lib/php/test/Resources/packages/phpjs lib/php/test/Resources/ThriftTest.thrift
compiler/cpp/thrift --gen php:classmap,server,rest -r --out ./lib/php/test/Resources/packages/phpcm lib/php/test/Resources/ThriftTest.thrift
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: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
compiler/cpp/thrift --gen php:classmap,server,rest,nsglobal="Classmap" -r --out ./lib/php/test/Resources/packages/phpcm lib/php/test/Resources/ThriftTest.thrift
- name: Run Tests
run: vendor/bin/phpunit -c lib/php/phpunit.xml
Expand Down
1 change: 1 addition & 0 deletions lib/php/lib/Protocol/TCompactProtocol.php
Expand Up @@ -154,6 +154,7 @@ public function readVarint(&$result)
$shift += 7;
}

#unreachable statement
return $idx;
}

Expand Down
88 changes: 3 additions & 85 deletions lib/php/lib/Protocol/TJSONProtocol.php
Expand Up @@ -23,6 +23,7 @@

namespace Thrift\Protocol;

use Thrift\Exception\TException;
use Thrift\Type\TType;
use Thrift\Exception\TProtocolException;
use Thrift\Protocol\JSON\BaseContext;
Expand Down Expand Up @@ -186,8 +187,6 @@ public function reset()
$this->reader_ = new LookaheadReader($this);
}

private $tmpbuf_ = array(4);

public function readJSONSyntaxChar($b)
{
$ch = $this->reader_->read();
Expand All @@ -197,68 +196,6 @@ public function readJSONSyntaxChar($b)
}
}

private function hexVal($s)
{
for ($i = 0; $i < strlen($s); $i++) {
$ch = substr($s, $i, 1);

if (!($ch >= "a" && $ch <= "f") && !($ch >= "0" && $ch <= "9")) {
throw new TProtocolException("Expected hex character " . $ch, TProtocolException::INVALID_DATA);
}
}

return hexdec($s);
}

private function hexChar($val)
{
return dechex($val);
}

private function hasJSONUnescapedUnicode()
{
if (PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4)) {
return true;
}

return false;
}

private function unescapedUnicode($str)
{
if ($this->hasJSONUnescapedUnicode()) {
return json_encode($str, JSON_UNESCAPED_UNICODE);
}

$json = json_encode($str);

/*
* Unescaped character outside the Basic Multilingual Plane
* High surrogate: 0xD800 - 0xDBFF
* Low surrogate: 0xDC00 - 0xDFFF
*/
$json = preg_replace_callback(
'/\\\\u(d[89ab][0-9a-f]{2})\\\\u(d[cdef][0-9a-f]{2})/i',
function ($matches) {
return mb_convert_encoding(pack('H*', $matches[1] . $matches[2]), 'UTF-8', 'UTF-16BE');
},
$json
);

/*
* Unescaped characters within the Basic Multilingual Plane
*/
$json = preg_replace_callback(
'/\\\\u([0-9a-f]{4})/i',
function ($matches) {
return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE');
},
$json
);

return $json;
}

private function writeJSONString($b)
{
$this->context_->write();
Expand All @@ -267,7 +204,7 @@ private function writeJSONString($b)
$this->trans_->write(self::QUOTE);
}

$this->trans_->write($this->unescapedUnicode($b));
$this->trans_->write(json_encode($b, JSON_UNESCAPED_UNICODE));

if (is_numeric($b) && $this->context_->escapeNum()) {
$this->trans_->write(self::QUOTE);
Expand Down Expand Up @@ -297,21 +234,14 @@ private function writeJSONDouble($num)
$this->trans_->write(self::QUOTE);
}

#TODO add compatibility with NAN and INF
$this->trans_->write(json_encode($num));

if ($this->context_->escapeNum()) {
$this->trans_->write(self::QUOTE);
}
}

private function writeJSONBase64($data)
{
$this->context_->write();
$this->trans_->write(self::QUOTE);
$this->trans_->write(json_encode(base64_encode($data)));
$this->trans_->write(self::QUOTE);
}

private function writeJSONObjectStart()
{
$this->context_->write();
Expand Down Expand Up @@ -481,18 +411,6 @@ private function readJSONDouble()
}
}

private function readJSONBase64()
{
$arr = $this->readJSONString(false);
$data = base64_decode($arr, true);

if ($data === false) {
throw new TProtocolException("Invalid base64 data " . $arr, TProtocolException::INVALID_DATA);
}

return $data;
}

private function readJSONObjectStart()
{
$this->context_->read();
Expand Down
1 change: 1 addition & 0 deletions lib/php/lib/Protocol/TSimpleJSONProtocol.php
Expand Up @@ -115,6 +115,7 @@ private function writeJSONDouble($num)
$this->trans_->write(self::QUOTE);
}

#TODO add compatibility with NAN and INF
$this->trans_->write(json_encode((float)$num));

if ($isMapKey) {
Expand Down
7 changes: 5 additions & 2 deletions lib/php/phpunit.xml
Expand Up @@ -18,7 +18,7 @@
under the License.
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="../../vendor/autoload.php"
bootstrap="test/bootstrap.php"
cacheResult="false"
colors="true"
convertErrorsToExceptions="true"
Expand All @@ -34,8 +34,11 @@
</whitelist>
</filter>
<testsuites>
<testsuite name="Thrift PHP Test Suite">
<testsuite name="Thrift PHP Unit Test Suite">
<directory>./test/Unit</directory>
</testsuite>
<testsuite name="Thrift PHP Integration Test Suite">
<directory>./test/Integration</directory>
</testsuite>
</testsuites>
</phpunit>
192 changes: 0 additions & 192 deletions lib/php/test/Fixtures/Fixtures.php

This file was deleted.

0 comments on commit 68139d1

Please sign in to comment.