-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added json hex tag serializer and use it.
code formatting Backwards compatibility fix use JSON_HEX_TAG option for not breaking things use new serializer instead of json_encode use correct serializer in test declare return type use serializer updated unit testing, now uses serializer removed space declare strict type improved comment Test new serializer Test for greater than and lesser than symbol, required by JSON_HEX_TAG option split comment for readability Declare strict type on test Use di to inject cleanup curly brackets code cleanup removed empty phpdoc Fix BC
- Loading branch information
Tommy Quissens
committed
Jan 14, 2019
1 parent
56b9c6c
commit 7e3ea1b
Showing
7 changed files
with
180 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/internal/Magento/Framework/Serialize/Serializer/JsonHexTag.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\Serialize\Serializer; | ||
|
||
use Magento\Framework\Serialize\SerializerInterface; | ||
|
||
/** | ||
* Serialize data to JSON with the JSON_HEX_TAG option enabled | ||
* (All < and > are converted to \u003C and \u003E), | ||
* unserialize JSON encoded data | ||
* | ||
* @api | ||
* @since 100.2.0 | ||
*/ | ||
class JsonHexTag extends Json implements SerializerInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
* @since 100.2.0 | ||
*/ | ||
public function serialize($data): string | ||
{ | ||
$result = json_encode($data, JSON_HEX_TAG); | ||
if (false === $result) { | ||
throw new \InvalidArgumentException('Unable to serialize value.'); | ||
} | ||
return $result; | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonHexTagTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\Serialize\Test\Unit\Serializer; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\Framework\Serialize\Serializer\JsonHexTag; | ||
|
||
class JsonHexTagTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \Magento\Framework\Serialize\Serializer\Json | ||
*/ | ||
private $json; | ||
|
||
protected function setUp() | ||
{ | ||
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
$this->json = $objectManager->getObject(JsonHexTag::class); | ||
} | ||
|
||
/** | ||
* @param string|int|float|bool|array|null $value | ||
* @param string $expected | ||
* @dataProvider serializeDataProvider | ||
*/ | ||
public function testSerialize($value, $expected) | ||
{ | ||
$this->assertEquals( | ||
$expected, | ||
$this->json->serialize($value) | ||
); | ||
} | ||
|
||
public function serializeDataProvider() | ||
{ | ||
$dataObject = new DataObject(['something']); | ||
return [ | ||
['', '""'], | ||
['string', '"string"'], | ||
[null, 'null'], | ||
[false, 'false'], | ||
[['a' => 'b', 'd' => 123], '{"a":"b","d":123}'], | ||
[123, '123'], | ||
[10.56, '10.56'], | ||
[$dataObject, '{}'], | ||
['< >', '"\u003C \u003E"'], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* @param string|int|float|bool|array|null $expected | ||
* @dataProvider unserializeDataProvider | ||
*/ | ||
public function testUnserialize($value, $expected) | ||
{ | ||
$this->assertEquals( | ||
$expected, | ||
$this->json->unserialize($value) | ||
); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function unserializeDataProvider(): array { | ||
return [ | ||
['""', ''], | ||
['"string"', 'string'], | ||
['null', null], | ||
['false', false], | ||
['{"a":"b","d":123}', ['a' => 'b', 'd' => 123]], | ||
['123', 123], | ||
['10.56', 10.56], | ||
['{}', []], | ||
['"\u003C \u003E"', '< >'], | ||
]; | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Unable to serialize value. | ||
*/ | ||
public function testSerializeException() | ||
{ | ||
$this->json->serialize(STDOUT); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Unable to unserialize value. | ||
* @dataProvider unserializeExceptionDataProvider | ||
*/ | ||
public function testUnserializeException($value) | ||
{ | ||
$this->json->unserialize($value); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function unserializeExceptionDataProvider(): array { | ||
return [ | ||
[''], | ||
[false], | ||
[null], | ||
['{'] | ||
]; | ||
} | ||
} |