From 00363d017abff16665387088c30acd37315408a1 Mon Sep 17 00:00:00 2001 From: Igor Melnikov <imelnikov@magento.com> Date: Fri, 21 Oct 2016 16:32:08 -0500 Subject: [PATCH] MAGETWO-59764: Create Serialize implementation of SerializerInterface Creating Serialize implementation --- .../Test/Unit/Model/Config/DataTest.php | 6 +-- .../Framework/App/Config/ScopePool.php | 6 +-- .../App/ObjectManager/ConfigCache.php | 10 ++-- .../App/ObjectManager/ConfigLoader.php | 10 ++-- .../ObjectManager/ConfigLoader/Compiled.php | 23 +-------- .../Framework/App/Router/ActionList.php | 8 ++-- .../App/Test/Unit/Config/ScopePoolTest.php | 6 +-- .../Unit/ObjectManager/ConfigCacheTest.php | 7 +-- .../Unit/ObjectManager/ConfigLoaderTest.php | 12 ++--- .../Config/Test/Unit/Data/ScopedTest.php | 12 ++--- .../Framework/Interception/Config/Config.php | 3 +- .../Interception/PluginList/PluginList.php | 18 ++++++- .../Test/Unit/Config/ConfigTest.php | 3 +- .../Test/Unit/PluginList/PluginListTest.php | 2 +- .../ObjectManager/Config/Compiled.php | 1 + .../Test/Unit/Config/ConfigTest.php | 6 +-- .../Magento/Framework/Serialize/README.md | 7 ++- .../Serialize/Serializer/Serialize.php | 40 ++++++++++++++++ .../Test/Unit/Serializer/SerializeTest.php | 48 +++++++++++++++++++ .../Magento/Framework/Unserialize/README.md | 3 +- .../Framework/Unserialize/Unserialize.php | 3 ++ .../Di/Compiler/Config/Writer/Filesystem.php | 23 +-------- 22 files changed, 164 insertions(+), 93 deletions(-) create mode 100644 lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php create mode 100644 lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php diff --git a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php index 9096bb8af1b..7ab42a5b914 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -95,15 +95,15 @@ class DataTest extends \PHPUnit_Framework_TestCase public function testConstructorWithCache() { - $jsonString = json_encode($this->indexers); + $serializedData = 'serialized data'; $this->cache->expects($this->once())->method('test')->with($this->cacheId)->will($this->returnValue(true)); $this->cache->expects($this->once()) ->method('load') ->with($this->cacheId) - ->willReturn($jsonString); + ->willReturn($serializedData); $this->serializerMock->method('unserialize') - ->with($jsonString) + ->with($serializedData) ->willReturn($this->indexers); $this->stateCollection->expects($this->never())->method('getItems'); diff --git a/lib/internal/Magento/Framework/App/Config/ScopePool.php b/lib/internal/Magento/Framework/App/Config/ScopePool.php index 018e21f86a7..4b4be9cfc53 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -7,6 +7,7 @@ namespace Magento\Framework\App\Config; use Magento\Framework\App\RequestInterface; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -168,9 +169,8 @@ class ScopePool */ private function getSerializer() { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class); } return $this->serializer; } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php index dbb3bed8956..4e685e3472e 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php @@ -8,6 +8,7 @@ namespace Magento\Framework\App\ObjectManager; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterface { @@ -24,7 +25,7 @@ class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterfa protected $_prefix = 'diConfig'; /** - * @var \Magento\Framework\Serialize\SerializerInterface + * @var SerializerInterface */ private $serializer; @@ -62,14 +63,13 @@ class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterfa /** * Get serializer * - * @return \Magento\Framework\Serialize\SerializerInterface + * @return SerializerInterface * @deprecated */ private function getSerializer() { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class); } return $this->serializer; } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php index 2cfa2b006ff..2770443b6d7 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php @@ -8,6 +8,7 @@ namespace Magento\Framework\App\ObjectManager; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; use Magento\Framework\ObjectManager\ConfigLoaderInterface; class ConfigLoader implements ConfigLoaderInterface @@ -34,7 +35,7 @@ class ConfigLoader implements ConfigLoaderInterface protected $_cache; /** - * @var \Magento\Framework\Serialize\SerializerInterface + * @var SerializerInterface */ private $serializer; @@ -82,16 +83,15 @@ class ConfigLoader implements ConfigLoaderInterface } /** - * Get json encoder/decoder + * Get serializer * * @return SerializerInterface * @deprecated */ private function getSerializer() { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class); } return $this->serializer; } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index d1dc30ee7c9..f3990498832 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -18,11 +18,6 @@ class Compiled implements ConfigLoaderInterface */ private $configCache = []; - /** - * @var \Magento\Framework\Serialize\SerializerInterface - */ - private $serializer; - /** * {inheritdoc} */ @@ -31,7 +26,7 @@ class Compiled implements ConfigLoaderInterface if (isset($this->configCache[$area])) { return $this->configCache[$area]; } - $this->configCache[$area] = $this->getSerializer()->unserialize(\file_get_contents(self::getFilePath($area))); + $this->configCache[$area] = unserialize(\file_get_contents(self::getFilePath($area))); return $this->configCache[$area]; } @@ -44,20 +39,6 @@ class Compiled implements ConfigLoaderInterface public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . $diPath . '/' . $area . '.json'; - } - - /** - * Get serializer - * - * @return \Magento\Framework\Serialize\SerializerInterface - * @deprecated - */ - private function getSerializer() - { - if ($this->serializer === null) { - $this->serializer = new \Magento\Framework\Serialize\Serializer\Json(); - } - return $this->serializer; + return BP . '/' . $diPath . '/' . $area . '.json'; } } diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index dbfc9d2b9f5..c31059a1462 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -7,6 +7,7 @@ namespace Magento\Framework\App\Router; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; use Magento\Framework\Module\Dir\Reader as ModuleReader; class ActionList @@ -102,14 +103,13 @@ class ActionList /** * Get serializer * - * @return \Magento\Framework\Serialize\SerializerInterface + * @return SerializerInterface * @deprecated */ private function getSerializer() { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class); } return $this->serializer; } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php index 06f35c94a46..b0465f28e24 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -113,14 +113,14 @@ class ScopePoolTest extends \PHPUnit_Framework_TestCase ->method('read') ->with('testScope') ->willReturn($data); - $jsonString = json_encode($data); + $serializedData = 'serialized data'; $this->serializerMock->method('serialize') ->with($data) - ->willReturn($jsonString); + ->willReturn($serializedData); $this->_cache->expects($this->once()) ->method('save') ->with( - $jsonString, + $serializedData, $cacheKey, [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] ); diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php index 21dce94bb6c..7f05857f7cc 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php @@ -72,7 +72,7 @@ class ConfigCacheTest extends \PHPUnit_Framework_TestCase { return [ [false, false], - ['["some data"]', ['some data']], + ['serialized data', ['some data']], ]; } @@ -80,10 +80,11 @@ class ConfigCacheTest extends \PHPUnit_Framework_TestCase { $key = 'key'; $config = ['config']; + $serializedData = 'serialized data'; $this->serializerMock->expects($this->once()) ->method('serialize') - ->willReturn('["config"]'); - $this->cacheFrontendMock->expects($this->once())->method('save')->with('["config"]', 'diConfig' . $key); + ->willReturn($serializedData); + $this->cacheFrontendMock->expects($this->once())->method('save')->with($serializedData, 'diConfig' . $key); $this->configCache->save($config, $key); } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php index bb048218a76..90dba092f05 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php @@ -88,7 +88,7 @@ class ConfigLoaderTest extends \PHPUnit_Framework_TestCase public function testLoadNotCached($area) { $configData = ['some' => 'config', 'data' => 'value']; - $serializedConfigData = '{"some":"config","data":"value"}'; + $serializedData = 'serialized data'; $this->cacheMock->expects($this->once()) ->method('load') @@ -97,12 +97,12 @@ class ConfigLoaderTest extends \PHPUnit_Framework_TestCase $this->cacheMock->expects($this->once()) ->method('save') - ->with($serializedConfigData); + ->with($serializedData); $this->readerMock->expects($this->once())->method('read')->with($area)->will($this->returnValue($configData)); $this->serializerMock->expects($this->once()) ->method('serialize') - ->willReturn($serializedConfigData); + ->willReturn($serializedData); $this->serializerMock->expects($this->never())->method('unserialize'); @@ -126,17 +126,17 @@ class ConfigLoaderTest extends \PHPUnit_Framework_TestCase public function testLoadCached() { $configData = ['some' => 'config', 'data' => 'value']; - $serializedConfigData = '{"some":"config","data":"value"}'; + $serializedData = 'serialized data'; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn($serializedConfigData); + ->willReturn($serializedData); $this->cacheMock->expects($this->never()) ->method('save'); $this->readerMock->expects($this->never())->method('read'); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->with($serializedConfigData) + ->with($serializedData) ->willReturn($configData); $this->serializerMock->expects($this->never())->method('serialize'); $this->assertEquals($configData, $this->object->load('testArea')); diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php index 2b0a677b518..f07eaad289f 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -102,7 +102,7 @@ class ScopedTest extends \PHPUnit_Framework_TestCase public function testGetScopeSwitchingWithNonCachedData() { $testValue = ['some' => 'testValue']; - $jsonString = '{"some":"testValue"}'; + $serializedData = 'serialized data'; /** change current area */ $this->_configScopeMock->expects( @@ -137,12 +137,12 @@ class ScopedTest extends \PHPUnit_Framework_TestCase $this->serializerMock->method('serialize') ->with($testValue) - ->willReturn($jsonString); + ->willReturn($serializedData); /** test cache saving */ $this->_cacheMock->expects($this->once()) ->method('save') - ->with($jsonString, 'adminhtml::tag'); + ->with($serializedData, 'adminhtml::tag'); /** test config value existence */ $this->assertEquals('testValue', $this->_model->get('some')); @@ -154,7 +154,7 @@ class ScopedTest extends \PHPUnit_Framework_TestCase public function testGetScopeSwitchingWithCachedData() { $testValue = ['some' => 'testValue']; - $jsonString = '{"some":"testValue"}'; + $serializedData = 'serialized data'; /** change current area */ $this->_configScopeMock->expects( @@ -166,14 +166,14 @@ class ScopedTest extends \PHPUnit_Framework_TestCase ); $this->serializerMock->method('unserialize') - ->with($jsonString) + ->with($serializedData) ->willReturn($testValue); /** set cache data */ $this->_cacheMock->expects($this->once()) ->method('load') ->with('adminhtml::tag') - ->willReturn($jsonString); + ->willReturn($serializedData); /** test preventing of getting data from reader */ $this->_readerMock->expects($this->never())->method('read'); diff --git a/lib/internal/Magento/Framework/Interception/Config/Config.php b/lib/internal/Magento/Framework/Interception/Config/Config.php index a536a8c7594..25039b5e09c 100644 --- a/lib/internal/Magento/Framework/Interception/Config/Config.php +++ b/lib/internal/Magento/Framework/Interception/Config/Config.php @@ -8,6 +8,7 @@ namespace Magento\Framework\Interception\Config; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; class Config implements \Magento\Framework\Interception\ConfigInterface { @@ -193,7 +194,7 @@ class Config implements \Magento\Framework\Interception\ConfigInterface { if ($this->serializer === null) { $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); + ->get(Serialize::class); } return $this->serializer; } diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index 5d67a9077b2..e3ee36ac3c7 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -17,6 +17,8 @@ use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\ObjectManager\RelationsInterface; use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions; use Magento\Framework\ObjectManagerInterface; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -375,7 +377,7 @@ class PluginList extends Scoped implements InterceptionPluginList } /** - * Returns logger instance + * Get logger * * @deprecated * @return \Psr\Log\LoggerInterface @@ -387,4 +389,18 @@ class PluginList extends Scoped implements InterceptionPluginList } return $this->logger; } + + /** + * Get serializer + * + * @return SerializerInterface + * @deprecated + */ + protected function getSerializer() + { + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class); + } + return $this->serializer; + } } diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php index 7ff809810cc..22c935b176d 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -7,6 +7,7 @@ namespace Magento\Framework\Interception\Test\Unit\Config; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; @@ -77,7 +78,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ); $this->serializerMock = $this->getMock(SerializerInterface::class); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->mockObjectManager([SerializerInterface::class => $this->serializerMock]); + $this->mockObjectManager([Serialize::class => $this->serializerMock]); } protected function tearDown() diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php index dd0d2f68ac6..f70af781df6 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -274,7 +274,7 @@ class PluginListTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue('scope')); $data = [['key'], ['key'], ['key']]; - $serializedData = '[["key"],["key"],["key"]]'; + $serializedData = 'serialized data'; $this->serializerMock->expects($this->never()) ->method('serialize'); diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index 95331616b5b..aa1fd86dbf7 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -7,6 +7,7 @@ namespace Magento\Framework\ObjectManager\Config; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; use Magento\Framework\ObjectManager\ConfigCacheInterface; use Magento\Framework\ObjectManager\RelationsInterface; diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php index 11dd0aeff1b..4cc51652fdb 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php @@ -52,10 +52,8 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $config = new Config(null, $definitions); $serializerMock = $this->getMock(SerializerInterface::class); - $serializerMock->method('serialize') - ->willReturnCallback(function ($data) { - return json_encode($data, true); - }); + $serializerMock->expects($this->exactly(2)) + ->method('serialize'); $this->objectManagerHelper->setBackwardCompatibleProperty( $config, 'serializer', diff --git a/lib/internal/Magento/Framework/Serialize/README.md b/lib/internal/Magento/Framework/Serialize/README.md index e636fc79818..5af8fb7f71b 100644 --- a/lib/internal/Magento/Framework/Serialize/README.md +++ b/lib/internal/Magento/Framework/Serialize/README.md @@ -1,5 +1,8 @@ # Serialize -**Serialize** libaray provides *SerializerInterface* and multiple implementations of serializer to support different kinds of needs of serializing/unserializing of data. Here are list of serializers in this library: +**Serialize** library provides interface *SerializerInterface* and multiple implementations: - * **Json** (default) - It can be used to serialize string, integer, float, boolean, or array data to json string; it unserializes json string to string, integer, float, boolean, or array. This is the recommended serializer. \ No newline at end of file + * *Json* - default implementation. Uses PHP native json_encode/json_decode functions; + * *Serialize* - less secure than *Json*, but gives higher performance on big arrays. Uses PHP native serialize/unserialize functions, does not unserialize objects on PHP 7. + +Using *Serialize* implementation directly is discouraged, always use *SerializerInterface*, using *Serialize* implementation may lead to security vulnerabilities. \ No newline at end of file diff --git a/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php b/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php new file mode 100644 index 00000000000..ef400e43e3c --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php @@ -0,0 +1,40 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Serialize\Serializer; + +use Magento\Framework\Serialize\SerializerInterface; + +class Serialize implements SerializerInterface +{ + /** + * {@inheritDoc} + */ + public function serialize($data) + { + return serialize($data); + } + + /** + * {@inheritDoc} + */ + public function unserialize($string) + { + if ($this->getPhpVersion() >= 7) { + return unserialize($string, ['allowed_classes' => false]); + } + return unserialize($string); + } + + /** + * Return major PHP version + * + * @return int + */ + private function getPhpVersion() + { + return PHP_MAJOR_VERSION; + } +} diff --git a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php new file mode 100644 index 00000000000..9a9252029d0 --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php @@ -0,0 +1,48 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Serialize\Test\Unit\Serializer; + +use Magento\Framework\Serialize\Serializer\Serialize; +use Magento\Framework\Serialize\Signer; +use Psr\Log\LoggerInterface; +use Magento\Framework\Serialize\InvalidSignatureException; + +class SerializeTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Serialize + */ + private $serialize; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->serialize = $objectManager->getObject(Serialize::class); + } + + /** + * @param string|int|bool|array|null $value + * @dataProvider serializeUnserializeDataProvider + */ + public function testSerializeUnserialize($value) + { + $this->assertEquals( + $this->serialize->unserialize($this->serialize->serialize($value)), + $value + ); + } + + public function serializeUnserializeDataProvider() + { + return [ + ['string'], + [''], + [null], + [false], + [['a' => 'b']], + ]; + } +} diff --git a/lib/internal/Magento/Framework/Unserialize/README.md b/lib/internal/Magento/Framework/Unserialize/README.md index 971bd6980ab..2dbf7436aa6 100644 --- a/lib/internal/Magento/Framework/Unserialize/README.md +++ b/lib/internal/Magento/Framework/Unserialize/README.md @@ -1,2 +1 @@ -Library provides custom unserialize method. Method checks if serialized string contains serialized object and do not -unserialize it. If string doesn't contain serialized object, method calls native PHP function unserialize. \ No newline at end of file +This library is deprecated, please use Magento\Framework\Serialize\SerializerInterface instead. \ No newline at end of file diff --git a/lib/internal/Magento/Framework/Unserialize/Unserialize.php b/lib/internal/Magento/Framework/Unserialize/Unserialize.php index 9d4785915a6..cfd3e81ca6b 100644 --- a/lib/internal/Magento/Framework/Unserialize/Unserialize.php +++ b/lib/internal/Magento/Framework/Unserialize/Unserialize.php @@ -6,6 +6,9 @@ namespace Magento\Framework\Unserialize; +/** + * @deprecated + */ class Unserialize { /** diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php index 3207e590adf..33203df3e72 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php @@ -8,7 +8,6 @@ namespace Magento\Setup\Module\Di\Compiler\Config\Writer; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Serialize\SerializerInterface; use Magento\Setup\Module\Di\Compiler\Config\WriterInterface; class Filesystem implements WriterInterface @@ -18,11 +17,6 @@ class Filesystem implements WriterInterface */ private $directoryList; - /** - * @var SerializerInterface - */ - private $serializer; - /** * Constructor * @@ -46,7 +40,7 @@ class Filesystem implements WriterInterface file_put_contents( $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.json', - $this->getSerializer()->serialize($config) + serialize($config) ); } @@ -61,19 +55,4 @@ class Filesystem implements WriterInterface mkdir($this->directoryList->getPath(DirectoryList::DI)); } } - - /** - * Get serializer - * - * @return SerializerInterface - * @deprecated - */ - private function getSerializer() - { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); - } - return $this->serializer; - } } -- GitLab