Skip to content

Commit

Permalink
[THRIFT-5757] Unit tests for php lib
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 23, 2024
1 parent ccbd00f commit 0df948e
Show file tree
Hide file tree
Showing 8 changed files with 906 additions and 1,163 deletions.
74 changes: 1 addition & 73 deletions lib/php/lib/Protocol/TJSONProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,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 +195,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 +203,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 @@ -304,14 +240,6 @@ private function writeJSONDouble($num)
}
}

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
192 changes: 0 additions & 192 deletions lib/php/test/Fixtures/Fixtures.php

This file was deleted.

Loading

0 comments on commit 0df948e

Please sign in to comment.