From d89d279e814f3fbafc56a1b0a25dbe8ff6656352 Mon Sep 17 00:00:00 2001
From: Olga Kopylova <okopylova@magento.com>
Date: Thu, 20 Oct 2016 16:16:25 -0500
Subject: [PATCH] MAGETWO-58693: Refactor Module_Integration,
 Module_MarketPlace

- replaced serialization with SerializeInterface
---
 app/code/Magento/Integration/Model/Config.php | 24 ++++-
 .../Integration/Model/ConsolidatedConfig.php  | 20 +++-
 .../Integration/Model/IntegrationConfig.php   | 20 +++-
 .../Unit/Model/ConsolidatedConfigTest.php     | 37 +++++--
 .../Test/Unit/Model/IntegrationConfigTest.php | 38 +++++--
 app/code/Magento/Marketplace/Helper/Cache.php | 17 +++-
 .../Test/Unit/Helper/CacheTest.php            | 98 ++++++++++---------
 7 files changed, 176 insertions(+), 78 deletions(-)

diff --git a/app/code/Magento/Integration/Model/Config.php b/app/code/Magento/Integration/Model/Config.php
index 70795cae003..3cea4d33743 100644
--- a/app/code/Magento/Integration/Model/Config.php
+++ b/app/code/Magento/Integration/Model/Config.php
@@ -5,6 +5,8 @@
  */
 namespace Magento\Integration\Model;
 
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Serialize\SerializerInterface;
 use Magento\Integration\Model\Cache\Type;
 
 /**
@@ -34,14 +36,24 @@ class Config
      */
     protected $_integrations;
 
+    /**
+     * @var SerializerInterface
+     */
+    private $serializer;
+
     /**
      * @param Cache\Type $configCacheType
      * @param Config\Reader $configReader
+     * @param SerializerInterface $serializer
      */
-    public function __construct(Cache\Type $configCacheType, Config\Reader $configReader)
-    {
+    public function __construct(
+        Cache\Type $configCacheType,
+        Config\Reader $configReader,
+        SerializerInterface $serializer = null
+    ) {
         $this->_configCacheType = $configCacheType;
         $this->_configReader = $configReader;
+        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
     }
 
     /**
@@ -55,10 +67,14 @@ class Config
         if (null === $this->_integrations) {
             $integrations = $this->_configCacheType->load(self::CACHE_ID);
             if ($integrations && is_string($integrations)) {
-                $this->_integrations = unserialize($integrations);
+                $this->_integrations = $this->serializer->unserialize($integrations);
             } else {
                 $this->_integrations = $this->_configReader->read();
-                $this->_configCacheType->save(serialize($this->_integrations), self::CACHE_ID, [Type::CACHE_TAG]);
+                $this->_configCacheType->save(
+                    $this->serializer->serialize($this->_integrations),
+                    self::CACHE_ID,
+                    [Type::CACHE_TAG]
+                );
             }
         }
         return $this->_integrations;
diff --git a/app/code/Magento/Integration/Model/ConsolidatedConfig.php b/app/code/Magento/Integration/Model/ConsolidatedConfig.php
index 9027bf774bc..9208d19e702 100644
--- a/app/code/Magento/Integration/Model/ConsolidatedConfig.php
+++ b/app/code/Magento/Integration/Model/ConsolidatedConfig.php
@@ -5,6 +5,8 @@
  */
 namespace Magento\Integration\Model;
 
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Serialize\SerializerInterface;
 use Magento\Integration\Model\Cache\TypeConsolidated;
 
 /**
@@ -31,14 +33,24 @@ class ConsolidatedConfig
      */
     protected $integrations;
 
+    /**
+     * @var SerializerInterface
+     */
+    private $serializer;
+
     /**
      * @param Cache\TypeConsolidated $configCacheType
      * @param Config\Consolidated\Reader $configReader
+     * @param SerializerInterface $serializer
      */
-    public function __construct(Cache\TypeConsolidated $configCacheType, Config\Consolidated\Reader $configReader)
-    {
+    public function __construct(
+        Cache\TypeConsolidated $configCacheType,
+        Config\Consolidated\Reader $configReader,
+        SerializerInterface $serializer = null
+    ) {
         $this->configCacheType = $configCacheType;
         $this->configReader = $configReader;
+        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
     }
 
     /**
@@ -51,11 +63,11 @@ class ConsolidatedConfig
         if (null === $this->integrations) {
             $integrations = $this->configCacheType->load(self::CACHE_ID);
             if ($integrations && is_string($integrations)) {
-                $this->integrations = unserialize($integrations);
+                $this->integrations = $this->serializer->unserialize($integrations);
             } else {
                 $this->integrations = $this->configReader->read();
                 $this->configCacheType->save(
-                    serialize($this->integrations),
+                    $this->serializer->serialize($this->integrations),
                     self::CACHE_ID,
                     [TypeConsolidated::CACHE_TAG]
                 );
diff --git a/app/code/Magento/Integration/Model/IntegrationConfig.php b/app/code/Magento/Integration/Model/IntegrationConfig.php
index 647bff70efe..cde4fc20d22 100644
--- a/app/code/Magento/Integration/Model/IntegrationConfig.php
+++ b/app/code/Magento/Integration/Model/IntegrationConfig.php
@@ -6,6 +6,8 @@
 
 namespace Magento\Integration\Model;
 
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Serialize\SerializerInterface;
 use Magento\Integration\Model\Cache\TypeIntegration;
 use Magento\Integration\Model\Config\Integration\Reader;
 
@@ -36,14 +38,24 @@ class IntegrationConfig
      */
     protected $_integrations;
 
+    /**
+     * @var SerializerInterface
+     */
+    private $serializer;
+
     /**
      * @param TypeIntegration $configCacheType
      * @param Reader $configReader
+     * @param SerializerInterface $serializer
      */
-    public function __construct(TypeIntegration $configCacheType, Reader $configReader)
-    {
+    public function __construct(
+        TypeIntegration $configCacheType,
+        Reader $configReader,
+        SerializerInterface $serializer = null
+    ) {
         $this->_configCacheType = $configCacheType;
         $this->_configReader = $configReader;
+        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
     }
 
     /**
@@ -57,11 +69,11 @@ class IntegrationConfig
         if (null === $this->_integrations) {
             $integrations = $this->_configCacheType->load(self::CACHE_ID);
             if ($integrations && is_string($integrations)) {
-                $this->_integrations = unserialize($integrations);
+                $this->_integrations = $this->serializer->unserialize($integrations);
             } else {
                 $this->_integrations = $this->_configReader->read();
                 $this->_configCacheType->save(
-                    serialize($this->_integrations),
+                    $this->serializer->serialize($this->_integrations),
                     self::CACHE_ID,
                     [TypeIntegration::CACHE_TAG]
                 );
diff --git a/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php b/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php
index 0b755927827..22b50a8aef8 100644
--- a/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php
+++ b/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php
@@ -5,6 +5,7 @@
  */
 namespace Magento\Integration\Test\Unit\Model;
 
+use Magento\Framework\Serialize\SerializerInterface;
 use Magento\Integration\Model\ConsolidatedConfig as Config;
 use Magento\Integration\Model\Cache\TypeConsolidated as Type;
 
@@ -18,17 +19,22 @@ class ConsolidatedConfigTest extends \PHPUnit_Framework_TestCase
      *
      * @var Config
      */
-    protected $configModel;
+    private $configModel;
 
     /**
      * @var Type|\PHPUnit_Framework_MockObject_MockObject
      */
-    protected $configCacheTypeMock;
+    private $configCacheTypeMock;
 
     /**
      * @var  \Magento\Integration\Model\Config\Consolidated\Reader|\PHPUnit_Framework_MockObject_MockObject
      */
-    protected $configReaderMock;
+    private $configReaderMock;
+
+    /**
+     * @var  SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
+     */
+    private $serializer;
 
     protected function setUp()
     {
@@ -38,12 +44,16 @@ class ConsolidatedConfigTest extends \PHPUnit_Framework_TestCase
         $this->configReaderMock = $this->getMockBuilder(\Magento\Integration\Model\Config\Consolidated\Reader::class)
             ->disableOriginalConstructor()
             ->getMock();
+        $this->serializer = $this->getMockBuilder(SerializerInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
         $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->configModel = $objectManagerHelper->getObject(
             \Magento\Integration\Model\ConsolidatedConfig::class,
             [
                 'configCacheType' => $this->configCacheTypeMock,
-                'configReader' => $this->configReaderMock
+                'configReader' => $this->configReaderMock,
+                'serializer' => $this->serializer,
             ]
         );
     }
@@ -51,10 +61,15 @@ class ConsolidatedConfigTest extends \PHPUnit_Framework_TestCase
     public function testGetIntegrationsFromConfigCacheType()
     {
         $integrations = ['foo', 'bar', 'baz'];
+        $serializedIntegrations = '["foo","bar","baz"]';
         $this->configCacheTypeMock->expects($this->once())
             ->method('load')
             ->with(Config::CACHE_ID)
-            ->will($this->returnValue(serialize($integrations)));
+            ->will($this->returnValue($serializedIntegrations));
+        $this->serializer->expects($this->once())
+            ->method('unserialize')
+            ->with($serializedIntegrations)
+            ->willReturn($integrations);
 
         $this->assertEquals($integrations, $this->configModel->getIntegrations());
     }
@@ -62,17 +77,21 @@ class ConsolidatedConfigTest extends \PHPUnit_Framework_TestCase
     public function testGetIntegrationsFromConfigReader()
     {
         $integrations = ['foo', 'bar', 'baz'];
+        $serializedIntegrations = '["foo","bar","baz"]';
         $this->configCacheTypeMock->expects($this->once())
             ->method('load')
             ->with(Config::CACHE_ID)
             ->will($this->returnValue(null));
-        $this->configCacheTypeMock->expects($this->once())
-            ->method('save')
-            ->with(serialize($integrations), Config::CACHE_ID, [Type::CACHE_TAG])
-            ->will($this->returnValue(null));
         $this->configReaderMock->expects($this->once())
             ->method('read')
             ->will($this->returnValue($integrations));
+        $this->serializer->expects($this->once())
+            ->method('serialize')
+            ->with($integrations)
+            ->willReturn($serializedIntegrations);
+        $this->configCacheTypeMock->expects($this->once())
+            ->method('save')
+            ->with($serializedIntegrations, Config::CACHE_ID, [Type::CACHE_TAG]);
 
         $this->assertEquals($integrations, $this->configModel->getIntegrations());
     }
diff --git a/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php b/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php
index aed4e02453d..14871420a62 100644
--- a/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php
+++ b/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php
@@ -5,6 +5,7 @@
  */
 namespace Magento\Integration\Test\Unit\Model;
 
+use Magento\Framework\Serialize\SerializerInterface;
 use Magento\Integration\Model\IntegrationConfig;
 use Magento\Integration\Model\Cache\TypeIntegration;
 
@@ -16,17 +17,22 @@ class IntegrationConfigTest extends \PHPUnit_Framework_TestCase
     /**
      * @var IntegrationConfig
      */
-    protected $integrationConfigModel;
+    private $integrationConfigModel;
 
     /**
      * @var TypeIntegration|\PHPUnit_Framework_MockObject_MockObject
      */
-    protected $configCacheTypeMock;
+    private $configCacheTypeMock;
 
     /**
      * @var  \Magento\Integration\Model\Config\Integration\Reader|\PHPUnit_Framework_MockObject_MockObject
      */
-    protected $configReaderMock;
+    private $configReaderMock;
+
+    /**
+     * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
+     */
+    private $serializer;
 
     protected function setUp()
     {
@@ -36,19 +42,28 @@ class IntegrationConfigTest extends \PHPUnit_Framework_TestCase
         $this->configReaderMock = $this->getMockBuilder(\Magento\Integration\Model\Config\Integration\Reader::class)
             ->disableOriginalConstructor()
             ->getMock();
+        $this->serializer = $this->getMockBuilder(SerializerInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
         $this->integrationConfigModel = new IntegrationConfig(
             $this->configCacheTypeMock,
-            $this->configReaderMock
+            $this->configReaderMock,
+            $this->serializer
         );
     }
 
     public function testGetIntegrationsFromConfigCacheType()
     {
         $integrations = ['foo', 'bar', 'baz'];
+        $serializedIntegrations = '["foo","bar","baz"]';
         $this->configCacheTypeMock->expects($this->once())
             ->method('load')
             ->with(IntegrationConfig::CACHE_ID)
-            ->will($this->returnValue(serialize($integrations)));
+            ->will($this->returnValue($serializedIntegrations));
+        $this->serializer->expects($this->once())
+            ->method('unserialize')
+            ->with($serializedIntegrations)
+            ->willReturn($integrations);
 
         $this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations());
     }
@@ -56,17 +71,22 @@ class IntegrationConfigTest extends \PHPUnit_Framework_TestCase
     public function testGetIntegrationsFromConfigReader()
     {
         $integrations = ['foo', 'bar', 'baz'];
+        $serializedIntegrations = '["foo","bar","baz"]';
         $this->configCacheTypeMock->expects($this->once())
             ->method('load')
             ->with(IntegrationConfig::CACHE_ID)
             ->will($this->returnValue(null));
-        $this->configCacheTypeMock->expects($this->once())
-            ->method('save')
-            ->with(serialize($integrations), IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG])
-            ->will($this->returnValue(null));
         $this->configReaderMock->expects($this->once())
             ->method('read')
             ->will($this->returnValue($integrations));
+        $this->serializer->expects($this->once())
+            ->method('serialize')
+            ->with($integrations)
+            ->willReturn($serializedIntegrations);
+        $this->configCacheTypeMock->expects($this->once())
+            ->method('save')
+            ->with($serializedIntegrations, IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG])
+            ->will($this->returnValue(null));
 
         $this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations());
     }
diff --git a/app/code/Magento/Marketplace/Helper/Cache.php b/app/code/Magento/Marketplace/Helper/Cache.php
index 1cb5fb9c710..a0a4ce73e03 100644
--- a/app/code/Magento/Marketplace/Helper/Cache.php
+++ b/app/code/Magento/Marketplace/Helper/Cache.php
@@ -6,7 +6,8 @@
 
 namespace Magento\Marketplace\Helper;
 
-use Magento\Framework\Filesystem;
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Serialize\SerializerInterface;
 
 /**
  * Cache helper
@@ -25,15 +26,23 @@ class Cache extends \Magento\Framework\App\Helper\AbstractHelper
      */
     protected $cache;
 
+    /**
+     * @var SerializerInterface
+     */
+    private $serializer;
+
     /**
      * @param \Magento\Framework\App\Helper\Context $context
      * @param \Magento\Framework\Config\CacheInterface $cache
+     * @param SerializerInterface $serializer
      */
     public function __construct(
         \Magento\Framework\App\Helper\Context $context,
-        \Magento\Framework\Config\CacheInterface $cache
+        \Magento\Framework\Config\CacheInterface $cache,
+        SerializerInterface $serializer = null
     ) {
         $this->cache = $cache;
+        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
         parent::__construct($context);
     }
 
@@ -46,7 +55,7 @@ class Cache extends \Magento\Framework\App\Helper\AbstractHelper
     {
         $data = $this->getCache()->load($this->pathToCacheFile);
         if (false !== $data) {
-            $data = unserialize($data);
+            $data = $this->serializer->unserialize($data);
         }
         return $data;
     }
@@ -59,7 +68,7 @@ class Cache extends \Magento\Framework\App\Helper\AbstractHelper
      */
     public function savePartnersToCache($partners)
     {
-        return $this->getCache()->save(serialize($partners), $this->pathToCacheFile);
+        return $this->getCache()->save($this->serializer->serialize($partners), $this->pathToCacheFile);
     }
 
     /**
diff --git a/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php b/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php
index 75c6e611038..411c81d7547 100644
--- a/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php
+++ b/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php
@@ -6,70 +6,80 @@
 
 namespace Magento\Marketplace\Test\Unit\Helper;
 
+use Magento\Framework\Serialize\SerializerInterface;
+
 class CacheTest extends \PHPUnit_Framework_TestCase
 {
     /**
-     * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Marketplace\Helper\Cache
+     * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
+     */
+    private $cache;
+
+    /**
+     * @var  SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
+     */
+    private $serializer;
+
+    /**
+     * @var \Magento\Marketplace\Helper\Cache
      */
-    private $cacheHelperMock;
+    private $cacheHelper;
 
     protected function setUp()
     {
-        $this->cacheHelperMock = $this->getCacheHelperMock(['getCache']);
+        $this->cache = $this->getMockForAbstractClass(\Magento\Framework\Config\CacheInterface::class);
+        $this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
+        $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
+        $this->cacheHelper = $objectManagerHelper->getObject(
+            \Magento\Marketplace\Helper\Cache::class,
+            [
+                'cache' => $this->cache,
+                'serializer' => $this->serializer,
+            ]
+        );
     }
 
-    /**
-     * @covers \Magento\Marketplace\Helper\Cache::loadPartnersFromCache
-     */
     public function testLoadPartnersFromCache()
     {
-        $cache = $this->getCacheMock();
-        $this->cacheHelperMock
-            ->expects($this->once())
-            ->method('getCache')
-            ->will($this->returnValue($cache));
-        $cache->expects($this->once())
+        $partners = ['partner1', 'partner2'];
+        $serializedPartners = '["partner1", "partner2"]';
+        $this->cache->expects($this->once())
             ->method('load')
-            ->will($this->returnValue(''));
+            ->with('partners')
+            ->willReturn($serializedPartners);
+        $this->serializer->expects($this->once())
+            ->method('unserialize')
+            ->with($serializedPartners)
+            ->willReturn($partners);
 
-        $this->cacheHelperMock->loadPartnersFromCache();
+
+        $this->assertSame($partners, $this->cacheHelper->loadPartnersFromCache());
     }
 
-    /**
-     * @covers \Magento\Marketplace\Helper\Cache::savePartnersToCache
-     */
-    public function testSavePartnersToCache()
+    public function testLoadPartnersFromCacheNoCachedData()
     {
-        $cache = $this->getCacheMock();
-        $this->cacheHelperMock
-            ->expects($this->once())
-            ->method('getCache')
-            ->will($this->returnValue($cache));
-        $cache->expects($this->once())
-            ->method('save')
-            ->will($this->returnValue(true));
+        $this->cache->expects($this->once())
+            ->method('load')
+            ->with('partners')
+            ->willReturn(false);
+        $this->serializer->expects($this->never())
+            ->method('unserialize');
 
-        $this->cacheHelperMock->savePartnersToCache([]);
+        $this->assertSame(false, $this->cacheHelper->loadPartnersFromCache());
     }
 
-    /**
-     * Gets cache helper mock
-     *
-     * @param null $methods
-     * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Marketplace\Helper\Cache
-     */
-    public function getCacheHelperMock($methods = null)
+    public function testSavePartnersToCache()
     {
-        return $this->getMock(\Magento\Marketplace\Helper\Cache::class, $methods, [], '', false);
-    }
+        $partners = ['partner1', 'partner2'];
+        $serializedPartners = '["partner1", "partner2"]';
+        $this->serializer->expects($this->once())
+            ->method('serialize')
+            ->with($partners)
+            ->willReturn($serializedPartners);
+        $this->cache->expects($this->once())
+            ->method('save')
+            ->with($serializedPartners);
 
-    /**
-     * Gets Filesystem mock
-     *
-     * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Config\CacheInterface
-     */
-    public function getCacheMock()
-    {
-        return $this->getMockForAbstractClass(\Magento\Framework\Config\CacheInterface::class);
+        $this->cacheHelper->savePartnersToCache($partners);
     }
 }
-- 
GitLab