Skip to content

Commit

Permalink
Merge pull request #574 from magento-jackalopes/MAGETWO-58680-remove-…
Browse files Browse the repository at this point in the history
…unserialize-cache-frontend-interface-rebased

Fixed issue:

- MAGETWO-58680 Remove uses of unserialize in usages of \Magento\Framework\Cache\FrontendInterface::load() 2
  • Loading branch information
sdwright authored Nov 5, 2016
2 parents 7e5e415 + 47ff9db commit ada45cc
Show file tree
Hide file tree
Showing 33 changed files with 1,358 additions and 568 deletions.
24 changes: 20 additions & 4 deletions app/code/Magento/Integration/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Integration\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Integration\Model\Cache\Type;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -55,10 +67,14 @@ public function getIntegrations()
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;
Expand Down
20 changes: 16 additions & 4 deletions app/code/Magento/Integration/Model/ConsolidatedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Integration\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Integration\Model\Cache\TypeConsolidated;

/**
Expand All @@ -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);
}

/**
Expand All @@ -51,11 +63,11 @@ public function getIntegrations()
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]
);
Expand Down
20 changes: 16 additions & 4 deletions app/code/Magento/Integration/Model/IntegrationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -57,11 +69,11 @@ public function getIntegrations()
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]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
{
Expand All @@ -38,41 +44,54 @@ protected function setUp()
$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,
]
);
}

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());
}

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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
{
Expand All @@ -36,37 +42,51 @@ protected function setUp()
$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());
}

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());
}
Expand Down
17 changes: 13 additions & 4 deletions app/code/Magento/Marketplace/Helper/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace Magento\Marketplace\Helper;

use Magento\Framework\Filesystem;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Cache helper
Expand All @@ -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);
}

Expand All @@ -46,7 +55,7 @@ public function loadPartnersFromCache()
{
$data = $this->getCache()->load($this->pathToCacheFile);
if (false !== $data) {
$data = unserialize($data);
$data = $this->serializer->unserialize($data);
}
return $data;
}
Expand All @@ -59,7 +68,7 @@ public function loadPartnersFromCache()
*/
public function savePartnersToCache($partners)
{
return $this->getCache()->save(serialize($partners), $this->pathToCacheFile);
return $this->getCache()->save($this->serializer->serialize($partners), $this->pathToCacheFile);
}

/**
Expand Down
Loading

0 comments on commit ada45cc

Please sign in to comment.