Skip to content

Commit ada45cc

Browse files
authored
Merge pull request #574 from magento-jackalopes/MAGETWO-58680-remove-unserialize-cache-frontend-interface-rebased
Fixed issue: - MAGETWO-58680 Remove uses of unserialize in usages of \Magento\Framework\Cache\FrontendInterface::load() 2
2 parents 7e5e415 + 47ff9db commit ada45cc

File tree

33 files changed

+1358
-568
lines changed

33 files changed

+1358
-568
lines changed

app/code/Magento/Integration/Model/Config.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Integration\Model;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\SerializerInterface;
810
use Magento\Integration\Model\Cache\Type;
911

1012
/**
@@ -34,14 +36,24 @@ class Config
3436
*/
3537
protected $_integrations;
3638

39+
/**
40+
* @var SerializerInterface
41+
*/
42+
private $serializer;
43+
3744
/**
3845
* @param Cache\Type $configCacheType
3946
* @param Config\Reader $configReader
47+
* @param SerializerInterface $serializer
4048
*/
41-
public function __construct(Cache\Type $configCacheType, Config\Reader $configReader)
42-
{
49+
public function __construct(
50+
Cache\Type $configCacheType,
51+
Config\Reader $configReader,
52+
SerializerInterface $serializer = null
53+
) {
4354
$this->_configCacheType = $configCacheType;
4455
$this->_configReader = $configReader;
56+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
4557
}
4658

4759
/**
@@ -55,10 +67,14 @@ public function getIntegrations()
5567
if (null === $this->_integrations) {
5668
$integrations = $this->_configCacheType->load(self::CACHE_ID);
5769
if ($integrations && is_string($integrations)) {
58-
$this->_integrations = unserialize($integrations);
70+
$this->_integrations = $this->serializer->unserialize($integrations);
5971
} else {
6072
$this->_integrations = $this->_configReader->read();
61-
$this->_configCacheType->save(serialize($this->_integrations), self::CACHE_ID, [Type::CACHE_TAG]);
73+
$this->_configCacheType->save(
74+
$this->serializer->serialize($this->_integrations),
75+
self::CACHE_ID,
76+
[Type::CACHE_TAG]
77+
);
6278
}
6379
}
6480
return $this->_integrations;

app/code/Magento/Integration/Model/ConsolidatedConfig.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Integration\Model;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\SerializerInterface;
810
use Magento\Integration\Model\Cache\TypeConsolidated;
911

1012
/**
@@ -31,14 +33,24 @@ class ConsolidatedConfig
3133
*/
3234
protected $integrations;
3335

36+
/**
37+
* @var SerializerInterface
38+
*/
39+
private $serializer;
40+
3441
/**
3542
* @param Cache\TypeConsolidated $configCacheType
3643
* @param Config\Consolidated\Reader $configReader
44+
* @param SerializerInterface $serializer
3745
*/
38-
public function __construct(Cache\TypeConsolidated $configCacheType, Config\Consolidated\Reader $configReader)
39-
{
46+
public function __construct(
47+
Cache\TypeConsolidated $configCacheType,
48+
Config\Consolidated\Reader $configReader,
49+
SerializerInterface $serializer = null
50+
) {
4051
$this->configCacheType = $configCacheType;
4152
$this->configReader = $configReader;
53+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
4254
}
4355

4456
/**
@@ -51,11 +63,11 @@ public function getIntegrations()
5163
if (null === $this->integrations) {
5264
$integrations = $this->configCacheType->load(self::CACHE_ID);
5365
if ($integrations && is_string($integrations)) {
54-
$this->integrations = unserialize($integrations);
66+
$this->integrations = $this->serializer->unserialize($integrations);
5567
} else {
5668
$this->integrations = $this->configReader->read();
5769
$this->configCacheType->save(
58-
serialize($this->integrations),
70+
$this->serializer->serialize($this->integrations),
5971
self::CACHE_ID,
6072
[TypeConsolidated::CACHE_TAG]
6173
);

app/code/Magento/Integration/Model/IntegrationConfig.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Integration\Model;
88

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Serialize\SerializerInterface;
911
use Magento\Integration\Model\Cache\TypeIntegration;
1012
use Magento\Integration\Model\Config\Integration\Reader;
1113

@@ -36,14 +38,24 @@ class IntegrationConfig
3638
*/
3739
protected $_integrations;
3840

41+
/**
42+
* @var SerializerInterface
43+
*/
44+
private $serializer;
45+
3946
/**
4047
* @param TypeIntegration $configCacheType
4148
* @param Reader $configReader
49+
* @param SerializerInterface $serializer
4250
*/
43-
public function __construct(TypeIntegration $configCacheType, Reader $configReader)
44-
{
51+
public function __construct(
52+
TypeIntegration $configCacheType,
53+
Reader $configReader,
54+
SerializerInterface $serializer = null
55+
) {
4556
$this->_configCacheType = $configCacheType;
4657
$this->_configReader = $configReader;
58+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
4759
}
4860

4961
/**
@@ -57,11 +69,11 @@ public function getIntegrations()
5769
if (null === $this->_integrations) {
5870
$integrations = $this->_configCacheType->load(self::CACHE_ID);
5971
if ($integrations && is_string($integrations)) {
60-
$this->_integrations = unserialize($integrations);
72+
$this->_integrations = $this->serializer->unserialize($integrations);
6173
} else {
6274
$this->_integrations = $this->_configReader->read();
6375
$this->_configCacheType->save(
64-
serialize($this->_integrations),
76+
$this->serializer->serialize($this->_integrations),
6577
self::CACHE_ID,
6678
[TypeIntegration::CACHE_TAG]
6779
);

app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Integration\Test\Unit\Model;
77

8+
use Magento\Framework\Serialize\SerializerInterface;
89
use Magento\Integration\Model\ConsolidatedConfig as Config;
910
use Magento\Integration\Model\Cache\TypeConsolidated as Type;
1011

@@ -18,17 +19,22 @@ class ConsolidatedConfigTest extends \PHPUnit_Framework_TestCase
1819
*
1920
* @var Config
2021
*/
21-
protected $configModel;
22+
private $configModel;
2223

2324
/**
2425
* @var Type|\PHPUnit_Framework_MockObject_MockObject
2526
*/
26-
protected $configCacheTypeMock;
27+
private $configCacheTypeMock;
2728

2829
/**
2930
* @var \Magento\Integration\Model\Config\Consolidated\Reader|\PHPUnit_Framework_MockObject_MockObject
3031
*/
31-
protected $configReaderMock;
32+
private $configReaderMock;
33+
34+
/**
35+
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $serializer;
3238

3339
protected function setUp()
3440
{
@@ -38,41 +44,54 @@ protected function setUp()
3844
$this->configReaderMock = $this->getMockBuilder(\Magento\Integration\Model\Config\Consolidated\Reader::class)
3945
->disableOriginalConstructor()
4046
->getMock();
47+
$this->serializer = $this->getMockBuilder(SerializerInterface::class)
48+
->disableOriginalConstructor()
49+
->getMock();
4150
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
4251
$this->configModel = $objectManagerHelper->getObject(
4352
\Magento\Integration\Model\ConsolidatedConfig::class,
4453
[
4554
'configCacheType' => $this->configCacheTypeMock,
46-
'configReader' => $this->configReaderMock
55+
'configReader' => $this->configReaderMock,
56+
'serializer' => $this->serializer,
4757
]
4858
);
4959
}
5060

5161
public function testGetIntegrationsFromConfigCacheType()
5262
{
5363
$integrations = ['foo', 'bar', 'baz'];
64+
$serializedIntegrations = '["foo","bar","baz"]';
5465
$this->configCacheTypeMock->expects($this->once())
5566
->method('load')
5667
->with(Config::CACHE_ID)
57-
->will($this->returnValue(serialize($integrations)));
68+
->will($this->returnValue($serializedIntegrations));
69+
$this->serializer->expects($this->once())
70+
->method('unserialize')
71+
->with($serializedIntegrations)
72+
->willReturn($integrations);
5873

5974
$this->assertEquals($integrations, $this->configModel->getIntegrations());
6075
}
6176

6277
public function testGetIntegrationsFromConfigReader()
6378
{
6479
$integrations = ['foo', 'bar', 'baz'];
80+
$serializedIntegrations = '["foo","bar","baz"]';
6581
$this->configCacheTypeMock->expects($this->once())
6682
->method('load')
6783
->with(Config::CACHE_ID)
6884
->will($this->returnValue(null));
69-
$this->configCacheTypeMock->expects($this->once())
70-
->method('save')
71-
->with(serialize($integrations), Config::CACHE_ID, [Type::CACHE_TAG])
72-
->will($this->returnValue(null));
7385
$this->configReaderMock->expects($this->once())
7486
->method('read')
7587
->will($this->returnValue($integrations));
88+
$this->serializer->expects($this->once())
89+
->method('serialize')
90+
->with($integrations)
91+
->willReturn($serializedIntegrations);
92+
$this->configCacheTypeMock->expects($this->once())
93+
->method('save')
94+
->with($serializedIntegrations, Config::CACHE_ID, [Type::CACHE_TAG]);
7695

7796
$this->assertEquals($integrations, $this->configModel->getIntegrations());
7897
}

app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Integration\Test\Unit\Model;
77

8+
use Magento\Framework\Serialize\SerializerInterface;
89
use Magento\Integration\Model\IntegrationConfig;
910
use Magento\Integration\Model\Cache\TypeIntegration;
1011

@@ -16,17 +17,22 @@ class IntegrationConfigTest extends \PHPUnit_Framework_TestCase
1617
/**
1718
* @var IntegrationConfig
1819
*/
19-
protected $integrationConfigModel;
20+
private $integrationConfigModel;
2021

2122
/**
2223
* @var TypeIntegration|\PHPUnit_Framework_MockObject_MockObject
2324
*/
24-
protected $configCacheTypeMock;
25+
private $configCacheTypeMock;
2526

2627
/**
2728
* @var \Magento\Integration\Model\Config\Integration\Reader|\PHPUnit_Framework_MockObject_MockObject
2829
*/
29-
protected $configReaderMock;
30+
private $configReaderMock;
31+
32+
/**
33+
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $serializer;
3036

3137
protected function setUp()
3238
{
@@ -36,37 +42,51 @@ protected function setUp()
3642
$this->configReaderMock = $this->getMockBuilder(\Magento\Integration\Model\Config\Integration\Reader::class)
3743
->disableOriginalConstructor()
3844
->getMock();
45+
$this->serializer = $this->getMockBuilder(SerializerInterface::class)
46+
->disableOriginalConstructor()
47+
->getMock();
3948
$this->integrationConfigModel = new IntegrationConfig(
4049
$this->configCacheTypeMock,
41-
$this->configReaderMock
50+
$this->configReaderMock,
51+
$this->serializer
4252
);
4353
}
4454

4555
public function testGetIntegrationsFromConfigCacheType()
4656
{
4757
$integrations = ['foo', 'bar', 'baz'];
58+
$serializedIntegrations = '["foo","bar","baz"]';
4859
$this->configCacheTypeMock->expects($this->once())
4960
->method('load')
5061
->with(IntegrationConfig::CACHE_ID)
51-
->will($this->returnValue(serialize($integrations)));
62+
->will($this->returnValue($serializedIntegrations));
63+
$this->serializer->expects($this->once())
64+
->method('unserialize')
65+
->with($serializedIntegrations)
66+
->willReturn($integrations);
5267

5368
$this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations());
5469
}
5570

5671
public function testGetIntegrationsFromConfigReader()
5772
{
5873
$integrations = ['foo', 'bar', 'baz'];
74+
$serializedIntegrations = '["foo","bar","baz"]';
5975
$this->configCacheTypeMock->expects($this->once())
6076
->method('load')
6177
->with(IntegrationConfig::CACHE_ID)
6278
->will($this->returnValue(null));
63-
$this->configCacheTypeMock->expects($this->once())
64-
->method('save')
65-
->with(serialize($integrations), IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG])
66-
->will($this->returnValue(null));
6779
$this->configReaderMock->expects($this->once())
6880
->method('read')
6981
->will($this->returnValue($integrations));
82+
$this->serializer->expects($this->once())
83+
->method('serialize')
84+
->with($integrations)
85+
->willReturn($serializedIntegrations);
86+
$this->configCacheTypeMock->expects($this->once())
87+
->method('save')
88+
->with($serializedIntegrations, IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG])
89+
->will($this->returnValue(null));
7090

7191
$this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations());
7292
}

app/code/Magento/Marketplace/Helper/Cache.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace Magento\Marketplace\Helper;
88

9-
use Magento\Framework\Filesystem;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Serialize\SerializerInterface;
1011

1112
/**
1213
* Cache helper
@@ -25,15 +26,23 @@ class Cache extends \Magento\Framework\App\Helper\AbstractHelper
2526
*/
2627
protected $cache;
2728

29+
/**
30+
* @var SerializerInterface
31+
*/
32+
private $serializer;
33+
2834
/**
2935
* @param \Magento\Framework\App\Helper\Context $context
3036
* @param \Magento\Framework\Config\CacheInterface $cache
37+
* @param SerializerInterface $serializer
3138
*/
3239
public function __construct(
3340
\Magento\Framework\App\Helper\Context $context,
34-
\Magento\Framework\Config\CacheInterface $cache
41+
\Magento\Framework\Config\CacheInterface $cache,
42+
SerializerInterface $serializer = null
3543
) {
3644
$this->cache = $cache;
45+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
3746
parent::__construct($context);
3847
}
3948

@@ -46,7 +55,7 @@ public function loadPartnersFromCache()
4655
{
4756
$data = $this->getCache()->load($this->pathToCacheFile);
4857
if (false !== $data) {
49-
$data = unserialize($data);
58+
$data = $this->serializer->unserialize($data);
5059
}
5160
return $data;
5261
}
@@ -59,7 +68,7 @@ public function loadPartnersFromCache()
5968
*/
6069
public function savePartnersToCache($partners)
6170
{
62-
return $this->getCache()->save(serialize($partners), $this->pathToCacheFile);
71+
return $this->getCache()->save($this->serializer->serialize($partners), $this->pathToCacheFile);
6372
}
6473

6574
/**

0 commit comments

Comments
 (0)