Skip to content
Snippets Groups Projects
Commit a6a7f20f authored by Olga Kopylova's avatar Olga Kopylova
Browse files

MAGETWO-58691: Refactor Module_Quote, Module_Sales

- replaced native functions with SerializerInterface
parent 9dfaec84
Branches
No related merge requests found
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
*/ */
namespace Magento\Quote\Model; namespace Magento\Quote\Model;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Config\CacheInterface; use Magento\Framework\Config\CacheInterface;
use Magento\Framework\App\ResourceConnection\ConfigInterface; use Magento\Framework\App\ResourceConnection\ConfigInterface;
use Magento\Framework\Serialize\SerializerInterface;
class QueryResolver class QueryResolver
{ {
...@@ -36,20 +38,27 @@ class QueryResolver ...@@ -36,20 +38,27 @@ class QueryResolver
* @var array * @var array
*/ */
private $cacheTags = []; private $cacheTags = [];
/**
* @var SerializerInterface
*/
private $serializer;
/** /**
* @param ConfigInterface $config * @param ConfigInterface $config
* @param CacheInterface $cache * @param CacheInterface $cache
* @param string $cacheId * @param string $cacheId
* @param SerializerInterface $serializer
*/ */
public function __construct( public function __construct(
ConfigInterface $config, ConfigInterface $config,
CacheInterface $cache, CacheInterface $cache,
$cacheId = 'connection_config_cache' $cacheId = 'connection_config_cache',
SerializerInterface $serializer = null
) { ) {
$this->config = $config; $this->config = $config;
$this->cache = $cache; $this->cache = $cache;
$this->cacheId = $cacheId; $this->cacheId = $cacheId;
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
} }
/** /**
...@@ -75,9 +84,9 @@ class QueryResolver ...@@ -75,9 +84,9 @@ class QueryResolver
if (false === $data) { if (false === $data) {
$singleQuery = $this->config->getConnectionName('checkout_setup') == 'default' ? true : false; $singleQuery = $this->config->getConnectionName('checkout_setup') == 'default' ? true : false;
$data['checkout'] = $singleQuery; $data['checkout'] = $singleQuery;
$this->cache->save(serialize($data), $this->cacheId, $this->cacheTags); $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags);
} else { } else {
$data = unserialize($data); $data = $this->serializer->unserialize($data);
} }
$this->merge($data); $this->merge($data);
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* See COPYING.txt for license details. * See COPYING.txt for license details.
*/ */
namespace Magento\Quote\Model\Quote\Address\Total; namespace Magento\Quote\Model\Quote\Address\Total;
use Magento\Framework\Serialize\SerializerInterface;
/** /**
* Address Total Collector model * Address Total Collector model
...@@ -69,6 +70,7 @@ class Collector extends \Magento\Sales\Model\Config\Ordered ...@@ -69,6 +70,7 @@ class Collector extends \Magento\Sales\Model\Config\Ordered
* @param \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory * @param \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory
* @param mixed $sourceData * @param mixed $sourceData
* @param mixed $store * @param mixed $store
* @param SerializerInterface $serializer
*/ */
public function __construct( public function __construct(
\Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Framework\App\Cache\Type\Config $configCacheType,
...@@ -78,11 +80,12 @@ class Collector extends \Magento\Sales\Model\Config\Ordered ...@@ -78,11 +80,12 @@ class Collector extends \Magento\Sales\Model\Config\Ordered
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory, \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory,
$sourceData = null, $sourceData = null,
$store = null $store = null,
SerializerInterface $serializer = null
) { ) {
$this->_scopeConfig = $scopeConfig; $this->_scopeConfig = $scopeConfig;
$this->_totalFactory = $totalFactory; $this->_totalFactory = $totalFactory;
parent::__construct($configCacheType, $logger, $salesConfig, $sourceData); parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer);
$this->_store = $store ?: $storeManager->getStore(); $this->_store = $store ?: $storeManager->getStore();
$this->_initModels()->_initCollectors()->_initRetrievers(); $this->_initModels()->_initCollectors()->_initRetrievers();
} }
......
...@@ -6,83 +6,100 @@ ...@@ -6,83 +6,100 @@
namespace Magento\Quote\Test\Unit\Model; namespace Magento\Quote\Test\Unit\Model;
use Magento\Framework\Serialize\SerializerInterface;
class QueryResolverTest extends \PHPUnit_Framework_TestCase class QueryResolverTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var \Magento\Quote\Model\QueryResolver * @var \Magento\Quote\Model\QueryResolver
*/ */
protected $quoteResolver; private $quoteResolver;
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject * @var \PHPUnit_Framework_MockObject_MockObject
*/ */
protected $configMock; private $configMock;
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject * @var \PHPUnit_Framework_MockObject_MockObject
*/ */
protected $cacheMock; private $cacheMock;
/**
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;
protected function setUp() protected function setUp()
{ {
$this->configMock = $this->getMock(\Magento\Framework\App\ResourceConnection\ConfigInterface::class); $this->configMock = $this->getMock(\Magento\Framework\App\ResourceConnection\ConfigInterface::class);
$this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class);
$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
$this->quoteResolver = new \Magento\Quote\Model\QueryResolver( $this->quoteResolver = new \Magento\Quote\Model\QueryResolver(
$this->configMock, $this->configMock,
$this->cacheMock, $this->cacheMock,
'connection_config_cache' 'connection_config_cache',
$this->serializer
); );
} }
public function testIsSingleQueryWhenDataWereCached() public function testIsSingleQueryWhenDataWereCached()
{ {
$queryData['checkout'] = true; $serializedData = '{"checkout":true}';
$data = ['checkout' => true];
$this->cacheMock $this->cacheMock
->expects($this->once()) ->expects($this->once())
->method('load') ->method('load')
->with('connection_config_cache') ->with('connection_config_cache')
->willReturn(serialize($queryData)); ->willReturn($serializedData);
$this->serializer->expects($this->once())
->method('unserialize')
->with($serializedData)
->willReturn($data);
$this->assertTrue($this->quoteResolver->isSingleQuery()); $this->assertTrue($this->quoteResolver->isSingleQuery());
} }
public function testIsSingleQueryWhenDataNotCached() /**
* @param string $connectionName
* @param bool $isSingleQuery
*
* @dataProvider isSingleQueryWhenDataNotCachedDataProvider
*/
public function testIsSingleQueryWhenDataNotCached($connectionName, $isSingleQuery)
{ {
$queryData['checkout'] = true; $data = ['checkout' => $isSingleQuery];
$serializedData = '{"checkout":true}';
$this->cacheMock $this->cacheMock
->expects($this->once()) ->expects($this->once())
->method('load') ->method('load')
->with('connection_config_cache') ->with('connection_config_cache')
->willReturn(false); ->willReturn(false);
$this->serializer->expects($this->never())
->method('unserialize');
$this->configMock $this->configMock
->expects($this->once()) ->expects($this->once())
->method('getConnectionName') ->method('getConnectionName')
->with('checkout_setup') ->with('checkout_setup')
->willReturn('default'); ->willReturn($connectionName);
$this->serializer->expects($this->once())
->method('serialize')
->with($data)
->willReturn($serializedData);
$this->cacheMock $this->cacheMock
->expects($this->once()) ->expects($this->once())
->method('save') ->method('save')
->with(serialize($queryData), 'connection_config_cache', []); ->with($serializedData, 'connection_config_cache', []);
$this->assertTrue($this->quoteResolver->isSingleQuery()); $this->assertEquals($isSingleQuery, $this->quoteResolver->isSingleQuery());
} }
public function testIsSingleQueryWhenSeveralConnectionsExist() /**
* @return array
*/
public function isSingleQueryWhenDataNotCachedDataProvider()
{ {
$queryData['checkout'] = false; return [
$this->cacheMock ['default', true],
->expects($this->once()) ['checkout', false],
->method('load') ];
->with('connection_config_cache')
->willReturn(false);
$this->configMock
->expects($this->once())
->method('getConnectionName')
->with('checkout_setup')
->willReturn('checkout');
$this->cacheMock
->expects($this->once())
->method('save')
->with(serialize($queryData), 'connection_config_cache', []);
$this->assertFalse($this->quoteResolver->isSingleQuery());
} }
} }
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
* See COPYING.txt for license details. * See COPYING.txt for license details.
*/ */
namespace Magento\Sales\Model\Config; namespace Magento\Sales\Model\Config;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\SerializerInterface;
/** /**
* Configuration class for ordered items * Configuration class for ordered items
...@@ -68,23 +70,30 @@ abstract class Ordered extends \Magento\Framework\App\Config\Base ...@@ -68,23 +70,30 @@ abstract class Ordered extends \Magento\Framework\App\Config\Base
* @var \Magento\Sales\Model\Config * @var \Magento\Sales\Model\Config
*/ */
protected $_salesConfig; protected $_salesConfig;
/**
* @var SerializerInterface
*/
private $serializer;
/** /**
* @param \Magento\Framework\App\Cache\Type\Config $configCacheType * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
* @param \Psr\Log\LoggerInterface $logger * @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Sales\Model\Config $salesConfig * @param \Magento\Sales\Model\Config $salesConfig
* @param \Magento\Framework\Simplexml\Element $sourceData * @param \Magento\Framework\Simplexml\Element $sourceData
* @param SerializerInterface $serializer
*/ */
public function __construct( public function __construct(
\Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Framework\App\Cache\Type\Config $configCacheType,
\Psr\Log\LoggerInterface $logger, \Psr\Log\LoggerInterface $logger,
\Magento\Sales\Model\Config $salesConfig, \Magento\Sales\Model\Config $salesConfig,
$sourceData = null $sourceData = null,
SerializerInterface $serializer = null
) { ) {
parent::__construct($sourceData); parent::__construct($sourceData);
$this->_configCacheType = $configCacheType; $this->_configCacheType = $configCacheType;
$this->_logger = $logger; $this->_logger = $logger;
$this->_salesConfig = $salesConfig; $this->_salesConfig = $salesConfig;
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
} }
/** /**
...@@ -179,11 +188,11 @@ abstract class Ordered extends \Magento\Framework\App\Config\Base ...@@ -179,11 +188,11 @@ abstract class Ordered extends \Magento\Framework\App\Config\Base
$sortedCodes = []; $sortedCodes = [];
$cachedData = $this->_configCacheType->load($this->_collectorsCacheKey); $cachedData = $this->_configCacheType->load($this->_collectorsCacheKey);
if ($cachedData) { if ($cachedData) {
$sortedCodes = unserialize($cachedData); $sortedCodes = $this->serializer->unserialize($cachedData);
} }
if (!$sortedCodes) { if (!$sortedCodes) {
$sortedCodes = $this->_getSortedCollectorCodes($this->_modelsConfig); $sortedCodes = $this->_getSortedCollectorCodes($this->_modelsConfig);
$this->_configCacheType->save(serialize($sortedCodes), $this->_collectorsCacheKey); $this->_configCacheType->save($this->serializer->serialize($sortedCodes), $this->_collectorsCacheKey);
} }
foreach ($sortedCodes as $code) { foreach ($sortedCodes as $code) {
$this->_collectors[$code] = $this->_models[$code]; $this->_collectors[$code] = $this->_models[$code];
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* See COPYING.txt for license details. * See COPYING.txt for license details.
*/ */
namespace Magento\Sales\Model\Order\Total\Config; namespace Magento\Sales\Model\Order\Total\Config;
use Magento\Framework\Serialize\SerializerInterface;
/** /**
* Configuration class for totals * Configuration class for totals
...@@ -42,15 +43,17 @@ class Base extends \Magento\Sales\Model\Config\Ordered ...@@ -42,15 +43,17 @@ class Base extends \Magento\Sales\Model\Config\Ordered
* @param \Magento\Sales\Model\Config $salesConfig * @param \Magento\Sales\Model\Config $salesConfig
* @param \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory * @param \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory
* @param mixed $sourceData * @param mixed $sourceData
* @param SerializerInterface $serializer
*/ */
public function __construct( public function __construct(
\Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Framework\App\Cache\Type\Config $configCacheType,
\Psr\Log\LoggerInterface $logger, \Psr\Log\LoggerInterface $logger,
\Magento\Sales\Model\Config $salesConfig, \Magento\Sales\Model\Config $salesConfig,
\Magento\Sales\Model\Order\TotalFactory $orderTotalFactory, \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory,
$sourceData = null $sourceData = null,
SerializerInterface $serializer = null
) { ) {
parent::__construct($configCacheType, $logger, $salesConfig, $sourceData); parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer);
$this->_orderTotalFactory = $orderTotalFactory; $this->_orderTotalFactory = $orderTotalFactory;
} }
......
...@@ -5,24 +5,28 @@ ...@@ -5,24 +5,28 @@
*/ */
namespace Magento\Sales\Test\Unit\Model\Order\Total\Config; namespace Magento\Sales\Test\Unit\Model\Order\Total\Config;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
class BaseTest extends \PHPUnit_Framework_TestCase class BaseTest extends \PHPUnit_Framework_TestCase
{ {
/** @var \Magento\Sales\Model\Order\Total\Config\Base */ /** @var \Magento\Sales\Model\Order\Total\Config\Base */
protected $object; private $object;
/** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */
private $serializer;
/** @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ /** @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */
protected $configCacheType; private $configCacheType;
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $logger; private $logger;
/** @var \Magento\Sales\Model\Config|\PHPUnit_Framework_MockObject_MockObject */ /** @var \Magento\Sales\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
protected $salesConfig; private $salesConfig;
/** @var \Magento\Sales\Model\Order\TotalFactory|\PHPUnit_Framework_MockObject_MockObject */ /** @var \Magento\Sales\Model\Order\TotalFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $orderTotalFactory; private $orderTotalFactory;
protected function setUp() protected function setUp()
{ {
...@@ -30,6 +34,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase ...@@ -30,6 +34,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->logger = $this->getMock(\Psr\Log\LoggerInterface::class); $this->logger = $this->getMock(\Psr\Log\LoggerInterface::class);
$this->salesConfig = $this->getMock(\Magento\Sales\Model\Config::class, [], [], '', false); $this->salesConfig = $this->getMock(\Magento\Sales\Model\Config::class, [], [], '', false);
$this->orderTotalFactory = $this->getMock(\Magento\Sales\Model\Order\TotalFactory::class, [], [], '', false); $this->orderTotalFactory = $this->getMock(\Magento\Sales\Model\Order\TotalFactory::class, [], [], '', false);
$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
$objectManager = new ObjectManager($this); $objectManager = new ObjectManager($this);
$this->object = $objectManager->getObject( $this->object = $objectManager->getObject(
...@@ -39,6 +44,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase ...@@ -39,6 +44,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
'logger' => $this->logger, 'logger' => $this->logger,
'salesConfig' => $this->salesConfig, 'salesConfig' => $this->salesConfig,
'orderTotalFactory' => $this->orderTotalFactory, 'orderTotalFactory' => $this->orderTotalFactory,
'serializer' => $this->serializer,
] ]
); );
} }
...@@ -59,8 +65,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase ...@@ -59,8 +65,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase
->with(\Magento\Sales\Model\Order\Total\AbstractTotal::class) ->with(\Magento\Sales\Model\Order\Total\AbstractTotal::class)
->will($this->returnValue($total)); ->will($this->returnValue($total));
$sortedCodes = ['other_code', 'some_code'];
$serializedCodes = '["other_code", "some_code"]';
$this->serializer->expects($this->once())
->method('serialize')
->with($sortedCodes)
->willReturn($serializedCodes);
$this->configCacheType->expects($this->once())->method('save') $this->configCacheType->expects($this->once())->method('save')
->with('a:2:{i:0;s:10:"other_code";i:1;s:9:"some_code";}', 'sorted_collectors'); ->with($serializedCodes, 'sorted_collectors');
$this->assertSame( $this->assertSame(
['other_code' => $total, 'some_code' => $total], ['other_code' => $total, 'some_code' => $total],
...@@ -106,8 +118,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase ...@@ -106,8 +118,14 @@ class BaseTest extends \PHPUnit_Framework_TestCase
->with(\Magento\Sales\Model\Order\Total\AbstractTotal::class) ->with(\Magento\Sales\Model\Order\Total\AbstractTotal::class)
->will($this->returnValue($total)); ->will($this->returnValue($total));
$sortedCodes = ['other_code', 'some_code'];
$serializedCodes = '["other_code", "some_code"]';
$this->configCacheType->expects($this->once())->method('load')->with('sorted_collectors') $this->configCacheType->expects($this->once())->method('load')->with('sorted_collectors')
->will($this->returnValue('a:2:{i:0;s:10:"other_code";i:1;s:9:"some_code";}')); ->will($this->returnValue($serializedCodes));
$this->serializer->expects($this->once())
->method('unserialize')
->with($serializedCodes)
->willReturn($sortedCodes);
$this->configCacheType->expects($this->never())->method('save'); $this->configCacheType->expects($this->never())->method('save');
$this->assertSame( $this->assertSame(
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment