From ca3c72b1d7bc8e3d0bdef68d62a5bd3786ec1a37 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 09:41:16 -0500 Subject: [PATCH 001/144] MAGETWO-58640: Refactor Framework_Translate --- .../Magento/Framework/TranslateTest.php | 61 +++++++++++++------ .../Framework/Test/Unit/TranslateTest.php | 20 +++++- lib/internal/Magento/Framework/Translate.php | 24 +++++++- 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php b/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php index 2af602ff91850..277716e63ac8f 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php @@ -8,11 +8,16 @@ use Magento\TestFramework\Helper\Bootstrap; /** - * @magentoCache all disabled * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class TranslateTest extends \PHPUnit_Framework_TestCase { + /** @var \Magento\Framework\Translate */ + private $translate; + + /** @var \Magento\TestFramework\ObjectManager */ + private $objectManager; + protected function setUp() { /** @var \Magento\Framework\View\FileSystem $viewFileSystem */ @@ -36,11 +41,11 @@ protected function setUp() $viewFileSystem->expects($this->any())->method('getDesignTheme')->will($this->returnValue($theme)); - $objectManager = Bootstrap::getObjectManager(); - $objectManager->addSharedInstance($viewFileSystem, \Magento\Framework\View\FileSystem::class); + $this->objectManager = Bootstrap::getObjectManager(); + $this->objectManager->addSharedInstance($viewFileSystem, \Magento\Framework\View\FileSystem::class); /** @var $moduleReader \Magento\Framework\Module\Dir\Reader */ - $moduleReader = $objectManager->get(\Magento\Framework\Module\Dir\Reader::class); + $moduleReader = $this->objectManager->get(\Magento\Framework\Module\Dir\Reader::class); $moduleReader->setModuleDir( 'Magento_Store', 'i18n', @@ -57,33 +62,45 @@ protected function setUp() \Magento\Theme\Model\View\Design::class, ['getDesignTheme'], [ - $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class), - $objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class), - $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class), - $objectManager->get(\Magento\Theme\Model\ThemeFactory::class), - $objectManager->get(\Magento\Framework\ObjectManagerInterface::class), - $objectManager->get(\Magento\Framework\App\State::class), + $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class), + $this->objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class), + $this->objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class), + $this->objectManager->get(\Magento\Theme\Model\ThemeFactory::class), + $this->objectManager->get(\Magento\Framework\ObjectManagerInterface::class), + $this->objectManager->get(\Magento\Framework\App\State::class), ['frontend' => 'Test/default'] ] ); $designModel->expects($this->any())->method('getDesignTheme')->will($this->returnValue($theme)); - $objectManager->addSharedInstance($designModel, \Magento\Theme\Model\View\Design\Proxy::class); + $this->objectManager->addSharedInstance($designModel, \Magento\Theme\Model\View\Design\Proxy::class); - $model = $objectManager->create(\Magento\Framework\Translate::class); - $objectManager->addSharedInstance($model, \Magento\Framework\Translate::class); - $objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Composite::class); - $objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class); - \Magento\Framework\Phrase::setRenderer($objectManager->get(\Magento\Framework\Phrase\RendererInterface::class)); - $model->loadData(\Magento\Framework\App\Area::AREA_FRONTEND); + $this->translate = $this->objectManager->create(\Magento\Framework\Translate::class); + $this->objectManager->addSharedInstance($this->translate, \Magento\Framework\Translate::class); + $this->objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Composite::class); + $this->objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class); + \Magento\Framework\Phrase::setRenderer( + $this->objectManager->get(\Magento\Framework\Phrase\RendererInterface::class) + ); + } + + public function testLoadData() + { + $data = $this->translate->loadData(null, true)->getData(); + $this->cleanAllCache(); + $this->translate->loadData()->getData(); + $dataCached = $this->translate->loadData()->getData(); + $this->assertEquals($data, $dataCached); } /** + * @magentoCache all disabled * @dataProvider translateDataProvider */ public function testTranslate($inputText, $expectedTranslation) { + $this->translate->loadData(\Magento\Framework\App\Area::AREA_FRONTEND); $actualTranslation = new \Magento\Framework\Phrase($inputText); $this->assertEquals($expectedTranslation, $actualTranslation); } @@ -100,4 +117,14 @@ public function translateDataProvider() ['Design value to translate', 'Design translated value'] ]; } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } } diff --git a/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php b/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php index 8d1692dd9cdc1..178d0e20629db 100644 --- a/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php +++ b/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\Test\Unit; +use Magento\Framework\Json\JsonInterface; use \Magento\Framework\Translate; /** @@ -104,6 +105,21 @@ protected function setUp() $this->csvParser, $this->packDictionary ); + + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->setBackwardCompatibleProperty( + $this->translate, + 'json', + $jsonMock + ); } /** @@ -119,7 +135,7 @@ public function testLoadData($area, $forceReload, $cachedData) $this->cache->expects($this->exactly($forceReload ? 0 : 1)) ->method('load') - ->will($this->returnValue(serialize($cachedData))); + ->will($this->returnValue(\Zend_Json::encode($cachedData))); if (!$forceReload && $cachedData !== false) { $this->translate->loadData($area, $forceReload); @@ -222,7 +238,7 @@ public function testGetData($data, $result) { $this->cache->expects($this->once()) ->method('load') - ->will($this->returnValue(serialize($data))); + ->will($this->returnValue(\Zend_Json::encode($data))); $this->expectsSetConfig('themeId'); $this->translate->loadData('frontend'); $this->assertEquals($result, $this->translate->getData()); diff --git a/lib/internal/Magento/Framework/Translate.php b/lib/internal/Magento/Framework/Translate.php index 36f693270f42f..8e9b47487ff3c 100644 --- a/lib/internal/Magento/Framework/Translate.php +++ b/lib/internal/Magento/Framework/Translate.php @@ -108,6 +108,11 @@ class Translate implements \Magento\Framework\TranslateInterface */ protected $packDictionary; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $json; + /** * @param \Magento\Framework\View\DesignInterface $viewDesign * @param \Magento\Framework\Cache\FrontendInterface $cache @@ -474,7 +479,7 @@ protected function _loadCache() { $data = $this->_cache->load($this->getCacheId()); if ($data) { - $data = unserialize($data); + $data = $this->getJson()->decode($data); } return $data; } @@ -486,7 +491,22 @@ protected function _loadCache() */ protected function _saveCache() { - $this->_cache->save(serialize($this->getData()), $this->getCacheId(true), [], false); + $this->_cache->save($this->getJson()->encode($this->getData()), $this->getCacheId(true), [], false); return $this; } + + /** + * Get json encoder/decoder + * + * @return \Magento\Framework\Json\JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Json\JsonInterface::class); + } + return $this->json; + } } From 930203c5d0c8f8b7083b99327d523b1941e9408f Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 09:54:17 -0500 Subject: [PATCH 002/144] MAGETWO-58644: Refactor Module_Catalog --- .../Attribute/Source/Countryofmanufacture.php | 24 ++++++++++- .../Source/CountryofmanufactureTest.php | 37 ++++++++++++---- .../Source/CountryofmanufactureTest.php | 43 +++++++++++++++++++ 3 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php b/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php index 7b45d162b5e98..dc7189b54c073 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php @@ -35,6 +35,11 @@ class Countryofmanufacture extends AbstractSource implements OptionSourceInterfa */ protected $_countryFactory; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $json; + /** * Construct * @@ -61,15 +66,30 @@ public function getAllOptions() { $cacheKey = 'COUNTRYOFMANUFACTURE_SELECT_STORE_' . $this->_storeManager->getStore()->getCode(); if ($cache = $this->_configCacheType->load($cacheKey)) { - $options = unserialize($cache); + $options = $this->getJson()->decode($cache); } else { /** @var \Magento\Directory\Model\Country $country */ $country = $this->_countryFactory->create(); /** @var \Magento\Directory\Model\ResourceModel\Country\Collection $collection */ $collection = $country->getResourceCollection(); $options = $collection->load()->toOptionArray(); - $this->_configCacheType->save(serialize($options), $cacheKey); + $this->_configCacheType->save($this->getJson()->encode($options), $cacheKey); } return $options; } + + /** + * Get json encoder/decoder + * + * @return \Magento\Framework\Json\JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Json\JsonInterface::class); + } + return $this->json; + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php index 2888a0a84f230..483897f69419f 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Source; +use Magento\Framework\Json\JsonInterface; + class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase { /** @@ -27,12 +29,36 @@ class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase */ protected $objectManagerHelper; + /** @var \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture */ + private $countryOfManufacture; protected function setUp() { $this->storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); $this->storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false); $this->cacheConfig = $this->getMock(\Magento\Framework\App\Cache\Type\Config::class, [], [], '', false); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->countryOfManufacture = $this->objectManagerHelper->getObject( + \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture::class, + [ + 'storeManager' => $this->storeManagerMock, + 'configCacheType' => $this->cacheConfig, + ] + ); + + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $this->objectManagerHelper->setBackwardCompatibleProperty( + $this->countryOfManufacture, + 'json', + $jsonMock + ); } /** @@ -52,14 +78,7 @@ public function testGetAllOptions($cachedDataSrl, $cachedDataUnsrl) ->with($this->equalTo('COUNTRYOFMANUFACTURE_SELECT_STORE_store_code')) ->will($this->returnValue($cachedDataSrl)); - $countryOfManufacture = $this->objectManagerHelper->getObject( - \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture::class, - [ - 'storeManager' => $this->storeManagerMock, - 'configCacheType' => $this->cacheConfig, - ] - ); - $this->assertEquals($cachedDataUnsrl, $countryOfManufacture->getAllOptions()); + $this->assertEquals($cachedDataUnsrl, $this->countryOfManufacture->getAllOptions()); } /** @@ -71,7 +90,7 @@ public function testGetAllOptionsDataProvider() { return [ - ['cachedDataSrl' => 'a:1:{s:3:"key";s:4:"data";}', 'cachedDataUnsrl' => ['key' => 'data']] + ['cachedDataSrl' => json_encode(['key' => 'data']), 'cachedDataUnsrl' => ['key' => 'data']] ]; } } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php new file mode 100644 index 0000000000000..ed33a6ff6da82 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php @@ -0,0 +1,43 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->model = $this->objectManager->create( + \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture::class + ); + } + + public function testGetAllOptions() + { + $this->cleanAllCache(); + $allOptions = $this->model->getAllOptions(); + $cachedAllOptions = $this->model->getAllOptions(); + $this->assertEquals($allOptions, $cachedAllOptions); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} From 59c3dd69284183295c094f6d50db21667b9bf89a Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 20 Sep 2016 17:10:18 -0500 Subject: [PATCH 003/144] MAGETWO-58639: Refactor Framework_View - replaced unserialize with Json encode/decode - implemented integration test for UI configuration templates --- .../Config/Provider/TemplateTest.php | 77 +++++++++++++++++++ .../_files/UiComponent/expected/config.xml | 16 ++++ .../Magento_Catalog/ui_component/test.xml | 14 ++++ .../Magento_Customer/ui_component/test.xml | 14 ++++ .../_files/UiComponent/theme/registration.php | 11 +++ .../View/_files/UiComponent/theme/theme.xml | 9 +++ .../Config/Provider/Component/Definition.php | 4 +- .../UiComponent/Config/Provider/Template.php | 4 +- 8 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml create mode 100644 dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Catalog/ui_component/test.xml create mode 100644 dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Customer/ui_component/test.xml create mode 100644 dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/registration.php create mode 100644 dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/theme.xml diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php new file mode 100644 index 0000000000000..a03c63374777d --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php @@ -0,0 +1,77 @@ +objectManager = Bootstrap::getObjectManager(); + $this->registerThemes(); + $this->model = $this->objectManager->create( + \Magento\Framework\View\Element\UiComponent\Config\Provider\Template::class + ); + } + + public function testGetTemplate() + { + $expected = file_get_contents(__DIR__ . '/../../../../_files/UiComponent/expected/config.xml'); + + \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('adminhtml'); + $this->objectManager->get(\Magento\Framework\View\DesignInterface::class) + ->setDesignTheme('FrameworkViewUiComponent/default'); + $this->cleanCache(); + + $resultOne = $this->model->getTemplate('test.xml'); + $resultTwo = $this->model->getTemplate('test.xml'); + + $this->assertEquals($expected, $resultOne); + $this->assertEquals($expected, $resultTwo); + } + + /** + * Clean application cache + */ + protected function cleanCache() + { + /** @var Pool $cachePool */ + $cachePool = $this->objectManager->get(Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } + + /** + * Register themes in the fixture folder + */ + protected function registerThemes() + { + /** @var \Magento\Theme\Model\Theme\Registration $registration */ + $registration = $this->objectManager->get( + \Magento\Theme\Model\Theme\Registration::class + ); + $registration->register(); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml new file mode 100644 index 0000000000000..7df98a51fe0b3 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml @@ -0,0 +1,16 @@ + + +
+ + + Magento\Catalog\Block\Adminhtml\Product\Edit\Button\CreateCategory + + + new_category_form.new_category_form_data_source + +
diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Catalog/ui_component/test.xml b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Catalog/ui_component/test.xml new file mode 100644 index 0000000000000..e0bd296bfda2b --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Catalog/ui_component/test.xml @@ -0,0 +1,14 @@ + + +
+ + + new_category_form.new_category_form_data_source + + +
diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Customer/ui_component/test.xml b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Customer/ui_component/test.xml new file mode 100644 index 0000000000000..c1e0d46aee53e --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/Magento_Customer/ui_component/test.xml @@ -0,0 +1,14 @@ + + +
+ + + Magento\Catalog\Block\Adminhtml\Product\Edit\Button\CreateCategory + + +
diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/registration.php b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/registration.php new file mode 100644 index 0000000000000..42145b38953bd --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/theme/registration.php @@ -0,0 +1,11 @@ + + + Test theme + diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php index 1980da578c248..8c40a64de6e0f 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php @@ -56,9 +56,9 @@ public function __construct( $cachedData = $this->cache->load(static::CACHE_ID); if ($cachedData === false) { $data = $uiReader->read(); - $this->cache->save(serialize($data), static::CACHE_ID); + $this->cache->save(\Zend_Json::encode($data), static::CACHE_ID); } else { - $data = unserialize($cachedData); + $data = \Zend_Json::decode($cachedData); } $this->prepareComponentData($data); } diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php index 79d185b2b26a7..eae97179ff555 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php @@ -81,7 +81,7 @@ public function __construct( $this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory; $cachedTemplates = $this->cache->load(static::CACHE_ID); - $this->cachedTemplates = $cachedTemplates === false ? [] : unserialize($cachedTemplates); + $this->cachedTemplates = $cachedTemplates === false ? [] : \Zend_Json::decode($cachedTemplates); } /** @@ -104,7 +104,7 @@ public function getTemplate($template) 'domMerger' => $this->domMerger ] )->getContent(); - $this->cache->save(serialize($this->cachedTemplates), static::CACHE_ID); + $this->cache->save(\Zend_Json::encode($this->cachedTemplates), static::CACHE_ID); return $this->cachedTemplates[$hash]; } From 8c8133f223e62bc24d4fa129efff91786210368e Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 10:33:07 -0500 Subject: [PATCH 004/144] MAGETWO-58639: Refactor Framework_View - replaced unserialize with json class --- .../Config/Provider/Component/Definition.php | 24 +++++++++++++++++-- .../UiComponent/Config/Provider/Template.php | 24 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php index 8c40a64de6e0f..7c719f1f8bc51 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php @@ -39,6 +39,11 @@ class Definition */ protected $componentData; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $json; + /** * Constructor * @@ -56,9 +61,9 @@ public function __construct( $cachedData = $this->cache->load(static::CACHE_ID); if ($cachedData === false) { $data = $uiReader->read(); - $this->cache->save(\Zend_Json::encode($data), static::CACHE_ID); + $this->cache->save($this->getJson()->encode($data), static::CACHE_ID); } else { - $data = \Zend_Json::decode($cachedData); + $data = $this->getJson()->decode($cachedData); } $this->prepareComponentData($data); } @@ -109,4 +114,19 @@ protected function prepareComponentData(array $componentsData) $this->setComponentData($name, reset($data)); } } + + /** + * Get json encoder/decoder + * + * @return \Magento\Framework\Json\JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Json\JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php index eae97179ff555..631c24aaa6e3f 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php @@ -58,6 +58,11 @@ class Template */ protected $cachedTemplates = []; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $json; + /** * Constructor * @@ -81,7 +86,7 @@ public function __construct( $this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory; $cachedTemplates = $this->cache->load(static::CACHE_ID); - $this->cachedTemplates = $cachedTemplates === false ? [] : \Zend_Json::decode($cachedTemplates); + $this->cachedTemplates = $cachedTemplates === false ? [] : $this->getJson()->decode($cachedTemplates); } /** @@ -104,8 +109,23 @@ public function getTemplate($template) 'domMerger' => $this->domMerger ] )->getContent(); - $this->cache->save(\Zend_Json::encode($this->cachedTemplates), static::CACHE_ID); + $this->cache->save($this->getJson()->encode($this->cachedTemplates), static::CACHE_ID); return $this->cachedTemplates[$hash]; } + + /** + * Get json encoder/decoder + * + * @return \Magento\Framework\Json\JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Json\JsonInterface::class); + } + return $this->json; + } } From f2ba8f899ee9cd7d09d79fdb4177a8d12c7db6a2 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 20 Sep 2016 12:03:37 -0500 Subject: [PATCH 005/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Replacing serialize/unserialize with json_encode/json_decode (cherry picked from commit ac2fd27) --- lib/internal/Magento/Framework/App/Config/Initial.php | 4 ++-- lib/internal/Magento/Framework/App/Config/ScopePool.php | 4 ++-- lib/internal/Magento/Framework/App/Route/Config.php | 4 ++-- lib/internal/Magento/Framework/App/Router/ActionList.php | 4 ++-- .../Magento/Framework/App/Test/Unit/Config/InitialTest.php | 2 +- .../Framework/App/Test/Unit/Config/ScopePoolTest.php | 4 ++-- .../Framework/App/Test/Unit/Router/ActionListTest.php | 2 +- lib/internal/Magento/Framework/Config/Data.php | 4 ++-- lib/internal/Magento/Framework/Config/Data/Scoped.php | 4 ++-- .../Magento/Framework/Config/Test/Unit/Data/ScopedTest.php | 6 +++--- .../Magento/Framework/Config/Test/Unit/DataTest.php | 2 +- 11 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Config/Initial.php b/lib/internal/Magento/Framework/App/Config/Initial.php index 0704fb8559396..85deca02f9131 100644 --- a/lib/internal/Magento/Framework/App/Config/Initial.php +++ b/lib/internal/Magento/Framework/App/Config/Initial.php @@ -41,9 +41,9 @@ public function __construct( $data = $cache->load(self::CACHE_ID); if (!$data) { $data = $reader->read(); - $cache->save(serialize($data), self::CACHE_ID); + $cache->save(\Zend_Json::encode($data), self::CACHE_ID); } else { - $data = unserialize($data); + $data = \Zend_Json::decode($data); } $this->_data = $data['data']; $this->_metadata = $data['metadata']; diff --git a/lib/internal/Magento/Framework/App/Config/ScopePool.php b/lib/internal/Magento/Framework/App/Config/ScopePool.php index 9e6a47d918f76..65fc09babc075 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -105,7 +105,7 @@ public function getScope($scopeType, $scopeCode = null) $data = $this->_cache->load($cacheKey); if ($data) { - $data = unserialize($data); + $data = \Zend_Json::decode($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { @@ -113,7 +113,7 @@ public function getScope($scopeType, $scopeCode = null) } else { $data = $reader->read($scopeCode); } - $this->_cache->save(serialize($data), $cacheKey, [self::CACHE_TAG]); + $this->_cache->save(\Zend_Json::encode($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index 70968c84d77fc..32da1fd335199 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -73,7 +73,7 @@ protected function _getRoutes($scope = null) return $this->_routes[$scope]; } $cacheId = $scope . '::' . $this->_cacheId; - $cachedRoutes = unserialize($this->_cache->load($cacheId)); + $cachedRoutes = \Zend_Json::decode($this->_cache->load($cacheId)); if (is_array($cachedRoutes)) { $this->_routes[$scope] = $cachedRoutes; return $cachedRoutes; @@ -81,7 +81,7 @@ protected function _getRoutes($scope = null) $routers = $this->_reader->read($scope); $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes']; - $this->_cache->save(serialize($routes), $cacheId); + $this->_cache->save(\Zend_Json::encode($routes), $cacheId); $this->_routes[$scope] = $routes; return $routes; } diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index 11ee22a5f375e..86d435b0f276e 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -54,9 +54,9 @@ public function __construct( $data = $cache->load($cacheKey); if (!$data) { $this->actions = $moduleReader->getActionFiles(); - $cache->save(serialize($this->actions), $cacheKey); + $cache->save(\Zend_Json::encode($this->actions), $cacheKey); } else { - $this->actions = unserialize($data); + $this->actions = \Zend_Json::decode($data); } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index ce85753bedab5..6b48498d726de 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -28,7 +28,7 @@ protected function setUp() $this->getMock(\Magento\Framework\App\Config\Initial\Reader::class, [], [], '', false); $this->_configCacheMock = $this->getMock(\Magento\Framework\App\Cache\Type\Config::class, [], [], '', false); - $serializedData = serialize( + $serializedData = \Zend_Json::encode( [ 'data' => [ 'default' => ['key' => 'default_value'], 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 6c9ae8a09a77d..9f21cf50bd663 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -112,7 +112,7 @@ public function testGetScope($scopeType, $scope, array $data, $cachedData) )->method( 'save' )->with( - serialize($data), + \Zend_Json::encode($data), $cacheKey, [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] ); @@ -148,7 +148,7 @@ public function getScopeDataProvider() $baseScope->expects($this->any())->method('getCode')->will($this->returnValue('testScope')); return [ ['scopeType1', 'testScope', ['key' => 'value'], null], - ['scopeType2', 'testScope', ['key' => 'value'], serialize(['key' => 'value'])], + ['scopeType2', 'testScope', ['key' => 'value'], \Zend_Json::encode(['key' => 'value'])], ['scopeType1', $baseScope, ['key' => 'value'], null] ]; } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index c24c31e282c62..4db8221e9a42d 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -44,7 +44,7 @@ public function testConstructorCachedData() { $this->cacheMock->expects($this->once()) ->method('load') - ->will($this->returnValue(serialize('data'))); + ->will($this->returnValue(\Zend_Json::encode('data'))); $this->cacheMock->expects($this->never()) ->method('save'); $this->moduleReaderMock->expects($this->never()) diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index ed5ed3ad9f2e2..c7c4b2db45bf3 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -89,9 +89,9 @@ protected function initData() $data = $this->cache->load($this->cacheId); if (false === $data) { $data = $this->reader->read(); - $this->cache->save(serialize($data), $this->cacheId, $this->cacheTags); + $this->cache->save(\Zend_Json::encode($data), $this->cacheId, $this->cacheTags); } else { - $data = unserialize($data); + $data = \Zend_Json::decode($data); } $this->merge($data); } diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 36b265ac9e6f4..f239308159eed 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -98,11 +98,11 @@ protected function _loadScopedData() if (false == isset($this->_loadedScopes[$scopeCode])) { if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId)) ) { - $data = unserialize($data); + $data = \Zend_Json::decode($data); } else { $data = $this->_reader->read($scopeCode); if ($scopeCode !== 'primary') { - $this->_cache->save(serialize($data), $scopeCode . '::' . $this->_cacheId); + $this->_cache->save(\Zend_Json::encode($data), $scopeCode . '::' . $this->_cacheId); } } $this->merge($data); 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 496da72ceef9b..b3c85b1b97bba 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -55,7 +55,7 @@ public function testgetConfigByPath($path, $expectedValue, $default) 'key_1.2' => ['some' => 'arrayValue'], ], ]; - $this->_cacheMock->expects($this->any())->method('load')->will($this->returnValue(serialize([]))); + $this->_cacheMock->expects($this->any())->method('load')->will($this->returnValue(\Zend_Json::encode([]))); $this->_model->merge($testData); $this->assertEquals($expectedValue, $this->_model->get($path, $default)); } @@ -110,7 +110,7 @@ public function testGetScopeSwitchingWithNonCachedData() ); /** test cache saving */ - $this->_cacheMock->expects($this->once())->method('save')->with(serialize($testValue), 'adminhtml::tag'); + $this->_cacheMock->expects($this->once())->method('save')->with(\Zend_Json::encode($testValue), 'adminhtml::tag'); /** test config value existence */ $this->assertEquals('testValue', $this->_model->get('some')); @@ -140,7 +140,7 @@ public function testGetScopeSwitchingWithCachedData() )->with( 'adminhtml::tag' )->will( - $this->returnValue(serialize($testValue)) + $this->returnValue(\Zend_Json::encode($testValue)) ); /** test preventing of getting data from reader */ diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index 5c58310e096bb..d77a1d96b79be 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -47,7 +47,7 @@ public function testGet() public function testReset() { $cacheid = 'test'; - $this->cache->expects($this->once())->method('load')->will($this->returnValue(serialize([]))); + $this->cache->expects($this->once())->method('load')->will($this->returnValue(\Zend_Json::encode([]))); $this->cache->expects($this->once())->method('remove')->with($cacheid); $config = new \Magento\Framework\Config\Data( From 0a7416b40381b8c22f6b5136d3e964b79530a6db Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 20 Sep 2016 16:51:53 -0500 Subject: [PATCH 006/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Replacing serialize/unserialize with json_encode/json_decode (cherry picked from commit 7c85ad7) --- .../Framework/App/Config/InitialTest.php | 65 +++++++++++++++++ .../Framework/App/Config/ScopePoolTest.php | 49 +++++++++++++ .../Framework/App/Route/ConfigTest.php | 63 +++++++++++++++++ .../View/Layout/Reader/BlockTest.php | 0 .../Framework/View/LayoutDirectivesTest.php | 0 .../App/Test/Unit/Route/ConfigTest.php | 12 ++-- .../Framework/Config/Test/Unit/DataTest.php | 70 +++++++++++++------ 7 files changed, 233 insertions(+), 26 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php mode change 100755 => 100644 dev/tests/integration/testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php mode change 100755 => 100644 dev/tests/integration/testsuite/Magento/Framework/View/LayoutDirectivesTest.php diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php new file mode 100644 index 0000000000000..70b69f68396ca --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php @@ -0,0 +1,65 @@ +get(Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + + $objectManager->removeSharedInstance(Config::class); + $this->config1 = $objectManager->get(Config::class); + + $objectManager->removeSharedInstance(Config::class); + $this->config2 = $objectManager->get(Config::class); + } + + public function testGetMetadata() + { + $this->assertEquals($this->config1->getMetadata(), $this->config2->getMetadata()); + } + + /** + * @param string $scope + * @dataProvider getDataDataProvider + */ + public function testGetData($scope) + { + $this->assertEquals($this->config1->getData($scope), $this->config2->getData($scope)); + } + + public function getDataDataProvider() + { + return [ + ['default'], + ['stores|default'], + ['websites|default'] + ]; + } +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php new file mode 100644 index 0000000000000..8e000676c7a68 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php @@ -0,0 +1,49 @@ +removeSharedInstance(ScopePool::class); + $this->scopePool = $objectManager->get(ScopePool::class); + } + + /** + * @param string $scopeType + * @param string $scopeCode + * @dataProvider getScopeDataProvider + */ + public function testGetScope($scopeType, $scopeCode = null) + { + $this->scopePool->clean(); + $this->assertEquals( + $this->scopePool->getScope($scopeType, $scopeCode), + $this->scopePool->getScope($scopeType, $scopeCode) + ); + } + + public function getScopeDataProvider() + { + return [ + ['default'], + ['stores', 'default'], + ['websites', 'default'] + ]; + } +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php new file mode 100644 index 0000000000000..ef8ada2514d9b --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php @@ -0,0 +1,63 @@ +get(Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + + $objectManager->removeSharedInstance(Config::class); + $this->config1 = $objectManager->get(Config::class); + + $objectManager->removeSharedInstance(Config::class); + $this->config2 = $objectManager->get(Config::class); + } + + /** + * @param string $route + * @param string $scope + * @dataProvider getRouteFrontNameDataProvider + */ + public function testGetRouteFrontName($route, $scope) + { + $this->assertEquals( + $this->config1->getRouteFrontName($route, $scope), + $this->config2->getRouteFrontName($route, $scope) + ); + } + + public function getRouteFrontNameDataProvider() + { + return [ + ['adminhtml', 'adminhtml'], + ['catalog', 'frontend'], + ]; + } +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php old mode 100755 new mode 100644 diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/LayoutDirectivesTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/LayoutDirectivesTest.php old mode 100755 new mode 100644 diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 949ed3f9953c9..7f20de1b03c9d 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -62,7 +62,7 @@ public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() )->with( 'areaCode::RoutesConfig' )->will( - $this->returnValue(serialize(['expected'])) + $this->returnValue(\Zend_Json::encode(['expected'])) ); $this->assertEquals('routerCode', $this->_config->getRouteFrontName('routerCode')); } @@ -76,7 +76,7 @@ public function testGetRouteByFrontName() )->with( 'areaCode::RoutesConfig' )->will( - $this->returnValue(serialize(['routerCode' => ['frontName' => 'routerName']])) + $this->returnValue(\Zend_Json::encode(['routerCode' => ['frontName' => 'routerName']])) ); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName')); @@ -94,7 +94,7 @@ public function testGetRouteByFrontNameNoRoutes() )->with( 'areaCode::RoutesConfig' )->will( - $this->returnValue(serialize([])) + $this->returnValue(\Zend_Json::encode([])) ); $this->assertFalse($this->_config->getRouteByFrontName('routerName')); @@ -112,7 +112,7 @@ public function testGetRouteByFrontNameNoCache() )->with( 'scope::RoutesConfig' )->will( - $this->returnValue(serialize(false)) + $this->returnValue(\Zend_Json::encode(false)) ); $routes = [ @@ -152,7 +152,7 @@ public function testGetRouteByFrontNameNoCache() )->method( 'save' )->with( - serialize($routes), + \Zend_Json::encode($routes), 'scope::RoutesConfig' ); @@ -172,7 +172,7 @@ public function testGetModulesByFrontName() 'areaCode::RoutesConfig' )->will( $this->returnValue( - serialize(['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]) + \Zend_Json::encode(['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]) ) ); $this->assertEquals(['Module1'], $this->_config->getModulesByFrontName('routerName')); diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index d77a1d96b79be..dabc17be45793 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -10,33 +10,39 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $reader; - /** @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cache; - /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ - protected $objectManagerHelper; + /** + * @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $readerMock; + + /** + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $cacheMock; protected function setUp() { - $this->reader = $this->getMockBuilder(\Magento\Framework\Config\ReaderInterface::class) + $this->readerMock = $this->getMockBuilder(\Magento\Framework\Config\ReaderInterface::class) ->disableOriginalConstructor() ->getMock(); - $this->cache = $this->getMockBuilder(\Magento\Framework\Config\CacheInterface::class) + $this->cacheMock = $this->getMockBuilder(\Magento\Framework\Config\CacheInterface::class) ->disableOriginalConstructor() ->getMock(); - $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); } - public function testGet() + public function testGetConfigNotCached() { $data = ['a' => 'b']; - $cacheid = 'test'; - $this->cache->expects($this->once())->method('load')->will($this->returnValue(false)); - $this->reader->expects($this->once())->method('read')->will($this->returnValue($data)); + $cacheId = 'test'; + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(false); + $this->readerMock->expects($this->once()) + ->method('read') + ->willReturn($data); $config = new \Magento\Framework\Config\Data( - $this->reader, $this->cache, $cacheid + $this->readerMock, $this->cacheMock, $cacheId ); $this->assertEquals($data, $config->get()); $this->assertEquals('b', $config->get('a')); @@ -44,16 +50,40 @@ public function testGet() $this->assertEquals(33, $config->get('a/b', 33)); } + public function testGetConfigCached() + { + $data = ['a' => 'b']; + $cacheId = 'test'; + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn('{"a":"b"}'); + $this->readerMock->expects($this->never()) + ->method('read'); + + $config = new \Magento\Framework\Config\Data( + $this->readerMock, + $this->cacheMock, + $cacheId + ); + + $this->assertEquals($data, $config->get()); + $this->assertEquals('b', $config->get('a')); + } + public function testReset() { - $cacheid = 'test'; - $this->cache->expects($this->once())->method('load')->will($this->returnValue(\Zend_Json::encode([]))); - $this->cache->expects($this->once())->method('remove')->with($cacheid); + $cacheId = 'test'; + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(\Zend_Json::encode([])); + $this->cacheMock->expects($this->once()) + ->method('remove') + ->with($cacheId); $config = new \Magento\Framework\Config\Data( - $this->reader, - $this->cache, - $cacheid + $this->readerMock, + $this->cacheMock, + $cacheId ); $config->reset(); From 7bf4b405bac6d641948535f7d1268de24a9831cc Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 20 Sep 2016 21:45:30 -0500 Subject: [PATCH 007/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Reverting change to file permissions (cherry picked from commit 54dda93) --- .../testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php | 0 .../testsuite/Magento/Framework/View/LayoutDirectivesTest.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 dev/tests/integration/testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php mode change 100644 => 100755 dev/tests/integration/testsuite/Magento/Framework/View/LayoutDirectivesTest.php diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/Reader/BlockTest.php old mode 100644 new mode 100755 diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/LayoutDirectivesTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/LayoutDirectivesTest.php old mode 100644 new mode 100755 From 667430d05f3383398aac34852f4ad47d7b8bb0e4 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 10:59:02 -0500 Subject: [PATCH 008/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit 126c720) --- .../Unit/Model/ProductTypes/ConfigTest.php | 18 +++++++++--------- .../Cron/Test/Unit/Model/Config/DataTest.php | 13 ++++--------- .../Test/Unit/Model/Address/ConfigTest.php | 14 ++++++-------- .../Model/Country/Postcode/Config/DataTest.php | 4 +++- .../Unit/Model/Entity/Attribute/ConfigTest.php | 13 ++++--------- .../Unit/ResourceConnection/ConfigTest.php | 10 +++------- 6 files changed, 29 insertions(+), 43 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index af0c1625f9cb6..331e875f7fcfe 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -42,7 +42,9 @@ protected function setUp() */ public function testGetType($value, $expected) { - $this->cacheMock->expects($this->any())->method('load')->will($this->returnValue(serialize($value))); + $this->cacheMock->expects($this->any()) + ->method('load') + ->willReturn(\Zend_Json::encode($value)); $this->model = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); $this->assertEquals($expected, $this->model->getType('global')); } @@ -58,20 +60,18 @@ public function getTypeDataProvider() public function testGetAll() { $expected = ['Expected Data']; - $this->cacheMock->expects( - $this->once() - )->method( - 'load' - )->will( - $this->returnValue(serialize(['types' => $expected])) - ); + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(\Zend_Json::encode(['types' => $expected])); $this->model = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); $this->assertEquals($expected, $this->model->getAll()); } public function testIsProductSet() { - $this->cacheMock->expects($this->once())->method('load')->will($this->returnValue(serialize([]))); + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(\Zend_Json::encode([])); $this->model = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); $this->assertEquals(false, $this->model->isProductSet('typeId')); diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 1803deb002f6d..481a737f72177 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -30,15 +30,10 @@ public function testGetJobs() 'job2' => ['schedule' => '* * * * *', 'instance' => 'JobModel2', 'method' => 'method2'], ]; - $cache->expects( - $this->any() - )->method( - 'load' - )->with( - $this->equalTo('test_cache_id') - )->will( - $this->returnValue(serialize($jobs)) - ); + $cache->expects($this->any()) + ->method('load') + ->with('test_cache_id') + ->willReturn(\Zend_Json::encode($jobs)); $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index 5c2aeec636cec..5ae28bff5ba87 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -85,14 +85,12 @@ protected function setUp() $this->_readerMock->expects($this->once())->method('read')->will($this->returnValue($fixtureConfigData)); - $this->_cacheMock->expects( - $this->once() - )->method( - 'save' - )->with( - serialize($fixtureConfigData), - $this->_cacheId - ); + $this->_cacheMock->expects($this->once()) + ->method('save') + ->with( + \Zend_Json::encode($fixtureConfigData), + $this->_cacheId + ); $this->_model = new \Magento\Customer\Model\Address\Config( $this->_readerMock, diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index c4681d88d28e7..717a26fcac054 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -30,7 +30,9 @@ protected function setUp() public function testGet() { $expected = ['someData' => ['someValue', 'someKey' => 'someValue']]; - $this->cacheMock->expects($this->any())->method('load')->will($this->returnValue(serialize($expected))); + $this->cacheMock->expects($this->any()) + ->method('load') + ->willReturn(\Zend_Json::encode($expected)); $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data($this->readerMock, $this->cacheMock); $this->assertEquals($expected, $configData->get()); diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index 78dbc6bed34c3..39aba701bab7b 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -54,15 +54,10 @@ protected function setUp() ); $this->_cacheMock = $this->getMock(\Magento\Framework\App\Cache\Type\Config::class, [], [], '', false); $this->_cacheId = 'eav_attributes'; - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - $this->equalTo($this->_cacheId) - )->will( - $this->returnValue(serialize([])) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with($this->_cacheId) + ->willReturn(\Zend_Json::encode([])); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php index 936a806432419..a43efc118444d 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php @@ -57,13 +57,9 @@ protected function setUp() 'validResource' => ['connection' => 'validConnectionName'], ]; - $this->_cacheMock->expects( - $this->any() - )->method( - 'load' - )->will( - $this->returnValue(serialize($this->_resourcesConfig)) - ); + $this->_cacheMock->expects($this->any()) + ->method('load') + ->willReturn(\Zend_Json::encode($this->_resourcesConfig)); $deploymentConfig = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); $deploymentConfig->expects($this->once()) From 0e745c7fee23d7291fc87da7fc35df4bbb04ca74 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 13:04:09 -0500 Subject: [PATCH 009/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit f5a7ab6) --- app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php | 2 +- app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php | 4 +++- .../Magento/Framework/Mview/Test/Unit/Config/DataTest.php | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) 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 5e0f7c314cf31..05ecbe02250b9 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -68,7 +68,7 @@ public function testConstructorWithCache() )->with( $this->cacheId )->will( - $this->returnValue(serialize($this->indexers)) + $this->returnValue(\Zend_Json::encode($this->indexers)) ); $this->stateCollection->expects($this->never())->method('getItems'); diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index 722fdb2b9495f..6635250744b5c 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -30,7 +30,9 @@ protected function setUp() public function testGet() { $expected = ['someData' => ['someValue', 'someKey' => 'someValue']]; - $this->_cacheMock->expects($this->any())->method('load')->will($this->returnValue(serialize($expected))); + $this->_cacheMock->expects($this->any()) + ->method('load') + ->willReturn(\Zend_Json::encode($expected)); $configData = new \Magento\Sales\Model\Config\Data($this->_readerMock, $this->_cacheMock); $this->assertEquals($expected, $configData->get()); diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index 244b1b1e1382d..71dda58a9fdac 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -70,7 +70,7 @@ public function testConstructorWithCache() )->with( $this->cacheId )->will( - $this->returnValue(serialize($this->views)) + $this->returnValue(\Zend_Json::encode($this->views)) ); $this->stateCollection->expects($this->never())->method('getItems'); From 9da9acf00d5add7c8c70c130be667c33014fa870 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 13:19:20 -0500 Subject: [PATCH 010/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit 5fad65a) --- .../Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php | 3 +++ .../Magento/Framework/Config/Test/Unit/Data/ScopedTest.php | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) 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 9f21cf50bd663..dad8498d745bc 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -8,6 +8,9 @@ use Magento\Framework\App\Config\Scope\ReaderInterface; use Magento\Framework\App\Config\Scope\ReaderPoolInterface; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class ScopePoolTest extends \PHPUnit_Framework_TestCase { /** 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 b3c85b1b97bba..6f08bd835f24a 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -110,7 +110,9 @@ public function testGetScopeSwitchingWithNonCachedData() ); /** test cache saving */ - $this->_cacheMock->expects($this->once())->method('save')->with(\Zend_Json::encode($testValue), 'adminhtml::tag'); + $this->_cacheMock->expects($this->once()) + ->method('save') + ->with(\Zend_Json::encode($testValue), 'adminhtml::tag'); /** test config value existence */ $this->assertEquals('testValue', $this->_model->get('some')); From 39e0b547d655b86204d2b611e14bceded0427b09 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 20:39:00 -0500 Subject: [PATCH 011/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit b85ecc0) --- .../Magento/Framework/App/Route/Config.php | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index 32da1fd335199..e3d28a7568fdf 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -7,6 +7,8 @@ */ namespace Magento\Framework\App\Route; +use Magento\Framework\Json\Json; + class Config implements ConfigInterface { /** @@ -39,6 +41,11 @@ class Config implements ConfigInterface */ protected $_routes; + /** + * @var Json + */ + private $json; + /** * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache @@ -73,7 +80,7 @@ protected function _getRoutes($scope = null) return $this->_routes[$scope]; } $cacheId = $scope . '::' . $this->_cacheId; - $cachedRoutes = \Zend_Json::decode($this->_cache->load($cacheId)); + $cachedRoutes = $this->getJson()->decode($this->_cache->load($cacheId)); if (is_array($cachedRoutes)) { $this->_routes[$scope] = $cachedRoutes; return $cachedRoutes; @@ -81,7 +88,7 @@ protected function _getRoutes($scope = null) $routers = $this->_reader->read($scope); $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes']; - $this->_cache->save(\Zend_Json::encode($routes), $cacheId); + $this->_cache->save($this->getJson()->encode($routes), $cacheId); $this->_routes[$scope] = $routes; return $routes; } @@ -133,4 +140,19 @@ public function getModulesByFrontName($frontName, $scope = null) return array_unique($modules); } + + /** + * Ger json encoder/decoder + * + * @return Json + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(Json::class); + } + return $this->json; + } } From 00a0f48a44307d4f1364e17e17c572231d633678 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 21:42:33 -0500 Subject: [PATCH 012/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit 24b2414) --- .../App/Test/Unit/Route/ConfigTest.php | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 7f20de1b03c9d..214f1cef59509 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -45,12 +45,32 @@ protected function setUp() )->will( $this->returnValue('areaCode') ); - $this->_config = new \Magento\Framework\App\Route\Config( - $this->_readerMock, - $this->_cacheMock, - $this->_configScopeMock, - $this->_areaList + + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->_config = $objectManager->getObject( + \Magento\Framework\App\Route\Config::class, + [ + 'reader' => $this->_readerMock, + 'cache' => $this->_cacheMock, + 'configScope' => $this->_configScopeMock, + 'areaList' => $this->_areaList + ] ); + + + $jsonMock = $this->getMock(\Magento\Framework\Json\Json::class, [], [], '', false); + + $objectManager->setBackwardCompatibleProperty($this->_config, 'json', new \Magento\Framework\Json\Json()); + + $jsonMock->method('encode') + ->willReturnCallback(function($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function($string) { + return json_decode($string); + }); } public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() @@ -62,7 +82,7 @@ public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() )->with( 'areaCode::RoutesConfig' )->will( - $this->returnValue(\Zend_Json::encode(['expected'])) + $this->returnValue(json_encode(['expected'])) ); $this->assertEquals('routerCode', $this->_config->getRouteFrontName('routerCode')); } @@ -76,7 +96,7 @@ public function testGetRouteByFrontName() )->with( 'areaCode::RoutesConfig' )->will( - $this->returnValue(\Zend_Json::encode(['routerCode' => ['frontName' => 'routerName']])) + $this->returnValue(json_encode(['routerCode' => ['frontName' => 'routerName']])) ); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName')); @@ -94,7 +114,7 @@ public function testGetRouteByFrontNameNoRoutes() )->with( 'areaCode::RoutesConfig' )->will( - $this->returnValue(\Zend_Json::encode([])) + $this->returnValue(json_encode([])) ); $this->assertFalse($this->_config->getRouteByFrontName('routerName')); @@ -112,7 +132,7 @@ public function testGetRouteByFrontNameNoCache() )->with( 'scope::RoutesConfig' )->will( - $this->returnValue(\Zend_Json::encode(false)) + $this->returnValue(json_encode(false)) ); $routes = [ @@ -164,17 +184,12 @@ public function testGetRouteByFrontNameNoCache() public function testGetModulesByFrontName() { - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - 'areaCode::RoutesConfig' - )->will( - $this->returnValue( - \Zend_Json::encode(['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]) - ) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with('areaCode::RoutesConfig') + ->willReturn( + json_encode(['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]) + ); $this->assertEquals(['Module1'], $this->_config->getModulesByFrontName('routerName')); } } From e4f65960213565931fa9e9b3383beeb457b37df1 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 21:48:52 -0500 Subject: [PATCH 013/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit 908b3d2) --- .../Framework/App/Test/Unit/Route/ConfigTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 214f1cef59509..70b6e31f280ba 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -58,18 +58,18 @@ protected function setUp() ] ); - $jsonMock = $this->getMock(\Magento\Framework\Json\Json::class, [], [], '', false); + $objectManager->setBackwardCompatibleProperty($this->_config, 'json', $jsonMock); - $objectManager->setBackwardCompatibleProperty($this->_config, 'json', new \Magento\Framework\Json\Json()); + $json = new \Magento\Framework\Json\Json(); $jsonMock->method('encode') - ->willReturnCallback(function($string) { - return json_encode($string); + ->willReturnCallback(function($string) use ($json) { + return $json->encode($string); }); $jsonMock->method('decode') - ->willReturnCallback(function($string) { - return json_decode($string); + ->willReturnCallback(function($string) use ($json) { + return $json->decode($string); }); } From 2ea7817ad3ca88b3e1df3fff267fbbd46a28dfeb Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 21:56:20 -0500 Subject: [PATCH 014/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring test (cherry picked from commit ef8b8a0) --- .../Framework/App/Config/InitialTest.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php index 70b69f68396ca..4d47909dee909 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\App\Config; +use Magento\TestFramework\ObjectManager; use Magento\TestFramework\Helper\Bootstrap; use Magento\Framework\App\Cache\Frontend\Pool; use Magento\Framework\App\Config\Initial as Config; @@ -12,37 +13,22 @@ class InitialTest extends \PHPUnit_Framework_TestCase { /** - * @var Config + * @var ObjectManager */ - private $config1; - - /** - * @var Config - */ - private $config2; + private $objectManager; protected function setUp() { - /** @var \Magento\TestFramework\ObjectManager $objectManager */ - $objectManager = Bootstrap::getObjectManager(); - - /** @var Pool $cachePool */ - $cachePool = $objectManager->get(Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - - $objectManager->removeSharedInstance(Config::class); - $this->config1 = $objectManager->get(Config::class); - - $objectManager->removeSharedInstance(Config::class); - $this->config2 = $objectManager->get(Config::class); + $this->objectManager = Bootstrap::getObjectManager(); } public function testGetMetadata() { - $this->assertEquals($this->config1->getMetadata(), $this->config2->getMetadata()); + $this->cleanCache(); + $this->assertEquals( + $this->objectManager->create(Config::class)->getMetadata(), + $this->objectManager->create(Config::class)->getMetadata() + ); } /** @@ -51,7 +37,11 @@ public function testGetMetadata() */ public function testGetData($scope) { - $this->assertEquals($this->config1->getData($scope), $this->config2->getData($scope)); + $this->cleanCache(); + $this->assertEquals( + $this->objectManager->create(Config::class)->getData($scope), + $this->objectManager->create(Config::class)->getData($scope) + ); } public function getDataDataProvider() @@ -62,4 +52,14 @@ public function getDataDataProvider() ['websites|default'] ]; } + + private function cleanCache() + { + /** @var Pool $cachePool */ + $cachePool = $this->objectManager->get(Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } } From a0855ad7ff0a45331a8d85cf1a0d27a6fb2a380e Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 21:58:07 -0500 Subject: [PATCH 015/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit b66c3ab) --- .../Magento/Framework/App/Test/Unit/Route/ConfigTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 70b6e31f280ba..5a897a9127c60 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -64,11 +64,11 @@ protected function setUp() $json = new \Magento\Framework\Json\Json(); $jsonMock->method('encode') - ->willReturnCallback(function($string) use ($json) { + ->willReturnCallback(function ($string) use ($json) { return $json->encode($string); }); $jsonMock->method('decode') - ->willReturnCallback(function($string) use ($json) { + ->willReturnCallback(function ($string) use ($json) { return $json->decode($string); }); } From 2552b070f30140133788cff65dfe22b10f04bcf5 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 21 Sep 2016 22:42:23 -0500 Subject: [PATCH 016/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests (cherry picked from commit 6f3c682) --- .../Test/Unit/Model/Config/DataTest.php | 13 ++-- .../App/Test/Unit/Config/ScopePoolTest.php | 16 +++-- .../App/Test/Unit/Route/ConfigTest.php | 63 ++++++------------- .../App/Test/Unit/Router/ActionListTest.php | 2 +- .../Config/Test/Unit/Data/ScopedTest.php | 17 +++-- .../Framework/Config/Test/Unit/DataTest.php | 4 +- .../Magento/Framework/Encryption/Crypt.php | 18 +++--- .../Mview/Test/Unit/Config/DataTest.php | 13 ++-- 8 files changed, 54 insertions(+), 92 deletions(-) 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 05ecbe02250b9..6fdf00329a7bc 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -61,15 +61,10 @@ protected function setUp() public function testConstructorWithCache() { $this->cache->expects($this->once())->method('test')->with($this->cacheId)->will($this->returnValue(true)); - $this->cache->expects( - $this->once() - )->method( - 'load' - )->with( - $this->cacheId - )->will( - $this->returnValue(\Zend_Json::encode($this->indexers)) - ); + $this->cache->expects($this->once()) + ->method('load') + ->with($this->cacheId) + ->willReturn(\Zend_Json::encode($this->indexers)); $this->stateCollection->expects($this->never())->method('getItems'); 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 dad8498d745bc..a406bc4175c11 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -110,15 +110,13 @@ public function testGetScope($scopeType, $scope, array $data, $cachedData) if (!$cachedData) { $this->_reader->expects($this->once())->method('read')->with('testScope')->will($this->returnValue($data)); - $this->_cache->expects( - $this->once() - )->method( - 'save' - )->with( - \Zend_Json::encode($data), - $cacheKey, - [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] - ); + $this->_cache->expects($this->once()) + ->method('save') + ->with( + \Zend_Json::encode($data), + $cacheKey, + [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] + ); } $configData = $this->getMockBuilder(\Magento\Framework\App\Config\Data::class) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 5a897a9127c60..3bed2826a2981 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -75,29 +75,19 @@ protected function setUp() public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() { - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - 'areaCode::RoutesConfig' - )->will( - $this->returnValue(json_encode(['expected'])) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with('areaCode::RoutesConfig') + ->willReturn(json_encode(['expected'])); $this->assertEquals('routerCode', $this->_config->getRouteFrontName('routerCode')); } public function testGetRouteByFrontName() { - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - 'areaCode::RoutesConfig' - )->will( - $this->returnValue(json_encode(['routerCode' => ['frontName' => 'routerName']])) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with('areaCode::RoutesConfig') + ->willReturn(json_encode(['routerCode' => ['frontName' => 'routerName']])); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName')); @@ -107,15 +97,10 @@ public function testGetRouteByFrontName() public function testGetRouteByFrontNameNoRoutes() { - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - 'areaCode::RoutesConfig' - )->will( - $this->returnValue(json_encode([])) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with('areaCode::RoutesConfig') + ->willReturn(json_encode([])); $this->assertFalse($this->_config->getRouteByFrontName('routerName')); @@ -125,15 +110,10 @@ public function testGetRouteByFrontNameNoRoutes() public function testGetRouteByFrontNameNoCache() { - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - 'scope::RoutesConfig' - )->will( - $this->returnValue(json_encode(false)) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with('scope::RoutesConfig') + ->willReturn(json_encode(false)); $routes = [ 'routerCode' => [ @@ -167,14 +147,9 @@ public function testGetRouteByFrontNameNoCache() $this->returnValue('default_router') ); - $this->_cacheMock->expects( - $this->once() - )->method( - 'save' - )->with( - \Zend_Json::encode($routes), - 'scope::RoutesConfig' - ); + $this->_cacheMock->expects($this->once()) + ->method('save') + ->with(json_encode($routes), 'scope::RoutesConfig'); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope')); diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 4db8221e9a42d..dae0bd37d225b 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -96,7 +96,7 @@ public function testGet($module, $area, $namespace, $action, $data, $expected) ->method('save'); $this->moduleReaderMock->expects($this->once()) ->method('getActionFiles') - ->will($this->returnValue($data)); + ->willReturn($data); $this->actionList = $this->objectManager->getObject( \Magento\Framework\App\Router\ActionList::class, [ 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 6f08bd835f24a..df7346eb8ac57 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -55,7 +55,9 @@ public function testgetConfigByPath($path, $expectedValue, $default) 'key_1.2' => ['some' => 'arrayValue'], ], ]; - $this->_cacheMock->expects($this->any())->method('load')->will($this->returnValue(\Zend_Json::encode([]))); + $this->_cacheMock->expects($this->any()) + ->method('load') + ->willReturn(\Zend_Json::encode([])); $this->_model->merge($testData); $this->assertEquals($expectedValue, $this->_model->get($path, $default)); } @@ -135,15 +137,10 @@ public function testGetScopeSwitchingWithCachedData() ); /** set cache data */ - $this->_cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - 'adminhtml::tag' - )->will( - $this->returnValue(\Zend_Json::encode($testValue)) - ); + $this->_cacheMock->expects($this->once()) + ->method('load') + ->with('adminhtml::tag') + ->willReturn(\Zend_Json::encode($testValue)); /** test preventing of getting data from reader */ $this->_readerMock->expects($this->never())->method('read'); diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index dabc17be45793..d633a932da817 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -42,7 +42,9 @@ public function testGetConfigNotCached() ->willReturn($data); $config = new \Magento\Framework\Config\Data( - $this->readerMock, $this->cacheMock, $cacheId + $this->readerMock, + $this->cacheMock, + $cacheId ); $this->assertEquals($data, $config->get()); $this->assertEquals('b', $config->get('a')); diff --git a/lib/internal/Magento/Framework/Encryption/Crypt.php b/lib/internal/Magento/Framework/Encryption/Crypt.php index 98fcb40757802..e63e0cf8709a6 100644 --- a/lib/internal/Magento/Framework/Encryption/Crypt.php +++ b/lib/internal/Magento/Framework/Encryption/Crypt.php @@ -51,15 +51,15 @@ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE { $this->_cipher = $cipher; $this->_mode = $mode; - $this->_handle = mcrypt_module_open($cipher, '', $mode, ''); + $this->_handle = @mcrypt_module_open($cipher, '', $mode, ''); try { - $maxKeySize = mcrypt_enc_get_key_size($this->_handle); + $maxKeySize = @mcrypt_enc_get_key_size($this->_handle); if (strlen($key) > $maxKeySize) { throw new \Magento\Framework\Exception\LocalizedException( new \Magento\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize]) ); } - $initVectorSize = mcrypt_enc_get_iv_size($this->_handle); + $initVectorSize = @mcrypt_enc_get_iv_size($this->_handle); if (true === $initVector) { /* Generate a random vector from human-readable characters */ $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; @@ -77,10 +77,10 @@ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE } $this->_initVector = $initVector; } catch (\Exception $e) { - mcrypt_module_close($this->_handle); + @mcrypt_module_close($this->_handle); throw $e; } - mcrypt_generic_init($this->_handle, $key, $initVector); + @mcrypt_generic_init($this->_handle, $key, $initVector); } /** @@ -90,8 +90,8 @@ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE */ public function __destruct() { - mcrypt_generic_deinit($this->_handle); - mcrypt_module_close($this->_handle); + @mcrypt_generic_deinit($this->_handle); + @mcrypt_module_close($this->_handle); } /** @@ -135,7 +135,7 @@ public function encrypt($data) if (strlen($data) == 0) { return $data; } - return mcrypt_generic($this->_handle, $data); + return @mcrypt_generic($this->_handle, $data); } /** @@ -149,7 +149,7 @@ public function decrypt($data) if (strlen($data) == 0) { return $data; } - $data = mdecrypt_generic($this->_handle, $data); + $data = @mdecrypt_generic($this->_handle, $data); /* * Returned string can in fact be longer than the unencrypted string due to the padding of the data * @link http://www.php.net/manual/en/function.mdecrypt-generic.php diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index 71dda58a9fdac..a76a58377dcda 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -63,15 +63,10 @@ protected function setUp() public function testConstructorWithCache() { $this->cache->expects($this->once())->method('test')->with($this->cacheId)->will($this->returnValue(true)); - $this->cache->expects( - $this->once() - )->method( - 'load' - )->with( - $this->cacheId - )->will( - $this->returnValue(\Zend_Json::encode($this->views)) - ); + $this->cache->expects($this->once()) + ->method('load') + ->with($this->cacheId) + ->willReturn(\Zend_Json::encode($this->views)); $this->stateCollection->expects($this->never())->method('getItems'); From e5afc6ac8aa87fa96679b3ab90af172432a0e0f6 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 22 Sep 2016 11:21:00 -0500 Subject: [PATCH 017/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Adding JsonInterface and Json class (cherry picked from commit a96f661) --- lib/internal/Magento/Framework/App/Route/Config.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index e3d28a7568fdf..1f330c9a5f961 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -7,7 +7,7 @@ */ namespace Magento\Framework\App\Route; -use Magento\Framework\Json\Json; +use Magento\Framework\Json\JsonInterface; class Config implements ConfigInterface { @@ -42,7 +42,7 @@ class Config implements ConfigInterface protected $_routes; /** - * @var Json + * @var JsonInterface */ private $json; @@ -144,14 +144,14 @@ public function getModulesByFrontName($frontName, $scope = null) /** * Ger json encoder/decoder * - * @return Json + * @return JsonInterface * @deprecated */ private function getJson() { if ($this->json === null) { $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(Json::class); + ->get(JsonInterface::class); } return $this->json; } From f13d81002ad33fb971afaa31b1734c9e1878827d Mon Sep 17 00:00:00 2001 From: Islam Elkhalifa Date: Thu, 22 Sep 2016 17:15:55 -0500 Subject: [PATCH 018/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring, updating unit tests (cherry picked from commit b3c7d34) --- .../Magento/Framework/Validator/Factory.php | 27 +- .../Validator/Test/Unit/FactoryTest.php | 243 +++++++++--------- 2 files changed, 152 insertions(+), 118 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index d2e3837d13138..d710670578c2d 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -11,6 +11,7 @@ namespace Magento\Framework\Validator; use Magento\Framework\Cache\FrontendInterface; +use Magento\Framework\Json\JsonInterface; class Factory { @@ -44,6 +45,11 @@ class Factory */ private $cache; + /** + * @var JsonInterface + */ + private $json; + /** * Initialize dependencies * @@ -70,9 +76,9 @@ protected function _initializeConfigList() $this->_configFiles = $this->cache->load(self::CACHE_KEY); if (!$this->_configFiles) { $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); - $this->cache->save(serialize($this->_configFiles), self::CACHE_KEY); + $this->cache->save($this->getJson()->encode($this->_configFiles), self::CACHE_KEY); } else { - $this->_configFiles = unserialize($this->_configFiles); + $this->_configFiles = $this->getJson()->decode($this->_configFiles); } } } @@ -109,7 +115,7 @@ public function getValidatorConfig() { $this->_initializeConfigList(); $this->_initializeDefaultTranslator(); - return $this->_objectManager->create( + return $this->_objectManager->create( \Magento\Framework\Validator\Config::class, ['configFiles' => $this->_configFiles]); } @@ -140,4 +146,19 @@ public function createValidator($entityName, $groupName, array $builderConfig = $this->_initializeDefaultTranslator(); return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig); } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index bb829010795e8..0d890db0570e1 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -10,89 +10,96 @@ class FactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\ObjectManagerInterface + * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_objectManager; + private $objectManagerMock; /** - * @var \Magento\Framework\Module\Dir\Reader + * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $_config; + private $readerMock; /** - * @var \Magento\Framework\TranslateInterface + * @var \Magento\Framework\Validator\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $_translateAdapter; + private $validatorConfigMock; /** - * @var \Magento\Framework\Validator\Config + * @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_validatorConfig; + private $cacheMock; - private $cache; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + + /** + * @var \Magento\Framework\Translate\AdapterInterface + */ + private $defaultTranslator; /** - * @var \Magento\Framework\Translate\AdapterInterface|null + * @var \Magento\Framework\Validator\Factory */ - protected $_defaultTranslator = null; + private $factory; /** - * Save default translator + * @var string */ + private $jsonString = '["\/tmp\/moduleOne\/etc\/validation.xml"]'; + + /** + * @var array + */ + private $data = ['/tmp/moduleOne/etc/validation.xml']; + protected function setUp() { - $this->_defaultTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); - $this->_objectManager = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $this->_validatorConfig = $this->getMockBuilder( - \Magento\Framework\Validator\Config::class - )->setMethods( - ['createValidatorBuilder', 'createValidator'] - )->disableOriginalConstructor()->getMock(); - - $this->_objectManager->expects( - $this->at(0) - )->method( - 'create' - )->with( - \Magento\Framework\Translate\Adapter::class - )->will( - $this->returnValue(new \Magento\Framework\Translate\Adapter()) - ); + $this->defaultTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); - $this->_objectManager->expects( - $this->at(1) - )->method( - 'create' - )->with( + $this->objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $this->validatorConfigMock = $this->getMock( \Magento\Framework\Validator\Config::class, - ['configFiles' => ['/tmp/moduleOne/etc/validation.xml']] - )->will( - $this->returnValue($this->_validatorConfig) + ['createValidatorBuilder', 'createValidator'], + [], + '', + false ); - - // Config mock - $this->_config = $this->getMockBuilder( - \Magento\Framework\Module\Dir\Reader::class - )->setMethods( - ['getConfigurationFiles'] - )->disableOriginalConstructor()->getMock(); - $this->_config->expects( - $this->once() - )->method( - 'getConfigurationFiles' - )->with( - 'validation.xml' - )->will( - $this->returnValue(['/tmp/moduleOne/etc/validation.xml']) + $translateAdapterMock = $this->getMock(\Magento\Framework\Translate\Adapter::class, [], [], '', false); + $this->objectManagerMock->expects($this->at(0)) + ->method('create') + ->with(\Magento\Framework\Translate\Adapter::class) + ->willReturn($translateAdapterMock); + $this->objectManagerMock->expects($this->at(1)) + ->method('create') + ->with( + \Magento\Framework\Validator\Config::class, + ['configFiles' => $this->data] + ) + ->willReturn($this->validatorConfigMock); + $this->readerMock = $this->getMock( + \Magento\Framework\Module\Dir\Reader::class, + ['getConfigurationFiles'], + [], + '', + false ); + $this->cacheMock = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); - // Translate adapter mock - $this->_translateAdapter = $this->getMockBuilder( - \Magento\Framework\TranslateInterface::class - )->disableOriginalConstructor()->getMock(); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->cache = $this->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class) - ->getMockForAbstractClass(); + $this->factory = $objectManager->getObject( + \Magento\Framework\Validator\Factory::class, + [ + 'objectManager' => $this->objectManagerMock, + 'moduleReader' => $this->readerMock, + 'cache' => $this->cacheMock + ] + ); + + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $objectManager->setBackwardCompatibleProperty($this->factory, 'json', $this->jsonMock); } /** @@ -100,87 +107,93 @@ protected function setUp() */ protected function tearDown() { - \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($this->_defaultTranslator); - unset($this->_defaultTranslator); + \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($this->defaultTranslator); + unset($this->defaultTranslator); } - /** - * Test getValidatorConfig created correct validator config. Check that validator translator was initialized. - */ public function testGetValidatorConfig() { - $factory = new \Magento\Framework\Validator\Factory( - $this->_objectManager, - $this->_config, - $this->cache - ); - $actualConfig = $factory->getValidatorConfig(); + $this->readerMock->method('getConfigurationFiles') + ->with('validation.xml') + ->willReturn($this->data); + $actualConfig = $this->factory->getValidatorConfig(); $this->assertInstanceOf( \Magento\Framework\Validator\Config::class, $actualConfig, 'Object of incorrect type was created' ); - - // Check that validator translator was correctly instantiated - $validatorTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); $this->assertInstanceOf( \Magento\Framework\Translate\Adapter::class, - $validatorTranslator, + \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(), 'Default validator translate adapter was not set correctly' ); } - /** - * Test createValidatorBuilder call - */ + public function testGetValidatorConfigCacheNotExist() + { + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(false); + $this->readerMock->expects($this->once()) + ->method('getConfigurationFiles') + ->willReturn($this->data); + $this->cacheMock->expects($this->once()) + ->method('save') + ->with($this->jsonString); + $this->jsonMock->expects($this->once()) + ->method('encode') + ->with($this->data) + ->willReturn($this->jsonString); + $this->factory->getValidatorConfig(); + $this->factory->getValidatorConfig(); + } + + public function testGetValidatorConfigCacheExist() + { + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn($this->jsonString); // why json mock??? + $this->readerMock->expects($this->never()) + ->method('getConfigurationFiles'); + $this->cacheMock->expects($this->never()) + ->method('save'); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->with($this->jsonString) + ->willReturn($this->data); + $this->factory->getValidatorConfig(); + $this->factory->getValidatorConfig(); + } + public function testCreateValidatorBuilder() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_validatorConfig->expects( - $this->once() - )->method( - 'createValidatorBuilder' - )->with( - 'test', - 'class', - [] - )->will( - $this->returnValue( - $objectManager->getObject(\Magento\Framework\Validator\Builder::class, ['constraints' => []]) - ) - ); - $factory = new \Magento\Framework\Validator\Factory( - $this->_objectManager, - $this->_config, - $this->cache - ); + $this->readerMock->method('getConfigurationFiles') + ->with('validation.xml') + ->willReturn($this->data); + $builderMock = $this->getMock(\Magento\Framework\Validator\Builder::class, [], [], '', false); + $this->validatorConfigMock->expects($this->once()) + ->method('createValidatorBuilder') + ->with('test', 'class', []) + ->willReturn($builderMock); $this->assertInstanceOf( \Magento\Framework\Validator\Builder::class, - $factory->createValidatorBuilder('test', 'class', []) + $this->factory->createValidatorBuilder('test', 'class', []) ); } - /** - * Test createValidatorBuilder call - */ public function testCreateValidator() { - $this->_validatorConfig->expects( - $this->once() - )->method( - 'createValidator' - )->with( - 'test', - 'class', - [] - )->will( - $this->returnValue(new \Magento\Framework\Validator()) - ); - $factory = new \Magento\Framework\Validator\Factory( - $this->_objectManager, - $this->_config, - $this->cache + $this->readerMock->method('getConfigurationFiles') + ->with('validation.xml') + ->willReturn($this->data); + $validatorMock = $this->getMock(\Magento\Framework\Validator::class, [], [], '', false); + $this->validatorConfigMock->expects($this->once()) + ->method('createValidator') + ->with('test', 'class', []) + ->willReturn($validatorMock); + $this->assertInstanceOf( + \Magento\Framework\Validator::class, + $this->factory->createValidator('test', 'class', []) ); - $this->assertInstanceOf(\Magento\Framework\Validator::class, $factory->createValidator('test', 'class', [])); } } From 1fbd98e07af3cb58c526659087d7dc3a883662dd Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 22 Sep 2016 18:30:42 -0500 Subject: [PATCH 019/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring (cherry picked from commit c4118e9) --- .../Unit/Model/ProductTypes/ConfigTest.php | 37 +++++--- .../Cron/Test/Unit/Model/Config/DataTest.php | 8 +- .../Test/Unit/Model/Address/ConfigTest.php | 13 +++ .../Country/Postcode/Config/DataTest.php | 38 +++++--- .../Model/Entity/Attribute/ConfigTest.php | 2 + .../Test/Unit/Model/Config/DataTest.php | 15 ++- .../Sales/Test/Unit/Model/Config/DataTest.php | 17 +++- .../Magento/Framework/App/Config/Initial.php | 37 +++++++- .../Framework/App/Config/ScopePool.php | 25 ++++- .../Magento/Framework/App/Route/Config.php | 2 +- .../Framework/App/Router/ActionList.php | 37 +++++++- .../App/Test/Unit/Config/InitialTest.php | 81 +++++++++------- .../App/Test/Unit/Config/ScopePoolTest.php | 31 +++++-- .../Unit/ResourceConnection/ConfigTest.php | 78 +++++++++------- .../App/Test/Unit/Router/ActionListTest.php | 93 +++++++++++-------- .../Magento/Framework/Config/Data.php | 38 +++++++- .../Magento/Framework/Config/Data/Scoped.php | 6 +- .../Config/Test/Unit/Data/ScopedTest.php | 31 ++++++- .../Framework/Config/Test/Unit/DataTest.php | 37 +++++--- .../Mview/Test/Unit/Config/DataTest.php | 20 +++- 20 files changed, 464 insertions(+), 182 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 331e875f7fcfe..711163f629a98 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -17,10 +17,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ protected $cacheMock; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + /** * @var \Magento\Catalog\Model\ProductTypes\Config */ - protected $model; + protected $config; protected function setUp() { @@ -32,21 +37,25 @@ protected function setUp() false ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Catalog\Model\ProductTypes\Config::setJson($this->jsonMock); } /** - * @dataProvider getTypeDataProvider - * * @param array $value * @param mixed $expected + * @dataProvider getTypeDataProvider */ public function testGetType($value, $expected) { $this->cacheMock->expects($this->any()) ->method('load') - ->willReturn(\Zend_Json::encode($value)); - $this->model = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); - $this->assertEquals($expected, $this->model->getType('global')); + ->willReturn(json_encode($value)); + + $this->jsonMock->method('decode') + ->willReturn($value); + $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + $this->assertEquals($expected, $this->config->getType('global')); } public function getTypeDataProvider() @@ -62,18 +71,22 @@ public function testGetAll() $expected = ['Expected Data']; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn(\Zend_Json::encode(['types' => $expected])); - $this->model = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); - $this->assertEquals($expected, $this->model->getAll()); + ->willReturn(json_encode('"types":["Expected Data"]]')); + $this->jsonMock->method('decode') + ->willReturn(['types' => $expected]); + $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + $this->assertEquals($expected, $this->config->getAll()); } public function testIsProductSet() { $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn(\Zend_Json::encode([])); - $this->model = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + ->willReturn(''); + $this->jsonMock->method('decode') + ->willReturn([]); + $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); - $this->assertEquals(false, $this->model->isProductSet('typeId')); + $this->assertEquals(false, $this->config->isProductSet('typeId')); } } diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 481a737f72177..4a4c6c2c392eb 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -33,10 +33,16 @@ public function testGetJobs() $cache->expects($this->any()) ->method('load') ->with('test_cache_id') - ->willReturn(\Zend_Json::encode($jobs)); + ->willReturn(json_encode($jobs)); $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); + $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Cron\Model\Config\Data::setJson($jsonMock); + + $jsonMock->method('decode') + ->willReturn($jobs); + $configData = new \Magento\Cron\Model\Config\Data($reader, $cache, $dbReader, 'test_cache_id'); $expected = [ diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index 5ae28bff5ba87..c8f3c39787c23 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -37,6 +37,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ protected $_scopeConfigMock; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + /** * @var \Magento\Customer\Model\Address\Config */ @@ -92,6 +97,14 @@ protected function setUp() $this->_cacheId ); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Customer\Model\Address\Config::setJson($this->jsonMock); + + $this->jsonMock->method('encode') + ->willReturn(json_encode($fixtureConfigData)); + $this->jsonMock->method('decode') + ->willReturn($fixtureConfigData); + $this->_model = new \Magento\Customer\Model\Address\Config( $this->_readerMock, $this->_cacheMock, diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 717a26fcac054..7bf0fd5ef3ae3 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -8,23 +8,38 @@ class DataTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Directory\Model\Country\Postcode\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $readerMock; + private $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; + + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; protected function setUp() { - $this->readerMock = $this->getMockBuilder( - \Magento\Directory\Model\Country\Postcode\Config\Reader::class - )->disableOriginalConstructor()->getMock(); - $this->cacheMock = $this->getMockBuilder( - \Magento\Framework\App\Cache\Type\Config::class - )->disableOriginalConstructor()->getMock(); + $this->readerMock = $this->getMock( + \Magento\Directory\Model\Country\Postcode\Config\Reader::class, + [], + [], + '', + false + ); + $this->cacheMock = $this->getMock( + \Magento\Framework\App\Cache\Type\Config::class, + [], + [], + '', + false + ); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Directory\Model\Country\Postcode\Config\Data::setJson($this->jsonMock); } public function testGet() @@ -32,9 +47,8 @@ public function testGet() $expected = ['someData' => ['someValue', 'someKey' => 'someValue']]; $this->cacheMock->expects($this->any()) ->method('load') - ->willReturn(\Zend_Json::encode($expected)); + ->willReturn(json_encode($expected)); $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data($this->readerMock, $this->cacheMock); - $this->assertEquals($expected, $configData->get()); } } diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index 39aba701bab7b..30f35f4edd925 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -59,6 +59,8 @@ protected function setUp() ->with($this->_cacheId) ->willReturn(\Zend_Json::encode([])); + $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Eav\Model\Entity\Attribute\Config::setJson($jsonMock); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, 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 6fdf00329a7bc..63cea630b563d 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -37,6 +37,11 @@ class DataTest extends \PHPUnit_Framework_TestCase */ protected $indexers = ['indexer1' => [], 'indexer3' => []]; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + protected function setUp() { $this->reader = $this->getMock(\Magento\Framework\Indexer\Config\Reader::class, ['read'], [], '', false); @@ -56,15 +61,23 @@ protected function setUp() '', false ); + + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Framework\Mview\Config\Data::setJson($this->jsonMock); } public function testConstructorWithCache() { + $jsonString = json_encode($this->indexers); $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(\Zend_Json::encode($this->indexers)); + ->willReturn($jsonString); + + $this->jsonMock->method('decode') + ->with($jsonString) + ->willReturn($this->indexers); $this->stateCollection->expects($this->never())->method('getItems'); diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index 6635250744b5c..11f16ad734253 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -17,6 +17,11 @@ class DataTest extends \PHPUnit_Framework_TestCase */ protected $_cacheMock; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + protected function setUp() { $this->_readerMock = $this->getMockBuilder( @@ -25,14 +30,20 @@ protected function setUp() $this->_cacheMock = $this->getMockBuilder( \Magento\Framework\App\Cache\Type\Config::class )->disableOriginalConstructor()->getMock(); + + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Framework\Mview\Config\Data::setJson($this->jsonMock); } public function testGet() { $expected = ['someData' => ['someValue', 'someKey' => 'someValue']]; - $this->_cacheMock->expects($this->any()) - ->method('load') - ->willReturn(\Zend_Json::encode($expected)); + $this->_cacheMock->expects($this->once()) + ->method('load'); + + $this->jsonMock->method('decode') + ->willReturn($expected); + $configData = new \Magento\Sales\Model\Config\Data($this->_readerMock, $this->_cacheMock); $this->assertEquals($expected, $configData->get()); diff --git a/lib/internal/Magento/Framework/App/Config/Initial.php b/lib/internal/Magento/Framework/App/Config/Initial.php index 85deca02f9131..27af55e61e96f 100644 --- a/lib/internal/Magento/Framework/App/Config/Initial.php +++ b/lib/internal/Magento/Framework/App/Config/Initial.php @@ -8,6 +8,7 @@ namespace Magento\Framework\App\Config; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\Json\JsonInterface; class Initial { @@ -30,6 +31,11 @@ class Initial */ protected $_metadata = []; + /** + * @var JsonInterface + */ + private static $json; + /** * @param \Magento\Framework\App\Config\Initial\Reader $reader * @param \Magento\Framework\App\Cache\Type\Config $cache @@ -41,9 +47,9 @@ public function __construct( $data = $cache->load(self::CACHE_ID); if (!$data) { $data = $reader->read(); - $cache->save(\Zend_Json::encode($data), self::CACHE_ID); + $cache->save($this->getJson()->encode($data), self::CACHE_ID); } else { - $data = \Zend_Json::decode($data); + $data = $this->getJson()->decode($data); } $this->_data = $data['data']; $this->_metadata = $data['metadata']; @@ -76,4 +82,31 @@ public function getMetadata() { return $this->_metadata; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if (self::$json === null) { + self::$json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return self::$json; + } + + /** + * Set json encoder/decoder + * + * @param JsonInterface $json + * @return void + * @deprecated + */ + public static function setJson(JsonInterface $json) + { + self::$json = $json; + } } diff --git a/lib/internal/Magento/Framework/App/Config/ScopePool.php b/lib/internal/Magento/Framework/App/Config/ScopePool.php index 65fc09babc075..55ffe3ff46446 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -6,6 +6,7 @@ namespace Magento\Framework\App\Config; use Magento\Framework\App\RequestInterface; +use Magento\Framework\Json\JsonInterface; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -49,6 +50,11 @@ class ScopePool */ private $request; + /** + * @var JsonInterface + */ + private $json; + /** * @param \Magento\Framework\App\Config\Scope\ReaderPoolInterface $readerPool * @param DataFactory $dataFactory @@ -105,7 +111,7 @@ public function getScope($scopeType, $scopeCode = null) $data = $this->_cache->load($cacheKey); if ($data) { - $data = \Zend_Json::decode($data); + $data = $this->getJson()->decode($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { @@ -113,7 +119,7 @@ public function getScope($scopeType, $scopeCode = null) } else { $data = $reader->read($scopeCode); } - $this->_cache->save(\Zend_Json::encode($data), $cacheKey, [self::CACHE_TAG]); + $this->_cache->save($this->getJson()->encode($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } @@ -153,4 +159,19 @@ protected function _getScopeCode($scopeType, $scopeCode) return $scopeCode; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index 1f330c9a5f961..716af96d342c0 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -142,7 +142,7 @@ public function getModulesByFrontName($frontName, $scope = null) } /** - * Ger json encoder/decoder + * Get json encoder/decoder * * @return JsonInterface * @deprecated diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index 86d435b0f276e..6c04f09159f7e 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -6,6 +6,7 @@ */ namespace Magento\Framework\App\Router; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\Module\Dir\Reader as ModuleReader; class ActionList @@ -35,6 +36,11 @@ class ActionList 'while', 'xor', ]; + /** + * @var JsonInterface + */ + private static $json; + /** * @param \Magento\Framework\Config\CacheInterface $cache * @param ModuleReader $moduleReader @@ -54,9 +60,9 @@ public function __construct( $data = $cache->load($cacheKey); if (!$data) { $this->actions = $moduleReader->getActionFiles(); - $cache->save(\Zend_Json::encode($this->actions), $cacheKey); + $cache->save($this->getJson()->encode($this->actions), $cacheKey); } else { - $this->actions = \Zend_Json::decode($data); + $this->actions = $this->getJson()->decode($data); } } @@ -92,4 +98,31 @@ public function get($module, $area, $namespace, $action) } return null; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if (self::$json === null) { + self::$json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return self::$json; + } + + /** + * Set json encoder/decoder + * + * @param JsonInterface $json + * @return void + * @deprecated + */ + public static function setJson(JsonInterface $json) + { + self::$json = $json; + } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index 6b48498d726de..66325090df656 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -10,56 +10,70 @@ class InitialTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\App\Config\Initial */ - protected $_model; + private $config; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Config\Initial\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $_initialReaderMock; + private $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $_configCacheMock; + private $cacheMock; + + /** + * @var array + */ + private $data = [ + 'data' => [ + 'default' => ['key' => 'default_value'], + 'stores' => ['default' => ['key' => 'store_value']], + 'websites' => ['default' => ['key' => 'website_value']], + ], + 'metadata' => ['metadata'], + ]; protected function setUp() { - $this->_initialReaderMock = - $this->getMock(\Magento\Framework\App\Config\Initial\Reader::class, [], [], '', false); - $this->_configCacheMock = - $this->getMock(\Magento\Framework\App\Cache\Type\Config::class, [], [], '', false); - $serializedData = \Zend_Json::encode( - [ - 'data' => [ - 'default' => ['key' => 'default_value'], - 'stores' => ['default' => ['key' => 'store_value']], - 'websites' => ['default' => ['key' => 'website_value']], - ], - 'metadata' => ['metadata'], - ] + $this->readerMock = $this->getMock( + \Magento\Framework\App\Config\Initial\Reader::class, + [], + [], + '', + false ); - $this->_configCacheMock->expects( - $this->any() - )->method( - 'load' - )->with( - 'initial_config' - )->will( - $this->returnValue($serializedData) + $this->cacheMock = $this->getMock( + \Magento\Framework\App\Cache\Type\Config::class, + [], + [], + '', + false ); + $this->cacheMock->expects($this->any()) + ->method('load') + ->with('initial_config') + ->willReturn(json_encode($this->data)); + $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $jsonMock->method('decode') + ->willReturn($this->data); + + \Magento\Framework\App\Config\Initial::setJson($jsonMock); - $this->_model = new \Magento\Framework\App\Config\Initial($this->_initialReaderMock, $this->_configCacheMock); + $this->config = new \Magento\Framework\App\Config\Initial( + $this->readerMock, + $this->cacheMock + ); } /** - * @dataProvider getDataDataProvider - * * @param string $scope - * @param array $expectedResult + * @param array $expected + * @dataProvider getDataDataProvider */ - public function testGetData($scope, $expectedResult) + public function testGetData($scope, $expected) { - $this->assertEquals($expectedResult, $this->_model->getData($scope)); + $this->assertEquals($expected, $this->config->getData($scope)); } public function getDataDataProvider() @@ -73,7 +87,6 @@ public function getDataDataProvider() public function testGetMetadata() { - $expectedResult = ['metadata']; - $this->assertEquals($expectedResult, $this->_model->getMetadata()); + $this->assertEquals(['metadata'], $this->config->getMetadata()); } } 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 a406bc4175c11..6232122eaaa14 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -16,38 +16,43 @@ class ScopePoolTest extends \PHPUnit_Framework_TestCase /** * @var ReaderInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_reader; + private $_reader; /** * @var ReaderPoolInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerPool; + private $_readerPool; /** * @var \Magento\Framework\App\Config\DataFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $_dataFactory; + private $_dataFactory; /** * @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_cache; + private $_cache; + + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; /** * @var \Magento\Framework\App\Config\ScopePool */ - protected $_object; + private $_object; protected function setUp() { - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerPool = $this->getMockForAbstractClass(ReaderPoolInterface::class); $this->_reader = $this->getMockForAbstractClass(ReaderInterface::class); $this->_dataFactory = $this->getMockBuilder( \Magento\Framework\App\Config\DataFactory::class )->disableOriginalConstructor()->getMock(); $this->_cache = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); - $this->_object = $helper->getObject( + $this->_object = $objectManager->getObject( \Magento\Framework\App\Config\ScopePool::class, [ 'readerPool' => $this->_readerPool, @@ -56,6 +61,8 @@ protected function setUp() 'cacheId' => 'test_cache_id' ] ); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $objectManager->setBackwardCompatibleProperty($this->_object, 'json', $this->jsonMock); $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) ->disableOriginalConstructor() @@ -113,12 +120,16 @@ public function testGetScope($scopeType, $scope, array $data, $cachedData) $this->_cache->expects($this->once()) ->method('save') ->with( - \Zend_Json::encode($data), + json_encode($data), $cacheKey, [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] ); + $this->jsonMock->method('encode') + ->willReturn(json_encode($data)); + } else { + $this->jsonMock->method('decode') + ->willReturn($data); } - $configData = $this->getMockBuilder(\Magento\Framework\App\Config\Data::class) ->disableOriginalConstructor() ->getMock(); @@ -149,7 +160,7 @@ public function getScopeDataProvider() $baseScope->expects($this->any())->method('getCode')->will($this->returnValue('testScope')); return [ ['scopeType1', 'testScope', ['key' => 'value'], null], - ['scopeType2', 'testScope', ['key' => 'value'], \Zend_Json::encode(['key' => 'value'])], + ['scopeType2', 'testScope', ['key' => 'value'], '{"key":"value"}'], ['scopeType1', $baseScope, ['key' => 'value'], null] ]; } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php index a43efc118444d..5e49f4b8ef67f 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php @@ -8,44 +8,51 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\App\\Config + * @var \Magento\Framework\App\Config */ - protected $_model; + private $config; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_scopeMock; + private $scopeMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_cacheMock; + private $cacheMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\ResourceConnection\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; + private $readerMock; /** * @var array */ - protected $_resourcesConfig; + private $resourcesConfig; /** * @var array */ - protected $_initialResources; + private $initialResources; protected function setUp() { - $this->_scopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); - $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - - $this->_readerMock = - $this->getMock(\Magento\Framework\App\ResourceConnection\Config\Reader::class, [], [], '', false); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->scopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + + $this->readerMock = $this->getMock( + \Magento\Framework\App\ResourceConnection\Config\Reader::class, + [], + [], + '', + false + ); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->_resourcesConfig = [ + $this->resourcesConfig = [ 'mainResourceName' => ['name' => 'mainResourceName', 'extends' => 'anotherResourceName'], 'otherResourceName' => ['name' => 'otherResourceName', 'connection' => 'otherConnectionName'], 'anotherResourceName' => ['name' => 'anotherResourceName', 'connection' => 'anotherConnection'], @@ -53,27 +60,30 @@ protected function setUp() 'extendedResourceName' => ['name' => 'extendedResourceName', 'extends' => 'validResource'], ]; - $this->_initialResources = [ + $this->initialResources = [ 'validResource' => ['connection' => 'validConnectionName'], ]; - $this->_cacheMock->expects($this->any()) + $this->cacheMock->expects($this->any()) ->method('load') - ->willReturn(\Zend_Json::encode($this->_resourcesConfig)); + ->willReturn(json_encode($this->resourcesConfig)); + $this->jsonMock->method('decode') + ->willReturn($this->resourcesConfig); - $deploymentConfig = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); - $deploymentConfig->expects($this->once()) + $deploymentConfigMock = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); + $deploymentConfigMock->expects($this->once()) ->method('getConfigData') ->with('resource') - ->willReturn($this->_initialResources); + ->willReturn($this->initialResources); - $this->_model = new \Magento\Framework\App\ResourceConnection\Config( - $this->_readerMock, - $this->_scopeMock, - $this->_cacheMock, - $deploymentConfig, + $this->config = new \Magento\Framework\App\ResourceConnection\Config( + $this->readerMock, + $this->scopeMock, + $this->cacheMock, + $deploymentConfigMock, 'cacheId' ); + $objectManager->setBackwardCompatibleProperty($this->config, 'json', $this->jsonMock); } /** @@ -83,7 +93,7 @@ protected function setUp() */ public function testGetConnectionName($resourceName, $connectionName) { - $this->assertEquals($connectionName, $this->_model->getConnectionName($resourceName)); + $this->assertEquals($connectionName, $this->config->getConnectionName($resourceName)); } /** @@ -91,17 +101,17 @@ public function testGetConnectionName($resourceName, $connectionName) */ public function testExceptionConstructor() { - $deploymentConfig = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); - $deploymentConfig->expects($this->once()) + $deploymentConfigMock = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); + $deploymentConfigMock->expects($this->once()) ->method('getConfigData') ->with('resource') ->willReturn(['validResource' => ['somekey' => 'validConnectionName']]); new \Magento\Framework\App\ResourceConnection\Config( - $this->_readerMock, - $this->_scopeMock, - $this->_cacheMock, - $deploymentConfig, + $this->readerMock, + $this->scopeMock, + $this->cacheMock, + $deploymentConfigMock, 'cacheId' ); } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index dae0bd37d225b..3aa0cde1be665 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -12,69 +12,75 @@ class ActionListTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ - protected $objectManager; + private $objectManager; /** - * @var \Magento\Framework\Config\CacheInterface | \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; /** - * @var \Magento\Framework\Module\Dir\Reader | \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $moduleReaderMock; + private $readerMock; /** * @var \Magento\Framework\App\Router\ActionList */ - protected $actionList; + private $actionList; + + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->cacheMock = $this->getMockBuilder(\Magento\Framework\Config\CacheInterface::class) - ->disableOriginalConstructor() - ->getMock(); - $this->moduleReaderMock = $this->getMockBuilder(\Magento\Framework\Module\Dir\Reader::class) - ->disableOriginalConstructor() - ->getMock(); + $this->cacheMock = $this->getMock( + \Magento\Framework\Config\CacheInterface::class, + [], + [], + '', + false + ); + $this->readerMock = $this->getMock( + \Magento\Framework\Module\Dir\Reader::class, + [], + [], + '', + false + ); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); } - public function testConstructorCachedData() + public function testConstructActionsCached() { $this->cacheMock->expects($this->once()) ->method('load') - ->will($this->returnValue(\Zend_Json::encode('data'))); + ->willReturn('"data"'); + $this->jsonMock->expects($this->once()) + ->method('decode'); $this->cacheMock->expects($this->never()) ->method('save'); - $this->moduleReaderMock->expects($this->never()) + $this->readerMock->expects($this->never()) ->method('getActionFiles'); - $this->actionList = $this->objectManager->getObject( - \Magento\Framework\App\Router\ActionList::class, - [ - 'cache' => $this->cacheMock, - 'moduleReader' => $this->moduleReaderMock, - ] - ); + $this->createActionListInstance(); } - public function testConstructorNoCachedData() + public function testConstructActionsNoCached() { $this->cacheMock->expects($this->once()) ->method('load') - ->will($this->returnValue(false)); + ->willReturn(false); + $this->jsonMock->expects($this->once()) + ->method('encode'); $this->cacheMock->expects($this->once()) ->method('save'); - $this->moduleReaderMock->expects($this->once()) + $this->readerMock->expects($this->once()) ->method('getActionFiles') ->will($this->returnValue('data')); - $this->actionList = $this->objectManager->getObject( - \Magento\Framework\App\Router\ActionList::class, - [ - 'cache' => $this->cacheMock, - 'moduleReader' => $this->moduleReaderMock, - ] - ); + $this->createActionListInstance(); } /** @@ -88,22 +94,15 @@ public function testConstructorNoCachedData() */ public function testGet($module, $area, $namespace, $action, $data, $expected) { - $this->cacheMock->expects($this->once()) ->method('load') ->will($this->returnValue(false)); $this->cacheMock->expects($this->once()) ->method('save'); - $this->moduleReaderMock->expects($this->once()) + $this->readerMock->expects($this->once()) ->method('getActionFiles') ->willReturn($data); - $this->actionList = $this->objectManager->getObject( - \Magento\Framework\App\Router\ActionList::class, - [ - 'cache' => $this->cacheMock, - 'moduleReader' => $this->moduleReaderMock, - ] - ); + $this->createActionListInstance(); $this->assertEquals($expected, $this->actionList->get($module, $area, $namespace, $action)); } @@ -168,4 +167,16 @@ public function getDataProvider() ], ]; } + + private function createActionListInstance() + { + \Magento\Framework\App\Router\ActionList::setJson($this->jsonMock); + $this->actionList = $this->objectManager->getObject( + \Magento\Framework\App\Router\ActionList::class, + [ + 'cache' => $this->cacheMock, + 'moduleReader' => $this->readerMock, + ] + ); + } } diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index c7c4b2db45bf3..af0c4fea74838 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -7,6 +7,8 @@ */ namespace Magento\Framework\Config; +use Magento\Framework\Json\JsonInterface; + /** * @SuppressWarnings(PHPMD.NumberOfChildren) */ @@ -62,6 +64,11 @@ class Data implements \Magento\Framework\Config\DataInterface */ private $cacheId; + /** + * @var JsonInterface + */ + protected static $json; + /** * Constructor * @@ -89,9 +96,9 @@ protected function initData() $data = $this->cache->load($this->cacheId); if (false === $data) { $data = $this->reader->read(); - $this->cache->save(\Zend_Json::encode($data), $this->cacheId, $this->cacheTags); + $this->cache->save($this->getJson()->encode($data), $this->cacheId, $this->cacheTags); } else { - $data = \Zend_Json::decode($data); + $data = $this->getJson()->decode($data); } $this->merge($data); } @@ -139,4 +146,31 @@ public function reset() { $this->cache->remove($this->cacheId); } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + protected function getJson() + { + if (self::$json === null) { + self::$json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return self::$json; + } + + /** + * Set json encoder/decoder + * + * @param JsonInterface $json + * @return void + * @deprecated + */ + public static function setJson(JsonInterface $json) + { + self::$json = $json; + } } diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index f239308159eed..9df6f61bd07f9 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -5,6 +5,8 @@ */ namespace Magento\Framework\Config\Data; +use Magento\Framework\Json\JsonInterface; + class Scoped extends \Magento\Framework\Config\Data { /** @@ -98,11 +100,11 @@ protected function _loadScopedData() if (false == isset($this->_loadedScopes[$scopeCode])) { if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId)) ) { - $data = \Zend_Json::decode($data); + $data = $this->getJson()->decode($data); } else { $data = $this->_reader->read($scopeCode); if ($scopeCode !== 'primary') { - $this->_cache->save(\Zend_Json::encode($data), $scopeCode . '::' . $this->_cacheId); + $this->_cache->save($this->getJson()->encode($data), $scopeCode . '::' . $this->_cacheId); } } $this->merge($data); 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 df7346eb8ac57..2189e90ae6e69 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -27,18 +27,26 @@ class ScopedTest extends \PHPUnit_Framework_TestCase */ protected $_cacheMock; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + protected function setUp() { $this->_readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $this->_model = new \Magento\Framework\Config\Data\Scoped( $this->_readerMock, $this->_configScopeMock, $this->_cacheMock, 'tag' ); + \Magento\Framework\Config\Data\Scoped::setJson($this->jsonMock); } /** @@ -47,7 +55,7 @@ protected function setUp() * @param string $default * @dataProvider getConfigByPathDataProvider */ - public function testgetConfigByPath($path, $expectedValue, $default) + public function testGetConfigByPath($path, $expectedValue, $default) { $testData = [ 'key_1' => [ @@ -55,9 +63,12 @@ public function testgetConfigByPath($path, $expectedValue, $default) 'key_1.2' => ['some' => 'arrayValue'], ], ]; - $this->_cacheMock->expects($this->any()) + $this->_cacheMock->expects($this->once()) ->method('load') - ->willReturn(\Zend_Json::encode([])); + ->willReturn(''); + $this->_readerMock->expects($this->once()) + ->method('read') + ->willReturn([]); $this->_model->merge($testData); $this->assertEquals($expectedValue, $this->_model->get($path, $default)); } @@ -79,6 +90,7 @@ public function getConfigByPathDataProvider() public function testGetScopeSwitchingWithNonCachedData() { $testValue = ['some' => 'testValue']; + $jsonString = '{"some":"testValue"}'; /** change current area */ $this->_configScopeMock->expects( @@ -111,10 +123,14 @@ public function testGetScopeSwitchingWithNonCachedData() $this->returnValue($testValue) ); + $this->jsonMock->method('encode') + ->with($testValue) + ->willReturn($jsonString); + /** test cache saving */ $this->_cacheMock->expects($this->once()) ->method('save') - ->with(\Zend_Json::encode($testValue), 'adminhtml::tag'); + ->with($jsonString, 'adminhtml::tag'); /** test config value existence */ $this->assertEquals('testValue', $this->_model->get('some')); @@ -126,6 +142,7 @@ public function testGetScopeSwitchingWithNonCachedData() public function testGetScopeSwitchingWithCachedData() { $testValue = ['some' => 'testValue']; + $jsonString = '{"some":"testValue"}'; /** change current area */ $this->_configScopeMock->expects( @@ -136,11 +153,15 @@ public function testGetScopeSwitchingWithCachedData() $this->returnValue('adminhtml') ); + $this->jsonMock->method('decode') + ->with($jsonString) + ->willReturn($testValue); + /** set cache data */ $this->_cacheMock->expects($this->once()) ->method('load') ->with('adminhtml::tag') - ->willReturn(\Zend_Json::encode($testValue)); + ->willReturn($jsonString); /** test preventing of getting data from reader */ $this->_readerMock->expects($this->never())->method('read'); diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index d633a932da817..2284cef6653cf 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -20,14 +20,17 @@ class DataTest extends \PHPUnit_Framework_TestCase */ private $cacheMock; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + protected function setUp() { - $this->readerMock = $this->getMockBuilder(\Magento\Framework\Config\ReaderInterface::class) - ->disableOriginalConstructor() - ->getMock(); - $this->cacheMock = $this->getMockBuilder(\Magento\Framework\Config\CacheInterface::class) - ->disableOriginalConstructor() - ->getMock(); + $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Framework\Config\Data::setJson($this->jsonMock); } public function testGetConfigNotCached() @@ -40,7 +43,9 @@ public function testGetConfigNotCached() $this->readerMock->expects($this->once()) ->method('read') ->willReturn($data); - + $this->jsonMock->expects($this->once()) + ->method('encode') + ->with($data); $config = new \Magento\Framework\Config\Data( $this->readerMock, $this->cacheMock, @@ -55,39 +60,45 @@ public function testGetConfigNotCached() public function testGetConfigCached() { $data = ['a' => 'b']; + $jsonString = '{"a":"b"}'; $cacheId = 'test'; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn('{"a":"b"}'); + ->willReturn($jsonString); $this->readerMock->expects($this->never()) ->method('read'); - + $this->jsonMock->expects($this->once()) + ->method('decode') + ->with($jsonString) + ->willReturn($data); $config = new \Magento\Framework\Config\Data( $this->readerMock, $this->cacheMock, $cacheId ); - $this->assertEquals($data, $config->get()); $this->assertEquals('b', $config->get('a')); } public function testReset() { + $jsonString = ''; $cacheId = 'test'; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn(\Zend_Json::encode([])); + ->willReturn($jsonString); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->with($jsonString) + ->willReturn([]); $this->cacheMock->expects($this->once()) ->method('remove') ->with($cacheId); - $config = new \Magento\Framework\Config\Data( $this->readerMock, $this->cacheMock, $cacheId ); - $config->reset(); } } diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index a76a58377dcda..e87b88f86fea4 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -10,7 +10,7 @@ class DataTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\Mview\Config\Data */ - protected $model; + protected $config; /** * @var \Magento\Framework\Mview\Config\Reader|\PHPUnit_Framework_MockObject_MockObject @@ -37,6 +37,11 @@ class DataTest extends \PHPUnit_Framework_TestCase */ protected $views = ['view1' => [], 'view3' => []]; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + protected function setUp() { $this->reader = $this->getMock(\Magento\Framework\Mview\Config\Reader::class, ['read'], [], '', false); @@ -58,6 +63,9 @@ protected function setUp() true, ['getItems'] ); + + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + \Magento\Framework\Mview\Config\Data::setJson($this->jsonMock); } public function testConstructorWithCache() @@ -65,12 +73,14 @@ public function testConstructorWithCache() $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(\Zend_Json::encode($this->views)); + ->with($this->cacheId); $this->stateCollection->expects($this->never())->method('getItems'); - $this->model = new \Magento\Framework\Mview\Config\Data( + $this->jsonMock->method('decode') + ->willReturn($this->views); + + $this->config = new \Magento\Framework\Mview\Config\Data( $this->reader, $this->cache, $this->stateCollection, @@ -109,7 +119,7 @@ public function testConstructorWithoutCache() $this->stateCollection->expects($this->once())->method('getItems')->will($this->returnValue($states)); - $this->model = new \Magento\Framework\Mview\Config\Data( + $this->config = new \Magento\Framework\Mview\Config\Data( $this->reader, $this->cache, $this->stateCollection, From 1935fe51b7da77c82bae9969d6311cea27ceb960 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 22 Sep 2016 19:28:39 -0500 Subject: [PATCH 020/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring (cherry picked from commit d7a6755) --- .../Test/Unit/Model/Country/Postcode/Config/DataTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 7bf0fd5ef3ae3..5ebbf89ee22ae 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -45,9 +45,12 @@ protected function setUp() public function testGet() { $expected = ['someData' => ['someValue', 'someKey' => 'someValue']]; - $this->cacheMock->expects($this->any()) + $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(json_encode($expected)); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->willReturn($expected); $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data($this->readerMock, $this->cacheMock); $this->assertEquals($expected, $configData->get()); } From 26375c7e465be90aa926ed00d63f7e3c7758350e Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 22 Sep 2016 19:44:52 -0500 Subject: [PATCH 021/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring (cherry picked from commit cc8aad2) --- .../Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index 30f35f4edd925..09e4b1b4e40ea 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -57,10 +57,13 @@ protected function setUp() $this->_cacheMock->expects($this->once()) ->method('load') ->with($this->_cacheId) - ->willReturn(\Zend_Json::encode([])); + ->willReturn(''); $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Eav\Model\Entity\Attribute\Config::setJson($jsonMock); + + $jsonMock->method('decode') + ->willReturn([]); + \Magento\Eav\Model\Entity\Attribute\Config::setJson($jsonMock); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, From 750ed78244c5f288b4a5fa28e2dc7a227e15cb5b Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 22 Sep 2016 22:01:52 -0500 Subject: [PATCH 022/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Reverting change (cherry picked from commit efe38b3) --- .../Magento/Framework/Encryption/Crypt.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/internal/Magento/Framework/Encryption/Crypt.php b/lib/internal/Magento/Framework/Encryption/Crypt.php index e63e0cf8709a6..98fcb40757802 100644 --- a/lib/internal/Magento/Framework/Encryption/Crypt.php +++ b/lib/internal/Magento/Framework/Encryption/Crypt.php @@ -51,15 +51,15 @@ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE { $this->_cipher = $cipher; $this->_mode = $mode; - $this->_handle = @mcrypt_module_open($cipher, '', $mode, ''); + $this->_handle = mcrypt_module_open($cipher, '', $mode, ''); try { - $maxKeySize = @mcrypt_enc_get_key_size($this->_handle); + $maxKeySize = mcrypt_enc_get_key_size($this->_handle); if (strlen($key) > $maxKeySize) { throw new \Magento\Framework\Exception\LocalizedException( new \Magento\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize]) ); } - $initVectorSize = @mcrypt_enc_get_iv_size($this->_handle); + $initVectorSize = mcrypt_enc_get_iv_size($this->_handle); if (true === $initVector) { /* Generate a random vector from human-readable characters */ $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; @@ -77,10 +77,10 @@ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE } $this->_initVector = $initVector; } catch (\Exception $e) { - @mcrypt_module_close($this->_handle); + mcrypt_module_close($this->_handle); throw $e; } - @mcrypt_generic_init($this->_handle, $key, $initVector); + mcrypt_generic_init($this->_handle, $key, $initVector); } /** @@ -90,8 +90,8 @@ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE */ public function __destruct() { - @mcrypt_generic_deinit($this->_handle); - @mcrypt_module_close($this->_handle); + mcrypt_generic_deinit($this->_handle); + mcrypt_module_close($this->_handle); } /** @@ -135,7 +135,7 @@ public function encrypt($data) if (strlen($data) == 0) { return $data; } - return @mcrypt_generic($this->_handle, $data); + return mcrypt_generic($this->_handle, $data); } /** @@ -149,7 +149,7 @@ public function decrypt($data) if (strlen($data) == 0) { return $data; } - $data = @mdecrypt_generic($this->_handle, $data); + $data = mdecrypt_generic($this->_handle, $data); /* * Returned string can in fact be longer than the unencrypted string due to the padding of the data * @link http://www.php.net/manual/en/function.mdecrypt-generic.php From 2bbdf4e965fe258132b7e55ac1c4b4d0e01e6b24 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 22 Sep 2016 22:55:38 -0500 Subject: [PATCH 023/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit fe1b44d) --- lib/internal/Magento/Framework/Config/Data.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index af0c4fea74838..4fcf428faace8 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -100,6 +100,9 @@ protected function initData() } else { $data = $this->getJson()->decode($data); } + if (!is_array($data)) { + $data = []; + } $this->merge($data); } From bea550eaf4167ac096a919d1471f64168ce2cc50 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 10:58:37 -0500 Subject: [PATCH 024/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests (cherry picked from commit 5819107) --- lib/internal/Magento/Framework/App/Config/ScopePool.php | 4 ++-- lib/internal/Magento/Framework/App/Route/Config.php | 4 ++-- lib/internal/Magento/Framework/Config/Data.php | 4 +--- lib/internal/Magento/Framework/Json/Json.php | 3 ++- lib/internal/Magento/Framework/Validator/Factory.php | 4 ++-- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Config/ScopePool.php b/lib/internal/Magento/Framework/App/Config/ScopePool.php index 55ffe3ff46446..953e4899fd35d 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -111,7 +111,7 @@ public function getScope($scopeType, $scopeCode = null) $data = $this->_cache->load($cacheKey); if ($data) { - $data = $this->getJson()->decode($data); + $data = unserialize($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { @@ -119,7 +119,7 @@ public function getScope($scopeType, $scopeCode = null) } else { $data = $reader->read($scopeCode); } - $this->_cache->save($this->getJson()->encode($data), $cacheKey, [self::CACHE_TAG]); + $this->_cache->save(serialize($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index 716af96d342c0..45f052c35742d 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -80,7 +80,7 @@ protected function _getRoutes($scope = null) return $this->_routes[$scope]; } $cacheId = $scope . '::' . $this->_cacheId; - $cachedRoutes = $this->getJson()->decode($this->_cache->load($cacheId)); + $cachedRoutes = unserialize($this->_cache->load($cacheId)); if (is_array($cachedRoutes)) { $this->_routes[$scope] = $cachedRoutes; return $cachedRoutes; @@ -88,7 +88,7 @@ protected function _getRoutes($scope = null) $routers = $this->_reader->read($scope); $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes']; - $this->_cache->save($this->getJson()->encode($routes), $cacheId); + $this->_cache->save(serialize($routes), $cacheId); $this->_routes[$scope] = $routes; return $routes; } diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 4fcf428faace8..5f52d5a5d62bf 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -100,9 +100,7 @@ protected function initData() } else { $data = $this->getJson()->decode($data); } - if (!is_array($data)) { - $data = []; - } + $this->merge($data); } diff --git a/lib/internal/Magento/Framework/Json/Json.php b/lib/internal/Magento/Framework/Json/Json.php index 35d94e6ee0939..c25f71cde4279 100644 --- a/lib/internal/Magento/Framework/Json/Json.php +++ b/lib/internal/Magento/Framework/Json/Json.php @@ -20,6 +20,7 @@ public function encode($data, $options = 0) */ public function decode($string, $objectDecodeType = self::TYPE_ARRAY) { - return json_decode($string, $objectDecodeType); + $result = json_decode($string, $objectDecodeType); + return $result; } } diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index d710670578c2d..7cbc0ed368444 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -76,9 +76,9 @@ protected function _initializeConfigList() $this->_configFiles = $this->cache->load(self::CACHE_KEY); if (!$this->_configFiles) { $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); - $this->cache->save($this->getJson()->encode($this->_configFiles), self::CACHE_KEY); + $this->cache->save(serialize($this->_configFiles), self::CACHE_KEY); } else { - $this->_configFiles = $this->getJson()->decode($this->_configFiles); + $this->_configFiles = unserialize($this->_configFiles); } } } From ccb1e46363a52f9bec416b3d7465b4192876a4ca Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 11:05:56 -0500 Subject: [PATCH 025/144] MAGETWO-58639: Refactor Framework_View - fix object coupling problem --- .../UiComponent/Config/Provider/Template.php | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php index 631c24aaa6e3f..5fc89aca1e060 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php @@ -5,9 +5,6 @@ */ namespace Magento\Framework\View\Element\UiComponent\Config\Provider; -use Magento\Framework\Config\CacheInterface; -use Magento\Framework\View\Element\UiComponent\Config\ReaderFactory; -use Magento\Framework\View\Element\UiComponent\Config\DomMergerInterface; use Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollector; use Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollectorFactory; @@ -32,19 +29,19 @@ class Template protected $aggregatedFileCollector; /** - * @var DomMergerInterface + * @var \Magento\Framework\View\Element\UiComponent\Config\DomMergerInterface */ protected $domMerger; /** - * @var CacheInterface + * @var \Magento\Framework\Config\CacheInterface */ protected $cache; /** * Factory for UI config reader * - * @var ReaderFactory + * @var \Magento\Framework\View\Element\UiComponent\Config\ReaderFactory */ protected $readerFactory; @@ -67,16 +64,16 @@ class Template * Constructor * * @param AggregatedFileCollector $aggregatedFileCollector - * @param DomMergerInterface $domMerger - * @param CacheInterface $cache - * @param ReaderFactory $readerFactory + * @param \Magento\Framework\View\Element\UiComponent\Config\DomMergerInterface $domMerger + * @param \Magento\Framework\Config\CacheInterface $cache + * @param \Magento\Framework\View\Element\UiComponent\Config\ReaderFactory $readerFactory * @param AggregatedFileCollectorFactory $aggregatedFileCollectorFactory */ public function __construct( AggregatedFileCollector $aggregatedFileCollector, - DomMergerInterface $domMerger, - CacheInterface $cache, - ReaderFactory $readerFactory, + \Magento\Framework\View\Element\UiComponent\Config\DomMergerInterface $domMerger, + \Magento\Framework\Config\CacheInterface $cache, + \Magento\Framework\View\Element\UiComponent\Config\ReaderFactory $readerFactory, AggregatedFileCollectorFactory $aggregatedFileCollectorFactory ) { $this->aggregatedFileCollector = $aggregatedFileCollector; From 67d7662f457d9147403eab61db0442bbebfb2e6d Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 12:01:47 -0500 Subject: [PATCH 026/144] MAGETWO-58644: Refactor Module_Catalog --- .../Model/Product/Attribute/Source/CountryofmanufactureTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php index 483897f69419f..6a4e73fa176cf 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php @@ -31,6 +31,7 @@ class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture */ private $countryOfManufacture; + protected function setUp() { $this->storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); From 5d672ca99acba14d6a5235da9cc416e4c04a93c0 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 14:43:13 -0500 Subject: [PATCH 027/144] MAGETWO-58639: Refactor Framework_View - Fix integration test --- .../UiComponent/Config/Provider/TemplateTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php index a03c63374777d..8003a01d1104c 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php @@ -11,6 +11,7 @@ /** * @magentoComponentsDir Magento/Framework/View/_files/UiComponent/theme + * @magentoAppIsolation enabled * @magentoDbIsolation enabled */ class TemplateTest extends \PHPUnit_Framework_TestCase @@ -29,6 +30,13 @@ protected function setUp() { $this->objectManager = Bootstrap::getObjectManager(); $this->registerThemes(); + $this->objectManager->addSharedInstance( + $this->objectManager->create( + \Magento\Framework\App\Arguments\ValidationState::class, + ['appMode' => 'default'] + ), + \Magento\Framework\App\Arguments\ValidationState::class + ); $this->model = $this->objectManager->create( \Magento\Framework\View\Element\UiComponent\Config\Provider\Template::class ); @@ -74,4 +82,11 @@ protected function registerThemes() ); $registration->register(); } + + protected function tearDown() + { + $this->objectManager->removeSharedInstance( + \Magento\Framework\App\Arguments\ValidationState::class + ); + } } From 4aeba1660a70870359cbadb7c7b29317a44951d5 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 12:42:04 -0500 Subject: [PATCH 028/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- .../Framework/App/Config/ScopePool.php | 4 +-- .../Magento/Framework/App/Route/Config.php | 4 +-- .../Magento/Framework/Validator/Factory.php | 26 +++++++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Config/ScopePool.php b/lib/internal/Magento/Framework/App/Config/ScopePool.php index 953e4899fd35d..55ffe3ff46446 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -111,7 +111,7 @@ public function getScope($scopeType, $scopeCode = null) $data = $this->_cache->load($cacheKey); if ($data) { - $data = unserialize($data); + $data = $this->getJson()->decode($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { @@ -119,7 +119,7 @@ public function getScope($scopeType, $scopeCode = null) } else { $data = $reader->read($scopeCode); } - $this->_cache->save(serialize($data), $cacheKey, [self::CACHE_TAG]); + $this->_cache->save($this->getJson()->encode($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index 45f052c35742d..716af96d342c0 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -80,7 +80,7 @@ protected function _getRoutes($scope = null) return $this->_routes[$scope]; } $cacheId = $scope . '::' . $this->_cacheId; - $cachedRoutes = unserialize($this->_cache->load($cacheId)); + $cachedRoutes = $this->getJson()->decode($this->_cache->load($cacheId)); if (is_array($cachedRoutes)) { $this->_routes[$scope] = $cachedRoutes; return $cachedRoutes; @@ -88,7 +88,7 @@ protected function _getRoutes($scope = null) $routers = $this->_reader->read($scope); $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes']; - $this->_cache->save(serialize($routes), $cacheId); + $this->_cache->save($this->getJson()->encode($routes), $cacheId); $this->_routes[$scope] = $routes; return $routes; } diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index 7cbc0ed368444..d160500819d92 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -12,6 +12,7 @@ use Magento\Framework\Cache\FrontendInterface; use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Config\FileIteratorFactory; class Factory { @@ -50,6 +51,11 @@ class Factory */ private $json; + /** + * @var FileIteratorFactory + */ + private $fileIteratorFactory; + /** * Initialize dependencies * @@ -76,9 +82,10 @@ protected function _initializeConfigList() $this->_configFiles = $this->cache->load(self::CACHE_KEY); if (!$this->_configFiles) { $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); - $this->cache->save(serialize($this->_configFiles), self::CACHE_KEY); + $this->cache->save($this->getJson()->encode($this->_configFiles->toArray()), self::CACHE_KEY); } else { - $this->_configFiles = unserialize($this->_configFiles); + $filesArray = $this->getJson()->decode($this->_configFiles); + $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray)); } } } @@ -161,4 +168,19 @@ private function getJson() } return $this->json; } + + /** + * Get file iterator factory + * + * @return FileIteratorFactory + * @deprecated + */ + private function getFileIteratorFactory() + { + if ($this->fileIteratorFactory === null) { + $this->fileIteratorFactory = \Magento\Framework\App\ObjectManager::getInstance() + ->get(FileIteratorFactory::class); + } + return $this->fileIteratorFactory; + } } From b9fb6e448eff70973a61f35fe7e64ae76db4834c Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 13:02:57 -0500 Subject: [PATCH 029/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- .../Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index 09e4b1b4e40ea..f67bdaaa22f87 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -63,7 +63,7 @@ protected function setUp() $jsonMock->method('decode') ->willReturn([]); - \Magento\Eav\Model\Entity\Attribute\Config::setJson($jsonMock); + \Magento\Eav\Model\Entity\Attribute\Config::setJson($jsonMock); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, From 4c13856ab7d08a2d2e62f7f09fb83dcfb18a0108 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 13:33:09 -0500 Subject: [PATCH 030/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- .../Validator/Test/Unit/FactoryTest.php | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index 0d890db0570e1..c51318c34a802 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -34,6 +34,16 @@ class FactoryTest extends \PHPUnit_Framework_TestCase */ private $jsonMock; + /** + * @var \Magento\Framework\Config\FileIteratorFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $fileIteratorFactoryMock; + + /** + * @var \Magento\Framework\Config\FileIterator|\PHPUnit_Framework_MockObject_MockObject + */ + private $fileIteratorMock; + /** * @var \Magento\Framework\Translate\AdapterInterface */ @@ -71,11 +81,18 @@ protected function setUp() ->method('create') ->with(\Magento\Framework\Translate\Adapter::class) ->willReturn($translateAdapterMock); + $this->fileIteratorMock = $this->getMock( + \Magento\Framework\Config\FileIterator::class, + [], + [], + '', + false + ); $this->objectManagerMock->expects($this->at(1)) ->method('create') ->with( \Magento\Framework\Validator\Config::class, - ['configFiles' => $this->data] + ['configFiles' => $this->fileIteratorMock] ) ->willReturn($this->validatorConfigMock); $this->readerMock = $this->getMock( @@ -99,7 +116,23 @@ protected function setUp() ); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $objectManager->setBackwardCompatibleProperty($this->factory, 'json', $this->jsonMock); + $this->fileIteratorFactoryMock = $this->getMock( + \Magento\Framework\Config\FileIteratorFactory::class, + [], + [], + '', + false + ); + $objectManager->setBackwardCompatibleProperty( + $this->factory, + 'json', + $this->jsonMock + ); + $objectManager->setBackwardCompatibleProperty( + $this->factory, + 'fileIteratorFactory', + $this->fileIteratorFactoryMock + ); } /** @@ -115,6 +148,8 @@ public function testGetValidatorConfig() { $this->readerMock->method('getConfigurationFiles') ->with('validation.xml') + ->willReturn($this->fileIteratorMock); + $this->fileIteratorMock->method('toArray') ->willReturn($this->data); $actualConfig = $this->factory->getValidatorConfig(); $this->assertInstanceOf( @@ -136,6 +171,8 @@ public function testGetValidatorConfigCacheNotExist() ->willReturn(false); $this->readerMock->expects($this->once()) ->method('getConfigurationFiles') + ->willReturn($this->fileIteratorMock); + $this->fileIteratorMock->method('toArray') ->willReturn($this->data); $this->cacheMock->expects($this->once()) ->method('save') @@ -152,7 +189,7 @@ public function testGetValidatorConfigCacheExist() { $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn($this->jsonString); // why json mock??? + ->willReturn($this->jsonString); $this->readerMock->expects($this->never()) ->method('getConfigurationFiles'); $this->cacheMock->expects($this->never()) @@ -161,6 +198,8 @@ public function testGetValidatorConfigCacheExist() ->method('decode') ->with($this->jsonString) ->willReturn($this->data); + $this->fileIteratorFactoryMock->method('create') + ->willReturn($this->fileIteratorMock); $this->factory->getValidatorConfig(); $this->factory->getValidatorConfig(); } @@ -169,6 +208,8 @@ public function testCreateValidatorBuilder() { $this->readerMock->method('getConfigurationFiles') ->with('validation.xml') + ->willReturn($this->fileIteratorMock); + $this->fileIteratorMock->method('toArray') ->willReturn($this->data); $builderMock = $this->getMock(\Magento\Framework\Validator\Builder::class, [], [], '', false); $this->validatorConfigMock->expects($this->once()) @@ -185,6 +226,8 @@ public function testCreateValidator() { $this->readerMock->method('getConfigurationFiles') ->with('validation.xml') + ->willReturn($this->fileIteratorMock); + $this->fileIteratorMock->method('toArray') ->willReturn($this->data); $validatorMock = $this->getMock(\Magento\Framework\Validator::class, [], [], '', false); $this->validatorConfigMock->expects($this->once()) From 873642428cd1719738971ed84a4c8ede659ba17f Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 13:41:06 -0500 Subject: [PATCH 031/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- .../Magento/Framework/Validator/Factory.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index d160500819d92..c662e7a7c435b 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -11,8 +11,6 @@ namespace Magento\Framework\Validator; use Magento\Framework\Cache\FrontendInterface; -use Magento\Framework\Json\JsonInterface; -use Magento\Framework\Config\FileIteratorFactory; class Factory { @@ -47,12 +45,12 @@ class Factory private $cache; /** - * @var JsonInterface + * @var \Magento\Framework\Json\JsonInterface */ private $json; /** - * @var FileIteratorFactory + * @var \Magento\Framework\Config\FileIteratorFactory */ private $fileIteratorFactory; @@ -157,14 +155,14 @@ public function createValidator($entityName, $groupName, array $builderConfig = /** * Get json encoder/decoder * - * @return JsonInterface + * @return \Magento\Framework\Json\JsonInterface * @deprecated */ private function getJson() { if ($this->json === null) { $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + ->get(\Magento\Framework\Json\JsonInterface::class); } return $this->json; } @@ -172,14 +170,14 @@ private function getJson() /** * Get file iterator factory * - * @return FileIteratorFactory + * @return \Magento\Framework\Config\FileIteratorFactory * @deprecated */ private function getFileIteratorFactory() { if ($this->fileIteratorFactory === null) { $this->fileIteratorFactory = \Magento\Framework\App\ObjectManager::getInstance() - ->get(FileIteratorFactory::class); + ->get(\Magento\Framework\Config\FileIteratorFactory::class); } return $this->fileIteratorFactory; } From 79776b3e25a0982b9aec4e589a4714df67147fa3 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 15:10:17 -0500 Subject: [PATCH 032/144] MAGETWO-58637: Refactor Module_Directory, Module_Store --- app/code/Magento/Directory/Block/Data.php | 31 ++++- .../Directory/Test/Unit/Block/DataTest.php | 129 +++++++++--------- .../Magento/Store/Model/StoreResolver.php | 49 +++++-- .../Magento/Directory/Block/DataTest.php | 49 +++++++ .../Magento/Store/Model/StoreResolverTest.php | 45 ++++++ 5 files changed, 220 insertions(+), 83 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php diff --git a/app/code/Magento/Directory/Block/Data.php b/app/code/Magento/Directory/Block/Data.php index 627e34d9ff404..920013c140935 100644 --- a/app/code/Magento/Directory/Block/Data.php +++ b/app/code/Magento/Directory/Block/Data.php @@ -5,6 +5,9 @@ */ namespace Magento\Directory\Block; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class Data extends \Magento\Framework\View\Element\Template { /** @@ -32,6 +35,11 @@ class Data extends \Magento\Framework\View\Element\Template */ protected $directoryHelper; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $json; + /** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Directory\Helper\Data $directoryHelper @@ -110,12 +118,12 @@ public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . $this->_storeManager->getStore()->getCode(); $cache = $this->_configCacheType->load($cacheKey); if ($cache) { - $options = unserialize($cache); + $options = $this->getJson()->decode($cache); } else { $options = $this->getCountryCollection() ->setForegroundCountries($this->getTopDestinations()) ->toOptionArray(); - $this->_configCacheType->save(serialize($options), $cacheKey); + $this->_configCacheType->save($this->getJson()->encode($options), $cacheKey); } $html = $this->getLayout()->createBlock( \Magento\Framework\View\Element\Html\Select::class @@ -160,10 +168,10 @@ public function getRegionHtmlSelect() $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . $this->_storeManager->getStore()->getId(); $cache = $this->_configCacheType->load($cacheKey); if ($cache) { - $options = unserialize($cache); + $options = $this->getJson()->decode($cache); } else { $options = $this->getRegionCollection()->toOptionArray(); - $this->_configCacheType->save(serialize($options), $cacheKey); + $this->_configCacheType->save($this->getJson()->encode($options), $cacheKey); } $html = $this->getLayout()->createBlock( \Magento\Framework\View\Element\Html\Select::class @@ -224,4 +232,19 @@ public function getRegionsJs() \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__); return $regionsJs; } + + /** + * Get json encoder/decoder + * + * @return \Magento\Framework\Json\JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Json\JsonInterface::class); + } + return $this->json; + } } diff --git a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php index 165a369b119b9..1a81ba24d20fe 100644 --- a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php @@ -9,10 +9,9 @@ use Magento\Directory\Helper\Data as HelperData; use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection; use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory; -use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory; use Magento\Framework\App\Cache\Type\Config; use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\Json\EncoderInterface; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\View\Element\Template\Context; use Magento\Framework\View\LayoutInterface; use Magento\Store\Model\ScopeInterface; @@ -25,117 +24,119 @@ class DataTest extends \PHPUnit_Framework_TestCase { /** @var Data */ - protected $model; + private $block; /** @var Context |\PHPUnit_Framework_MockObject_MockObject */ - protected $context; + private $contextMock; /** @var HelperData |\PHPUnit_Framework_MockObject_MockObject */ - protected $helperData; - - /** @var EncoderInterface |\PHPUnit_Framework_MockObject_MockObject */ - protected $jsonEncoder; + private $helperDataMock; /** @var Config |\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheTypeConfig; - - /** @var RegionCollectionFactory |\PHPUnit_Framework_MockObject_MockObject */ - protected $regionCollectionFactory; + private $cacheTypeConfigMock; /** @var CountryCollectionFactory |\PHPUnit_Framework_MockObject_MockObject */ - protected $countryCollectionFactory; + private $countryCollectionFactoryMock; /** @var ScopeConfigInterface |\PHPUnit_Framework_MockObject_MockObject */ - protected $scopeConfig; + private $scopeConfigMock; /** @var StoreManagerInterface |\PHPUnit_Framework_MockObject_MockObject */ - protected $storeManager; + private $storeManagerMock; /** @var Store |\PHPUnit_Framework_MockObject_MockObject */ - protected $store; + private $storeMock; /** @var CountryCollection |\PHPUnit_Framework_MockObject_MockObject */ - protected $countryCollection; + private $countryCollectionMock; /** @var LayoutInterface |\PHPUnit_Framework_MockObject_MockObject */ - protected $layout; + private $layoutMock; protected function setUp() { + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->prepareContext(); - $this->helperData = $this->getMockBuilder(\Magento\Directory\Helper\Data::class) + $this->helperDataMock = $this->getMockBuilder(\Magento\Directory\Helper\Data::class) ->disableOriginalConstructor() ->getMock(); - $this->jsonEncoder = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class) - ->getMockForAbstractClass(); - - $this->cacheTypeConfig = $this->getMockBuilder(\Magento\Framework\App\Cache\Type\Config::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->regionCollectionFactory = $this->getMockBuilder( - \Magento\Directory\Model\ResourceModel\Region\CollectionFactory::class - ) + $this->cacheTypeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Cache\Type\Config::class) ->disableOriginalConstructor() ->getMock(); $this->prepareCountryCollection(); - $this->model = new Data( - $this->context, - $this->helperData, - $this->jsonEncoder, - $this->cacheTypeConfig, - $this->regionCollectionFactory, - $this->countryCollectionFactory + $this->block = $objectManagerHelper->getObject( + Data::class, + [ + 'context' => $this->contextMock, + 'directoryHelper' => $this->helperDataMock, + 'configCacheType' => $this->cacheTypeConfigMock, + 'countryCollectionFactory' => $this->countryCollectionFactoryMock + ] + ); + + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->block, + 'json', + $jsonMock ); } protected function prepareContext() { - $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class) + $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) ->disableOriginalConstructor() ->getMock(); - $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class) + $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class) ->getMockForAbstractClass(); - $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class) + $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class) ->getMockForAbstractClass(); - $this->storeManager->expects($this->any()) + $this->storeManagerMock->expects($this->any()) ->method('getStore') - ->willReturn($this->store); + ->willReturn($this->storeMock); - $this->layout = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class) + $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class) ->getMockForAbstractClass(); - $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class) + $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class) ->disableOriginalConstructor() ->getMock(); - $this->context->expects($this->any()) + $this->contextMock->expects($this->any()) ->method('getScopeConfig') - ->willReturn($this->scopeConfig); + ->willReturn($this->scopeConfigMock); - $this->context->expects($this->any()) + $this->contextMock->expects($this->any()) ->method('getStoreManager') - ->willReturn($this->storeManager); + ->willReturn($this->storeManagerMock); - $this->context->expects($this->any()) + $this->contextMock->expects($this->any()) ->method('getLayout') - ->willReturn($this->layout); + ->willReturn($this->layoutMock); } protected function prepareCountryCollection() { - $this->countryCollection = $this->getMockBuilder( + $this->countryCollectionMock = $this->getMockBuilder( \Magento\Directory\Model\ResourceModel\Country\Collection::class )->disableOriginalConstructor()->getMock(); - $this->countryCollectionFactory = $this->getMockBuilder( + $this->countryCollectionFactoryMock = $this->getMockBuilder( \Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class ) ->disableOriginalConstructor() @@ -144,9 +145,9 @@ protected function prepareCountryCollection() ]) ->getMock(); - $this->countryCollectionFactory->expects($this->any()) + $this->countryCollectionFactoryMock->expects($this->any()) ->method('create') - ->willReturn($this->countryCollection); + ->willReturn($this->countryCollectionMock); } /** @@ -166,46 +167,46 @@ public function testGetCountryHtmlSelect( $options, $resultHtml ) { - $this->helperData->expects($this->once()) + $this->helperDataMock->expects($this->once()) ->method('getDefaultCountry') ->willReturn($defaultCountry); - $this->store->expects($this->once()) + $this->storeMock->expects($this->once()) ->method('getCode') ->willReturn($storeCode); - $this->cacheTypeConfig->expects($this->once()) + $this->cacheTypeConfigMock->expects($this->once()) ->method('load') ->with('DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) ->willReturn(false); - $this->cacheTypeConfig->expects($this->once()) + $this->cacheTypeConfigMock->expects($this->once()) ->method('save') - ->with(serialize($options), 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) + ->with(\Zend_Json::encode($options), 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) ->willReturnSelf(); - $this->scopeConfig->expects($this->once()) + $this->scopeConfigMock->expects($this->once()) ->method('getValue') ->with('general/country/destinations', ScopeInterface::SCOPE_STORE) ->willReturn($destinations); - $this->countryCollection->expects($this->once()) + $this->countryCollectionMock->expects($this->once()) ->method('loadByStore') ->willReturnSelf(); - $this->countryCollection->expects($this->any()) + $this->countryCollectionMock->expects($this->any()) ->method('setForegroundCountries') ->with($expectedDestinations) ->willReturnSelf(); - $this->countryCollection->expects($this->once()) + $this->countryCollectionMock->expects($this->once()) ->method('toOptionArray') ->willReturn($options); $elementHtmlSelect = $this->mockElementHtmlSelect($defaultCountry, $options, $resultHtml); - $this->layout->expects($this->once()) + $this->layoutMock->expects($this->once()) ->method('createBlock') ->willReturn($elementHtmlSelect); - $this->assertEquals($resultHtml, $this->model->getCountryHtmlSelect()); + $this->assertEquals($resultHtml, $this->block->getCountryHtmlSelect()); } /** diff --git a/app/code/Magento/Store/Model/StoreResolver.php b/app/code/Magento/Store/Model/StoreResolver.php index a19d75d9f47e0..a885e59d0bc01 100644 --- a/app/code/Magento/Store/Model/StoreResolver.php +++ b/app/code/Magento/Store/Model/StoreResolver.php @@ -5,8 +5,7 @@ */ namespace Magento\Store\Model; -use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Store\Api\StoreCookieManagerInterface; +use Magento\Framework\Json\JsonInterface; class StoreResolver implements \Magento\Store\Api\StoreResolverInterface { @@ -21,7 +20,7 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface protected $storeRepository; /** - * @var StoreCookieManagerInterface + * @var \Magento\Store\Api\StoreCookieManagerInterface */ protected $storeCookieManager; @@ -31,7 +30,7 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface protected $cache; /** - * @var StoreResolver\ReaderList + * @var \Magento\Store\Model\StoreResolver\ReaderList */ protected $readerList; @@ -50,21 +49,26 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface */ protected $request; + /** + * @var JsonInterface + */ + private $json; + /** * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository - * @param StoreCookieManagerInterface $storeCookieManager + * @param \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager * @param \Magento\Framework\App\RequestInterface $request * @param \Magento\Framework\Cache\FrontendInterface $cache - * @param StoreResolver\ReaderList $readerList + * @param \Magento\Store\Model\StoreResolver\ReaderList $readerList * @param string $runMode * @param null $scopeCode */ public function __construct( \Magento\Store\Api\StoreRepositoryInterface $storeRepository, - StoreCookieManagerInterface $storeCookieManager, + \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\Cache\FrontendInterface $cache, - StoreResolver\ReaderList $readerList, + \Magento\Store\Model\StoreResolver\ReaderList $readerList, $runMode = ScopeInterface::SCOPE_STORE, $scopeCode = null ) { @@ -94,7 +98,7 @@ public function getCurrentStoreId() if ($storeCode) { try { $store = $this->getRequestedStoreByCode($storeCode); - } catch (NoSuchEntityException $e) { + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $store = $this->getDefaultStoreById($defaultStoreId); } @@ -117,10 +121,10 @@ protected function getStoresData() $cacheKey = 'resolved_stores_' . md5($this->runMode . $this->scopeCode); $cacheData = $this->cache->load($cacheKey); if ($cacheData) { - $storesData = unserialize($cacheData); + $storesData = $this->getJson()->decode($cacheData); } else { $storesData = $this->readStoresData(); - $this->cache->save(serialize($storesData), $cacheKey, [self::CACHE_TAG]); + $this->cache->save($this->getJson()->encode($storesData), $cacheKey, [self::CACHE_TAG]); } return $storesData; } @@ -141,14 +145,14 @@ protected function readStoresData() * * @param string $storeCode * @return \Magento\Store\Api\Data\StoreInterface - * @throws NoSuchEntityException + * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function getRequestedStoreByCode($storeCode) { try { $store = $this->storeRepository->getActiveStoreByCode($storeCode); } catch (StoreIsInactiveException $e) { - throw new NoSuchEntityException(__('Requested store is inactive')); + throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested store is inactive')); } return $store; @@ -159,16 +163,31 @@ protected function getRequestedStoreByCode($storeCode) * * @param int $id * @return \Magento\Store\Api\Data\StoreInterface - * @throws NoSuchEntityException + * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function getDefaultStoreById($id) { try { $store = $this->storeRepository->getActiveStoreById($id); } catch (StoreIsInactiveException $e) { - throw new NoSuchEntityException(__('Default store is inactive')); + throw new \Magento\Framework\Exception\NoSuchEntityException(__('Default store is inactive')); } return $store; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php b/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php new file mode 100644 index 0000000000000..8f367163b3ab5 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php @@ -0,0 +1,49 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->block = $this->objectManager->get(\Magento\Directory\Block\Data::class); + } + + public function testGetCountryHtmlSelect() + { + $this->cleanAllCache(); + $result = $this->block->getCountryHtmlSelect(); + $result2 = $this->block->getCountryHtmlSelect(); + $this->assertEquals($result, $result2); + } + + public function testGetRegionHtmlSelect() + { + $this->cleanAllCache(); + $result = $this->block->getRegionHtmlSelect(); + $result2 = $this->block->getRegionHtmlSelect(); + $this->assertEquals($result, $result2); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php new file mode 100644 index 0000000000000..e175fead09cae --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php @@ -0,0 +1,45 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->block = $this->objectManager->get(\Magento\Directory\Block\Data::class); + } + + public function testGetStoreData() + { + $methodGetStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'getStoresData'); + $methodGetStoresData->setAccessible(true); + $methodReadStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'readStoresData'); + $methodReadStoresData->setAccessible(true); + + $storeResover = $this->objectManager->get(\Magento\Store\Model\StoreResolver::class); + + $storesDataRead = $methodReadStoresData->invoke($storeResover); + $this->cleanAllCache(); + $storesData = $methodGetStoresData->invoke($storeResover); + $storesDataCached = $methodGetStoresData->invoke($storeResover); + $this->assertEquals($storesDataRead, $storesData); + $this->assertEquals($storesDataRead, $storesDataCached); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} From 70fb26304ab3865c8d3424435b07d84a5aa4c1f2 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 15:46:49 -0500 Subject: [PATCH 033/144] MAGETWO-58642: Refactor Module_Customer, Framework_Validator --- .../Model/Customer/NotificationStorage.php | 24 +- .../Customer/NotificationStorageTest.php | 38 ++- .../Magento/Framework/Validator/Factory.php | 6 +- .../Validator/Test/Unit/FactoryTest.php | 222 ++++++++---------- 4 files changed, 162 insertions(+), 128 deletions(-) diff --git a/app/code/Magento/Customer/Model/Customer/NotificationStorage.php b/app/code/Magento/Customer/Model/Customer/NotificationStorage.php index 93cb2e24d40f3..551540f950b59 100644 --- a/app/code/Magento/Customer/Model/Customer/NotificationStorage.php +++ b/app/code/Magento/Customer/Model/Customer/NotificationStorage.php @@ -6,6 +6,7 @@ namespace Magento\Customer\Model\Customer; use Magento\Framework\Cache\FrontendInterface; +use Magento\Framework\Json\JsonInterface; class NotificationStorage { @@ -19,6 +20,12 @@ class NotificationStorage /** * @param FrontendInterface $cache */ + + /** + * @var JsonInterface + */ + private $json; + public function __construct(FrontendInterface $cache) { $this->cache = $cache; @@ -34,7 +41,7 @@ public function __construct(FrontendInterface $cache) public function add($notificationType, $customerId) { $this->cache->save( - serialize([ + $this->getJson()->encode([ 'customer_id' => $customerId, 'notification_type' => $notificationType ]), @@ -77,4 +84,19 @@ private function getCacheKey($notificationType, $customerId) { return 'notification_' . $notificationType . '_' . $customerId; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php index 0d2b32f747a80..88ed6583bb1ac 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php @@ -18,37 +18,65 @@ class NotificationStorageTest extends \PHPUnit_Framework_TestCase /** * @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */ - protected $model; + private $model; /** * @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cache; + private $cache; /** * Set up * * @return void */ + + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + protected function setUp() { $this->cache = $this->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class) ->getMockForAbstractClass(); - $this->model = new NotificationStorage($this->cache); + + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->model = $objectManager->getObject( + NotificationStorage::class, + [ + 'cache' => $this->cache + ] + ); + + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $objectManager->setBackwardCompatibleProperty($this->model, 'json', $this->jsonMock); } public function testAdd() { $customerId = 1; $notificationType = 'some_type'; + $this->jsonMock->expects($this->once()) + ->method('encode') + ->with( + [ + 'customer_id' => $customerId, + 'notification_type' => $notificationType + ] + ) + ->willReturn( + '{"customer_id":1,"notification_type":"some_type"}' + ); $this->cache->expects($this->once()) ->method('save') ->with( - serialize([ + json_encode([ 'customer_id' => $customerId, 'notification_type' => $notificationType ]), - $this->getCacheKey($notificationType, $customerId) + $this->getCacheKey($notificationType, $customerId) ); $this->model->add($notificationType, $customerId); } diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index d2e3837d13138..1f54af9ff1953 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -70,9 +70,9 @@ protected function _initializeConfigList() $this->_configFiles = $this->cache->load(self::CACHE_KEY); if (!$this->_configFiles) { $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); - $this->cache->save(serialize($this->_configFiles), self::CACHE_KEY); + $this->cache->save(\Zend_Json::encode($this->_configFiles), self::CACHE_KEY); } else { - $this->_configFiles = unserialize($this->_configFiles); + $this->_configFiles = \Zend_Json::decode($this->_configFiles); } } } @@ -109,7 +109,7 @@ public function getValidatorConfig() { $this->_initializeConfigList(); $this->_initializeDefaultTranslator(); - return $this->_objectManager->create( + return $this->_objectManager->create( \Magento\Framework\Validator\Config::class, ['configFiles' => $this->_configFiles]); } diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index bb829010795e8..36773855fa5cd 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -10,177 +10,161 @@ class FactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\ObjectManagerInterface + * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_objectManager; + private $objectManagerMock; /** - * @var \Magento\Framework\Module\Dir\Reader + * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $_config; + private $readerMock; /** - * @var \Magento\Framework\TranslateInterface + * @var \Magento\Framework\Validator\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $_translateAdapter; + private $validatorConfigMock; /** - * @var \Magento\Framework\Validator\Config + * @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_validatorConfig; - - private $cache; + private $cacheMock; /** - * @var \Magento\Framework\Translate\AdapterInterface|null + * @var \Magento\Framework\Translate\AdapterInterface */ - protected $_defaultTranslator = null; + private $defaultTranslator; /** - * Save default translator + * @var \Magento\Framework\Validator\Factory */ + private $factory; + protected function setUp() { - $this->_defaultTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); - $this->_objectManager = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $this->_validatorConfig = $this->getMockBuilder( - \Magento\Framework\Validator\Config::class - )->setMethods( - ['createValidatorBuilder', 'createValidator'] - )->disableOriginalConstructor()->getMock(); - - $this->_objectManager->expects( - $this->at(0) - )->method( - 'create' - )->with( - \Magento\Framework\Translate\Adapter::class - )->will( - $this->returnValue(new \Magento\Framework\Translate\Adapter()) - ); + $this->defaultTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); - $this->_objectManager->expects( - $this->at(1) - )->method( - 'create' - )->with( + $this->objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $this->validatorConfigMock = $this->getMock( \Magento\Framework\Validator\Config::class, - ['configFiles' => ['/tmp/moduleOne/etc/validation.xml']] - )->will( - $this->returnValue($this->_validatorConfig) + ['createValidatorBuilder', 'createValidator'], + [], + '', + false ); - - // Config mock - $this->_config = $this->getMockBuilder( - \Magento\Framework\Module\Dir\Reader::class - )->setMethods( - ['getConfigurationFiles'] - )->disableOriginalConstructor()->getMock(); - $this->_config->expects( - $this->once() - )->method( - 'getConfigurationFiles' - )->with( - 'validation.xml' - )->will( - $this->returnValue(['/tmp/moduleOne/etc/validation.xml']) + $translateAdapterMock = $this->getMock(\Magento\Framework\Translate\Adapter::class, [], [], '', false); + $this->objectManagerMock->expects($this->at(0)) + ->method('create') + ->with(\Magento\Framework\Translate\Adapter::class) + ->willReturn($translateAdapterMock); + $this->objectManagerMock->expects($this->at(1)) + ->method('create') + ->with( + \Magento\Framework\Validator\Config::class, + ['configFiles' => ['/tmp/moduleOne/etc/validation.xml']] + ) + ->willReturn($this->validatorConfigMock); + $this->readerMock = $this->getMock( + \Magento\Framework\Module\Dir\Reader::class, + ['getConfigurationFiles'], + [], + '', + false ); + $this->cacheMock = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); - // Translate adapter mock - $this->_translateAdapter = $this->getMockBuilder( - \Magento\Framework\TranslateInterface::class - )->disableOriginalConstructor()->getMock(); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->cache = $this->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class) - ->getMockForAbstractClass(); + $this->factory = $objectManager->getObject( + \Magento\Framework\Validator\Factory::class, + [ + 'objectManager' => $this->objectManagerMock, + 'moduleReader' => $this->readerMock, + 'cache' => $this->cacheMock + ] + ); } - /** - * Restore default translator - */ protected function tearDown() { - \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($this->_defaultTranslator); - unset($this->_defaultTranslator); + \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($this->defaultTranslator); + unset($this->defaultTranslator); } - /** - * Test getValidatorConfig created correct validator config. Check that validator translator was initialized. - */ public function testGetValidatorConfig() { - $factory = new \Magento\Framework\Validator\Factory( - $this->_objectManager, - $this->_config, - $this->cache - ); - $actualConfig = $factory->getValidatorConfig(); + $this->readerMock->method('getConfigurationFiles') + ->with('validation.xml') + ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + $actualConfig = $this->factory->getValidatorConfig(); $this->assertInstanceOf( \Magento\Framework\Validator\Config::class, $actualConfig, 'Object of incorrect type was created' ); - - // Check that validator translator was correctly instantiated - $validatorTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); $this->assertInstanceOf( \Magento\Framework\Translate\Adapter::class, - $validatorTranslator, + \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(), 'Default validator translate adapter was not set correctly' ); } - /** - * Test createValidatorBuilder call - */ + public function testGetValidatorConfigCacheNotExist() + { + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(false); + $this->readerMock->expects($this->once()) + ->method('getConfigurationFiles') + ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + $this->cacheMock->expects($this->once()) + ->method('save') + ->with('["\/tmp\/moduleOne\/etc\/validation.xml"]'); + $this->factory->getValidatorConfig(); + $this->factory->getValidatorConfig(); + } + + public function testGetValidatorConfigCacheExist() + { + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn('["\/tmp\/moduleOne\/etc\/validation.xml"]'); + $this->readerMock->expects($this->never()) + ->method('getConfigurationFiles'); + $this->cacheMock->expects($this->never()) + ->method('save'); + $this->factory->getValidatorConfig(); + $this->factory->getValidatorConfig(); + } + public function testCreateValidatorBuilder() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_validatorConfig->expects( - $this->once() - )->method( - 'createValidatorBuilder' - )->with( - 'test', - 'class', - [] - )->will( - $this->returnValue( - $objectManager->getObject(\Magento\Framework\Validator\Builder::class, ['constraints' => []]) - ) - ); - $factory = new \Magento\Framework\Validator\Factory( - $this->_objectManager, - $this->_config, - $this->cache - ); + $this->readerMock->method('getConfigurationFiles') + ->with('validation.xml') + ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + $builderMock = $this->getMock(\Magento\Framework\Validator\Builder::class, [], [], '', false); + $this->validatorConfigMock->expects($this->once()) + ->method('createValidatorBuilder') + ->with('test', 'class', []) + ->willReturn($builderMock); $this->assertInstanceOf( \Magento\Framework\Validator\Builder::class, - $factory->createValidatorBuilder('test', 'class', []) + $this->factory->createValidatorBuilder('test', 'class', []) ); } - /** - * Test createValidatorBuilder call - */ public function testCreateValidator() { - $this->_validatorConfig->expects( - $this->once() - )->method( - 'createValidator' - )->with( - 'test', - 'class', - [] - )->will( - $this->returnValue(new \Magento\Framework\Validator()) - ); - $factory = new \Magento\Framework\Validator\Factory( - $this->_objectManager, - $this->_config, - $this->cache + $this->readerMock->method('getConfigurationFiles') + ->with('validation.xml') + ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + $validatorMock = $this->getMock(\Magento\Framework\Validator::class, [], [], '', false); + $this->validatorConfigMock->expects($this->once()) + ->method('createValidator') + ->with('test', 'class', []) + ->willReturn($validatorMock); + $this->assertInstanceOf( + \Magento\Framework\Validator::class, + $this->factory->createValidator('test', 'class', []) ); - $this->assertInstanceOf(\Magento\Framework\Validator::class, $factory->createValidator('test', 'class', [])); } } From f621a2b12fb3a00ded2e8a6403730c20a8777ba7 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 16:12:50 -0500 Subject: [PATCH 034/144] MAGETWO-58642: Refactor Module_Customer, Framework_Validator - fixed static test failures --- .../Magento/Customer/Model/Customer/NotificationStorage.php | 4 ++++ .../Test/Unit/Model/Customer/NotificationStorageTest.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/Model/Customer/NotificationStorage.php b/app/code/Magento/Customer/Model/Customer/NotificationStorage.php index 551540f950b59..7c77fe9c8d500 100644 --- a/app/code/Magento/Customer/Model/Customer/NotificationStorage.php +++ b/app/code/Magento/Customer/Model/Customer/NotificationStorage.php @@ -26,6 +26,10 @@ class NotificationStorage */ private $json; + /** + * NotificationStorage constructor. + * @param FrontendInterface $cache + */ public function __construct(FrontendInterface $cache) { $this->cache = $cache; diff --git a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php index 88ed6583bb1ac..84bad7fe88dc0 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php @@ -76,7 +76,7 @@ public function testAdd() 'customer_id' => $customerId, 'notification_type' => $notificationType ]), - $this->getCacheKey($notificationType, $customerId) + $this->getCacheKey($notificationType, $customerId) ); $this->model->add($notificationType, $customerId); } From 0a6b5027e27779dd10ff80cd4fa1e2313bfb044a Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 17:24:39 -0500 Subject: [PATCH 035/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- .../Unit/Model/ProductTypes/ConfigTest.php | 12 +- .../Test/Unit/Model/Config/DataTest.php | 2 +- .../Sales/Test/Unit/Model/Config/DataTest.php | 2 +- .../Framework/App/Route/ConfigTest.php | 41 +++--- .../Magento/Store/Model/StoreResolverTest.php | 45 +++++++ .../App/Test/Unit/Config/ScopePoolTest.php | 121 +++++++++++------- .../Unit/ResourceConnection/ConfigTest.php | 4 +- .../App/Test/Unit/Route/ConfigTest.php | 28 ++-- .../App/Test/Unit/Router/ActionListTest.php | 5 +- .../Config/Test/Unit/Data/ScopedTest.php | 2 +- .../Mview/Test/Unit/Config/DataTest.php | 12 +- .../Validator/Test/Unit/FactoryTest.php | 2 - 12 files changed, 165 insertions(+), 111 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 711163f629a98..101057271a436 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -8,14 +8,14 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\ProductTypes\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $readerMock; + private $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; /** * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject @@ -23,9 +23,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase private $jsonMock; /** - * @var \Magento\Catalog\Model\ProductTypes\Config + * @var \Magento\Catalog\Model\ProductTypes\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $config; + private $config; protected function setUp() { 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 63cea630b563d..e8c776d7aa39a 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -63,7 +63,7 @@ protected function setUp() ); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Framework\Mview\Config\Data::setJson($this->jsonMock); + \Magento\Indexer\Model\Config\Data::setJson($this->jsonMock); } public function testConstructorWithCache() diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index 11f16ad734253..9695e4e83a0e2 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -32,7 +32,7 @@ protected function setUp() )->disableOriginalConstructor()->getMock(); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Framework\Mview\Config\Data::setJson($this->jsonMock); + \Magento\Sales\Model\Config\Data::setJson($this->jsonMock); } public function testGet() diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php index ef8ada2514d9b..af99b7226bc75 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php @@ -8,36 +8,18 @@ use Magento\TestFramework\Helper\Bootstrap; use Magento\Framework\App\Cache\Frontend\Pool; use Magento\Framework\App\Route\Config; +use Magento\TestFramework\ObjectManager; class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var Config + * @var ObjectManager */ - private $config1; - - /** - * @var Config - */ - private $config2; + private $objectManager; protected function setUp() { - /** @var \Magento\TestFramework\ObjectManager $objectManager */ - $objectManager = Bootstrap::getObjectManager(); - - /** @var Pool $cachePool */ - $cachePool = $objectManager->get(Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - - $objectManager->removeSharedInstance(Config::class); - $this->config1 = $objectManager->get(Config::class); - - $objectManager->removeSharedInstance(Config::class); - $this->config2 = $objectManager->get(Config::class); + $this->objectManager = Bootstrap::getObjectManager(); } /** @@ -47,9 +29,10 @@ protected function setUp() */ public function testGetRouteFrontName($route, $scope) { + $this->cleanCache(); $this->assertEquals( - $this->config1->getRouteFrontName($route, $scope), - $this->config2->getRouteFrontName($route, $scope) + $this->objectManager->create(Config::class)->getRouteFrontName($route, $scope), + $this->objectManager->create(Config::class)->getRouteFrontName($route, $scope) ); } @@ -60,4 +43,14 @@ public function getRouteFrontNameDataProvider() ['catalog', 'frontend'], ]; } + + private function cleanCache() + { + /** @var Pool $cachePool */ + $cachePool = $this->objectManager->get(Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } } diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php new file mode 100644 index 0000000000000..e175fead09cae --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php @@ -0,0 +1,45 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->block = $this->objectManager->get(\Magento\Directory\Block\Data::class); + } + + public function testGetStoreData() + { + $methodGetStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'getStoresData'); + $methodGetStoresData->setAccessible(true); + $methodReadStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'readStoresData'); + $methodReadStoresData->setAccessible(true); + + $storeResover = $this->objectManager->get(\Magento\Store\Model\StoreResolver::class); + + $storesDataRead = $methodReadStoresData->invoke($storeResover); + $this->cleanAllCache(); + $storesData = $methodGetStoresData->invoke($storeResover); + $storesDataCached = $methodGetStoresData->invoke($storeResover); + $this->assertEquals($storesDataRead, $storesData); + $this->assertEquals($storesDataRead, $storesDataCached); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} 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 6232122eaaa14..d274b650f56b7 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -92,79 +92,106 @@ protected function setUp() } /** - * @dataProvider getScopeDataProvider - * * @param string $scopeType * @param string $scope * @param array $data - * @param string|null $cachedData + * @dataProvider getScopeConfigNotCachedProvider */ - public function testGetScope($scopeType, $scope, array $data, $cachedData) + public function testGetScopeConfigNotCached($scopeType, $scope, array $data) { $scopeCode = $scope instanceof \Magento\Framework\App\ScopeInterface ? $scope->getCode() : $scope; $cacheKey = "test_cache_id|{$scopeType}|{$scopeCode}|baseUrl"; - - $this->_readerPool->expects( - $this->any() - )->method( - 'getReader' - )->with( - $scopeType - )->will( - $this->returnValue($this->_reader) - ); - $this->_cache->expects($this->once())->method('load')->with($cacheKey)->will($this->returnValue($cachedData)); - - if (!$cachedData) { - $this->_reader->expects($this->once())->method('read')->with('testScope')->will($this->returnValue($data)); - $this->_cache->expects($this->once()) - ->method('save') - ->with( - json_encode($data), - $cacheKey, - [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] - ); - $this->jsonMock->method('encode') - ->willReturn(json_encode($data)); - } else { - $this->jsonMock->method('decode') - ->willReturn($data); - } - $configData = $this->getMockBuilder(\Magento\Framework\App\Config\Data::class) - ->disableOriginalConstructor() - ->getMock(); - $this->_dataFactory->expects( - $this->once() - )->method( - 'create' - )->with( - ['data' => $data] - )->will( - $this->returnValue($configData) - ); + $this->_readerPool->expects($this->any()) + ->method('getReader') + ->with($scopeType) + ->willReturn($this->_reader); + $this->_cache->expects($this->once()) + ->method('load') + ->with($cacheKey) + ->willReturn(null); + $this->_reader->expects($this->once()) + ->method('read') + ->with('testScope') + ->willReturn($data); + $jsonString = json_encode($data); + $this->jsonMock->method('encode') + ->with($data) + ->willReturn($jsonString); + $this->_cache->expects($this->once()) + ->method('save') + ->with( + $jsonString, + $cacheKey, + [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] + ); + $configData = $this->getMock(\Magento\Framework\App\Config\Data::class, [], [], '', false); + $this->_dataFactory->expects($this->once()) + ->method('create') + ->with(['data' => $data]) + ->willReturn($configData); $this->assertInstanceOf( \Magento\Framework\App\Config\DataInterface::class, $this->_object->getScope($scopeType, $scope) ); - - // second call to check caching $this->assertInstanceOf( \Magento\Framework\App\Config\DataInterface::class, $this->_object->getScope($scopeType, $scope) ); } - public function getScopeDataProvider() + public function getScopeConfigNotCachedProvider() { $baseScope = $this->getMockForAbstractClass(\Magento\Framework\App\ScopeInterface::class); $baseScope->expects($this->any())->method('getCode')->will($this->returnValue('testScope')); return [ ['scopeType1', 'testScope', ['key' => 'value'], null], - ['scopeType2', 'testScope', ['key' => 'value'], '{"key":"value"}'], ['scopeType1', $baseScope, ['key' => 'value'], null] ]; } + /** + * @param string $scopeType + * @param string $scope + * @param array $data + * @param string $cachedData + * @dataProvider getScopeConfigCachedProvider + */ + public function testGetScopeConfigCached($scopeType, $scope, array $data, $cachedData) + { + $scopeCode = $scope instanceof \Magento\Framework\App\ScopeInterface ? $scope->getCode() : $scope; + $cacheKey = "test_cache_id|{$scopeType}|{$scopeCode}|baseUrl"; + $this->_readerPool->expects($this->any()) + ->method('getReader') + ->with($scopeType) + ->willReturn($this->_reader); + $this->_cache->expects($this->once()) + ->method('load') + ->with($cacheKey) + ->willReturn($cachedData); + $this->jsonMock->method('decode') + ->willReturn($data); + $configData = $this->getMock(\Magento\Framework\App\Config\Data::class, [], [], '', false); + $this->_dataFactory->expects($this->once()) + ->method('create') + ->with(['data' => $data]) + ->willReturn($configData); + $this->assertInstanceOf( + \Magento\Framework\App\Config\DataInterface::class, + $this->_object->getScope($scopeType, $scope) + ); + $this->assertInstanceOf( + \Magento\Framework\App\Config\DataInterface::class, + $this->_object->getScope($scopeType, $scope) + ); + } + + public function getScopeConfigCachedProvider() + { + return [ + ['scopeType2', 'testScope', ['key' => 'value'], '{"key":"value"}'], + ]; + } + public function testClean() { $this->_cache->expects( diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php index 5e49f4b8ef67f..d4284491df880 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php @@ -64,10 +64,12 @@ protected function setUp() 'validResource' => ['connection' => 'validConnectionName'], ]; + $jsonString = json_encode($this->resourcesConfig); $this->cacheMock->expects($this->any()) ->method('load') - ->willReturn(json_encode($this->resourcesConfig)); + ->willReturn($jsonString); $this->jsonMock->method('decode') + ->with($jsonString) ->willReturn($this->resourcesConfig); $deploymentConfigMock = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 3bed2826a2981..6adb7fac6078b 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -38,16 +38,10 @@ protected function setUp() $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->_areaList = $this->getMock(\Magento\Framework\App\AreaList::class, [], [], '', false); - $this->_configScopeMock->expects( - $this->any() - )->method( - 'getCurrentScope' - )->will( - $this->returnValue('areaCode') - ); - + $this->_configScopeMock->expects($this->any()) + ->method('getCurrentScope') + ->willReturn('areaCode'); $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_config = $objectManager->getObject( \Magento\Framework\App\Route\Config::class, [ @@ -57,19 +51,15 @@ protected function setUp() 'areaList' => $this->_areaList ] ); - $jsonMock = $this->getMock(\Magento\Framework\Json\Json::class, [], [], '', false); $objectManager->setBackwardCompatibleProperty($this->_config, 'json', $jsonMock); - - $json = new \Magento\Framework\Json\Json(); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) use ($json) { - return $json->encode($string); + ->willReturnCallback(function ($string) { + return json_encode($string); }); $jsonMock->method('decode') - ->willReturnCallback(function ($string) use ($json) { - return $json->decode($string); + ->willReturnCallback(function ($string) { + return json_decode($string); }); } @@ -78,7 +68,7 @@ public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn(json_encode(['expected'])); + ->willReturn('["expected"]'); $this->assertEquals('routerCode', $this->_config->getRouteFrontName('routerCode')); } @@ -113,7 +103,7 @@ public function testGetRouteByFrontNameNoCache() $this->_cacheMock->expects($this->once()) ->method('load') ->with('scope::RoutesConfig') - ->willReturn(json_encode(false)); + ->willReturn('false'); $routes = [ 'routerCode' => [ diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 3aa0cde1be665..bf9ba1d47b1ba 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -1,7 +1,5 @@ method('save'); $this->readerMock->expects($this->once()) ->method('getActionFiles') - ->will($this->returnValue('data')); + ->willReturn('data') + ; $this->createActionListInstance(); } 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 2189e90ae6e69..e4f0696a98460 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -65,7 +65,7 @@ public function testGetConfigByPath($path, $expectedValue, $default) ]; $this->_cacheMock->expects($this->once()) ->method('load') - ->willReturn(''); + ->willReturn(false); $this->_readerMock->expects($this->once()) ->method('read') ->willReturn([]); diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index e87b88f86fea4..8dd9dfd50ec93 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -10,32 +10,32 @@ class DataTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\Mview\Config\Data */ - protected $config; + private $config; /** * @var \Magento\Framework\Mview\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $reader; + private $reader; /** * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cache; + private $cache; /** * @var \Magento\Framework\Mview\View\State\CollectionInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $stateCollection; + private $stateCollection; /** * @var string */ - protected $cacheId = 'mview_config'; + private $cacheId = 'mview_config'; /** * @var string */ - protected $views = ['view1' => [], 'view3' => []]; + private $views = ['view1' => [], 'view3' => []]; /** * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index c51318c34a802..abc1c6d7a92d9 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -1,7 +1,5 @@ Date: Thu, 22 Sep 2016 17:15:55 -0500 Subject: [PATCH 036/144] MAGETWO-58642: Refactor Module_Customer, Framework_Validator - Refactoring, updating unit tests --- .../Magento/Framework/Validator/Factory.php | 26 +++++++++++- .../Validator/Test/Unit/FactoryTest.php | 40 +++++++++++++++---- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index 1f54af9ff1953..4b552ff02727e 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -11,6 +11,7 @@ namespace Magento\Framework\Validator; use Magento\Framework\Cache\FrontendInterface; +use Magento\Framework\Json\JsonInterface; class Factory { @@ -44,6 +45,11 @@ class Factory */ private $cache; + /** + * @var JsonInterface + */ + private $json; + /** * Initialize dependencies * @@ -70,9 +76,9 @@ protected function _initializeConfigList() $this->_configFiles = $this->cache->load(self::CACHE_KEY); if (!$this->_configFiles) { $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); - $this->cache->save(\Zend_Json::encode($this->_configFiles), self::CACHE_KEY); + $this->cache->save($this->getJson()->encode($this->_configFiles), self::CACHE_KEY); } else { - $this->_configFiles = \Zend_Json::decode($this->_configFiles); + $this->_configFiles = $this->getJson()->decode($this->_configFiles); } } } @@ -140,4 +146,20 @@ public function createValidator($entityName, $groupName, array $builderConfig = $this->_initializeDefaultTranslator(); return $this->getValidatorConfig()->createValidator($entityName, $groupName, $builderConfig); } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } + diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index 36773855fa5cd..10ad0cbb8161e 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -29,6 +29,11 @@ class FactoryTest extends \PHPUnit_Framework_TestCase */ private $cacheMock; + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; + /** * @var \Magento\Framework\Translate\AdapterInterface */ @@ -39,6 +44,16 @@ class FactoryTest extends \PHPUnit_Framework_TestCase */ private $factory; + /** + * @var string + */ + private $jsonString = '["\/tmp\/moduleOne\/etc\/validation.xml"]'; + + /** + * @var array + */ + private $data = ['/tmp/moduleOne/etc/validation.xml']; + protected function setUp() { $this->defaultTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); @@ -60,7 +75,7 @@ protected function setUp() ->method('create') ->with( \Magento\Framework\Validator\Config::class, - ['configFiles' => ['/tmp/moduleOne/etc/validation.xml']] + ['configFiles' => $this->data] ) ->willReturn($this->validatorConfigMock); $this->readerMock = $this->getMock( @@ -82,6 +97,9 @@ protected function setUp() 'cache' => $this->cacheMock ] ); + + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $objectManager->setBackwardCompatibleProperty($this->factory, 'json', $this->jsonMock); } protected function tearDown() @@ -94,7 +112,7 @@ public function testGetValidatorConfig() { $this->readerMock->method('getConfigurationFiles') ->with('validation.xml') - ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + ->willReturn($this->data); $actualConfig = $this->factory->getValidatorConfig(); $this->assertInstanceOf( \Magento\Framework\Validator\Config::class, @@ -115,10 +133,14 @@ public function testGetValidatorConfigCacheNotExist() ->willReturn(false); $this->readerMock->expects($this->once()) ->method('getConfigurationFiles') - ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + ->willReturn($this->data); $this->cacheMock->expects($this->once()) ->method('save') - ->with('["\/tmp\/moduleOne\/etc\/validation.xml"]'); + ->with($this->jsonString); + $this->jsonMock->expects($this->once()) + ->method('encode') + ->with($this->data) + ->willReturn($this->jsonString); $this->factory->getValidatorConfig(); $this->factory->getValidatorConfig(); } @@ -127,11 +149,15 @@ public function testGetValidatorConfigCacheExist() { $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn('["\/tmp\/moduleOne\/etc\/validation.xml"]'); + ->willReturn($this->jsonString); // why json mock??? $this->readerMock->expects($this->never()) ->method('getConfigurationFiles'); $this->cacheMock->expects($this->never()) ->method('save'); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->with($this->jsonString) + ->willReturn($this->data); $this->factory->getValidatorConfig(); $this->factory->getValidatorConfig(); } @@ -140,7 +166,7 @@ public function testCreateValidatorBuilder() { $this->readerMock->method('getConfigurationFiles') ->with('validation.xml') - ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + ->willReturn($this->data); $builderMock = $this->getMock(\Magento\Framework\Validator\Builder::class, [], [], '', false); $this->validatorConfigMock->expects($this->once()) ->method('createValidatorBuilder') @@ -156,7 +182,7 @@ public function testCreateValidator() { $this->readerMock->method('getConfigurationFiles') ->with('validation.xml') - ->willReturn(['/tmp/moduleOne/etc/validation.xml']); + ->willReturn($this->data); $validatorMock = $this->getMock(\Magento\Framework\Validator::class, [], [], '', false); $this->validatorConfigMock->expects($this->once()) ->method('createValidator') From 3b10f4a1e605fb4d215033e591fe3d9287a7a59b Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 26 Sep 2016 10:41:20 -0500 Subject: [PATCH 037/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- .../Magento/Framework/App/Test/Unit/Route/ConfigTest.php | 4 ++-- .../Magento/Framework/Validator/Test/Unit/FactoryTest.php | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 6adb7fac6078b..95101b42d2143 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -13,7 +13,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $_config; /** - * @var Cache_Mock_Wrapper + * @var \Magento\Framework\App\Route\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ protected $_readerMock; @@ -59,7 +59,7 @@ protected function setUp() }); $jsonMock->method('decode') ->willReturnCallback(function ($string) { - return json_decode($string); + return json_decode($string, true); }); } diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index abc1c6d7a92d9..0c60f4bcdf9e9 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\Validator\Test\Unit; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class FactoryTest extends \PHPUnit_Framework_TestCase { /** From 13b14b0aa74085e277731723415ed05455d70d6e Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 26 Sep 2016 15:55:34 -0500 Subject: [PATCH 038/144] MAGETWO-58642: Refactor Module_Customer, Framework_Validator Merging with remote tracking branch, conflicts: app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php --- .../Customer/NotificationStorageTest.php | 70 +++++++------------ 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php index 84bad7fe88dc0..6c391d2d6d55e 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php @@ -7,109 +7,87 @@ use Magento\Customer\Model\Customer\NotificationStorage; -/** - * Class NotificationStorageTest - * - * Test for class \Magento\Customer\Model\Customer\NotificationStorage - */ class NotificationStorageTest extends \PHPUnit_Framework_TestCase { /** - * @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject + * @var NotificationStorage */ - private $model; + private $notificationStorage; /** * @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $cache; - - /** - * Set up - * - * @return void - */ + private $cacheMock; /** * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ private $jsonMock; - + protected function setUp() { - $this->cache = $this->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class) - ->getMockForAbstractClass(); - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - - $this->model = $objectManager->getObject( + $this->cacheMock = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); + $this->notificationStorage = $objectManager->getObject( NotificationStorage::class, - [ - 'cache' => $this->cache - ] + ['cache' => $this->cacheMock] ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $objectManager->setBackwardCompatibleProperty($this->model, 'json', $this->jsonMock); + $objectManager->setBackwardCompatibleProperty($this->notificationStorage, 'json', $this->jsonMock); } public function testAdd() { $customerId = 1; $notificationType = 'some_type'; + $data = [ + 'customer_id' => $customerId, + 'notification_type' => $notificationType + ]; + $jsonString = json_encode($data); $this->jsonMock->expects($this->once()) ->method('encode') - ->with( - [ - 'customer_id' => $customerId, - 'notification_type' => $notificationType - ] - ) - ->willReturn( - '{"customer_id":1,"notification_type":"some_type"}' - ); - $this->cache->expects($this->once()) + ->with($data) + ->willReturn($jsonString); + $this->cacheMock->expects($this->once()) ->method('save') ->with( - json_encode([ - 'customer_id' => $customerId, - 'notification_type' => $notificationType - ]), + $jsonString, $this->getCacheKey($notificationType, $customerId) ); - $this->model->add($notificationType, $customerId); + $this->notificationStorage->add($notificationType, $customerId); } public function testIsExists() { $customerId = 1; $notificationType = 'some_type'; - $this->cache->expects($this->once()) + $this->cacheMock->expects($this->once()) ->method('test') ->with($this->getCacheKey($notificationType, $customerId)) ->willReturn(true); - $this->assertTrue($this->model->isExists($notificationType, $customerId)); + $this->assertTrue($this->notificationStorage->isExists($notificationType, $customerId)); } public function testRemove() { $customerId = 1; $notificationType = 'some_type'; - $this->cache->expects($this->once()) + $this->cacheMock->expects($this->once()) ->method('remove') ->with($this->getCacheKey($notificationType, $customerId)); - $this->model->remove($notificationType, $customerId); + $this->notificationStorage->remove($notificationType, $customerId); } /** - * Retrieve cache key + * Get cache key * * @param string $notificationType * @param string $customerId * @return string */ - protected function getCacheKey($notificationType, $customerId) + private function getCacheKey($notificationType, $customerId) { return 'notification_' . $notificationType . '_' . $customerId; } From 7cab614809669125921a9a0b8ad970579103b019 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 26 Sep 2016 16:33:51 -0500 Subject: [PATCH 039/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Fixing tests --- lib/internal/Magento/Framework/Validator/Factory.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index c662e7a7c435b..3ac6829f0dbe0 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -12,6 +12,9 @@ use Magento\Framework\Cache\FrontendInterface; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class Factory { /** cache key */ From 60c7ac8305e5ad1f70c1015de86211a79ad0152c Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 09:34:14 -0500 Subject: [PATCH 040/144] MAGETWO-58639: Refactor Framework_View Fixing formatting --- .../Framework/View/_files/UiComponent/expected/config.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml index 7df98a51fe0b3..467afb2004cbe 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml +++ b/dev/tests/integration/testsuite/Magento/Framework/View/_files/UiComponent/expected/config.xml @@ -10,7 +10,8 @@ Magento\Catalog\Block\Adminhtml\Product\Edit\Button\CreateCategory - + new_category_form.new_category_form_data_source - + + From ac2ab05cad03bf9ac5ed7bf0b8c175421cd11de5 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 09:48:02 -0500 Subject: [PATCH 041/144] MAGETWO-58640: Refactor Framework_Translate Refactoring tests --- .../Magento/Framework/Test/Unit/TranslateTest.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php b/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php index 178d0e20629db..8bbc7f433c9c4 100644 --- a/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php +++ b/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php @@ -60,6 +60,7 @@ class TranslateTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->viewDesign = $this->getMock(\Magento\Framework\View\DesignInterface::class, [], [], '', false); $this->cache = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class, [], [], '', false); $this->viewFileSystem = $this->getMock(\Magento\Framework\View\FileSystem::class, [], [], '', false); @@ -106,16 +107,16 @@ protected function setUp() $this->packDictionary ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock = $this->getMock(JsonInterface::class); $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); + ->willReturnCallback(function ($data) { + return json_encode($data); }); $jsonMock->method('decode') ->willReturnCallback(function ($string) { return json_decode($string, true); }); - (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->setBackwardCompatibleProperty( + $objectManager->setBackwardCompatibleProperty( $this->translate, 'json', $jsonMock @@ -135,7 +136,7 @@ public function testLoadData($area, $forceReload, $cachedData) $this->cache->expects($this->exactly($forceReload ? 0 : 1)) ->method('load') - ->will($this->returnValue(\Zend_Json::encode($cachedData))); + ->will($this->returnValue(json_encode($cachedData))); if (!$forceReload && $cachedData !== false) { $this->translate->loadData($area, $forceReload); @@ -238,7 +239,7 @@ public function testGetData($data, $result) { $this->cache->expects($this->once()) ->method('load') - ->will($this->returnValue(\Zend_Json::encode($data))); + ->will($this->returnValue(json_encode($data))); $this->expectsSetConfig('themeId'); $this->translate->loadData('frontend'); $this->assertEquals($result, $this->translate->getData()); From 87ade2d864e461d364e05a3773a5c2d7bef394d9 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 14:18:22 -0500 Subject: [PATCH 042/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring --- .../Unit/Model/ProductTypes/ConfigTest.php | 13 +++++- .../Cron/Test/Unit/Model/Config/DataTest.php | 17 +++++++- .../Test/Unit/Model/Address/ConfigTest.php | 13 +++++- .../Country/Postcode/Config/DataTest.php | 13 +++++- .../Model/Entity/Attribute/ConfigTest.php | 13 +++++- .../Test/Unit/Model/Config/DataTest.php | 14 ++++++- .../Sales/Test/Unit/Model/Config/DataTest.php | 18 +++++++-- .../Magento/Framework/App/Config/Initial.php | 20 ++-------- .../Framework/App/Router/ActionList.php | 20 ++-------- .../App/Test/Unit/Config/InitialTest.php | 20 +++++++++- .../App/Test/Unit/Router/ActionListTest.php | 7 +++- .../Magento/Framework/Config/Data.php | 20 ++-------- .../Config/Test/Unit/Data/ScopedTest.php | 27 ++++++++++++- .../Framework/Config/Test/Unit/DataTest.php | 13 +++++- .../Mview/Test/Unit/Config/DataTest.php | 13 +++++- .../Unit/Helper/ObjectManager.php | 40 +++++++++++++++++++ 16 files changed, 216 insertions(+), 65 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 101057271a436..c6e5618947d03 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -7,6 +7,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Catalog\Model\ProductTypes\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ @@ -29,6 +34,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock( \Magento\Catalog\Model\ProductTypes\Config\Reader::class, [], @@ -38,7 +44,12 @@ protected function setUp() ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Catalog\Model\ProductTypes\Config::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } /** diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 4a4c6c2c392eb..f816163d1397e 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -7,6 +7,16 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + + public function setUp() + { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + } + /** * Testing return jobs from different sources (DB, XML) */ @@ -38,7 +48,7 @@ public function testGetJobs() $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Cron\Model\Config\Data::setJson($jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $jsonMock]); $jsonMock->method('decode') ->willReturn($jobs); @@ -54,4 +64,9 @@ public function testGetJobs() $result = $configData->getJobs(); $this->assertEquals($expected, $result); } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); + } } diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index c8f3c39787c23..51b1f559b2769 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -7,6 +7,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -54,6 +59,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false); $this->_scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); @@ -98,7 +104,7 @@ protected function setUp() ); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Customer\Model\Address\Config::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); $this->jsonMock->method('encode') ->willReturn(json_encode($fixtureConfigData)); @@ -115,6 +121,11 @@ protected function setUp() ); } + public function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testGetStore() { $this->assertEquals($this->_storeMock, $this->_model->getStore()); diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 5ebbf89ee22ae..ca6e95585a6a8 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -7,6 +7,11 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Directory\Model\Country\Postcode\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ @@ -24,6 +29,7 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock( \Magento\Directory\Model\Country\Postcode\Config\Reader::class, [], @@ -39,7 +45,7 @@ protected function setUp() false ); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Directory\Model\Country\Postcode\Config\Data::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } public function testGet() @@ -54,4 +60,9 @@ public function testGet() $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data($this->readerMock, $this->cacheMock); $this->assertEquals($expected, $configData->get()); } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); + } } diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index f67bdaaa22f87..c08959e625fa5 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -11,6 +11,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Eav\Model\Entity\Attribute\Config */ @@ -43,6 +48,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_attribute = $this->getMock(\Magento\Eav\Model\Entity\Attribute::class, [], [], '', false); $this->_entityType = $this->getMock(\Magento\Eav\Model\Entity\Type::class, [], [], '', false); $this->_readerMock = $this->getMock( @@ -63,7 +69,7 @@ protected function setUp() $jsonMock->method('decode') ->willReturn([]); - \Magento\Eav\Model\Entity\Attribute\Config::setJson($jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, @@ -71,6 +77,11 @@ protected function setUp() ); } + public function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testGetLockedFieldsEmpty() { $this->_entityType->expects($this->once())->method('getEntityTypeCode')->will($this->returnValue('test_code')); 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 e8c776d7aa39a..b1c9a919765a2 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -7,6 +7,11 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Indexer\Model\Config\Data */ @@ -44,6 +49,7 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reader = $this->getMock(\Magento\Framework\Indexer\Config\Reader::class, ['read'], [], '', false); $this->cache = $this->getMockForAbstractClass( \Magento\Framework\Config\CacheInterface::class, @@ -61,9 +67,13 @@ protected function setUp() '', false ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Indexer\Model\Config\Data::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } public function testConstructorWithCache() diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index 9695e4e83a0e2..ca85fcc9686d2 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -7,15 +7,20 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; + private $_readerMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_cacheMock; + private $_cacheMock; /** * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject @@ -24,15 +29,20 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMockBuilder( \Magento\Sales\Model\Config\Reader::class )->disableOriginalConstructor()->getMock(); $this->_cacheMock = $this->getMockBuilder( \Magento\Framework\App\Cache\Type\Config::class )->disableOriginalConstructor()->getMock(); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Sales\Model\Config\Data::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } public function testGet() diff --git a/lib/internal/Magento/Framework/App/Config/Initial.php b/lib/internal/Magento/Framework/App/Config/Initial.php index 27af55e61e96f..45b1f31b944dd 100644 --- a/lib/internal/Magento/Framework/App/Config/Initial.php +++ b/lib/internal/Magento/Framework/App/Config/Initial.php @@ -34,7 +34,7 @@ class Initial /** * @var JsonInterface */ - private static $json; + private $json; /** * @param \Magento\Framework\App\Config\Initial\Reader $reader @@ -91,22 +91,10 @@ public function getMetadata() */ private function getJson() { - if (self::$json === null) { - self::$json = \Magento\Framework\App\ObjectManager::getInstance() + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() ->get(JsonInterface::class); } - return self::$json; - } - - /** - * Set json encoder/decoder - * - * @param JsonInterface $json - * @return void - * @deprecated - */ - public static function setJson(JsonInterface $json) - { - self::$json = $json; + return $this->json; } } diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index 6c04f09159f7e..f2f0a5015fb91 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -39,7 +39,7 @@ class ActionList /** * @var JsonInterface */ - private static $json; + private $json; /** * @param \Magento\Framework\Config\CacheInterface $cache @@ -107,22 +107,10 @@ public function get($module, $area, $namespace, $action) */ private function getJson() { - if (self::$json === null) { - self::$json = \Magento\Framework\App\ObjectManager::getInstance() + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() ->get(JsonInterface::class); } - return self::$json; - } - - /** - * Set json encoder/decoder - * - * @param JsonInterface $json - * @return void - * @deprecated - */ - public static function setJson(JsonInterface $json) - { - self::$json = $json; + return $this->json; } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index 66325090df656..ec6941c8dee48 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -57,8 +57,10 @@ protected function setUp() $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); $jsonMock->method('decode') ->willReturn($this->data); - - \Magento\Framework\App\Config\Initial::setJson($jsonMock); + + $this->prepareObjectManager([ + [\Magento\Framework\Json\JsonInterface::class, $jsonMock] + ]); $this->config = new \Magento\Framework\App\Config\Initial( $this->readerMock, @@ -89,4 +91,18 @@ public function testGetMetadata() { $this->assertEquals(['metadata'], $this->config->getMetadata()); } + + /** + * @param array $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index bf9ba1d47b1ba..c0604b1e953d1 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -50,6 +50,12 @@ protected function setUp() false ); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } public function testConstructActionsCached() @@ -169,7 +175,6 @@ public function getDataProvider() private function createActionListInstance() { - \Magento\Framework\App\Router\ActionList::setJson($this->jsonMock); $this->actionList = $this->objectManager->getObject( \Magento\Framework\App\Router\ActionList::class, [ diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 5f52d5a5d62bf..83bd95543bdb7 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -67,7 +67,7 @@ class Data implements \Magento\Framework\Config\DataInterface /** * @var JsonInterface */ - protected static $json; + private $json; /** * Constructor @@ -156,22 +156,10 @@ public function reset() */ protected function getJson() { - if (self::$json === null) { - self::$json = \Magento\Framework\App\ObjectManager::getInstance() + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() ->get(JsonInterface::class); } - return self::$json; - } - - /** - * Set json encoder/decoder - * - * @param JsonInterface $json - * @return void - * @deprecated - */ - public static function setJson(JsonInterface $json) - { - self::$json = $json; + return $this->json; } } 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 e4f0696a98460..9867c4d1ad1ee 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -7,6 +7,11 @@ class ScopedTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\Config\Data\Scoped */ @@ -34,11 +39,13 @@ class ScopedTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); $this->_model = new \Magento\Framework\Config\Data\Scoped( $this->_readerMock, @@ -46,7 +53,11 @@ protected function setUp() $this->_cacheMock, 'tag' ); - \Magento\Framework\Config\Data\Scoped::setJson($this->jsonMock); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } /** @@ -175,4 +186,18 @@ public function testGetScopeSwitchingWithCachedData() /** test preventing of double config data loading from reader */ $this->assertEquals('testValue', $this->_model->get('some')); } + + /** + * @param array $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index 2284cef6653cf..6bb0f95784c32 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -10,6 +10,11 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -27,10 +32,16 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Framework\Config\Data::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } public function testGetConfigNotCached() diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index 8dd9dfd50ec93..e85fe982b5f5e 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -7,6 +7,11 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\Mview\Config\Data */ @@ -44,6 +49,7 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reader = $this->getMock(\Magento\Framework\Mview\Config\Reader::class, ['read'], [], '', false); $this->cache = $this->getMockForAbstractClass( \Magento\Framework\Config\CacheInterface::class, @@ -65,7 +71,12 @@ protected function setUp() ); $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - \Magento\Framework\Mview\Config\Data::setJson($this->jsonMock); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } public function testConstructorWithCache() diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php index 16493899d7d4e..826fdd265d7d8 100644 --- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php +++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php @@ -31,6 +31,11 @@ class ObjectManager */ protected $_testObject; + /** + * @var \Magento\Framework\ObjectManagerInterface + */ + private $objectManager; + /** * Class constructor * @@ -342,4 +347,39 @@ public function setBackwardCompatibleProperty($object, $propertyName, $propertyV $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($object, $propertyValue); } + + /** + * Mock application object manager to return configured dependencies. Can be used in the case when need to add a + * new dependency that is used in the constructor keeping backwards compatibility + * + * $dependencies = [\Magento\Framework\Json\JsonInterface::class => $jsonMock] + * + * @param array $dependencies + */ + public function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->_testObject->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->_testObject->any()) + ->method('getInstance') + ->willReturnSelf(); + $objectManagerMock->expects($this->_testObject->any()) + ->method('get') + ->will($this->_testObject->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } + + /** + * Unset mocked object manager, must be used to restore + * \Magento\Framework\App\ObjectManager::_instance after mockObjectManager called + */ + public function restoreObjectManager() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } } From 2176cc32ad872854337904e9864a61d1f222c701 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 14:45:26 -0500 Subject: [PATCH 043/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests --- .../Test/Unit/Model/Export/ConfigTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index 797c9f7a1ae32..9012990c6ff66 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -7,6 +7,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -17,6 +22,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ protected $_configScopeMock; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $jsonMock; + /** * @var string */ @@ -29,6 +39,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMock( \Magento\ImportExport\Model\Export\Config\Reader::class, [], @@ -37,6 +48,13 @@ protected function setUp() false ); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } /** From 491bd33d339764c0ae497a1fd5776756c3f4867b Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 14:59:45 -0500 Subject: [PATCH 044/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests --- .../Unit/Model/Entity/Attribute/ConfigTest.php | 2 +- .../Test/Unit/Model/Import/ConfigTest.php | 18 ++++++++++++++++++ lib/internal/Magento/Framework/Config/Data.php | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index c08959e625fa5..97bf28f5638da 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -69,7 +69,7 @@ protected function setUp() $jsonMock->method('decode') ->willReturn([]); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $jsonMock]); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php index 1530c0b4a4d00..22127a6b54b57 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php @@ -7,6 +7,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -17,6 +22,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ protected $_configScopeMock; + /** + * @var \Magento\Framework\Json\JsonInterface + */ + private $jsonMock; + /** * @var string */ @@ -29,6 +39,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMock( \Magento\ImportExport\Model\Import\Config\Reader::class, [], @@ -37,6 +48,13 @@ protected function setUp() false ); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + } + + public function tearDown() + { + $this->objectManager->restoreObjectManager(); } /** diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 83bd95543bdb7..005bbb4eedc6a 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -67,7 +67,7 @@ class Data implements \Magento\Framework\Config\DataInterface /** * @var JsonInterface */ - private $json; + protected $json; /** * Constructor From a67e1946f398a8ca74bfc6efb6877c30ebaf0b68 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 15:02:10 -0500 Subject: [PATCH 045/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests --- .../Framework/Config/Test/Unit/Data/ScopedTest.php | 14 -------------- .../TestFramework/Unit/Helper/ObjectManager.php | 3 +++ 2 files changed, 3 insertions(+), 14 deletions(-) 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 9867c4d1ad1ee..dcf43347aec23 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -186,18 +186,4 @@ public function testGetScopeSwitchingWithCachedData() /** test preventing of double config data loading from reader */ $this->assertEquals('testValue', $this->_model->get('some')); } - - /** - * @param array $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php index 826fdd265d7d8..d4c60a0adf574 100644 --- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php +++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php @@ -355,6 +355,7 @@ public function setBackwardCompatibleProperty($object, $propertyName, $propertyV * $dependencies = [\Magento\Framework\Json\JsonInterface::class => $jsonMock] * * @param array $dependencies + * @return void */ public function mockObjectManager($dependencies) { @@ -375,6 +376,8 @@ public function mockObjectManager($dependencies) /** * Unset mocked object manager, must be used to restore * \Magento\Framework\App\ObjectManager::_instance after mockObjectManager called + * + * @return void */ public function restoreObjectManager() { From bebd0a6ee48c9bce31d2c0a931130e338f92e1fc Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 27 Sep 2016 15:26:16 -0500 Subject: [PATCH 046/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests --- .../Magento/Framework/Mview/Test/Unit/Config/DataTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index e85fe982b5f5e..71075b64b7dbc 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -88,7 +88,8 @@ public function testConstructorWithCache() $this->stateCollection->expects($this->never())->method('getItems'); - $this->jsonMock->method('decode') + $this->jsonMock->expects($this->once()) + ->method('decode') ->willReturn($this->views); $this->config = new \Magento\Framework\Mview\Config\Data( From 4e61b3922dfacad3b98eb481e01014bba0314c76 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 27 Sep 2016 22:53:16 -0500 Subject: [PATCH 047/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework --- .../Unit/Model/DataObjectProcessorTest.php | 15 +++ .../App/ObjectManager/ConfigLoaderTest.php | 44 +++++++++ .../Framework/Interception/AbstractPlugin.php | 4 +- .../Framework/Reflection/MethodsMapTest.php | 59 +++++++++++ .../App/ObjectManager/ConfigCache.php | 26 ++++- .../App/ObjectManager/ConfigLoader.php | 25 ++++- .../ObjectManager/ConfigLoader/Compiled.php | 22 ++++- .../Unit/ObjectManager/ConfigCacheTest.php | 39 ++++++-- .../Unit/ObjectManager/ConfigLoaderTest.php | 98 ++++++++++++++---- .../Framework/Interception/Config/Config.php | 26 ++++- .../Interception/PluginList/PluginList.php | 28 +++++- .../Test/Unit/Config/ConfigTest.php | 78 +++++++++++---- .../Test/Unit/PluginList/PluginListTest.php | 99 +++++++++++-------- .../Framework/Reflection/MethodsMap.php | 29 +++++- .../Reflection/Test/Unit/MethodsMapTest.php | 32 ++++-- .../Test/Unit/ServiceInputProcessorTest.php | 15 +++ .../Di/Compiler/Config/Writer/Filesystem.php | 28 +++++- 17 files changed, 556 insertions(+), 111 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php diff --git a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php index 4ab1609816332..fd239bea1be3f 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php @@ -6,6 +6,7 @@ namespace Magento\Webapi\Test\Unit\Model; +use Magento\Framework\Json\JsonInterface; use Magento\Webapi\Model\Config as ModelConfig; class DataObjectProcessorTest extends \PHPUnit_Framework_TestCase @@ -30,6 +31,20 @@ protected function setup() 'typeProcessor' => $objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class), ] ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManager->setBackwardCompatibleProperty( + $methodsMapProcessor, + 'json', + $jsonMock + ); $this->dataObjectProcessor = $objectManager->getObject( \Magento\Framework\Reflection\DataObjectProcessor::class, [ diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php new file mode 100644 index 0000000000000..74a3aee56ba28 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php @@ -0,0 +1,44 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->object = $this->objectManager->create( + \Magento\Framework\App\ObjectManager\ConfigLoader::class + ); + } + + public function testLoad() + { + $this->cleanAllCache(); + $data = $this->object->load('global'); + $this->assertNotEmpty($data); + $cachedData = $this->object->load('global'); + $this->assertEquals($data, $cachedData); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php index 5c0406ec372a5..a6371c607c4af 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php @@ -71,6 +71,7 @@ public function setUpInterceptionConfig($pluginConfig) $definitions ); $interceptionDefinitions = new Definition\Runtime(); + $json = new \Magento\Framework\Json\Json(); $sharedInstances = [ \Magento\Framework\Config\CacheInterface::class => $cache, \Magento\Framework\Config\ScopeInterface::class => $configScope, @@ -79,7 +80,8 @@ public function setUpInterceptionConfig($pluginConfig) \Magento\Framework\ObjectManager\ConfigInterface::class => $config, \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => $config, \Magento\Framework\ObjectManager\DefinitionInterface::class => $definitions, - \Magento\Framework\Interception\DefinitionInterface::class => $interceptionDefinitions + \Magento\Framework\Interception\DefinitionInterface::class => $interceptionDefinitions, + \Magento\Framework\Json\JsonInterface::class => $json, ]; $this->_objectManager = new \Magento\Framework\ObjectManager\ObjectManager( $factory, diff --git a/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php b/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php new file mode 100644 index 0000000000000..ecb815836625f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php @@ -0,0 +1,59 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->object = $this->objectManager->create( + \Magento\Framework\Reflection\MethodsMap::class + ); + } + + public function testGetMethodsMap() + { + $this->cleanAllCache(); + $data = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); + $this->assertArrayHasKey('getMethodsMap', $data); + $cachedData = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); + $this->assertEquals($data, $cachedData); + } + + public function testGetMethodParams() + { + $this->cleanAllCache(); + $data = $this->object->getMethodParams( + \Magento\Framework\Reflection\MethodsMap::class, + 'getMethodParams' + ); + $this->assertCount(2, $data); + $cachedData = $this->object->getMethodParams( + \Magento\Framework\Reflection\MethodsMap::class, + 'getMethodParams' + ); + $this->assertEquals($data, $cachedData); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php index 7e8711d027bb7..342c9ab7bfb31 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php @@ -7,6 +7,8 @@ */ namespace Magento\Framework\App\ObjectManager; +use Magento\Framework\Json\JsonInterface; + class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterface { /** @@ -21,6 +23,11 @@ class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterfa */ protected $_prefix = 'diConfig'; + /** + * @var JsonInterface + */ + private $json; + /** * @param \Magento\Framework\Cache\FrontendInterface $cacheFrontend */ @@ -37,7 +44,7 @@ public function __construct(\Magento\Framework\Cache\FrontendInterface $cacheFro */ public function get($key) { - return unserialize($this->_cacheFrontend->load($this->_prefix . $key)); + return $this->getJson()->decode($this->_cacheFrontend->load($this->_prefix . $key)); } /** @@ -49,6 +56,21 @@ public function get($key) */ public function save(array $config, $key) { - $this->_cacheFrontend->save(serialize($config), $this->_prefix . $key); + $this->_cacheFrontend->save($this->getJson()->encode($config), $this->_prefix . $key); + } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; } } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php index 2190ff6cdb37f..0ef3e5d3e5954 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php @@ -7,6 +7,7 @@ */ namespace Magento\Framework\App\ObjectManager; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\ConfigLoaderInterface; class ConfigLoader implements ConfigLoaderInterface @@ -32,6 +33,11 @@ class ConfigLoader implements ConfigLoaderInterface */ protected $_cache; + /** + * @var JsonInterface + */ + private $json; + /** * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Framework\ObjectManager\Config\Reader\DomFactory $readerFactory @@ -67,11 +73,26 @@ public function load($area) if (!$data) { $data = $this->_getReader()->read($area); - $this->_cache->save(serialize($data), $cacheId); + $this->_cache->save($this->getJson()->encode($data), $cacheId); } else { - $data = unserialize($data); + $data = $this->getJson()->decode($data); } return $data; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index 844d3f038aefe..c4a4b64221f4c 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -6,6 +6,7 @@ */ namespace Magento\Framework\App\ObjectManager\ConfigLoader; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\ConfigLoaderInterface; class Compiled implements ConfigLoaderInterface @@ -17,6 +18,11 @@ class Compiled implements ConfigLoaderInterface */ private $configCache = []; + /** + * @var JsonInterface + */ + private $json; + /** * {inheritdoc} */ @@ -25,7 +31,7 @@ public function load($area) if (isset($this->configCache[$area])) { return $this->configCache[$area]; } - $this->configCache[$area] = \unserialize(\file_get_contents(self::getFilePath($area))); + $this->configCache[$area] = $this->getJson()->decode(\file_get_contents(self::getFilePath($area))); return $this->configCache[$area]; } @@ -39,4 +45,18 @@ public static function getFilePath($area) { return BP . '/var/di/' . $area . '.ser'; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = new \Magento\Framework\Json\Json(); + } + return $this->json; + } } 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 336e958403f91..fa67df0a0dd00 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php @@ -5,33 +5,54 @@ */ namespace Magento\Framework\App\Test\Unit\ObjectManager; +use Magento\Framework\Json\JsonInterface; + class ConfigCacheTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Framework\App\ObjectManager\ConfigCache */ - protected $_configCache; + protected $configCache; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_cacheFrontendMock; + protected $cacheFrontendMock; protected function setUp() { - $this->_cacheFrontendMock = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); - $this->_configCache = new \Magento\Framework\App\ObjectManager\ConfigCache($this->_cacheFrontendMock); + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->cacheFrontendMock = $this->getMock(\Magento\Framework\Cache\FrontendInterface::class); + $this->configCache = $objectManagerHelper->getObject( + \Magento\Framework\App\ObjectManager\ConfigCache::class, + ['cacheFrontend' => $this->cacheFrontendMock] + ); + + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->configCache, + 'json', + $jsonMock + ); } protected function tearDown() { - unset($this->_configCache); + unset($this->configCache); } public function testGet() { $key = 'key'; - $this->_cacheFrontendMock->expects( + $this->cacheFrontendMock->expects( $this->once() )->method( 'load' @@ -40,14 +61,14 @@ public function testGet() )->will( $this->returnValue(false) ); - $this->assertEquals(false, $this->_configCache->get($key)); + $this->assertEquals(false, $this->configCache->get($key)); } public function testSave() { $key = 'key'; $config = ['config']; - $this->_cacheFrontendMock->expects($this->once())->method('save')->with(serialize($config), 'diConfig' . $key); - $this->_configCache->save($config, $key); + $this->cacheFrontendMock->expects($this->once())->method('save')->with(json_encode($config), '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 5a0b4ae96f26c..625ed6c088c1a 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php @@ -8,31 +8,33 @@ namespace Magento\Framework\App\Test\Unit\ObjectManager; +use Magento\Framework\Json\JsonInterface; + class ConfigLoaderTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Framework\App\ObjectManager\ConfigLoader */ - protected $_model; + protected $object; /** - * @var \Magento\Framework\ObjectManager\Config\Reader\DomFactory + * @var \Magento\Framework\ObjectManager\Config\Reader\DomFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerFactoryMock; + protected $readerFactoryMock; /** - * @var \Magento\Framework\ObjectManager\Config\Reader\Dom + * @var \Magento\Framework\ObjectManager\Config\Reader\Dom|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; + protected $readerMock; /** - * @var \Magento\Framework\App\Cache\Type\Config + * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $_cacheMock; + protected $cacheMock; protected function setUp() { - $this->_readerMock = $this->getMock( + $this->readerMock = $this->getMock( \Magento\Framework\ObjectManager\Config\Reader\Dom::class, [], [], @@ -40,7 +42,7 @@ protected function setUp() false ); - $this->_readerFactoryMock = $this->getMock( + $this->readerFactoryMock = $this->getMock( \Magento\Framework\ObjectManager\Config\Reader\DomFactory::class, ['create'], [], @@ -48,17 +50,79 @@ protected function setUp() false ); - $this->_readerFactoryMock->expects( + $this->readerFactoryMock->expects( $this->any() )->method( 'create' )->will( - $this->returnValue($this->_readerMock) + $this->returnValue($this->readerMock) ); - $this->_cacheMock = $this->getMock(\Magento\Framework\App\Cache\Type\Config::class, [], [], '', false); - $this->_model = new \Magento\Framework\App\ObjectManager\ConfigLoader( - $this->_cacheMock, $this->_readerFactoryMock + $this->cacheMock = $this->getMock(\Magento\Framework\App\Cache\Type\Config::class, [], [], '', false); + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->object = $objectManagerHelper->getObject( + \Magento\Framework\App\ObjectManager\ConfigLoader::class, + [ + 'cache' => $this->cacheMock, + 'readerFactory' => $this->readerFactoryMock, + ] + ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->object, + 'json', + $jsonMock + ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->object, + 'json', + $jsonMock + ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->object, + 'json', + $jsonMock + ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->object, + 'json', + $jsonMock ); } @@ -70,7 +134,7 @@ public function testLoad($area) { $configData = ['some' => 'config', 'data' => 'value']; - $this->_cacheMock->expects( + $this->cacheMock->expects( $this->once() )->method( 'load' @@ -80,9 +144,9 @@ public function testLoad($area) $this->returnValue(false) ); - $this->_readerMock->expects($this->once())->method('read')->with($area)->will($this->returnValue($configData)); + $this->readerMock->expects($this->once())->method('read')->with($area)->will($this->returnValue($configData)); - $this->assertEquals($configData, $this->_model->load($area)); + $this->assertEquals($configData, $this->object->load($area)); } /** diff --git a/lib/internal/Magento/Framework/Interception/Config/Config.php b/lib/internal/Magento/Framework/Interception/Config/Config.php index 9812e505cc397..72cb9aef0e30a 100644 --- a/lib/internal/Magento/Framework/Interception/Config/Config.php +++ b/lib/internal/Magento/Framework/Interception/Config/Config.php @@ -7,6 +7,8 @@ */ namespace Magento\Framework\Interception\Config; +use Magento\Framework\Json\JsonInterface; + class Config implements \Magento\Framework\Interception\ConfigInterface { /** @@ -70,6 +72,11 @@ class Config implements \Magento\Framework\Interception\ConfigInterface */ protected $_scopeList; + /** + * @var JsonInterface + */ + private $json; + /** * @param \Magento\Framework\Config\ReaderInterface $reader * @param \Magento\Framework\Config\ScopeListInterface $scopeList @@ -98,7 +105,7 @@ public function __construct( $intercepted = $this->_cache->load($this->_cacheId); if ($intercepted !== false) { - $this->_intercepted = unserialize($intercepted); + $this->_intercepted = $this->getJson()->decode($intercepted); } else { $this->initialize($this->_classDefinitions->getClasses()); } @@ -129,7 +136,7 @@ public function initialize($classDefinitions = []) foreach ($classDefinitions as $class) { $this->hasPlugins($class); } - $this->_cache->save(serialize($this->_intercepted), $this->_cacheId); + $this->_cache->save($this->getJson()->encode($this->_intercepted), $this->_cacheId); } /** @@ -175,4 +182,19 @@ public function hasPlugins($type) } return $this->_inheritInterception($type); } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index befff77015617..df9042da46ab0 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -14,6 +14,7 @@ use Magento\Framework\Interception\DefinitionInterface; use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\RelationsInterface; use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions; use Magento\Framework\ObjectManagerInterface; @@ -75,6 +76,11 @@ class PluginList extends Scoped implements InterceptionPluginList */ protected $_pluginInstances = []; + /** + * @var JsonInterface + */ + private $json; + /** * @param ReaderInterface $reader * @param ScopeInterface $configScope @@ -269,7 +275,7 @@ protected function _loadScopedData() $cacheId = implode('|', $this->_scopePriorityScheme) . "|" . $this->_cacheId; $data = $this->_cache->load($cacheId); if ($data) { - list($this->_data, $this->_inherited, $this->_processed) = unserialize($data); + list($this->_data, $this->_inherited, $this->_processed) = $this->getJson()->decode($data); foreach ($this->_scopePriorityScheme as $scope) { $this->_loadedScopes[$scope] = true; } @@ -302,7 +308,10 @@ protected function _loadScopedData() foreach ($this->getClassDefinitions() as $class) { $this->_inheritPlugins($class); } - $this->_cache->save(serialize([$this->_data, $this->_inherited, $this->_processed]), $cacheId); + $this->_cache->save( + $this->getJson()->encode([$this->_data, $this->_inherited, $this->_processed]), + $cacheId + ); } $this->_pluginInstances = []; } @@ -348,4 +357,19 @@ public function merge(array $config) } } } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } 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 992ed838b7c52..90d2b81a1d284 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -6,6 +6,8 @@ // @codingStandardsIgnoreFile namespace Magento\Framework\Interception\Test\Unit\Config; +use Magento\Framework\Json\JsonInterface; + require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; require_once __DIR__ . '/../Custom/Module/Model/ItemContainer.php'; @@ -49,6 +51,12 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ protected $relationsMock; + /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $jsonMock; + + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManagerHelper; + protected function setUp() { $this->readerMock = $this->getMock( @@ -67,6 +75,19 @@ protected function setUp() $this->relationsMock = $this->getMockForAbstractClass( \Magento\Framework\ObjectManager\RelationsInterface::class ); + $this->jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $this->jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $this->jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $this->prepareObjectManager([ + [JsonInterface::class, $this->jsonMock] + ]); + $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); } /** @@ -131,14 +152,16 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type, $entit $this->relationsMock->expects($this->any())->method('has')->will($this->returnValue($expectedResult)); $this->relationsMock->expects($this->any())->method('getParents')->will($this->returnValue($entityParents)); - $model = new \Magento\Framework\Interception\Config\Config( - $this->readerMock, - $this->configScopeMock, - $this->cacheMock, - $this->relationsMock, - $this->omConfigMock, - $this->definitionMock, - 'interception' + $model = $this->objectManagerHelper->getObject( + \Magento\Framework\Interception\Config\Config::class, + [ + 'reader' => $this->readerMock, + 'scopeList' => $this->configScopeMock, + 'cache' => $this->cacheMock, + 'relations' => $this->relationsMock, + 'omConfig' => $this->omConfigMock, + 'classDefinitions' => $this->definitionMock, + ] ); $this->assertEquals($expectedResult, $model->hasPlugins($type)); @@ -166,15 +189,21 @@ public function testHasPluginsWhenDataIsCached($expectedResult, $type) $this->cacheMock->expects($this->any()) ->method('load') ->with($cacheId) - ->will($this->returnValue(serialize($interceptionData))); - $model = new \Magento\Framework\Interception\Config\Config( - $this->readerMock, - $this->configScopeMock, - $this->cacheMock, - new \Magento\Framework\ObjectManager\Relations\Runtime(), - $this->omConfigMock, - $this->definitionMock, - $cacheId + ->will($this->returnValue(json_encode($interceptionData))); + + $model = $this->objectManagerHelper->getObject( + \Magento\Framework\Interception\Config\Config::class, + [ + 'reader' => $this->readerMock, + 'scopeList' => $this->configScopeMock, + 'cache' => $this->cacheMock, + 'relations' => $this->objectManagerHelper->getObject( + \Magento\Framework\ObjectManager\Relations\Runtime::class + ), + 'omConfig' => $this->omConfigMock, + 'classDefinitions' => $this->definitionMock, + 'cacheId' => $cacheId, + ] ); $this->assertEquals($expectedResult, $model->hasPlugins($type)); @@ -217,4 +246,19 @@ public function hasPluginsDataProvider() ] ]; } + + /** + * @param array $map + * @deprecated + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } 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 1291ae44bce65..09fdc9cb9ceaa 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Framework\Interception\Test\Unit\PluginList; +use Magento\Framework\Json\JsonInterface; + require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; require_once __DIR__ . '/../Custom/Module/Model/ItemContainer.php'; @@ -23,22 +25,17 @@ class PluginListTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\Interception\PluginList\PluginList */ - protected $_model; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_configScopeMock; + protected $object; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_objectManagerMock; + protected $configScopeMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_cacheMock; + protected $cacheMock; protected function setUp() { @@ -46,10 +43,10 @@ protected function setUp() $readerMock = $this->getMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class, [], [], '', false); $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap)); - $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); - $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); // turn cache off - $this->_cacheMock->expects($this->any()) + $this->cacheMock->expects($this->any()) ->method('get') ->will($this->returnValue(false)); @@ -59,62 +56,80 @@ protected function setUp() $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); - $this->_objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $this->_objectManagerMock->expects($this->any())->method('get')->will($this->returnArgument(0)); + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any())->method('get')->will($this->returnArgument(0)); $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); - $this->_model = new \Magento\Framework\Interception\PluginList\PluginList( - $readerMock, - $this->_configScopeMock, - $this->_cacheMock, - new \Magento\Framework\ObjectManager\Relations\Runtime(), - $omConfigMock, - new \Magento\Framework\Interception\Definition\Runtime(), - $this->_objectManagerMock, - $definitions, - ['global'], - 'interception' + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->object = $objectManagerHelper->getObject( + \Magento\Framework\Interception\PluginList\PluginList::class, + [ + 'reader' => $readerMock, + 'configScope' => $this->configScopeMock, + 'cache' => $this->cacheMock, + 'relations' => new \Magento\Framework\ObjectManager\Relations\Runtime(), + 'omConfig' => $omConfigMock, + 'definitions' => new \Magento\Framework\Interception\Definition\Runtime(), + 'objectManager' => $objectManagerMock, + 'classDefinitions' => $definitions, + 'scopePriorityScheme' => ['global'], + 'cacheId' => 'interception' + ] + ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->object, + 'json', + $jsonMock ); } public function testGetPlugin() { - $this->_configScopeMock->expects($this->any())->method('getCurrentScope')->will($this->returnValue('backend')); - $this->_model->getNext(\Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'getName'); - $this->_model->getNext( + $this->configScopeMock->expects($this->any())->method('getCurrentScope')->will($this->returnValue('backend')); + $this->object->getNext(\Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'getName'); + $this->object->getNext( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class, 'getName' ); - $this->_model->getNext( + $this->object->getNext( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash::class, 'getName' ); $this->assertEquals( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, - $this->_model->getPlugin( + $this->object->getPlugin( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'simple_plugin' ) ); $this->assertEquals( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, - $this->_model->getPlugin( + $this->object->getPlugin( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'advanced_plugin' ) ); $this->assertEquals( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainerPlugin\Simple::class, - $this->_model->getPlugin( + $this->object->getPlugin( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemContainer::class, 'simple_plugin' ) ); $this->assertEquals( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash\Plugin::class, - $this->_model->getPlugin( + $this->object->getPlugin( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash::class, 'simple_plugin' ) @@ -131,14 +146,14 @@ public function testGetPlugin() */ public function testGetPlugins($expectedResult, $type, $method, $scopeCode, $code = '__self') { - $this->_configScopeMock->expects( + $this->configScopeMock->expects( $this->any() )->method( 'getCurrentScope' )->will( $this->returnValue($scopeCode) ); - $this->assertEquals($expectedResult, $this->_model->getNext($type, $method, $code)); + $this->assertEquals($expectedResult, $this->object->getNext($type, $method, $code)); } /** @@ -206,11 +221,11 @@ public function getPluginsDataProvider() */ public function testInheritPluginsWithNonExistingClass() { - $this->_configScopeMock->expects($this->any()) + $this->configScopeMock->expects($this->any()) ->method('getCurrentScope') ->will($this->returnValue('frontend')); - $this->_model->getNext('SomeType', 'someMethod'); + $this->object->getNext('SomeType', 'someMethod'); } /** @@ -219,17 +234,17 @@ public function testInheritPluginsWithNonExistingClass() */ public function testLoadScopedDataCached() { - $this->_configScopeMock->expects($this->once()) + $this->configScopeMock->expects($this->once()) ->method('getCurrentScope') ->will($this->returnValue('scope')); $data = [['key'], ['key'], ['key']]; - $this->_cacheMock->expects($this->once()) + $this->cacheMock->expects($this->once()) ->method('load') ->with('global|scope|interception') - ->will($this->returnValue(serialize($data))); + ->will($this->returnValue(json_encode($data))); - $this->assertEquals(null, $this->_model->getNext('Type', 'method')); + $this->assertEquals(null, $this->object->getNext('Type', 'method')); } } diff --git a/lib/internal/Magento/Framework/Reflection/MethodsMap.php b/lib/internal/Magento/Framework/Reflection/MethodsMap.php index f7f402ae4b8a0..9a815df385501 100644 --- a/lib/internal/Magento/Framework/Reflection/MethodsMap.php +++ b/lib/internal/Magento/Framework/Reflection/MethodsMap.php @@ -6,6 +6,7 @@ namespace Magento\Framework\Reflection; +use Magento\Framework\Json\JsonInterface; use Zend\Code\Reflection\ClassReflection; use Zend\Code\Reflection\MethodReflection; use Zend\Code\Reflection\ParameterReflection; @@ -45,6 +46,11 @@ class MethodsMap */ private $fieldNamer; + /** + * @var JsonInterface + */ + private $json; + /** * @param \Magento\Framework\Cache\FrontendInterface $cache * @param TypeProcessor $typeProcessor @@ -95,11 +101,11 @@ public function getMethodsMap($interfaceName) if (!isset($this->serviceInterfaceMethodsMap[$key])) { $methodMap = $this->cache->load($key); if ($methodMap) { - $this->serviceInterfaceMethodsMap[$key] = unserialize($methodMap); + $this->serviceInterfaceMethodsMap[$key] = $this->getJson()->decode($methodMap); } else { $methodMap = $this->getMethodMapViaReflection($interfaceName); $this->serviceInterfaceMethodsMap[$key] = $methodMap; - $this->cache->save(serialize($this->serviceInterfaceMethodsMap[$key]), $key); + $this->cache->save($this->getJson()->encode($this->serviceInterfaceMethodsMap[$key]), $key); } } return $this->serviceInterfaceMethodsMap[$key]; @@ -117,7 +123,7 @@ public function getMethodParams($serviceClassName, $serviceMethodName) $cacheId = self::SERVICE_METHOD_PARAMS_CACHE_PREFIX . hash('md5', $serviceClassName . $serviceMethodName); $params = $this->cache->load($cacheId); if ($params !== false) { - return unserialize($params); + return $this->getJson()->decode($params); } $serviceClass = new ClassReflection($serviceClassName); /** @var MethodReflection $serviceMethod */ @@ -133,7 +139,7 @@ public function getMethodParams($serviceClassName, $serviceMethodName) self::METHOD_META_DEFAULT_VALUE => $isDefaultValueAvailable ? $paramReflection->getDefaultValue() : null ]; } - $this->cache->save(serialize($params), $cacheId, [ReflectionCache::CACHE_TAG]); + $this->cache->save($this->getJson()->encode($params), $cacheId, [ReflectionCache::CACHE_TAG]); return $params; } @@ -217,4 +223,19 @@ public function isMethodReturnValueRequired($type, $methodName) $methods = $this->getMethodsMap($type); return $methods[$methodName]['isRequired']; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php index 6f4dc37b8faff..72dfc690ca0f4 100644 --- a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php +++ b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php @@ -6,9 +6,9 @@ namespace Magento\Framework\Reflection\Test\Unit; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\Reflection\MethodsMap; use Magento\Framework\Reflection\TypeProcessor; -use Magento\Framework\Reflection\FieldNamer; /** * MethodsMap test @@ -18,7 +18,7 @@ class MethodsMapTest extends \PHPUnit_Framework_TestCase /** * @var MethodsMap */ - private $model; + private $object; /** * Set up helper. @@ -39,7 +39,7 @@ protected function setUp() ->getMockForAbstractClass(); $fieldNamerMock = $this->getMockBuilder(\Magento\Framework\Reflection\FieldNamer::class) ->getMockForAbstractClass(); - $this->model = $objectManager->getObject( + $this->object = $objectManager->getObject( \Magento\Framework\Reflection\MethodsMap::class, [ 'cache' => $cacheMock, @@ -48,27 +48,41 @@ protected function setUp() 'fieldNamer' => $fieldNamerMock, ] ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManager->setBackwardCompatibleProperty( + $this->object, + 'json', + $jsonMock + ); } public function testGetMethodReturnType() { $this->assertEquals( 'string', - $this->model->getMethodReturnType( + $this->object->getMethodReturnType( \Magento\Framework\Reflection\FieldNamer::class, 'getFieldNameForMethodName' ) ); $this->assertEquals( 'mixed', - $this->model->getMethodReturnType( + $this->object->getMethodReturnType( \Magento\Framework\Reflection\TypeCaster::class, 'castValueToType' ) ); $this->assertEquals( 'array', - $this->model->getMethodReturnType( + $this->object->getMethodReturnType( \Magento\Framework\Reflection\MethodsMap::class, 'getMethodsMap' ) @@ -77,7 +91,7 @@ public function testGetMethodReturnType() public function testGetMethodsMap() { - $methodsMap = $this->model->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); + $methodsMap = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); $this->assertEquals( [ 'getMethodReturnType' => [ @@ -125,7 +139,7 @@ public function testGetMethodsMap() */ public function testIsMethodValidForDataField($type, $methodName, $expectedResult) { - $this->assertEquals($this->model->isMethodValidForDataField($type, $methodName), $expectedResult); + $this->assertEquals($this->object->isMethodValidForDataField($type, $methodName), $expectedResult); } /** @@ -157,7 +171,7 @@ public function isMethodValidForDataFieldProvider() */ public function testIsMethodReturnValueRequired($type, $methodName, $expectedResult) { - $this->assertEquals($this->model->isMethodValidForDataField($type, $methodName), $expectedResult); + $this->assertEquals($this->object->isMethodValidForDataField($type, $methodName), $expectedResult); } /** diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php index 0ebb6d9be4f3d..264963b9a1205 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php @@ -8,6 +8,7 @@ namespace Magento\Framework\Webapi\Test\Unit; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\Webapi\ServiceInputProcessor; use Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\WebapiBuilderFactory; use Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\AssociativeArray; @@ -97,6 +98,20 @@ function () use ($objectManager) { 'fieldNamer' => $this->fieldNamer ] ); + $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $objectManager->setBackwardCompatibleProperty( + $this->methodsMap, + 'json', + $jsonMock + ); $this->serviceInputProcessor = $objectManager->getObject( \Magento\Framework\Webapi\ServiceInputProcessor::class, 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 daf8425802ac5..4a34d8cefab49 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,7 @@ namespace Magento\Setup\Module\Di\Compiler\Config\Writer; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Filesystem\DriverInterface; +use Magento\Framework\Json\JsonInterface; use Magento\Setup\Module\Di\Compiler\Config\WriterInterface; class Filesystem implements WriterInterface @@ -18,6 +18,11 @@ class Filesystem implements WriterInterface */ private $directoryList; + /** + * @var JsonInterface + */ + private $json; + /** * Constructor * @@ -39,8 +44,10 @@ public function write($key, array $config) { $this->initialize(); - $serialized = serialize($config); - file_put_contents($this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.ser', $serialized); + file_put_contents( + $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.ser', + $this->getJson()->encode($config) + ); } /** @@ -54,4 +61,19 @@ private function initialize() mkdir($this->directoryList->getPath(DirectoryList::DI)); } } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } From c6db7ea16d6e7e654f76d718ae4660908a9932a9 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 28 Sep 2016 08:44:47 -0500 Subject: [PATCH 048/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - Refactor Interception Framework --- .../Interception/PluginList/PluginList.php | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index df9042da46ab0..5687771a80266 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -76,11 +76,6 @@ class PluginList extends Scoped implements InterceptionPluginList */ protected $_pluginInstances = []; - /** - * @var JsonInterface - */ - private $json; - /** * @param ReaderInterface $reader * @param ScopeInterface $configScope @@ -357,19 +352,4 @@ public function merge(array $config) } } } - - /** - * Get json encoder/decoder - * - * @return JsonInterface - * @deprecated - */ - private function getJson() - { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); - } - return $this->json; - } } From a6dbb81f68473295bf863f5fc7ae8406dce58e08 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 28 Sep 2016 09:32:40 -0500 Subject: [PATCH 049/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - Refactor Interception Framework --- .../Test/Unit/Config/ConfigTest.php | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) 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 90d2b81a1d284..ba98791c6750a 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -84,10 +84,13 @@ protected function setUp() ->willReturnCallback(function ($string) { return json_decode($string, true); }); - $this->prepareObjectManager([ - [JsonInterface::class, $this->jsonMock] - ]); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManagerHelper->mockObjectManager([JsonInterface::class => $this->jsonMock]); + } + + protected function tearDown() + { + $this->objectManagerHelper->restoreObjectManager(); } /** @@ -246,19 +249,4 @@ public function hasPluginsDataProvider() ] ]; } - - /** - * @param array $map - * @deprecated - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } From 280a7f0323e25d54b46dabe95b5311672b2509e8 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 28 Sep 2016 09:50:02 -0500 Subject: [PATCH 050/144] MAGETWO-58639: Refactor Framework_View - Fix integration test --- .../View/Element/UiComponent/Config/Provider/TemplateTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php index 8003a01d1104c..5d0476832a984 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php @@ -54,8 +54,8 @@ public function testGetTemplate() $resultOne = $this->model->getTemplate('test.xml'); $resultTwo = $this->model->getTemplate('test.xml'); - $this->assertEquals($expected, $resultOne); - $this->assertEquals($expected, $resultTwo); + $this->assertXmlStringEqualsXmlString($expected, $resultOne); + $this->assertXmlStringEqualsXmlString($expected, $resultTwo); } /** From df3602488b1b81ebb7dd4f455ab356f1ad1d75f6 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 28 Sep 2016 10:57:18 -0500 Subject: [PATCH 051/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests --- .../Unit/Model/ProductTypes/ConfigTest.php | 2 +- .../Cron/Test/Unit/Model/Config/DataTest.php | 12 ++++---- .../Test/Unit/Model/Address/ConfigTest.php | 2 +- .../Model/Entity/Attribute/ConfigTest.php | 2 +- .../Test/Unit/Model/Export/ConfigTest.php | 2 +- .../Test/Unit/Model/Config/DataTest.php | 2 +- .../Sales/Test/Unit/Model/Config/DataTest.php | 2 +- .../App/Test/Unit/Config/InitialTest.php | 29 ++++++++----------- .../App/Test/Unit/Router/ActionListTest.php | 2 +- .../Config/Test/Unit/Data/ScopedTest.php | 2 +- .../Framework/Config/Test/Unit/DataTest.php | 2 +- .../Unit/Helper/ObjectManager.php | 5 ---- 12 files changed, 27 insertions(+), 37 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index c6e5618947d03..4098a28964040 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -47,7 +47,7 @@ protected function setUp() $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index f816163d1397e..5ef35c015d029 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -12,11 +12,16 @@ class DataTest extends \PHPUnit_Framework_TestCase */ private $objectManager; - public function setUp() + protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * Testing return jobs from different sources (DB, XML) */ @@ -64,9 +69,4 @@ public function testGetJobs() $result = $configData->getJobs(); $this->assertEquals($expected, $result); } - - public function tearDown() - { - $this->objectManager->restoreObjectManager(); - } } diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index 51b1f559b2769..e62c29173e2d0 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -121,7 +121,7 @@ protected function setUp() ); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index 97bf28f5638da..b3d522f1c7699 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -77,7 +77,7 @@ protected function setUp() ); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index 9012990c6ff66..d15a5f345d4a9 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -52,7 +52,7 @@ protected function setUp() $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } 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 b1c9a919765a2..939b30a576b09 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -71,7 +71,7 @@ protected function setUp() $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index ca85fcc9686d2..8514c77215622 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -40,7 +40,7 @@ protected function setUp() $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index ec6941c8dee48..5cf32b81b4566 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -7,6 +7,11 @@ class InitialTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\App\Config\Initial */ @@ -36,6 +41,7 @@ class InitialTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock( \Magento\Framework\App\Config\Initial\Reader::class, [], @@ -58,9 +64,7 @@ protected function setUp() $jsonMock->method('decode') ->willReturn($this->data); - $this->prepareObjectManager([ - [\Magento\Framework\Json\JsonInterface::class, $jsonMock] - ]); + $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $jsonMock]); $this->config = new \Magento\Framework\App\Config\Initial( $this->readerMock, @@ -68,6 +72,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @param string $scope * @param array $expected @@ -91,18 +100,4 @@ public function testGetMetadata() { $this->assertEquals(['metadata'], $this->config->getMetadata()); } - - /** - * @param array $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index c0604b1e953d1..9101fa790e7f2 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -53,7 +53,7 @@ protected function setUp() $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } 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 dcf43347aec23..81ea1b820bf9e 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -55,7 +55,7 @@ protected function setUp() ); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index 6bb0f95784c32..82f33b129edb0 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -39,7 +39,7 @@ protected function setUp() $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); } - public function tearDown() + protected function tearDown() { $this->objectManager->restoreObjectManager(); } diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php index d4c60a0adf574..91ecf8424748f 100644 --- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php +++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php @@ -31,11 +31,6 @@ class ObjectManager */ protected $_testObject; - /** - * @var \Magento\Framework\ObjectManagerInterface - */ - private $objectManager; - /** * Class constructor * From ee772ec6be0ebe19ddc3ad12d76673894e981baf Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 28 Sep 2016 16:49:06 -0500 Subject: [PATCH 052/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - Refactor ObjectManager Framework --- .../ObjectManager/ConfigLoader/Compiled.php | 8 +- .../Framework/App/ObjectManagerFactory.php | 4 +- .../Interception/Definition/Compiled.php | 2 + .../ObjectManager/Config/Compiled.php | 23 +- .../Framework/ObjectManager/Config/Config.php | 27 +- .../Definition/Compiled/Binary.php | 27 - .../Definition/Compiled/Json.php | 47 ++ .../Definition/Compiled/Serialized.php | 27 - .../ObjectManager/DefinitionFactory.php | 19 +- .../ObjectManager/Relations/Compiled.php | 2 + .../Test/Unit/Config/ConfigTest.php | 19 + .../Unit/Definition/Compiled/BinaryTest.php | 21 - .../Unit/Definition/Compiled/JsonTest.php | 59 +++ .../Definition/Compiled/SerializedTest.php | 35 -- .../Test/Unit/Definition/CompiledStub.php | 2 +- .../Test/Unit/DefinitionFactoryTest.php | 16 +- .../Command/DiCompileMultiTenantCommand.php | 493 ------------------ .../Di/Compiler/Config/Writer/Filesystem.php | 3 +- 18 files changed, 200 insertions(+), 634 deletions(-) delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php create mode 100644 lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/BinaryTest.php create mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/SerializedTest.php delete mode 100644 setup/src/Magento/Setup/Console/Command/DiCompileMultiTenantCommand.php diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index c4a4b64221f4c..c48f7a0dc4d65 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -6,11 +6,14 @@ */ namespace Magento\Framework\App\ObjectManager\ConfigLoader; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\ConfigLoaderInterface; class Compiled implements ConfigLoaderInterface { + const COMPILED_FILE_EXTENSION = '.json'; + /** * Global config * @@ -36,14 +39,15 @@ public function load($area) } /** - * Returns path to cached configuration + * Returns path to compiled configuration * * @param string $area * @return string */ public static function getFilePath($area) { - return BP . '/var/di/' . $area . '.ser'; + $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; + return BP . $diPath . '/' . $area . self::COMPILED_FILE_EXTENSION; } /** diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index 3a007841532cc..f91c389b493fb 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -10,7 +10,7 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem\DriverPool; use Magento\Framework\Interception\ObjectManager\ConfigInterface; -use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; +use Magento\Framework\ObjectManager\Definition\Compiled\Json; use Magento\Framework\App\ObjectManager\Environment; use Magento\Framework\Config\File\ConfigFilePool; use Magento\Framework\Code\GeneratedFiles; @@ -119,7 +119,7 @@ public function create(array $arguments) $this->driverPool->getDriver(DriverPool::FILE), $this->directoryList->getPath(DirectoryList::DI), $this->directoryList->getPath(DirectoryList::GENERATION), - $deploymentConfig->get(self::CONFIG_PATH_DEFINITION_FORMAT, Serialized::MODE_NAME) + $deploymentConfig->get(self::CONFIG_PATH_DEFINITION_FORMAT, Json::MODE_NAME) ); $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions')); diff --git a/lib/internal/Magento/Framework/Interception/Definition/Compiled.php b/lib/internal/Magento/Framework/Interception/Definition/Compiled.php index 6fbe9c99dce86..959d1da1026ee 100644 --- a/lib/internal/Magento/Framework/Interception/Definition/Compiled.php +++ b/lib/internal/Magento/Framework/Interception/Definition/Compiled.php @@ -11,6 +11,8 @@ class Compiled implements DefinitionInterface { + const FILE_NAME = 'plugins.json'; + /** * List of plugin definitions * diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index a3ed3b941844e..3cc4fe8c25e08 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -6,6 +6,7 @@ */ namespace Magento\Framework\ObjectManager\Config; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\ConfigCacheInterface; use Magento\Framework\ObjectManager\RelationsInterface; @@ -26,6 +27,11 @@ class Compiled implements \Magento\Framework\ObjectManager\ConfigInterface */ private $preferences; + /** + * @var JsonInterface + */ + private $json; + /** * @param array $data */ @@ -72,7 +78,7 @@ public function getArguments($type) { if (isset($this->arguments[$type])) { if (is_string($this->arguments[$type])) { - $this->arguments[$type] = unserialize($this->arguments[$type]); + $this->arguments[$type] = $this->getJson()->decode($this->arguments[$type]); } return $this->arguments[$type]; } else { @@ -153,4 +159,19 @@ public function getPreferences() { return $this->preferences; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Config.php b/lib/internal/Magento/Framework/ObjectManager/Config/Config.php index 5dc02e6ba7d97..b80b5b30ed152 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Config.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Config.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\ObjectManager\Config; +use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\ConfigCacheInterface; use Magento\Framework\ObjectManager\DefinitionInterface; use Magento\Framework\ObjectManager\RelationsInterface; @@ -74,6 +75,11 @@ class Config implements \Magento\Framework\ObjectManager\ConfigInterface */ protected $_mergedArguments; + /** + * @var JsonInterface + */ + private $json; + /** * @param RelationsInterface $relations * @param DefinitionInterface $definitions @@ -267,10 +273,12 @@ public function extend(array $configuration) if ($this->_cache) { if (!$this->_currentCacheKey) { $this->_currentCacheKey = md5( - serialize([$this->_arguments, $this->_nonShared, $this->_preferences, $this->_virtualTypes]) + $this->getJson()->encode( + [$this->_arguments, $this->_nonShared, $this->_preferences, $this->_virtualTypes] + ) ); } - $key = md5($this->_currentCacheKey . serialize($configuration)); + $key = md5($this->_currentCacheKey . $this->getJson()->encode($configuration)); $cached = $this->_cache->get($key); if ($cached) { list( @@ -323,4 +331,19 @@ public function getPreferences() { return $this->_preferences; } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php deleted file mode 100644 index bba816e072e6b..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Binary.php +++ /dev/null @@ -1,27 +0,0 @@ -getJson()->decode($signature); + } + + /** + * Get json encoder/decoder + * + * @return JsonInterface + * @deprecated + */ + private function getJson() + { + if ($this->json === null) { + $this->json = \Magento\Framework\App\ObjectManager::getInstance() + ->get(JsonInterface::class); + } + return $this->json; + } +} diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php deleted file mode 100644 index a799725c39099..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Serialized.php +++ /dev/null @@ -1,27 +0,0 @@ - \Magento\Framework\ObjectManager\Definition\Compiled\Binary::class, - Serialized::MODE_NAME => \Magento\Framework\ObjectManager\Definition\Compiled\Serialized::class, + Json::MODE_NAME => \Magento\Framework\ObjectManager\Definition\Compiled\Json::class, ]; /** @@ -116,7 +108,7 @@ public function createClassDefinition($definitions = false) */ public function createPluginDefinition() { - $path = $this->_definitionDir . '/plugins.ser'; + $path = $this->_definitionDir . '/' . \Magento\Framework\Interception\Definition\Compiled::FILE_NAME; if ($this->_filesystemDriver->isReadable($path)) { return new \Magento\Framework\Interception\Definition\Compiled( $this->_unpack($this->_filesystemDriver->fileGetContents($path)) @@ -133,7 +125,7 @@ public function createPluginDefinition() */ public function createRelations() { - $path = $this->_definitionDir . '/' . 'relations.ser'; + $path = $this->_definitionDir . '/' . \Magento\Framework\ObjectManager\Relations\Compiled::FILE_NAME; if ($this->_filesystemDriver->isReadable($path)) { return new \Magento\Framework\ObjectManager\Relations\Compiled( $this->_unpack($this->_filesystemDriver->fileGetContents($path)) @@ -161,8 +153,7 @@ public static function getSupportedFormats() */ protected function _unpack($definitions) { - $extractor = $this->_definitionFormat == Binary::MODE_NAME ? 'igbinary_unserialize' : 'unserialize'; - return $extractor($definitions); + return json_decode($definitions); } /** diff --git a/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php index 71455dbf9acd4..56163410f351d 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php @@ -9,6 +9,8 @@ class Compiled implements \Magento\Framework\ObjectManager\RelationsInterface { + const FILE_NAME = 'relations.json'; + /** * List of class relations * 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 844d5fa94a627..5c869304eb3c1 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php @@ -5,10 +5,19 @@ */ namespace Magento\Framework\ObjectManager\Test\Unit\Config; +use Magento\Framework\Json\JsonInterface; use \Magento\Framework\ObjectManager\Config\Config; class ConfigTest extends \PHPUnit_Framework_TestCase { + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManagerHelper; + + protected function setUp() + { + $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + } + public function testGetArgumentsEmpty() { $config = new Config(); @@ -42,6 +51,16 @@ public function testExtendWithCacheMock() $cache->expects($this->once())->method('get')->will($this->returnValue(false)); $config = new Config(null, $definitions); + $jsonMock = $this->getMock(JsonInterface::class); + $jsonMock->method('encode') + ->willReturnCallback(function ($data) { + return json_encode($data, true); + }); + $this->objectManagerHelper->setBackwardCompatibleProperty( + $config, + 'json', + $jsonMock + ); $config->setCache($cache); $this->_assertFooTypeArguments($config); diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/BinaryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/BinaryTest.php deleted file mode 100644 index 80cf73a44e7ac..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/BinaryTest.php +++ /dev/null @@ -1,21 +0,0 @@ -markTestSkipped('This test requires igbinary PHP extension'); - } - $checkString = 'packed code'; - $signatures = ['wonderfulClass' => igbinary_serialize($checkString)]; - $definitions = ['wonderful' => 'wonderfulClass']; - $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Binary([$signatures, $definitions]); - $this->assertEquals($checkString, $model->getParameters('wonderful')); - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php new file mode 100644 index 0000000000000..06beccec2b075 --- /dev/null +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php @@ -0,0 +1,59 @@ +objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + } + + public function testGetParametersWithoutDefinition() + { + $signatures = []; + $definitions = ['wonderful' => null]; + $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); + $this->assertEquals(null, $model->getParameters('wonderful')); + } + + public function testGetParametersWithSignatureObject() + { + $wonderfulSignature = new \stdClass(); + $signatures = ['wonderfulClass' => $wonderfulSignature]; + $definitions = ['wonderful' => 'wonderfulClass']; + $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); + $this->assertEquals($wonderfulSignature, $model->getParameters('wonderful')); + } + + public function testGetParametersWithUnpacking() + { + $checkString = 'code to pack'; + $signatures = ['wonderfulClass' => json_encode($checkString)]; + $definitions = ['wonderful' => 'wonderfulClass']; + $object = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); + $jsonMock = $this->getMock(JsonInterface::class); + $jsonMock->method('encode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $jsonMock->method('decode') + ->willReturnCallback(function ($data) { + return json_decode($data, true); + }); + $this->objectManagerHelper->setBackwardCompatibleProperty( + $object, + 'json', + $jsonMock + ); + $this->assertEquals($checkString, $object->getParameters('wonderful')); + } +} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/SerializedTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/SerializedTest.php deleted file mode 100644 index 9454377c3b720..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/SerializedTest.php +++ /dev/null @@ -1,35 +0,0 @@ - null]; - $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Serialized([$signatures, $definitions]); - $this->assertEquals(null, $model->getParameters('wonderful')); - } - - public function testGetParametersWithSignatureObject() - { - $wonderfulSignature = new \stdClass(); - $signatures = ['wonderfulClass' => $wonderfulSignature]; - $definitions = ['wonderful' => 'wonderfulClass']; - $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Serialized([$signatures, $definitions]); - $this->assertEquals($wonderfulSignature, $model->getParameters('wonderful')); - } - - public function testGetParametersWithUnpacking() - { - $checkString = 'code to pack'; - $signatures = ['wonderfulClass' => serialize($checkString)]; - $definitions = ['wonderful' => 'wonderfulClass']; - $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Serialized([$signatures, $definitions]); - $this->assertEquals($checkString, $model->getParameters('wonderful')); - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php index 16b8436a0c41a..bb5826a7b17ec 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php @@ -20,6 +20,6 @@ class CompiledStub extends Compiled */ protected function _unpack($signature) { - return unserialize($signature); + return json_decode($signature); } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index 468e01ce80aba..3051a671a4e77 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -6,7 +6,7 @@ namespace Magento\Framework\ObjectManager\Test\Unit; -use Magento\Framework\ObjectManager\Definition\Compiled\Serialized; +use Magento\Framework\ObjectManager\Definition\Compiled\Json; class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { @@ -27,7 +27,7 @@ class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->sampleContent = serialize([1, 2, 3]); + $this->sampleContent = json_encode([1, 2, 3]); $this->filesystemDriverMock = $this->getMock( \Magento\Framework\Filesystem\Driver\File::class, [], @@ -39,14 +39,14 @@ protected function setUp() $this->filesystemDriverMock, 'DefinitionDir', 'GenerationDir', - Serialized::MODE_NAME + Json::MODE_NAME ); } public function testCreateClassDefinitionFromString() { $this->assertInstanceOf( - \Magento\Framework\ObjectManager\Definition\Compiled\Serialized::class, + \Magento\Framework\ObjectManager\Definition\Compiled\Json::class, $this->model->createClassDefinition($this->sampleContent) ); } @@ -72,11 +72,11 @@ public function createPluginsAndRelationsReadableDataProvider() { return [ 'relations' => [ - 'DefinitionDir/relations.ser', + 'DefinitionDir/relations.json', 'createRelations', \Magento\Framework\ObjectManager\Relations\Compiled::class, ], 'plugins' => [ - 'DefinitionDir/plugins.ser', + 'DefinitionDir/plugins.json', 'createPluginDefinition', \Magento\Framework\Interception\Definition\Compiled::class, ], ]; @@ -100,11 +100,11 @@ public function createPluginsAndRelationsNotReadableDataProvider() { return [ 'relations' => [ - 'DefinitionDir/relations.ser', + 'DefinitionDir/relations.json', 'createRelations', \Magento\Framework\ObjectManager\Relations\Runtime::class, ], 'plugins' => [ - 'DefinitionDir/plugins.ser', + 'DefinitionDir/plugins.json', 'createPluginDefinition', \Magento\Framework\Interception\Definition\Runtime::class, ], ]; diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileMultiTenantCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileMultiTenantCommand.php deleted file mode 100644 index ac6a43189d7ab..0000000000000 --- a/setup/src/Magento/Setup/Console/Command/DiCompileMultiTenantCommand.php +++ /dev/null @@ -1,493 +0,0 @@ -objectManager = $objectManagerProvider->get(); - $this->directoryList = $directoryList; - $this->componentRegistrar = $componentRegistrar; - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - protected function configure() - { - $options = [ - new InputOption( - self::INPUT_KEY_SERIALIZER, - null, - InputOption::VALUE_REQUIRED, - 'Serializer function that should be used (' . self::SERIALIZER_VALUE_SERIALIZE . '|' - . self::SERIALIZER_VALUE_IGBINARY . ') default: ' . self::SERIALIZER_VALUE_SERIALIZE - ), - new InputOption( - self::INPUT_KEY_EXTRA_CLASSES_FILE, - null, - InputOption::VALUE_REQUIRED, - 'Path to file with extra proxies and factories to generate' - ), - new InputOption( - self::INPUT_KEY_GENERATION, - null, - InputOption::VALUE_REQUIRED, - 'Absolute path to generated classes, /var/generation by default' - ), - new InputOption( - self::INPUT_KEY_DI, - null, - InputOption::VALUE_REQUIRED, - 'Absolute path to DI definitions directory, /var/di by default' - ), - new InputOption( - self::INPUT_KEY_EXCLUDE_PATTERN, - null, - InputOption::VALUE_REQUIRED, - 'Allows to exclude Paths from compilation (default is #[\\\\/]m1[\\\\/]#i)' - ), - ]; - $this->setName(self::NAME) - ->setDescription( - 'Generates all non-existing proxies and factories, and pre-compile class definitions, ' - . 'inheritance information and plugin definitions' - ) - ->setDefinition($options); - parent::configure(); - } - - /** - * Get module directories exclude patterns - * - * @return array - */ - private function getModuleExcludePatterns() - { - $modulesExcludePatterns = []; - foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) { - $modulesExcludePatterns[] = "#^" . $modulePath . "/Test#"; - } - return $modulesExcludePatterns; - } - - /** - * Get library directories exclude patterns - * - * @return array - */ - private function getLibraryExcludePatterns() - { - $libraryExcludePatterns = []; - foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryPath) { - $libraryExcludePatterns[] = "#^" . $libraryPath . "/([\\w]+/)?Test#"; - } - return $libraryExcludePatterns; - } - - /** - * {@inheritdoc} - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $errors = $this->validate($input); - if ($errors) { - $output->writeln($errors); - return; - } - - $generationDir = $input->getOption(self::INPUT_KEY_GENERATION) ? $input->getOption(self::INPUT_KEY_GENERATION) - : $this->directoryList->getPath(DirectoryList::GENERATION); - $modulesExcludePatterns = $this->getModuleExcludePatterns(); - $testExcludePatterns = [ - "#^" . $this->directoryList->getPath(DirectoryList::SETUP) . "/[\\w]+/[\\w]+/Test#", - "#^" . $this->directoryList->getRoot() . "/dev/tools/Magento/Tools/[\\w]+/Test#" - ]; - $librariesExcludePatterns = $this->getLibraryExcludePatterns(); - $testExcludePatterns = array_merge($testExcludePatterns, $modulesExcludePatterns, $librariesExcludePatterns); - $fileExcludePatterns = $input->getOption('exclude-pattern') ? - [$input->getOption(self::INPUT_KEY_EXCLUDE_PATTERN)] : ['#[\\\\/]M1[\\\\/]#i']; - $fileExcludePatterns = array_merge($fileExcludePatterns, $testExcludePatterns); - /** @var Writer\Console logWriter Writer model for success messages */ - $logWriter = new Writer\Console($output); - $this->log = new Log($logWriter, $logWriter); - AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', $generationDir . '/Magento/'); - // 1 Code generation - $this->generateCode($generationDir, $fileExcludePatterns, $input); - // 2. Compilation - $this->compileCode($generationDir, $fileExcludePatterns, $input); - //Reporter - $this->log->report(); - if (!$this->log->hasError()) { - $output->writeln( - 'On *nix systems, verify the Magento application has permissions to modify files ' - . 'created by the compiler in the "var" directory. For instance, if you run the Magento application ' - . 'using Apache, the owner of the files in the "var" directory should be the Apache user (example ' - . 'command: "chown -R www-data:www-data /var" where MAGENTO_ROOT is the Magento ' - . 'root directory).' - ); - } - } - - /** - * Generate Code - * - * @param string $generationDir - * @param array $fileExcludePatterns - * @param InputInterface $input - * @return void - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * @SuppressWarnings(PHPMD.NPathComplexity) - */ - public function generateCode($generationDir, $fileExcludePatterns, $input) - { - // 1.1 Code scan - $filePatterns = ['php' => '/.*\.php$/', 'di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']; - $directoryScanner = new Scanner\DirectoryScanner(); - foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $codeScanDir) { - $this->files = array_merge_recursive( - $this->files, - $directoryScanner->scan($codeScanDir, $filePatterns, $fileExcludePatterns) - ); - } - $this->files['di'][] = $this->directoryList->getPath( - \Magento\Framework\App\Filesystem\DirectoryList::CONFIG - ) . '/di.xml'; - $this->files['additional'] = [$input->getOption(self::INPUT_KEY_EXTRA_CLASSES_FILE)]; - $repositoryScanner = new Scanner\RepositoryScanner(); - $repositories = $repositoryScanner->collectEntities($this->files['di']); - $scanner = new Scanner\CompositeScanner(); - $scanner->addChild(new Scanner\PhpScanner($this->log), 'php'); - $scanner->addChild(new Scanner\XmlScanner($this->log), 'di'); - $scanner->addChild(new Scanner\ArrayScanner(), 'additional'); - $this->entities = $scanner->collectEntities($this->files); - $interceptorScanner = new Scanner\XmlInterceptorScanner(); - $this->entities['interceptors'] = $interceptorScanner->collectEntities($this->files['di']); - // 1.2 Generation of Factory and Additional Classes - $generatorIo = $this->objectManager->create( - \Magento\Framework\Code\Generator\Io::class, - ['generationDirectory' => $generationDir] - ); - $this->generator = $this->objectManager->create( - \Magento\Framework\Code\Generator::class, - ['ioObject' => $generatorIo] - ); - /** Initialize object manager for code generation based on configs */ - $this->generator->setObjectManager($this->objectManager); - $generatorAutoloader = new \Magento\Framework\Code\Generator\Autoloader($this->generator); - spl_autoload_register([$generatorAutoloader, 'load']); - - foreach ($repositories as $entityName) { - switch ($this->generator->generateClass($entityName)) { - case CodeGenerator::GENERATION_SUCCESS: - $this->log->add(Log::GENERATION_SUCCESS, $entityName); - break; - case CodeGenerator::GENERATION_ERROR: - $this->log->add(Log::GENERATION_ERROR, $entityName); - break; - case CodeGenerator::GENERATION_SKIP: - default: - //no log - break; - } - } - foreach (['php', 'additional'] as $type) { - sort($this->entities[$type]); - foreach ($this->entities[$type] as $entityName) { - switch ($this->generator->generateClass($entityName)) { - case CodeGenerator::GENERATION_SUCCESS: - $this->log->add(Log::GENERATION_SUCCESS, $entityName); - break; - case CodeGenerator::GENERATION_ERROR: - $this->log->add(Log::GENERATION_ERROR, $entityName); - break; - case CodeGenerator::GENERATION_SKIP: - default: - //no log - break; - } - } - } - } - - /** - * Compile Code - * - * @param string $generationDir - * @param array $fileExcludePatterns - * @param InputInterface $input - * @return void - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * @SuppressWarnings(PHPMD.NPathComplexity) - */ - private function compileCode($generationDir, $fileExcludePatterns, $input) - { - $diDir = $input->getOption(self::INPUT_KEY_DI) ? $input->getOption(self::INPUT_KEY_DI) : - $this->directoryList->getPath(DirectoryList::DI); - $relationsFile = $diDir . '/relations.ser'; - $pluginDefFile = $diDir . '/plugins.ser'; - $compilationDirs = [ - $this->directoryList->getPath(DirectoryList::SETUP) . '/Magento/Setup/Module', - $this->directoryList->getRoot() . '/dev/tools/Magento/Tools', - ]; - $compilationDirs = array_merge( - $compilationDirs, - $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE), - $this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) - ); - $serializer = $input->getOption(self::INPUT_KEY_SERIALIZER) == Igbinary::NAME ? new Igbinary() : new Standard(); - // 2.1 Code scan - $validator = new \Magento\Framework\Code\Validator(); - $validator->add(new \Magento\Framework\Code\Validator\ConstructorIntegrity()); - $validator->add(new \Magento\Framework\Code\Validator\ContextAggregation()); - $classesScanner = new \Magento\Setup\Module\Di\Code\Reader\ClassesScanner(); - $classesScanner->addExcludePatterns($fileExcludePatterns); - $directoryInstancesNamesList = new \Magento\Setup\Module\Di\Code\Reader\Decorator\Directory( - $this->log, - new \Magento\Framework\Code\Reader\ClassReader(), - $classesScanner, - $validator, - $generationDir - ); - foreach ($compilationDirs as $path) { - if (is_readable($path)) { - $directoryInstancesNamesList->getList($path); - } - } - $inheritanceScanner = new Scanner\InheritanceInterceptorScanner( - new \Magento\Framework\ObjectManager\InterceptableValidator() - ); - $this->entities['interceptors'] = $inheritanceScanner->collectEntities( - get_declared_classes(), - $this->entities['interceptors'] - ); - // 2.1.1 Generation of Proxy and Interceptor Classes - foreach (['interceptors', 'di'] as $type) { - foreach ($this->entities[$type] as $entityName) { - switch ($this->generator->generateClass($entityName)) { - case CodeGenerator::GENERATION_SUCCESS: - $this->log->add(Log::GENERATION_SUCCESS, $entityName); - break; - case CodeGenerator::GENERATION_ERROR: - $this->log->add(Log::GENERATION_ERROR, $entityName); - break; - case CodeGenerator::GENERATION_SKIP: - default: - //no log - break; - } - } - } - //2.1.2 Compile relations for Proxy/Interceptor classes - $directoryInstancesNamesList->getList($generationDir); - $relations = $directoryInstancesNamesList->getRelations(); - // 2.2 Compression - $relationsFileDir = dirname($relationsFile); - if (!file_exists($relationsFileDir)) { - mkdir($relationsFileDir, 0777, true); - } - $relations = array_filter($relations); - file_put_contents($relationsFile, $serializer->serialize($relations)); - // 3. Plugin Definition Compilation - $pluginScanner = new Scanner\CompositeScanner(); - $pluginScanner->addChild(new Scanner\PluginScanner(), 'di'); - $pluginDefinitions = []; - $pluginList = $pluginScanner->collectEntities($this->files); - $pluginDefinitionList = new \Magento\Framework\Interception\Definition\Runtime(); - foreach ($pluginList as $type => $entityList) { - foreach ($entityList as $entity) { - $pluginDefinitions[ltrim($entity, '\\')] = $pluginDefinitionList->getMethodList($entity); - } - } - $outputContent = $serializer->serialize($pluginDefinitions); - $pluginDefFileDir = dirname($pluginDefFile); - if (!file_exists($pluginDefFileDir)) { - mkdir($pluginDefFileDir, 0777, true); - } - file_put_contents($pluginDefFile, $outputContent); - } - - /** - * Check if all option values provided by the user are valid - * - * @param InputInterface $input - * @return string[] - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - private function validate(InputInterface $input) - { - $errors = []; - $options = $input->getOptions(); - foreach ($options as $key => $value) { - if (!$value) { - continue; - } - switch ($key) { - case self::INPUT_KEY_SERIALIZER: - if (($value !== self::SERIALIZER_VALUE_SERIALIZE) && ($value !== self::SERIALIZER_VALUE_IGBINARY)) { - $errors[] = 'Invalid value for command option \'' . self::INPUT_KEY_SERIALIZER - . '\'. Possible values (' . self::SERIALIZER_VALUE_SERIALIZE . '|' - . self::SERIALIZER_VALUE_IGBINARY . ').'; - } - break; - case self::INPUT_KEY_EXTRA_CLASSES_FILE: - if (!file_exists($value)) { - $errors[] = 'Path does not exist for the value of command option \'' - . self::INPUT_KEY_EXTRA_CLASSES_FILE . '\'.'; - } - break; - case self::INPUT_KEY_GENERATION: - $errorMsg = $this->validateOutputPath($value, self::INPUT_KEY_GENERATION); - if ($errorMsg !== '') { - $errors[] = $errorMsg; - } - break; - case self::INPUT_KEY_DI: - $errorMsg = $this->validateOutputPath($value, self::INPUT_KEY_DI); - if ($errorMsg !== '') { - $errors[] = $errorMsg; - } - break; - case self::INPUT_KEY_EXCLUDE_PATTERN: - if (@preg_match($value, null) === false) { - $errors[] = 'Invalid pattern for command option \'' . self::INPUT_KEY_EXCLUDE_PATTERN - . '\'.'; - } - break; - } - } - return $errors; - } - - /** - * Validate output path based on type - * - * @param string $value - * @param string $type - * @return string - */ - private function validateOutputPath($value, $type) - { - $errorMsg = ''; - if (!file_exists($value)) { - $errorMsg = 'Path does not exist for the value of command option \'' . $type . '\'.'; - } - if (file_exists($value) && !is_writeable($value)) { - $errorMsg .= 'Non-writable directory is provided by the value of command option \'' - . $type . '\'.'; - - } - return $errorMsg; - } -} 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 4a34d8cefab49..6145af60699c8 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 @@ -45,7 +45,8 @@ public function write($key, array $config) $this->initialize(); file_put_contents( - $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.ser', + $this->directoryList->getPath(DirectoryList::DI) . '/' . $key + . \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::COMPILED_FILE_EXTENSION, $this->getJson()->encode($config) ); } From 626c2dd6b7717f16f88a08b843e2aea39aacf283 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 29 Sep 2016 21:57:28 -0500 Subject: [PATCH 053/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - Refactor ObjectManager Framework --- .../Unit/Model/DataObjectProcessorTest.php | 7 ++++--- .../ObjectManager/ConfigLoader/Compiled.php | 4 ++-- .../Reflection/Test/Unit/MethodsMapTest.php | 20 +++++++++---------- .../Test/Unit/ServiceInputProcessorTest.php | 15 +++++++------- .../Di/Compiler/Config/Writer/Filesystem.php | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php index fd239bea1be3f..c869a5361597f 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php @@ -7,6 +7,7 @@ namespace Magento\Webapi\Test\Unit\Model; use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Reflection\DataObjectProcessor; use Magento\Webapi\Model\Config as ModelConfig; class DataObjectProcessorTest extends \PHPUnit_Framework_TestCase @@ -31,10 +32,10 @@ protected function setup() 'typeProcessor' => $objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class), ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock = $this->getMock(JsonInterface::class); $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); + ->willReturnCallback(function ($data) { + return json_encode($data); }); $jsonMock->method('decode') ->willReturnCallback(function ($string) { diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index c48f7a0dc4d65..e31fdc4954eb4 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -12,7 +12,7 @@ class Compiled implements ConfigLoaderInterface { - const COMPILED_FILE_EXTENSION = '.json'; + const FILE_EXTENSION = '.json'; /** * Global config @@ -47,7 +47,7 @@ public function load($area) public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . $diPath . '/' . $area . self::COMPILED_FILE_EXTENSION; + return BP . $diPath . '/' . $area . self::FILE_EXTENSION; } /** diff --git a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php index 72dfc690ca0f4..f49583b1d3377 100644 --- a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php +++ b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php @@ -20,6 +20,9 @@ class MethodsMapTest extends \PHPUnit_Framework_TestCase */ private $object; + /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $jsonMock; + /** * Set up helper. */ @@ -48,19 +51,11 @@ protected function setUp() 'fieldNamer' => $fieldNamerMock, ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + $this->jsonMock = $this->getMock(JsonInterface::class); $objectManager->setBackwardCompatibleProperty( $this->object, 'json', - $jsonMock + $this->jsonMock ); } @@ -91,6 +86,11 @@ public function testGetMethodReturnType() public function testGetMethodsMap() { + $this->jsonMock->expects($this->once()) + ->method('encode') + ->willReturnCallback(function ($data) { + return json_encode($data); + }); $methodsMap = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); $this->assertEquals( [ diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php index 264963b9a1205..d89213636a45d 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php @@ -98,10 +98,10 @@ function () use ($objectManager) { 'fieldNamer' => $this->fieldNamer ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); + $jsonMock = $this->getMock(JsonInterface::class); $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); + ->willReturnCallback(function ($data) { + return json_encode($data); }); $jsonMock->method('decode') ->willReturnCallback(function ($string) { @@ -126,10 +126,11 @@ function () use ($objectManager) { /** @var \Magento\Framework\Reflection\NameFinder $nameFinder */ $nameFinder = $objectManager->getObject(\Magento\Framework\Reflection\NameFinder::class); - $serviceInputProcessorReflection = new \ReflectionClass(get_class($this->serviceInputProcessor)); - $typeResolverReflection = $serviceInputProcessorReflection->getProperty('nameFinder'); - $typeResolverReflection->setAccessible(true); - $typeResolverReflection->setValue($this->serviceInputProcessor, $nameFinder); + $objectManager->setBackwardCompatibleProperty( + $this->serviceInputProcessor, + 'nameFinder', + $nameFinder + ); } public function testSimpleProperties() 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 6145af60699c8..3a97bf5f2f2d5 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 @@ -46,7 +46,7 @@ public function write($key, array $config) file_put_contents( $this->directoryList->getPath(DirectoryList::DI) . '/' . $key - . \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::COMPILED_FILE_EXTENSION, + . \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::FILE_EXTENSION, $this->getJson()->encode($config) ); } From 13afb758a3f41337551efacb8c02a812a24f8723 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 29 Sep 2016 22:03:30 -0500 Subject: [PATCH 054/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - Refactor ObjectManager Framework --- .../Unit/ObjectManager/ConfigCacheTest.php | 50 +++++--- .../Unit/ObjectManager/ConfigLoaderTest.php | 111 +++++++----------- .../Test/Unit/Config/ConfigTest.php | 36 +++--- .../Test/Unit/PluginList/PluginListTest.php | 49 +++++--- .../Unit/Definition/Compiled/JsonTest.php | 33 ++++-- 5 files changed, 157 insertions(+), 122 deletions(-) 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 fa67df0a0dd00..86da375832550 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php @@ -12,12 +12,17 @@ class ConfigCacheTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\App\ObjectManager\ConfigCache */ - protected $configCache; + private $configCache; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\ObjectManager\ConfigCache|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheFrontendMock; + private $cacheFrontendMock; + + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; protected function setUp() { @@ -28,19 +33,11 @@ protected function setUp() ['cacheFrontend' => $this->cacheFrontendMock] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string); - }); + $this->jsonMock = $this->getMock(JsonInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( $this->configCache, 'json', - $jsonMock + $this->jsonMock ); } @@ -49,7 +46,10 @@ protected function tearDown() unset($this->configCache); } - public function testGet() + /** + * @dataProvider getDataProvider + */ + public function testGet($loadData, $expectedResult) { $key = 'key'; $this->cacheFrontendMock->expects( @@ -59,15 +59,33 @@ public function testGet() )->with( 'diConfig' . $key )->will( - $this->returnValue(false) + $this->returnValue($loadData) ); - $this->assertEquals(false, $this->configCache->get($key)); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $this->assertEquals($expectedResult, $this->configCache->get($key)); + } + + public function getDataProvider() + { + return [ + [false, false], + [json_encode(['some data']), ['some data']], + ]; } public function testSave() { $key = 'key'; $config = ['config']; + $this->jsonMock->expects($this->once()) + ->method('encode') + ->willReturnCallback(function ($data) { + return json_encode($data); + }); $this->cacheFrontendMock->expects($this->once())->method('save')->with(json_encode($config), '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 625ed6c088c1a..6c2a8455f9621 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php @@ -15,22 +15,27 @@ class ConfigLoaderTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\App\ObjectManager\ConfigLoader */ - protected $object; + private $object; /** * @var \Magento\Framework\ObjectManager\Config\Reader\DomFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $readerFactoryMock; + private $readerFactoryMock; /** * @var \Magento\Framework\ObjectManager\Config\Reader\Dom|\PHPUnit_Framework_MockObject_MockObject */ - protected $readerMock; + private $readerMock; /** * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; + + /** + * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $jsonMock; protected function setUp() { @@ -68,61 +73,11 @@ protected function setUp() 'readerFactory' => $this->readerFactoryMock, ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); - $objectManagerHelper->setBackwardCompatibleProperty( - $this->object, - 'json', - $jsonMock - ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); - $objectManagerHelper->setBackwardCompatibleProperty( - $this->object, - 'json', - $jsonMock - ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); - $objectManagerHelper->setBackwardCompatibleProperty( - $this->object, - 'json', - $jsonMock - ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + $this->jsonMock = $this->getMock(JsonInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( $this->object, 'json', - $jsonMock + $this->jsonMock ); } @@ -130,22 +85,27 @@ protected function setUp() * @param $area * @dataProvider loadDataProvider */ - public function testLoad($area) + public function testLoadNotCached($area) { $configData = ['some' => 'config', 'data' => 'value']; - $this->cacheMock->expects( - $this->once() - )->method( - 'load' - )->with( - $area . '::DiConfig' - )->will( - $this->returnValue(false) - ); + $this->cacheMock->expects($this->once()) + ->method('load') + ->with($area . '::DiConfig') + ->will($this->returnValue(false)); + $this->cacheMock->expects($this->once()) + ->method('save') + ->with(json_encode($configData)); $this->readerMock->expects($this->once())->method('read')->with($area)->will($this->returnValue($configData)); + $this->jsonMock->expects($this->once()) + ->method('encode') + ->willReturnCallback(function ($string) { + return json_encode($string); + }); + $this->jsonMock->expects($this->never())->method('decode'); + $this->assertEquals($configData, $this->object->load($area)); } @@ -162,4 +122,23 @@ public function loadDataProvider() 'any area files' => ['any'] ]; } + + public function testLoadCached() + { + $configData = ['some' => 'config', 'data' => 'value']; + + $this->cacheMock->expects($this->once()) + ->method('load') + ->willReturn(json_encode($configData)); + $this->cacheMock->expects($this->never()) + ->method('save'); + $this->readerMock->expects($this->never())->method('read'); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $this->jsonMock->expects($this->never())->method('encode'); + $this->assertEquals($configData, $this->object->load('testArea')); + } } 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 ba98791c6750a..0a94cb5bcb069 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -24,32 +24,32 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $configScopeMock; + private $configScopeMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $readerMock; + private $readerMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $omConfigMock; + private $omConfigMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $definitionMock; + private $definitionMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $relationsMock; + private $relationsMock; /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ private $jsonMock; @@ -75,15 +75,7 @@ protected function setUp() $this->relationsMock = $this->getMockForAbstractClass( \Magento\Framework\ObjectManager\RelationsInterface::class ); - $this->jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $this->jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $this->jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + $this->jsonMock = $this->getMock(JsonInterface::class); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->objectManagerHelper->mockObjectManager([JsonInterface::class => $this->jsonMock]); } @@ -155,6 +147,13 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type, $entit $this->relationsMock->expects($this->any())->method('has')->will($this->returnValue($expectedResult)); $this->relationsMock->expects($this->any())->method('getParents')->will($this->returnValue($entityParents)); + $this->jsonMock->expects($this->once()) + ->method('encode') + ->willReturnCallback(function ($data) { + return json_encode($data); + }); + $this->jsonMock->expects($this->never())->method('decode'); + $model = $this->objectManagerHelper->getObject( \Magento\Framework\Interception\Config\Config::class, [ @@ -194,6 +193,13 @@ public function testHasPluginsWhenDataIsCached($expectedResult, $type) ->with($cacheId) ->will($this->returnValue(json_encode($interceptionData))); + $this->jsonMock->expects($this->never())->method('encode'); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); + $model = $this->objectManagerHelper->getObject( \Magento\Framework\Interception\Config\Config::class, [ 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 09fdc9cb9ceaa..078a709d94fa2 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -25,17 +25,20 @@ class PluginListTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\Interception\PluginList\PluginList */ - protected $object; + private $object; /** * @var \Magento\Framework\Config\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $configScopeMock; + private $configScopeMock; /** * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; + + /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $jsonMock; protected function setUp() { @@ -77,19 +80,11 @@ protected function setUp() 'cacheId' => 'interception' ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $jsonMock->method('decode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + $this->jsonMock = $this->getMock(JsonInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( $this->object, 'json', - $jsonMock + $this->jsonMock ); } @@ -105,7 +100,6 @@ public function testGetPlugin() \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\StartingBackslash::class, 'getName' ); - $this->assertEquals( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, $this->object->getPlugin( @@ -228,6 +222,26 @@ public function testInheritPluginsWithNonExistingClass() $this->object->getNext('SomeType', 'someMethod'); } + public function testLoadScopedDataNotCached() + { + $this->configScopeMock->expects($this->exactly(2)) + ->method('getCurrentScope') + ->will($this->returnValue('scope')); + $this->jsonMock->expects($this->once()) + ->method('encode') + ->willReturnCallback( + function ($data) { + return json_encode($data); + } + ); + $this->jsonMock->expects($this->never()) + ->method('decode'); + $this->cacheMock->expects($this->once()) + ->method('save'); + + $this->assertEquals(null, $this->object->getNext('Type', 'method')); + } + /** * @covers \Magento\Framework\Interception\PluginList\PluginList::getNext * @covers \Magento\Framework\Interception\PluginList\PluginList::_loadScopedData @@ -240,6 +254,13 @@ public function testLoadScopedDataCached() $data = [['key'], ['key'], ['key']]; + $this->jsonMock->expects($this->never()) + ->method('encode'); + $this->jsonMock->expects($this->once()) + ->method('decode') + ->willReturnCallback(function ($string) { + return json_decode($string, true); + }); $this->cacheMock->expects($this->once()) ->method('load') ->with('global|scope|interception') diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php index 06beccec2b075..1dc4caee93ae9 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php @@ -17,7 +17,13 @@ protected function setUp() $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); } - public function testGetParametersWithoutDefinition() + /** + * @param array $signatures + * @param array $definitions + * @param mixed $expected + * @dataProvider getParametersDataProvider + */ + public function testGetParametersWithoutDefinition($signatures, $definitions, $expected) { $signatures = []; $definitions = ['wonderful' => null]; @@ -25,13 +31,21 @@ public function testGetParametersWithoutDefinition() $this->assertEquals(null, $model->getParameters('wonderful')); } - public function testGetParametersWithSignatureObject() + public function getParametersDataProvider() { $wonderfulSignature = new \stdClass(); - $signatures = ['wonderfulClass' => $wonderfulSignature]; - $definitions = ['wonderful' => 'wonderfulClass']; - $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); - $this->assertEquals($wonderfulSignature, $model->getParameters('wonderful')); + return [ + [ + [], + ['wonderful' => null], + null, + ], + [ + ['wonderfulClass' => $wonderfulSignature], + ['wonderful' => 'wonderfulClass'], + $wonderfulSignature, + ] + ]; } public function testGetParametersWithUnpacking() @@ -41,11 +55,8 @@ public function testGetParametersWithUnpacking() $definitions = ['wonderful' => 'wonderfulClass']; $object = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); $jsonMock = $this->getMock(JsonInterface::class); - $jsonMock->method('encode') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); - $jsonMock->method('decode') + $jsonMock->expects($this->once()) + ->method('decode') ->willReturnCallback(function ($data) { return json_decode($data, true); }); From 95707702748e4de389d3da6362081e2d949da59f Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 29 Sep 2016 22:10:36 -0500 Subject: [PATCH 055/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Refactoring tests --- .../Test/Unit/Model/Session/QuoteTest.php | 30 +++++------- .../ResourceModel/Product/CollectionTest.php | 41 ++++++---------- .../Product/Link/Product/CollectionTest.php | 37 +++++---------- .../Product/Option/CollectionTest.php | 36 ++++++-------- .../CatalogRule/Test/Unit/Model/RuleTest.php | 47 +++++-------------- .../ResourceModel/Advanced/CollectionTest.php | 37 ++++++--------- .../ResourceModel/Fulltext/CollectionTest.php | 37 ++++++--------- .../Test/Unit/Model/RuleTest.php | 46 +++++++----------- .../ShippingInformationManagementTest.php | 35 ++++++-------- .../Import/Product/Type/DownloadableTest.php | 26 ++++------ .../Unit/Model/Quote/Item/RepositoryTest.php | 34 +++++--------- .../Model/ShippingAddressManagementTest.php | 29 ++++-------- .../Unit/Model/ResourceModel/RuleTest.php | 38 ++++++--------- .../SalesRule/Test/Unit/Model/RuleTest.php | 43 +++++++---------- 14 files changed, 184 insertions(+), 332 deletions(-) diff --git a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php index 5db3c2e41c04a..f6ddb41abf583 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php @@ -12,6 +12,11 @@ */ class QuoteTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -105,6 +110,7 @@ class QuoteTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->customerRepositoryMock = $this->getMockForAbstractClass( \Magento\Customer\Api\CustomerRepositoryInterface::class, [], @@ -227,11 +233,16 @@ protected function setUp() ] ); - $this->prepareObjectManager([ - [\Magento\Quote\Api\CartManagementInterface::class, $this->cartManagementMock] + $this->objectManager->mockObjectManager([ + \Magento\Quote\Api\CartManagementInterface::class => $this->cartManagementMock ]); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * Run test getQuote method * @@ -416,19 +427,4 @@ public function getQuoteDataProvider() 'customer ids same' => [66, 66, 'never'], ]; } - - /** - * @param array $map - * @deprecated - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php index 332e775b019b2..7a948c3f869d6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php @@ -3,18 +3,18 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; - /** - * Class CollectionTest - * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CollectionTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -35,6 +35,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $entityFactory = $this->getMock(\Magento\Framework\Data\Collection\EntityFactory::class, [], [], '', false); $logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class) ->disableOriginalConstructor() @@ -113,14 +114,13 @@ protected function setUp() $entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]); $entityMock->expects($this->any())->method('getTable')->willReturnArgument(0); $this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock); - $helper = new ObjectManager($this); - $this->prepareObjectManager([ - [\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ] + ]); - $this->collection = $helper->getObject( + $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Collection::class, [ 'entityFactory' => $entityFactory, @@ -148,6 +148,11 @@ protected function setUp() $this->collection->setConnection($this->connectionMock); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testAddProductCategoriesFilter() { $condition = ['in' => [1,2]]; @@ -173,20 +178,4 @@ public function testAddProductCategoriesFilter() )->willReturnSelf(); $this->collection->addCategoriesFilter([$conditionType => $values]); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php index 3c92cde30012d..11eb542773d59 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php @@ -3,11 +3,8 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Link\Product; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; - /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) @@ -17,8 +14,8 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */ protected $collection; - /** @var ObjectManagerHelper */ - protected $objectManagerHelper; + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManager; /** @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $entityFactoryMock; @@ -76,6 +73,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->entityFactoryMock = $this->getMock( \Magento\Framework\Data\Collection\EntityFactory::class, [], @@ -133,14 +131,12 @@ function ($store) { $this->timezoneInterfaceMock = $this->getMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class); $this->sessionMock = $this->getMock(\Magento\Customer\Model\Session::class, [], [], '', false); $this->dateTimeMock = $this->getMock(\Magento\Framework\Stdlib\DateTime::class); - $this->objectManagerHelper = new ObjectManagerHelper($this); - $this->prepareObjectManager([ - [\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ] ]); - $this->collection = $this->objectManagerHelper->getObject( + $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class, [ 'entityFactory' => $this->entityFactoryMock, @@ -165,6 +161,11 @@ function ($store) { ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testSetProduct() { /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ @@ -175,20 +176,4 @@ public function testSetProduct() $this->collection->setProduct($product); $this->assertEquals(33, $this->collection->getStoreId()); } - - /** - * @param $map - */ - public function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php index decf8e66f6738..0aa45ddb7fb4a 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php @@ -3,20 +3,22 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Option; use \Magento\Catalog\Model\ResourceModel\Product\Option\Collection; use \Magento\Catalog\Model\ResourceModel\Product\Option\Value; /** - * Class CollectionTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @codingStandardsIgnoreFile */ class CollectionTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\EntityManager\MetadataPool */ @@ -79,6 +81,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->entityFactoryMock = $this->getMock( \Magento\Framework\Data\Collection\EntityFactory::class, ['create'], [], '', false ); @@ -147,9 +150,9 @@ protected function setUp() $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->selectMock->expects($this->exactly(2))->method('join'); - $this->prepareObjectManager([ - [\Magento\Framework\EntityManager\MetadataPool::class, $this->metadataPoolMock], - [\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class, $this->joinProcessor] + $this->objectManager->mockObjectManager([ + \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, + \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class => $this->joinProcessor ]); $this->collection = new Collection( @@ -164,24 +167,13 @@ protected function setUp() ); } - public function testReset() + protected function tearDown() { - $this->collection->reset(); + $this->objectManager->restoreObjectManager(); } - /** - * @param $map - */ - private function prepareObjectManager($map) + public function testReset() { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); + $this->collection->reset(); } } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index a624b87ebbe13..f9a8ead3abff9 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -3,14 +3,9 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\CatalogRule\Test\Unit\Model; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; - /** - * Class RuleTest - * @package Magento\CatalogRule\Test\Unit\Model * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class RuleTest extends \PHPUnit_Framework_TestCase @@ -18,8 +13,8 @@ class RuleTest extends \PHPUnit_Framework_TestCase /** @var \Magento\CatalogRule\Model\Rule */ protected $rule; - /** @var ObjectManagerHelper */ - protected $objectManagerHelper; + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManager; /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $storeManager; @@ -63,6 +58,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); $this->storeModel = $this->getMock(\Magento\Store\Model\Store::class, ['__wakeup', 'getId'], [], '', false); $this->combineFactory = $this->getMock( @@ -128,20 +124,14 @@ protected function setUp() false ); - $this->objectManagerHelper = new ObjectManagerHelper($this); - - $this->prepareObjectManager([ - [ - \Magento\Framework\Api\ExtensionAttributesFactory::class, - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false) - ], - [ - \Magento\Framework\Api\AttributeValueFactory::class, + $this->objectManager->mockObjectManager([ + \Magento\Framework\Api\ExtensionAttributesFactory::class => + $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), + \Magento\Framework\Api\AttributeValueFactory::class => $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ], ]); - $this->rule = $this->objectManagerHelper->getObject( + $this->rule = $this->objectManager->getObject( \Magento\CatalogRule\Model\Rule::class, [ 'storeManager' => $this->storeManager, @@ -153,6 +143,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @dataProvider dataProviderCallbackValidateProduct * @param bool $validate @@ -375,20 +370,4 @@ public function testGetConditionsFieldSetId() $expectedResult = 'form_namerule_conditions_fieldset_100'; $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName)); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php index 96580c6764e93..0a7363d1821af 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php @@ -7,7 +7,6 @@ use Magento\Catalog\Model\Product; use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; /** * Tests Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection @@ -16,6 +15,11 @@ */ class CollectionTest extends BaseCollectionTest { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection */ @@ -51,8 +55,7 @@ class CollectionTest extends BaseCollectionTest */ protected function setUp() { - $helper = new ObjectManagerHelper($this); - + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, [], [], '', false); $storeManager = $this->getStoreManager(); $universalFactory = $this->getUniversalFactory(); @@ -67,13 +70,12 @@ protected function setUp() ); $this->search = $this->getMock(\Magento\Search\Api\SearchInterface::class, [], [], '', false); - $this->prepareObjectManager([ - [\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ], ]); - $this->advancedCollection = $helper->getObject( + $this->advancedCollection = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class, [ 'eavConfig' => $this->eavConfig, @@ -87,6 +89,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testLoadWithFilterNoFilters() { $this->advancedCollection->loadWithFilter(); @@ -150,20 +157,4 @@ protected function getCriteriaBuilder() ->getMock(); return $criteriaBuilder; } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php index 84430c56476c0..0b762646cc7f1 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php @@ -14,6 +14,11 @@ */ class CollectionTest extends BaseCollectionTest { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\Search\Adapter\Mysql\TemporaryStorage|\PHPUnit_Framework_MockObject_MockObject */ @@ -64,20 +69,17 @@ class CollectionTest extends BaseCollectionTest */ protected function setUp() { - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->storeManager = $this->getStoreManager(); $this->universalFactory = $this->getUniversalFactory(); $this->scopeConfig = $this->getScopeConfig(); $this->criteriaBuilder = $this->getCriteriaBuilder(); $this->filterBuilder = $this->getFilterBuilder(); - $this->prepareObjectManager( + $this->objectManager->mockObjectManager( [ - [ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ], ] ); @@ -92,7 +94,7 @@ protected function setUp() ->method('create') ->willReturn($this->temporaryStorage); - $this->model = $helper->getObject( + $this->model = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::class, [ 'storeManager' => $this->storeManager, @@ -110,6 +112,11 @@ protected function setUp() $this->model->setFilterBuilder($this->filterBuilder); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @expectedException \Exception * @expectedExceptionCode 333 @@ -208,22 +215,6 @@ protected function addFiltersToFilterBuilder(MockObject $filterBuilder, array $f return $filterBuilder; } - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } - protected function createFilter() { $filter = $this->getMockBuilder(\Magento\Framework\Api\Filter::class) diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index afcc540a37545..4a3443b6d28f4 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -3,13 +3,15 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\CatalogWidget\Test\Unit\Model; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; - class RuleTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\CatalogWidget\Model\Rule */ @@ -22,25 +24,20 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->combineFactory = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\CombineFactory::class) ->setMethods(['create']) ->disableOriginalConstructor() ->getMock(); - $objectManagerHelper = new ObjectManagerHelper($this); - - $this->prepareObjectManager([ - [ - \Magento\Framework\Api\ExtensionAttributesFactory::class, - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false) - ], - [ - \Magento\Framework\Api\AttributeValueFactory::class, + $this->objectManager->mockObjectManager([ + \Magento\Framework\Api\ExtensionAttributesFactory::class => + $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), + \Magento\Framework\Api\AttributeValueFactory::class => $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ], ]); - $this->rule = $objectManagerHelper->getObject( + $this->rule = $this->objectManager->getObject( \Magento\CatalogWidget\Model\Rule::class, [ 'conditionsFactory' => $this->combineFactory @@ -48,6 +45,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testGetConditionsInstance() { $condition = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\Combine::class) @@ -62,20 +64,4 @@ public function testGetActionsInstance() { $this->assertNull($this->rule->getActionsInstance()); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php index 04d347ec237a9..7a5438ba17a5d 100644 --- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php @@ -11,6 +11,11 @@ */ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -103,6 +108,7 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->paymentMethodManagementMock = $this->getMock(\Magento\Quote\Api\PaymentMethodManagementInterface::class); $this->paymentDetailsFactoryMock = $this->getMock( \Magento\Checkout\Model\PaymentDetailsFactory::class, @@ -175,10 +181,10 @@ protected function setUp() $this->shippingFactoryMock = $this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false); - $this->prepareObjectManager([ - [\Magento\Quote\Model\ShippingAssignmentFactory::class, $this->shippingAssignmentFactoryMock], - [\Magento\Quote\Api\Data\CartExtensionFactory::class, $this->cartExtensionFactoryMock], - [\Magento\Quote\Model\ShippingFactory::class, $this->shippingFactoryMock], + $this->objectManager->mockObjectManager([ + \Magento\Quote\Model\ShippingAssignmentFactory::class => $this->shippingAssignmentFactoryMock, + \Magento\Quote\Api\Data\CartExtensionFactory::class => $this->cartExtensionFactoryMock, + \Magento\Quote\Model\ShippingFactory::class => $this->shippingFactoryMock, ]); $this->model = new \Magento\Checkout\Model\ShippingInformationManagement( @@ -194,6 +200,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Shipping method is not applicable for empty cart @@ -457,20 +468,4 @@ public function testSaveAddressInformation() $this->model->saveAddressInformation($cartId, $addressInformationMock) ); } - - /** - * @param array $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php index d3cd979b31a91..f3d5d4f5a9db0 100644 --- a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php +++ b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php @@ -710,8 +710,8 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) ->getMock(\Magento\Framework\EntityManager\MetadataPool::class, ['getLinkField'], [], '', false); $metadataPoolMock->expects($this->any())->method('getMetadata')->willReturnSelf(); - $this->prepareObjectManager([ - [\Magento\Framework\EntityManager\MetadataPool::class, $metadataPoolMock], + $this->objectManagerHelper->mockObjectManager([ + \Magento\Framework\EntityManager\MetadataPool::class => $metadataPoolMock ]); $this->downloadableModelMock = $this->objectManagerHelper->getObject( @@ -736,6 +736,12 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) $this->downloadableModelMock->saveData(); } + protected function tearDown() + { + parent::tearDown(); + $this->objectManagerHelper->restoreObjectManager(); + } + /** * Data for methods testSetUploaderDirFalse, testSetDestDirFalse, testDirWithoutPermissions * @@ -870,20 +876,4 @@ protected function setPropertyValue(&$object, $property, $value) $reflectionProperty->setValue($object, $value); return $object; } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php index dae3d56aa04b0..0929cc16fb3ec 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php @@ -12,6 +12,11 @@ */ class RepositoryTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Quote\Api\CartItemRepositoryInterface */ @@ -68,6 +73,7 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->quoteRepositoryMock = $this->getMock(\Magento\Quote\Api\CartRepositoryInterface::class); $this->productRepositoryMock = $this->getMock(\Magento\Catalog\Api\ProductRepositoryInterface::class); $this->itemDataFactoryMock = @@ -100,11 +106,8 @@ protected function setUp() '', false ); - $this->prepareObjectManager([ - [ - \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class, - $this->optionsProcessorMock - ] + $this->objectManager->mockObjectManager([ + \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class => $this->optionsProcessorMock ]); $this->repository = new \Magento\Quote\Model\Quote\Item\Repository( @@ -115,6 +118,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @return void */ @@ -246,20 +254,4 @@ public function testDeleteById() $this->assertTrue($this->repository->deleteById($cartId, $itemId)); } - - /** - * @param array $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php index abae7bae110e6..0b820cdfca035 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php @@ -104,11 +104,9 @@ protected function setUp() '', false ); - $this->prepareObjectManager([ - [ - \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class, - $this->amountErrorMessageMock - ] + $this->objectManager->mockObjectManager([ + \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class + => $this->amountErrorMessageMock ]); $this->service = $this->objectManager->getObject( @@ -124,6 +122,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @expected ExceptionMessage error345 @@ -375,20 +378,4 @@ public function testGetAddressOfQuoteWithVirtualProducts() $this->service->get('cartId'); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php index 5c60007543916..b492d59f56895 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -3,7 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\SalesRule\Test\Unit\Model\ResourceModel; use Magento\SalesRule\Api\Data\RuleInterface; @@ -13,6 +12,11 @@ */ class RuleTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\SalesRule\Model\ResourceModel\Rule */ @@ -60,7 +64,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->rule = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class) ->disableOriginalConstructor() ->getMock(); @@ -135,14 +139,11 @@ protected function setUp() ] ); - $this->prepareObjectManager([ - [ - \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class, - $associatedEntitiesMap - ], + $this->objectManager->mockObjectManager([ + \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class => $associatedEntitiesMap ]); - $this->model = $objectManager->getObject( + $this->model = $this->objectManager->getObject( \Magento\SalesRule\Model\ResourceModel\Rule::class, [ 'context' => $context, @@ -152,6 +153,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * test load */ @@ -184,20 +190,4 @@ public function testDelete() ->with($this->rule); $this->assertEquals($this->model->delete($this->rule), $this->model); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index f3d16c188fdf9..adf98a1528cd8 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -3,11 +3,15 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\SalesRule\Test\Unit\Model; class RuleTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\SalesRule\Model\Rule */ @@ -30,7 +34,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->coupon = $this->getMockBuilder(\Magento\SalesRule\Model\Coupon::class) ->disableOriginalConstructor() @@ -57,18 +61,14 @@ protected function setUp() ->setMethods(['create']) ->getMock(); - $this->prepareObjectManager([ - [ - \Magento\Framework\Api\ExtensionAttributesFactory::class, - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false) - ], - [ - \Magento\Framework\Api\AttributeValueFactory::class, + $this->objectManager->mockObjectManager([ + \Magento\Framework\Api\ExtensionAttributesFactory::class => + $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), + \Magento\Framework\Api\AttributeValueFactory::class => $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ], ]); - $this->model = $objectManager->getObject( + $this->model = $this->objectManager->getObject( \Magento\SalesRule\Model\Rule::class, [ 'couponFactory' => $couponFactory, @@ -78,6 +78,11 @@ protected function setUp() ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testLoadCouponCode() { $this->coupon->expects($this->once()) @@ -179,20 +184,4 @@ public function testGetActionsFieldSetId() $expectedResult = 'form_namerule_actions_fieldset_100'; $this->assertEquals($expectedResult, $this->model->getActionsFieldSetId($formName)); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } From 31d3c5622d6fbba424a7cf3fcf84cf6fcb4a9675 Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 17 Oct 2016 16:02:27 -0500 Subject: [PATCH 056/144] MAGETWO-58446: Remove uses of unserialize in usages of \Magento\Framework\Cache\FrontendInterface::load() - Merge upstream/develop resolve conflict --- .../Test/Unit/PluginList/PluginListTest.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) 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 f1dc8aee39e96..b32a14547eee0 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -40,10 +40,8 @@ class PluginListTest extends \PHPUnit_Framework_TestCase /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ private $jsonMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $objectManagerMock; + /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $loggerMock; protected function setUp() { @@ -64,8 +62,8 @@ protected function setUp() $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); - $this->objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $this->objectManagerMock->expects($this->any())->method('get')->will($this->returnArgument(0)); + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any())->method('get')->will($this->returnArgument(0)); $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); @@ -79,7 +77,7 @@ protected function setUp() 'relations' => new \Magento\Framework\ObjectManager\Relations\Runtime(), 'omConfig' => $omConfigMock, 'definitions' => new \Magento\Framework\Interception\Definition\Runtime(), - 'objectManager' => $this->objectManagerMock, + 'objectManager' => $objectManagerMock, 'classDefinitions' => $definitions, 'scopePriorityScheme' => ['global'], 'cacheId' => 'interception' @@ -91,6 +89,13 @@ protected function setUp() 'json', $this->jsonMock ); + + $this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); + $objectManagerHelper->setBackwardCompatibleProperty( + $this->object, + 'logger', + $this->loggerMock + ); } public function testGetPlugin() @@ -253,12 +258,7 @@ function ($data) { */ public function testInheritPluginsWithNotExistingPlugin() { - $loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); - $this->objectManagerMock->expects($this->once()) - ->method('get') - ->with(\Psr\Log\LoggerInterface::class) - ->willReturn($loggerMock); - $loggerMock->expects($this->once()) + $this->loggerMock->expects($this->once()) ->method('info') ->with("Reference to undeclared plugin with name 'simple_plugin'."); $this->configScopeMock->expects($this->any()) From 0c6df402a76c88f3464980b69d382abc1c0d2a23 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 18 Oct 2016 12:11:36 -0500 Subject: [PATCH 057/144] MAGETWO-59444: Create serializer interface and json class in framework - Refactor all the use cases --- .../Attribute/Source/Countryofmanufacture.php | 22 +++++++-------- .../Source/CountryofmanufactureTest.php | 12 ++++---- .../Unit/Model/ProductTypes/ConfigTest.php | 16 ++++++----- .../Cron/Test/Unit/Model/Config/DataTest.php | 8 ++++-- .../Model/Customer/NotificationStorage.php | 22 +++++++-------- .../Test/Unit/Model/Address/ConfigTest.php | 16 ++++++----- .../Customer/NotificationStorageTest.php | 12 ++++---- app/code/Magento/Directory/Block/Data.php | 26 ++++++++--------- .../Directory/Test/Unit/Block/DataTest.php | 14 +++++----- .../Country/Postcode/Config/DataTest.php | 14 ++++++---- .../Model/Entity/Attribute/ConfigTest.php | 10 ++++--- .../Test/Unit/Model/Export/ConfigTest.php | 10 ++++--- .../Test/Unit/Model/Import/ConfigTest.php | 10 ++++--- .../Test/Unit/Model/Config/DataTest.php | 12 ++++---- .../Sales/Test/Unit/Model/Config/DataTest.php | 12 ++++---- .../Magento/Store/Model/StoreResolver.php | 24 ++++++++-------- .../Unit/Model/DataObjectProcessorTest.php | 12 ++++---- .../Framework/Interception/AbstractPlugin.php | 4 +-- .../Magento/Framework/App/Config/Initial.php | 24 ++++++++-------- .../Framework/App/Config/ScopePool.php | 24 ++++++++-------- .../App/ObjectManager/ConfigCache.php | 24 ++++++++-------- .../App/ObjectManager/ConfigLoader.php | 22 +++++++-------- .../ObjectManager/ConfigLoader/Compiled.php | 19 ++++++------- .../Magento/Framework/App/Route/Config.php | 25 +++++++++-------- .../Framework/App/Router/ActionList.php | 24 ++++++++-------- .../App/Test/Unit/Config/InitialTest.php | 8 ++++-- .../App/Test/Unit/Config/ScopePoolTest.php | 12 ++++---- .../Unit/ObjectManager/ConfigCacheTest.php | 20 ++++++------- .../Unit/ObjectManager/ConfigLoaderTest.php | 24 ++++++++-------- .../Unit/ResourceConnection/ConfigTest.php | 6 ++-- .../App/Test/Unit/Route/ConfigTest.php | 12 ++++---- .../App/Test/Unit/Router/ActionListTest.php | 18 ++++++------ .../Magento/Framework/Config/Data.php | 24 ++++++++-------- .../Magento/Framework/Config/Data/Scoped.php | 9 +++--- .../Config/Test/Unit/Data/ScopedTest.php | 14 ++++++---- .../Framework/Config/Test/Unit/DataTest.php | 20 ++++++------- .../Framework/Interception/Config/Config.php | 24 ++++++++-------- .../Interception/PluginList/PluginList.php | 5 ++-- .../Test/Unit/Config/ConfigTest.php | 22 +++++++-------- .../Test/Unit/PluginList/PluginListTest.php | 28 +++++++++---------- .../Mview/Test/Unit/Config/DataTest.php | 14 ++++++---- .../ObjectManager/Config/Compiled.php | 22 +++++++-------- .../Framework/ObjectManager/Config/Config.php | 24 ++++++++-------- .../Definition/Compiled/Json.php | 22 +++++++-------- .../Test/Unit/Config/ConfigTest.php | 10 +++---- .../Unit/Definition/Compiled/JsonTest.php | 16 +++++------ .../Framework/Reflection/MethodsMap.php | 28 +++++++++---------- .../Reflection/Test/Unit/MethodsMapTest.php | 16 +++++------ .../Framework/Test/Unit/TranslateTest.php | 12 ++++---- lib/internal/Magento/Framework/Translate.php | 22 +++++++-------- .../Magento/Framework/Validator/Factory.php | 22 +++++++-------- .../Validator/Test/Unit/FactoryTest.php | 18 ++++++------ .../Config/Provider/Component/Definition.php | 22 +++++++-------- .../UiComponent/Config/Provider/Template.php | 24 ++++++++-------- .../Test/Unit/ServiceInputProcessorTest.php | 12 ++++---- .../Di/Compiler/Config/Writer/Filesystem.php | 22 +++++++-------- 56 files changed, 498 insertions(+), 472 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php b/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php index dc7189b54c073..8bcf01ba3db38 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Source/Countryofmanufacture.php @@ -36,9 +36,9 @@ class Countryofmanufacture extends AbstractSource implements OptionSourceInterfa protected $_countryFactory; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * Construct @@ -66,30 +66,30 @@ public function getAllOptions() { $cacheKey = 'COUNTRYOFMANUFACTURE_SELECT_STORE_' . $this->_storeManager->getStore()->getCode(); if ($cache = $this->_configCacheType->load($cacheKey)) { - $options = $this->getJson()->decode($cache); + $options = $this->getSerializer()->unserialize($cache); } else { /** @var \Magento\Directory\Model\Country $country */ $country = $this->_countryFactory->create(); /** @var \Magento\Directory\Model\ResourceModel\Country\Collection $collection */ $collection = $country->getResourceCollection(); $options = $collection->load()->toOptionArray(); - $this->_configCacheType->save($this->getJson()->encode($options), $cacheKey); + $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey); } return $options; } /** - * Get json encoder/decoder + * Get serializer * - * @return \Magento\Framework\Json\JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Json\JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php index 6a4e73fa176cf..8c0e090bfe941 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Catalog\Test\Unit\Model\Product\Attribute\Source; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase { @@ -46,19 +46,19 @@ protected function setUp() ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') + $serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); + $serializerMock->method('serialize') ->willReturnCallback(function ($string) { return json_encode($string); }); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); $this->objectManagerHelper->setBackwardCompatibleProperty( $this->countryOfManufacture, - 'json', - $jsonMock + 'serializer', + $serializerMock ); } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 4098a28964040..44ef53a34d0ef 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -23,9 +23,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase private $cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; /** * @var \Magento\Catalog\Model\ProductTypes\Config|\PHPUnit_Framework_MockObject_MockObject @@ -43,8 +43,10 @@ protected function setUp() false ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } protected function tearDown() @@ -63,7 +65,7 @@ public function testGetType($value, $expected) ->method('load') ->willReturn(json_encode($value)); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->willReturn($value); $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); $this->assertEquals($expected, $this->config->getType('global')); @@ -83,7 +85,7 @@ public function testGetAll() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(json_encode('"types":["Expected Data"]]')); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->willReturn(['types' => $expected]); $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); $this->assertEquals($expected, $this->config->getAll()); @@ -94,7 +96,7 @@ public function testIsProductSet() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(''); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->willReturn([]); $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 5ef35c015d029..5eecaa147fcbd 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -52,10 +52,12 @@ public function testGetJobs() $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); - $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $jsonMock]); + $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] + ); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturn($jobs); $configData = new \Magento\Cron\Model\Config\Data($reader, $cache, $dbReader, 'test_cache_id'); diff --git a/app/code/Magento/Customer/Model/Customer/NotificationStorage.php b/app/code/Magento/Customer/Model/Customer/NotificationStorage.php index 7c77fe9c8d500..67ee60971d98a 100644 --- a/app/code/Magento/Customer/Model/Customer/NotificationStorage.php +++ b/app/code/Magento/Customer/Model/Customer/NotificationStorage.php @@ -6,7 +6,7 @@ namespace Magento\Customer\Model\Customer; use Magento\Framework\Cache\FrontendInterface; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class NotificationStorage { @@ -22,9 +22,9 @@ class NotificationStorage */ /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * NotificationStorage constructor. @@ -45,7 +45,7 @@ public function __construct(FrontendInterface $cache) public function add($notificationType, $customerId) { $this->cache->save( - $this->getJson()->encode([ + $this->getSerializer()->serialize([ 'customer_id' => $customerId, 'notification_type' => $notificationType ]), @@ -90,17 +90,17 @@ private function getCacheKey($notificationType, $customerId) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index e62c29173e2d0..40a754f78ec2f 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -43,9 +43,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $_scopeConfigMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; /** * @var \Magento\Customer\Model\Address\Config @@ -99,16 +99,18 @@ protected function setUp() $this->_cacheMock->expects($this->once()) ->method('save') ->with( - \Zend_Json::encode($fixtureConfigData), + json_encode($fixtureConfigData), $this->_cacheId ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); - $this->jsonMock->method('encode') + $this->serializerMock->method('serialize') ->willReturn(json_encode($fixtureConfigData)); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->willReturn($fixtureConfigData); $this->_model = new \Magento\Customer\Model\Address\Config( diff --git a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php index 6c391d2d6d55e..e75cacc0781d5 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php @@ -21,9 +21,9 @@ class NotificationStorageTest extends \PHPUnit_Framework_TestCase private $cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -33,8 +33,8 @@ protected function setUp() NotificationStorage::class, ['cache' => $this->cacheMock] ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $objectManager->setBackwardCompatibleProperty($this->notificationStorage, 'json', $this->jsonMock); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $objectManager->setBackwardCompatibleProperty($this->notificationStorage, 'serializer', $this->serializerMock); } public function testAdd() @@ -46,8 +46,8 @@ public function testAdd() 'notification_type' => $notificationType ]; $jsonString = json_encode($data); - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->with($data) ->willReturn($jsonString); $this->cacheMock->expects($this->once()) diff --git a/app/code/Magento/Directory/Block/Data.php b/app/code/Magento/Directory/Block/Data.php index 920013c140935..3c8b682d1a1e8 100644 --- a/app/code/Magento/Directory/Block/Data.php +++ b/app/code/Magento/Directory/Block/Data.php @@ -36,9 +36,9 @@ class Data extends \Magento\Framework\View\Element\Template protected $directoryHelper; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\View\Element\Template\Context $context @@ -118,12 +118,12 @@ public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . $this->_storeManager->getStore()->getCode(); $cache = $this->_configCacheType->load($cacheKey); if ($cache) { - $options = $this->getJson()->decode($cache); + $options = $this->getSerializer()->unserialize($cache); } else { $options = $this->getCountryCollection() ->setForegroundCountries($this->getTopDestinations()) ->toOptionArray(); - $this->_configCacheType->save($this->getJson()->encode($options), $cacheKey); + $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey); } $html = $this->getLayout()->createBlock( \Magento\Framework\View\Element\Html\Select::class @@ -168,10 +168,10 @@ public function getRegionHtmlSelect() $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . $this->_storeManager->getStore()->getId(); $cache = $this->_configCacheType->load($cacheKey); if ($cache) { - $options = $this->getJson()->decode($cache); + $options = $this->getSerializer()->unserialize($cache); } else { $options = $this->getRegionCollection()->toOptionArray(); - $this->_configCacheType->save($this->getJson()->encode($options), $cacheKey); + $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey); } $html = $this->getLayout()->createBlock( \Magento\Framework\View\Element\Html\Select::class @@ -234,17 +234,17 @@ public function getRegionsJs() } /** - * Get json encoder/decoder + * Get serializer * - * @return \Magento\Framework\Json\JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Json\JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php index 1a81ba24d20fe..6b41140240a30 100644 --- a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php @@ -11,7 +11,7 @@ use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory; use Magento\Framework\App\Cache\Type\Config; use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\View\Element\Template\Context; use Magento\Framework\View\LayoutInterface; use Magento\Store\Model\ScopeInterface; @@ -78,19 +78,19 @@ protected function setUp() ] ); - $jsonMock = $this->getMock(JsonInterface::class, [], [], '', false); - $jsonMock->method('encode') + $serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); + $serializerMock->method('serialize') ->willReturnCallback(function ($string) { return json_encode($string); }); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); $objectManagerHelper->setBackwardCompatibleProperty( $this->block, - 'json', - $jsonMock + 'serializer', + $serializerMock ); } @@ -181,7 +181,7 @@ public function testGetCountryHtmlSelect( ->willReturn(false); $this->cacheTypeConfigMock->expects($this->once()) ->method('save') - ->with(\Zend_Json::encode($options), 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) + ->with(json_encode($options), 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) ->willReturnSelf(); $this->scopeConfigMock->expects($this->once()) diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index ca6e95585a6a8..2ae230da35c22 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -23,9 +23,9 @@ class DataTest extends \PHPUnit_Framework_TestCase private $cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -44,8 +44,10 @@ protected function setUp() '', false ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } public function testGet() @@ -54,8 +56,8 @@ public function testGet() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(json_encode($expected)); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturn($expected); $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data($this->readerMock, $this->cacheMock); $this->assertEquals($expected, $configData->get()); diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index b3d522f1c7699..b111bf8c72a16 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -37,7 +37,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $_cacheId; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Eav\Model\Entity\Attribute|\PHPUnit_Framework_MockObject_MockObject */ protected $_attribute; @@ -65,11 +65,13 @@ protected function setUp() ->with($this->_cacheId) ->willReturn(''); - $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturn([]); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $jsonMock]); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] + ); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index d15a5f345d4a9..04cb6bbcab687 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -23,9 +23,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $_configScopeMock; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $jsonMock; + private $serializerMock; /** * @var string @@ -48,8 +48,10 @@ protected function setUp() false ); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } protected function tearDown() diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php index 22127a6b54b57..2e182c4e35562 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php @@ -23,9 +23,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $_configScopeMock; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $jsonMock; + private $serializerMock; /** * @var string @@ -48,8 +48,10 @@ protected function setUp() false ); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } public function tearDown() 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 939b30a576b09..6747025eed2a8 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -43,9 +43,9 @@ class DataTest extends \PHPUnit_Framework_TestCase protected $indexers = ['indexer1' => [], 'indexer3' => []]; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -67,8 +67,10 @@ protected function setUp() '', false ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } protected function tearDown() @@ -85,7 +87,7 @@ public function testConstructorWithCache() ->with($this->cacheId) ->willReturn($jsonString); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->with($jsonString) ->willReturn($this->indexers); diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index 8514c77215622..ed001a14c9924 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -23,9 +23,9 @@ class DataTest extends \PHPUnit_Framework_TestCase private $_cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -36,8 +36,10 @@ protected function setUp() $this->_cacheMock = $this->getMockBuilder( \Magento\Framework\App\Cache\Type\Config::class )->disableOriginalConstructor()->getMock(); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } protected function tearDown() @@ -51,7 +53,7 @@ public function testGet() $this->_cacheMock->expects($this->once()) ->method('load'); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->willReturn($expected); $configData = new \Magento\Sales\Model\Config\Data($this->_readerMock, $this->_cacheMock); diff --git a/app/code/Magento/Store/Model/StoreResolver.php b/app/code/Magento/Store/Model/StoreResolver.php index a885e59d0bc01..cfb57849f25a8 100644 --- a/app/code/Magento/Store/Model/StoreResolver.php +++ b/app/code/Magento/Store/Model/StoreResolver.php @@ -5,7 +5,7 @@ */ namespace Magento\Store\Model; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class StoreResolver implements \Magento\Store\Api\StoreResolverInterface { @@ -50,9 +50,9 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface protected $request; /** - * @var JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository @@ -121,10 +121,10 @@ protected function getStoresData() $cacheKey = 'resolved_stores_' . md5($this->runMode . $this->scopeCode); $cacheData = $this->cache->load($cacheKey); if ($cacheData) { - $storesData = $this->getJson()->decode($cacheData); + $storesData = $this->getSerializer()->unserialize($cacheData); } else { $storesData = $this->readStoresData(); - $this->cache->save($this->getJson()->encode($storesData), $cacheKey, [self::CACHE_TAG]); + $this->cache->save($this->getSerializer()->serialize($storesData), $cacheKey, [self::CACHE_TAG]); } return $storesData; } @@ -177,17 +177,17 @@ protected function getDefaultStoreById($id) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php index c869a5361597f..8c62cfcbc09d4 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php @@ -6,7 +6,7 @@ namespace Magento\Webapi\Test\Unit\Model; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Reflection\DataObjectProcessor; use Magento\Webapi\Model\Config as ModelConfig; @@ -32,19 +32,19 @@ protected function setup() 'typeProcessor' => $objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class), ] ); - $jsonMock = $this->getMock(JsonInterface::class); - $jsonMock->method('encode') + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data); }); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); $objectManager->setBackwardCompatibleProperty( $methodsMapProcessor, - 'json', - $jsonMock + 'serializer', + $serializerMock ); $this->dataObjectProcessor = $objectManager->getObject( \Magento\Framework\Reflection\DataObjectProcessor::class, diff --git a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php index a6371c607c4af..2e3711c3be0b6 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php @@ -71,7 +71,7 @@ public function setUpInterceptionConfig($pluginConfig) $definitions ); $interceptionDefinitions = new Definition\Runtime(); - $json = new \Magento\Framework\Json\Json(); + $json = new \Magento\Framework\Serialize\Serializer\Json(); $sharedInstances = [ \Magento\Framework\Config\CacheInterface::class => $cache, \Magento\Framework\Config\ScopeInterface::class => $configScope, @@ -81,7 +81,7 @@ public function setUpInterceptionConfig($pluginConfig) \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => $config, \Magento\Framework\ObjectManager\DefinitionInterface::class => $definitions, \Magento\Framework\Interception\DefinitionInterface::class => $interceptionDefinitions, - \Magento\Framework\Json\JsonInterface::class => $json, + \Magento\Framework\Serialize\SerializerInterface::class => $json, ]; $this->_objectManager = new \Magento\Framework\ObjectManager\ObjectManager( $factory, diff --git a/lib/internal/Magento/Framework/App/Config/Initial.php b/lib/internal/Magento/Framework/App/Config/Initial.php index 45b1f31b944dd..84fd2bfa52d70 100644 --- a/lib/internal/Magento/Framework/App/Config/Initial.php +++ b/lib/internal/Magento/Framework/App/Config/Initial.php @@ -8,7 +8,7 @@ namespace Magento\Framework\App\Config; use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class Initial { @@ -32,9 +32,9 @@ class Initial protected $_metadata = []; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\App\Config\Initial\Reader $reader @@ -47,9 +47,9 @@ public function __construct( $data = $cache->load(self::CACHE_ID); if (!$data) { $data = $reader->read(); - $cache->save($this->getJson()->encode($data), self::CACHE_ID); + $cache->save($this->getSerializer()->serialize($data), self::CACHE_ID); } else { - $data = $this->getJson()->decode($data); + $data = $this->getSerializer()->unserialize($data); } $this->_data = $data['data']; $this->_metadata = $data['metadata']; @@ -84,17 +84,17 @@ public function getMetadata() } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/App/Config/ScopePool.php b/lib/internal/Magento/Framework/App/Config/ScopePool.php index 55ffe3ff46446..018e21f86a782 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -6,7 +6,7 @@ namespace Magento\Framework\App\Config; use Magento\Framework\App\RequestInterface; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -51,9 +51,9 @@ class ScopePool private $request; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\App\Config\Scope\ReaderPoolInterface $readerPool @@ -111,7 +111,7 @@ public function getScope($scopeType, $scopeCode = null) $data = $this->_cache->load($cacheKey); if ($data) { - $data = $this->getJson()->decode($data); + $data = $this->getSerializer()->unserialize($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { @@ -119,7 +119,7 @@ public function getScope($scopeType, $scopeCode = null) } else { $data = $reader->read($scopeCode); } - $this->_cache->save($this->getJson()->encode($data), $cacheKey, [self::CACHE_TAG]); + $this->_cache->save($this->getSerializer()->serialize($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } @@ -161,17 +161,17 @@ protected function _getScopeCode($scopeType, $scopeCode) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php index 342c9ab7bfb31..dbb3bed895697 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigCache.php @@ -7,7 +7,7 @@ */ namespace Magento\Framework\App\ObjectManager; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterface { @@ -24,9 +24,9 @@ class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterfa protected $_prefix = 'diConfig'; /** - * @var JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\Cache\FrontendInterface $cacheFrontend @@ -44,7 +44,7 @@ public function __construct(\Magento\Framework\Cache\FrontendInterface $cacheFro */ public function get($key) { - return $this->getJson()->decode($this->_cacheFrontend->load($this->_prefix . $key)); + return $this->getSerializer()->unserialize($this->_cacheFrontend->load($this->_prefix . $key)); } /** @@ -56,21 +56,21 @@ public function get($key) */ public function save(array $config, $key) { - $this->_cacheFrontend->save($this->getJson()->encode($config), $this->_prefix . $key); + $this->_cacheFrontend->save($this->getSerializer()->serialize($config), $this->_prefix . $key); } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php index 0ef3e5d3e5954..2cfa2b006ff1e 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader.php @@ -7,7 +7,7 @@ */ namespace Magento\Framework\App\ObjectManager; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\ObjectManager\ConfigLoaderInterface; class ConfigLoader implements ConfigLoaderInterface @@ -34,9 +34,9 @@ class ConfigLoader implements ConfigLoaderInterface protected $_cache; /** - * @var JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\Config\CacheInterface $cache @@ -73,9 +73,9 @@ public function load($area) if (!$data) { $data = $this->_getReader()->read($area); - $this->_cache->save($this->getJson()->encode($data), $cacheId); + $this->_cache->save($this->getSerializer()->serialize($data), $cacheId); } else { - $data = $this->getJson()->decode($data); + $data = $this->getSerializer()->unserialize($data); } return $data; @@ -84,15 +84,15 @@ public function load($area) /** * Get json encoder/decoder * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + 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 e31fdc4954eb4..ac55963655b87 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -7,7 +7,6 @@ namespace Magento\Framework\App\ObjectManager\ConfigLoader; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\ConfigLoaderInterface; class Compiled implements ConfigLoaderInterface @@ -22,9 +21,9 @@ class Compiled implements ConfigLoaderInterface private $configCache = []; /** - * @var JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * {inheritdoc} @@ -34,7 +33,7 @@ public function load($area) if (isset($this->configCache[$area])) { return $this->configCache[$area]; } - $this->configCache[$area] = $this->getJson()->decode(\file_get_contents(self::getFilePath($area))); + $this->configCache[$area] = $this->getSerializer()->unserialize(\file_get_contents(self::getFilePath($area))); return $this->configCache[$area]; } @@ -51,16 +50,16 @@ public static function getFilePath($area) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = new \Magento\Framework\Json\Json(); + if ($this->serializer === null) { + $this->serializer = new \Magento\Framework\Serialize\Serializer\Json(); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/App/Route/Config.php b/lib/internal/Magento/Framework/App/Route/Config.php index 716af96d342c0..60412e7fa888b 100644 --- a/lib/internal/Magento/Framework/App/Route/Config.php +++ b/lib/internal/Magento/Framework/App/Route/Config.php @@ -7,7 +7,7 @@ */ namespace Magento\Framework\App\Route; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class Config implements ConfigInterface { @@ -42,9 +42,9 @@ class Config implements ConfigInterface protected $_routes; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * @param Config\Reader $reader @@ -80,7 +80,7 @@ protected function _getRoutes($scope = null) return $this->_routes[$scope]; } $cacheId = $scope . '::' . $this->_cacheId; - $cachedRoutes = $this->getJson()->decode($this->_cache->load($cacheId)); + $cachedRoutes = $this->getSerializer()->unserialize($this->_cache->load($cacheId)); if (is_array($cachedRoutes)) { $this->_routes[$scope] = $cachedRoutes; return $cachedRoutes; @@ -88,7 +88,8 @@ protected function _getRoutes($scope = null) $routers = $this->_reader->read($scope); $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes']; - $this->_cache->save($this->getJson()->encode($routes), $cacheId); + $routesData = $this->getSerializer()->serialize($routes); + $this->_cache->save($routesData, $cacheId); $this->_routes[$scope] = $routes; return $routes; } @@ -142,17 +143,17 @@ public function getModulesByFrontName($frontName, $scope = null) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index f2f0a5015fb91..dbfc9d2b9f502 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -6,7 +6,7 @@ */ namespace Magento\Framework\App\Router; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Module\Dir\Reader as ModuleReader; class ActionList @@ -37,9 +37,9 @@ class ActionList ]; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\Config\CacheInterface $cache @@ -60,9 +60,9 @@ public function __construct( $data = $cache->load($cacheKey); if (!$data) { $this->actions = $moduleReader->getActionFiles(); - $cache->save($this->getJson()->encode($this->actions), $cacheKey); + $cache->save($this->getSerializer()->serialize($this->actions), $cacheKey); } else { - $this->actions = $this->getJson()->decode($data); + $this->actions = $this->getSerializer()->unserialize($data); } } @@ -100,17 +100,17 @@ public function get($module, $area, $namespace, $action) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index 5cf32b81b4566..ab200ea88ef17 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -60,11 +60,13 @@ protected function setUp() ->method('load') ->with('initial_config') ->willReturn(json_encode($this->data)); - $jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $jsonMock->method('decode') + $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $serializerMock->method('unserialize') ->willReturn($this->data); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $jsonMock]); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] + ); $this->config = new \Magento\Framework\App\Config\Initial( $this->readerMock, 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 d274b650f56b7..06f35c94a4661 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -34,9 +34,9 @@ class ScopePoolTest extends \PHPUnit_Framework_TestCase private $_cache; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; /** * @var \Magento\Framework\App\Config\ScopePool @@ -61,8 +61,8 @@ protected function setUp() 'cacheId' => 'test_cache_id' ] ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $objectManager->setBackwardCompatibleProperty($this->_object, 'json', $this->jsonMock); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $objectManager->setBackwardCompatibleProperty($this->_object, 'serializer', $this->serializerMock); $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) ->disableOriginalConstructor() @@ -114,7 +114,7 @@ public function testGetScopeConfigNotCached($scopeType, $scope, array $data) ->with('testScope') ->willReturn($data); $jsonString = json_encode($data); - $this->jsonMock->method('encode') + $this->serializerMock->method('serialize') ->with($data) ->willReturn($jsonString); $this->_cache->expects($this->once()) @@ -168,7 +168,7 @@ public function testGetScopeConfigCached($scopeType, $scope, array $data, $cache ->method('load') ->with($cacheKey) ->willReturn($cachedData); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->willReturn($data); $configData = $this->getMock(\Magento\Framework\App\Config\Data::class, [], [], '', false); $this->_dataFactory->expects($this->once()) 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 86da375832550..315bfeeb51f79 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\App\Test\Unit\ObjectManager; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class ConfigCacheTest extends \PHPUnit_Framework_TestCase { @@ -20,9 +20,9 @@ class ConfigCacheTest extends \PHPUnit_Framework_TestCase private $cacheFrontendMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -33,11 +33,11 @@ protected function setUp() ['cacheFrontend' => $this->cacheFrontendMock] ); - $this->jsonMock = $this->getMock(JsonInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( $this->configCache, - 'json', - $this->jsonMock + 'serializer', + $this->serializerMock ); } @@ -61,8 +61,8 @@ public function testGet($loadData, $expectedResult) )->will( $this->returnValue($loadData) ); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); @@ -81,8 +81,8 @@ public function testSave() { $key = 'key'; $config = ['config']; - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data); }); 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 6c2a8455f9621..b5e7c46913949 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php @@ -8,7 +8,7 @@ namespace Magento\Framework\App\Test\Unit\ObjectManager; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class ConfigLoaderTest extends \PHPUnit_Framework_TestCase { @@ -33,9 +33,9 @@ class ConfigLoaderTest extends \PHPUnit_Framework_TestCase private $cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -73,11 +73,11 @@ protected function setUp() 'readerFactory' => $this->readerFactoryMock, ] ); - $this->jsonMock = $this->getMock(JsonInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( $this->object, - 'json', - $this->jsonMock + 'serializer', + $this->serializerMock ); } @@ -99,12 +99,12 @@ public function testLoadNotCached($area) ->with(json_encode($configData)); $this->readerMock->expects($this->once())->method('read')->with($area)->will($this->returnValue($configData)); - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturnCallback(function ($string) { return json_encode($string); }); - $this->jsonMock->expects($this->never())->method('decode'); + $this->serializerMock->expects($this->never())->method('unserialize'); $this->assertEquals($configData, $this->object->load($area)); } @@ -133,12 +133,12 @@ public function testLoadCached() $this->cacheMock->expects($this->never()) ->method('save'); $this->readerMock->expects($this->never())->method('read'); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); - $this->jsonMock->expects($this->never())->method('encode'); + $this->serializerMock->expects($this->never())->method('serialize'); $this->assertEquals($configData, $this->object->load('testArea')); } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php index d4284491df880..fa96ef92f7b0b 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php @@ -50,7 +50,7 @@ protected function setUp() '', false ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); $this->resourcesConfig = [ 'mainResourceName' => ['name' => 'mainResourceName', 'extends' => 'anotherResourceName'], @@ -68,7 +68,7 @@ protected function setUp() $this->cacheMock->expects($this->any()) ->method('load') ->willReturn($jsonString); - $this->jsonMock->method('decode') + $serializerMock->method('unserialize') ->with($jsonString) ->willReturn($this->resourcesConfig); @@ -85,7 +85,7 @@ protected function setUp() $deploymentConfigMock, 'cacheId' ); - $objectManager->setBackwardCompatibleProperty($this->config, 'json', $this->jsonMock); + $objectManager->setBackwardCompatibleProperty($this->config, 'serializer', $serializerMock); } /** diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 95101b42d2143..77dd4479e3198 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -51,13 +51,13 @@ protected function setUp() 'areaList' => $this->_areaList ] ); - $jsonMock = $this->getMock(\Magento\Framework\Json\Json::class, [], [], '', false); - $objectManager->setBackwardCompatibleProperty($this->_config, 'json', $jsonMock); - $jsonMock->method('encode') + $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $objectManager->setBackwardCompatibleProperty($this->_config, 'serializer', $serializerMock); + $serializerMock->method('serialize') ->willReturnCallback(function ($string) { return json_encode($string); }); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); @@ -138,8 +138,8 @@ public function testGetRouteByFrontNameNoCache() ); $this->_cacheMock->expects($this->once()) - ->method('save') - ->with(json_encode($routes), 'scope::RoutesConfig'); + ->method('save'); +// ->with(json_encode($routes), 'scope::RoutesConfig'); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope')); diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 9101fa790e7f2..993faa1dcd679 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -28,9 +28,9 @@ class ActionListTest extends \PHPUnit_Framework_TestCase private $actionList; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -49,8 +49,10 @@ protected function setUp() '', false ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } protected function tearDown() @@ -63,8 +65,8 @@ public function testConstructActionsCached() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn('"data"'); - $this->jsonMock->expects($this->once()) - ->method('decode'); + $this->serializerMock->expects($this->once()) + ->method('unserialize'); $this->cacheMock->expects($this->never()) ->method('save'); $this->readerMock->expects($this->never()) @@ -77,8 +79,8 @@ public function testConstructActionsNoCached() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(false); - $this->jsonMock->expects($this->once()) - ->method('encode'); + $this->serializerMock->expects($this->once()) + ->method('serialize'); $this->cacheMock->expects($this->once()) ->method('save'); $this->readerMock->expects($this->once()) diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 005bbb4eedc6a..7598596dc9f04 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -7,7 +7,7 @@ */ namespace Magento\Framework\Config; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; /** * @SuppressWarnings(PHPMD.NumberOfChildren) @@ -65,9 +65,9 @@ class Data implements \Magento\Framework\Config\DataInterface private $cacheId; /** - * @var JsonInterface + * @var SerializerInterface */ - protected $json; + protected $serializer; /** * Constructor @@ -96,9 +96,9 @@ protected function initData() $data = $this->cache->load($this->cacheId); if (false === $data) { $data = $this->reader->read(); - $this->cache->save($this->getJson()->encode($data), $this->cacheId, $this->cacheTags); + $this->cache->save($this->getSerializer()->serialize($data), $this->cacheId, $this->cacheTags); } else { - $data = $this->getJson()->decode($data); + $data = $this->getSerializer()->unserialize($data); } $this->merge($data); @@ -149,17 +149,17 @@ public function reset() } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - protected function getJson() + protected function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 9df6f61bd07f9..b5f01a14335fa 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -5,8 +5,6 @@ */ namespace Magento\Framework\Config\Data; -use Magento\Framework\Json\JsonInterface; - class Scoped extends \Magento\Framework\Config\Data { /** @@ -100,11 +98,14 @@ protected function _loadScopedData() if (false == isset($this->_loadedScopes[$scopeCode])) { if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId)) ) { - $data = $this->getJson()->decode($data); + $data = $this->getSerializer()->unserialize($data); } else { $data = $this->_reader->read($scopeCode); if ($scopeCode !== 'primary') { - $this->_cache->save($this->getJson()->encode($data), $scopeCode . '::' . $this->_cacheId); + $this->_cache->save( + $this->getSerializer()->serialize($data), + $scopeCode . '::' . $this->_cacheId + ); } } $this->merge($data); 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 81ea1b820bf9e..8af81fe5dc608 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -33,9 +33,9 @@ class ScopedTest extends \PHPUnit_Framework_TestCase protected $_cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -44,8 +44,10 @@ protected function setUp() $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); $this->_model = new \Magento\Framework\Config\Data\Scoped( $this->_readerMock, @@ -134,7 +136,7 @@ public function testGetScopeSwitchingWithNonCachedData() $this->returnValue($testValue) ); - $this->jsonMock->method('encode') + $this->serializerMock->method('serialize') ->with($testValue) ->willReturn($jsonString); @@ -164,7 +166,7 @@ public function testGetScopeSwitchingWithCachedData() $this->returnValue('adminhtml') ); - $this->jsonMock->method('decode') + $this->serializerMock->method('unserialize') ->with($jsonString) ->willReturn($testValue); diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index 82f33b129edb0..b04257713389f 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -26,17 +26,17 @@ class DataTest extends \PHPUnit_Framework_TestCase private $cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]); } protected function tearDown() @@ -54,8 +54,8 @@ public function testGetConfigNotCached() $this->readerMock->expects($this->once()) ->method('read') ->willReturn($data); - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->with($data); $config = new \Magento\Framework\Config\Data( $this->readerMock, @@ -78,8 +78,8 @@ public function testGetConfigCached() ->willReturn($jsonString); $this->readerMock->expects($this->never()) ->method('read'); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->with($jsonString) ->willReturn($data); $config = new \Magento\Framework\Config\Data( @@ -98,8 +98,8 @@ public function testReset() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn($jsonString); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->with($jsonString) ->willReturn([]); $this->cacheMock->expects($this->once()) diff --git a/lib/internal/Magento/Framework/Interception/Config/Config.php b/lib/internal/Magento/Framework/Interception/Config/Config.php index 72cb9aef0e30a..a536a8c759432 100644 --- a/lib/internal/Magento/Framework/Interception/Config/Config.php +++ b/lib/internal/Magento/Framework/Interception/Config/Config.php @@ -7,7 +7,7 @@ */ namespace Magento\Framework\Interception\Config; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class Config implements \Magento\Framework\Interception\ConfigInterface { @@ -73,9 +73,9 @@ class Config implements \Magento\Framework\Interception\ConfigInterface protected $_scopeList; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\Config\ReaderInterface $reader @@ -105,7 +105,7 @@ public function __construct( $intercepted = $this->_cache->load($this->_cacheId); if ($intercepted !== false) { - $this->_intercepted = $this->getJson()->decode($intercepted); + $this->_intercepted = $this->getSerializer()->unserialize($intercepted); } else { $this->initialize($this->_classDefinitions->getClasses()); } @@ -136,7 +136,7 @@ public function initialize($classDefinitions = []) foreach ($classDefinitions as $class) { $this->hasPlugins($class); } - $this->_cache->save($this->getJson()->encode($this->_intercepted), $this->_cacheId); + $this->_cache->save($this->getSerializer()->serialize($this->_intercepted), $this->_cacheId); } /** @@ -184,17 +184,17 @@ public function hasPlugins($type) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index 812b47ddba854..5d67a9077b2cf 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -14,7 +14,6 @@ use Magento\Framework\Interception\DefinitionInterface; use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; -use Magento\Framework\Json\JsonInterface; use Magento\Framework\ObjectManager\RelationsInterface; use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions; use Magento\Framework\ObjectManagerInterface; @@ -276,7 +275,7 @@ protected function _loadScopedData() $cacheId = implode('|', $this->_scopePriorityScheme) . "|" . $this->_cacheId; $data = $this->_cache->load($cacheId); if ($data) { - list($this->_data, $this->_inherited, $this->_processed) = $this->getJson()->decode($data); + list($this->_data, $this->_inherited, $this->_processed) = $this->getSerializer()->unserialize($data); foreach ($this->_scopePriorityScheme as $scope) { $this->_loadedScopes[$scope] = true; } @@ -310,7 +309,7 @@ protected function _loadScopedData() $this->_inheritPlugins($class); } $this->_cache->save( - $this->getJson()->encode([$this->_data, $this->_inherited, $this->_processed]), + $this->getSerializer()->serialize([$this->_data, $this->_inherited, $this->_processed]), $cacheId ); } 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 0a94cb5bcb069..d51db8bc36469 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -6,7 +6,7 @@ // @codingStandardsIgnoreFile namespace Magento\Framework\Interception\Test\Unit\Config; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; @@ -51,8 +51,8 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ private $relationsMock; - /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ private $objectManagerHelper; @@ -75,9 +75,9 @@ protected function setUp() $this->relationsMock = $this->getMockForAbstractClass( \Magento\Framework\ObjectManager\RelationsInterface::class ); - $this->jsonMock = $this->getMock(JsonInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->objectManagerHelper->mockObjectManager([JsonInterface::class => $this->jsonMock]); + $this->objectManagerHelper->mockObjectManager([SerializerInterface::class => $this->serializerMock]); } protected function tearDown() @@ -147,12 +147,12 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type, $entit $this->relationsMock->expects($this->any())->method('has')->will($this->returnValue($expectedResult)); $this->relationsMock->expects($this->any())->method('getParents')->will($this->returnValue($entityParents)); - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data); }); - $this->jsonMock->expects($this->never())->method('decode'); + $this->serializerMock->expects($this->never())->method('unserialize'); $model = $this->objectManagerHelper->getObject( \Magento\Framework\Interception\Config\Config::class, @@ -193,9 +193,9 @@ public function testHasPluginsWhenDataIsCached($expectedResult, $type) ->with($cacheId) ->will($this->returnValue(json_encode($interceptionData))); - $this->jsonMock->expects($this->never())->method('encode'); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->never())->method('serialize'); + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); 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 b32a14547eee0..e162cd37c789b 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\Interception\Test\Unit\PluginList; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; @@ -37,8 +37,8 @@ class PluginListTest extends \PHPUnit_Framework_TestCase */ private $cacheMock; - /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $loggerMock; @@ -83,11 +83,11 @@ protected function setUp() 'cacheId' => 'interception' ] ); - $this->jsonMock = $this->getMock(JsonInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( $this->object, - 'json', - $this->jsonMock + 'serializer', + $this->serializerMock ); $this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); @@ -237,15 +237,15 @@ public function testLoadScopedDataNotCached() $this->configScopeMock->expects($this->exactly(2)) ->method('getCurrentScope') ->will($this->returnValue('scope')); - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturnCallback( function ($data) { return json_encode($data); } ); - $this->jsonMock->expects($this->never()) - ->method('decode'); + $this->serializerMock->expects($this->never()) + ->method('unserialize'); $this->cacheMock->expects($this->once()) ->method('save'); @@ -280,10 +280,10 @@ public function testLoadScopedDataCached() $data = [['key'], ['key'], ['key']]; - $this->jsonMock->expects($this->never()) - ->method('encode'); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->never()) + ->method('serialize'); + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index 71075b64b7dbc..ce9a97b7ac90a 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -43,9 +43,9 @@ class DataTest extends \PHPUnit_Framework_TestCase private $views = ['view1' => [], 'view3' => []]; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; protected function setUp() { @@ -70,8 +70,10 @@ protected function setUp() ['getItems'] ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Json\JsonInterface::class => $this->jsonMock]); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->objectManager->mockObjectManager( + [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + ); } public function tearDown() @@ -88,8 +90,8 @@ public function testConstructorWithCache() $this->stateCollection->expects($this->never())->method('getItems'); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturn($this->views); $this->config = new \Magento\Framework\Mview\Config\Data( diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index 3cc4fe8c25e08..95331616b5b9f 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -6,7 +6,7 @@ */ namespace Magento\Framework\ObjectManager\Config; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\ObjectManager\ConfigCacheInterface; use Magento\Framework\ObjectManager\RelationsInterface; @@ -28,9 +28,9 @@ class Compiled implements \Magento\Framework\ObjectManager\ConfigInterface private $preferences; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * @param array $data @@ -78,7 +78,7 @@ public function getArguments($type) { if (isset($this->arguments[$type])) { if (is_string($this->arguments[$type])) { - $this->arguments[$type] = $this->getJson()->decode($this->arguments[$type]); + $this->arguments[$type] = $this->getSerializer()->unserialize($this->arguments[$type]); } return $this->arguments[$type]; } else { @@ -161,17 +161,17 @@ public function getPreferences() } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Config.php b/lib/internal/Magento/Framework/ObjectManager/Config/Config.php index b80b5b30ed152..7ad196cdd34b4 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Config.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Config.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\ObjectManager\Config; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\ObjectManager\ConfigCacheInterface; use Magento\Framework\ObjectManager\DefinitionInterface; use Magento\Framework\ObjectManager\RelationsInterface; @@ -76,9 +76,9 @@ class Config implements \Magento\Framework\ObjectManager\ConfigInterface protected $_mergedArguments; /** - * @var JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param RelationsInterface $relations @@ -273,12 +273,12 @@ public function extend(array $configuration) if ($this->_cache) { if (!$this->_currentCacheKey) { $this->_currentCacheKey = md5( - $this->getJson()->encode( + $this->getSerializer()->serialize( [$this->_arguments, $this->_nonShared, $this->_preferences, $this->_virtualTypes] ) ); } - $key = md5($this->_currentCacheKey . $this->getJson()->encode($configuration)); + $key = md5($this->_currentCacheKey . $this->getSerializer()->serialize($configuration)); $cached = $this->_cache->get($key); if ($cached) { list( @@ -333,17 +333,17 @@ public function getPreferences() } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php index 1c049f3ab5313..f68e5203a23fe 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\ObjectManager\Definition\Compiled; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class Json extends \Magento\Framework\ObjectManager\Definition\Compiled { @@ -15,9 +15,9 @@ class Json extends \Magento\Framework\ObjectManager\Definition\Compiled const MODE_NAME = 'json'; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * Unpack signature @@ -27,21 +27,21 @@ class Json extends \Magento\Framework\ObjectManager\Definition\Compiled */ protected function _unpack($signature) { - return $this->getJson()->decode($signature); + return $this->getSerializer()->unserialize($signature); } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } 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 5c869304eb3c1..11dd0aeff1b63 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/ConfigTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\ObjectManager\Test\Unit\Config; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use \Magento\Framework\ObjectManager\Config\Config; class ConfigTest extends \PHPUnit_Framework_TestCase @@ -51,15 +51,15 @@ public function testExtendWithCacheMock() $cache->expects($this->once())->method('get')->will($this->returnValue(false)); $config = new Config(null, $definitions); - $jsonMock = $this->getMock(JsonInterface::class); - $jsonMock->method('encode') + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data, true); }); $this->objectManagerHelper->setBackwardCompatibleProperty( $config, - 'json', - $jsonMock + 'serializer', + $serializerMock ); $config->setCache($cache); diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php index 1dc4caee93ae9..4910eb0d2774a 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\ObjectManager\Test\Unit\Definition\Compiled; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; class JsonTest extends \PHPUnit_Framework_TestCase { @@ -25,10 +25,8 @@ protected function setUp() */ public function testGetParametersWithoutDefinition($signatures, $definitions, $expected) { - $signatures = []; - $definitions = ['wonderful' => null]; $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); - $this->assertEquals(null, $model->getParameters('wonderful')); + $this->assertEquals($expected, $model->getParameters('wonderful')); } public function getParametersDataProvider() @@ -54,16 +52,16 @@ public function testGetParametersWithUnpacking() $signatures = ['wonderfulClass' => json_encode($checkString)]; $definitions = ['wonderful' => 'wonderfulClass']; $object = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); - $jsonMock = $this->getMock(JsonInterface::class); - $jsonMock->expects($this->once()) - ->method('decode') + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->expects($this->once()) + ->method('unserialize') ->willReturnCallback(function ($data) { return json_decode($data, true); }); $this->objectManagerHelper->setBackwardCompatibleProperty( $object, - 'json', - $jsonMock + 'serializer', + $serializerMock ); $this->assertEquals($checkString, $object->getParameters('wonderful')); } diff --git a/lib/internal/Magento/Framework/Reflection/MethodsMap.php b/lib/internal/Magento/Framework/Reflection/MethodsMap.php index 9a815df385501..c7a9183a78ee5 100644 --- a/lib/internal/Magento/Framework/Reflection/MethodsMap.php +++ b/lib/internal/Magento/Framework/Reflection/MethodsMap.php @@ -6,7 +6,7 @@ namespace Magento\Framework\Reflection; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Zend\Code\Reflection\ClassReflection; use Zend\Code\Reflection\MethodReflection; use Zend\Code\Reflection\ParameterReflection; @@ -47,9 +47,9 @@ class MethodsMap private $fieldNamer; /** - * @var JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\Cache\FrontendInterface $cache @@ -101,11 +101,11 @@ public function getMethodsMap($interfaceName) if (!isset($this->serviceInterfaceMethodsMap[$key])) { $methodMap = $this->cache->load($key); if ($methodMap) { - $this->serviceInterfaceMethodsMap[$key] = $this->getJson()->decode($methodMap); + $this->serviceInterfaceMethodsMap[$key] = $this->getSerializer()->unserialize($methodMap); } else { $methodMap = $this->getMethodMapViaReflection($interfaceName); $this->serviceInterfaceMethodsMap[$key] = $methodMap; - $this->cache->save($this->getJson()->encode($this->serviceInterfaceMethodsMap[$key]), $key); + $this->cache->save($this->getSerializer()->serialize($this->serviceInterfaceMethodsMap[$key]), $key); } } return $this->serviceInterfaceMethodsMap[$key]; @@ -123,7 +123,7 @@ public function getMethodParams($serviceClassName, $serviceMethodName) $cacheId = self::SERVICE_METHOD_PARAMS_CACHE_PREFIX . hash('md5', $serviceClassName . $serviceMethodName); $params = $this->cache->load($cacheId); if ($params !== false) { - return $this->getJson()->decode($params); + return $this->getSerializer()->unserialize($params); } $serviceClass = new ClassReflection($serviceClassName); /** @var MethodReflection $serviceMethod */ @@ -139,7 +139,7 @@ public function getMethodParams($serviceClassName, $serviceMethodName) self::METHOD_META_DEFAULT_VALUE => $isDefaultValueAvailable ? $paramReflection->getDefaultValue() : null ]; } - $this->cache->save($this->getJson()->encode($params), $cacheId, [ReflectionCache::CACHE_TAG]); + $this->cache->save($this->getSerializer()->serialize($params), $cacheId, [ReflectionCache::CACHE_TAG]); return $params; } @@ -225,17 +225,17 @@ public function isMethodReturnValueRequired($type, $methodName) } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php index f49583b1d3377..4fa07f938facf 100644 --- a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php +++ b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php @@ -6,7 +6,7 @@ namespace Magento\Framework\Reflection\Test\Unit; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Reflection\MethodsMap; use Magento\Framework\Reflection\TypeProcessor; @@ -20,8 +20,8 @@ class MethodsMapTest extends \PHPUnit_Framework_TestCase */ private $object; - /** @var JsonInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; /** * Set up helper. @@ -51,11 +51,11 @@ protected function setUp() 'fieldNamer' => $fieldNamerMock, ] ); - $this->jsonMock = $this->getMock(JsonInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); $objectManager->setBackwardCompatibleProperty( $this->object, - 'json', - $this->jsonMock + 'serializer', + $this->serializerMock ); } @@ -86,8 +86,8 @@ public function testGetMethodReturnType() public function testGetMethodsMap() { - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data); }); diff --git a/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php b/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php index 8bbc7f433c9c4..a8199768a365f 100644 --- a/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php +++ b/lib/internal/Magento/Framework/Test/Unit/TranslateTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\Test\Unit; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use \Magento\Framework\Translate; /** @@ -107,19 +107,19 @@ protected function setUp() $this->packDictionary ); - $jsonMock = $this->getMock(JsonInterface::class); - $jsonMock->method('encode') + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data); }); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); $objectManager->setBackwardCompatibleProperty( $this->translate, - 'json', - $jsonMock + 'serializer', + $serializerMock ); } diff --git a/lib/internal/Magento/Framework/Translate.php b/lib/internal/Magento/Framework/Translate.php index 8e9b47487ff3c..c5571ab0b8edb 100644 --- a/lib/internal/Magento/Framework/Translate.php +++ b/lib/internal/Magento/Framework/Translate.php @@ -109,9 +109,9 @@ class Translate implements \Magento\Framework\TranslateInterface protected $packDictionary; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @param \Magento\Framework\View\DesignInterface $viewDesign @@ -479,7 +479,7 @@ protected function _loadCache() { $data = $this->_cache->load($this->getCacheId()); if ($data) { - $data = $this->getJson()->decode($data); + $data = $this->getSerializer()->unserialize($data); } return $data; } @@ -491,22 +491,22 @@ protected function _loadCache() */ protected function _saveCache() { - $this->_cache->save($this->getJson()->encode($this->getData()), $this->getCacheId(true), [], false); + $this->_cache->save($this->getSerializer()->serialize($this->getData()), $this->getCacheId(true), [], false); return $this; } /** - * Get json encoder/decoder + * Get serializer * - * @return \Magento\Framework\Json\JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Json\JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(Serialize\SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index 3ac6829f0dbe0..b3d7147f42029 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -48,9 +48,9 @@ class Factory private $cache; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * @var \Magento\Framework\Config\FileIteratorFactory @@ -83,9 +83,9 @@ protected function _initializeConfigList() $this->_configFiles = $this->cache->load(self::CACHE_KEY); if (!$this->_configFiles) { $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); - $this->cache->save($this->getJson()->encode($this->_configFiles->toArray()), self::CACHE_KEY); + $this->cache->save($this->getSerializer()->serialize($this->_configFiles->toArray()), self::CACHE_KEY); } else { - $filesArray = $this->getJson()->decode($this->_configFiles); + $filesArray = $this->getSerializer()->unserialize($this->_configFiles); $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray)); } } @@ -156,18 +156,18 @@ public function createValidator($entityName, $groupName, array $builderConfig = } /** - * Get json encoder/decoder + * Get serializer * - * @return \Magento\Framework\Json\JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Json\JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\SerializerInterface::class); } - return $this->json; + return $this->serializer; } /** diff --git a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php index 0c60f4bcdf9e9..4c4ef93bc15be 100644 --- a/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php +++ b/lib/internal/Magento/Framework/Validator/Test/Unit/FactoryTest.php @@ -31,9 +31,9 @@ class FactoryTest extends \PHPUnit_Framework_TestCase private $cacheMock; /** - * @var \Magento\Framework\Json\JsonInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $jsonMock; + private $serializerMock; /** * @var \Magento\Framework\Config\FileIteratorFactory|\PHPUnit_Framework_MockObject_MockObject @@ -116,7 +116,7 @@ protected function setUp() ] ); - $this->jsonMock = $this->getMock(\Magento\Framework\Json\JsonInterface::class); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); $this->fileIteratorFactoryMock = $this->getMock( \Magento\Framework\Config\FileIteratorFactory::class, [], @@ -126,8 +126,8 @@ protected function setUp() ); $objectManager->setBackwardCompatibleProperty( $this->factory, - 'json', - $this->jsonMock + 'serializer', + $this->serializerMock ); $objectManager->setBackwardCompatibleProperty( $this->factory, @@ -178,8 +178,8 @@ public function testGetValidatorConfigCacheNotExist() $this->cacheMock->expects($this->once()) ->method('save') ->with($this->jsonString); - $this->jsonMock->expects($this->once()) - ->method('encode') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->with($this->data) ->willReturn($this->jsonString); $this->factory->getValidatorConfig(); @@ -195,8 +195,8 @@ public function testGetValidatorConfigCacheExist() ->method('getConfigurationFiles'); $this->cacheMock->expects($this->never()) ->method('save'); - $this->jsonMock->expects($this->once()) - ->method('decode') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->with($this->jsonString) ->willReturn($this->data); $this->fileIteratorFactoryMock->method('create') diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php index 7c719f1f8bc51..3778839894adb 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Component/Definition.php @@ -40,9 +40,9 @@ class Definition protected $componentData; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * Constructor @@ -61,9 +61,9 @@ public function __construct( $cachedData = $this->cache->load(static::CACHE_ID); if ($cachedData === false) { $data = $uiReader->read(); - $this->cache->save($this->getJson()->encode($data), static::CACHE_ID); + $this->cache->save($this->getSerializer()->serialize($data), static::CACHE_ID); } else { - $data = $this->getJson()->decode($cachedData); + $data = $this->getSerializer()->unserialize($cachedData); } $this->prepareComponentData($data); } @@ -116,17 +116,17 @@ protected function prepareComponentData(array $componentsData) } /** - * Get json encoder/decoder + * Get serializer * - * @return \Magento\Framework\Json\JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Json\JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php index 5fc89aca1e060..f740a2e403bb8 100644 --- a/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php +++ b/lib/internal/Magento/Framework/View/Element/UiComponent/Config/Provider/Template.php @@ -56,9 +56,9 @@ class Template protected $cachedTemplates = []; /** - * @var \Magento\Framework\Json\JsonInterface + * @var \Magento\Framework\Serialize\SerializerInterface */ - private $json; + private $serializer; /** * Constructor @@ -83,7 +83,9 @@ public function __construct( $this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory; $cachedTemplates = $this->cache->load(static::CACHE_ID); - $this->cachedTemplates = $cachedTemplates === false ? [] : $this->getJson()->decode($cachedTemplates); + $this->cachedTemplates = $cachedTemplates === false ? [] : $this->getSerializer()->unserialize( + $cachedTemplates + ); } /** @@ -106,23 +108,23 @@ public function getTemplate($template) 'domMerger' => $this->domMerger ] )->getContent(); - $this->cache->save($this->getJson()->encode($this->cachedTemplates), static::CACHE_ID); + $this->cache->save($this->getSerializer()->serialize($this->cachedTemplates), static::CACHE_ID); return $this->cachedTemplates[$hash]; } /** - * Get json encoder/decoder + * Get serializer * - * @return \Magento\Framework\Json\JsonInterface + * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Json\JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\SerializerInterface::class); } - return $this->json; + return $this->serializer; } } diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php index d89213636a45d..4892c96a22058 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php @@ -8,7 +8,7 @@ namespace Magento\Framework\Webapi\Test\Unit; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Webapi\ServiceInputProcessor; use Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\WebapiBuilderFactory; use Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\AssociativeArray; @@ -98,19 +98,19 @@ function () use ($objectManager) { 'fieldNamer' => $this->fieldNamer ] ); - $jsonMock = $this->getMock(JsonInterface::class); - $jsonMock->method('encode') + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->method('serialize') ->willReturnCallback(function ($data) { return json_encode($data); }); - $jsonMock->method('decode') + $serializerMock->method('unserialize') ->willReturnCallback(function ($string) { return json_decode($string, true); }); $objectManager->setBackwardCompatibleProperty( $this->methodsMap, - 'json', - $jsonMock + 'serializer', + $serializerMock ); $this->serviceInputProcessor = $objectManager->getObject( 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 3a97bf5f2f2d5..d384303d5e31a 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,7 @@ namespace Magento\Setup\Module\Di\Compiler\Config\Writer; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Json\JsonInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Setup\Module\Di\Compiler\Config\WriterInterface; class Filesystem implements WriterInterface @@ -19,9 +19,9 @@ class Filesystem implements WriterInterface private $directoryList; /** - * @var JsonInterface + * @var SerializerInterface */ - private $json; + private $serializer; /** * Constructor @@ -47,7 +47,7 @@ public function write($key, array $config) file_put_contents( $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::FILE_EXTENSION, - $this->getJson()->encode($config) + $this->getSerializer()->serialize($config) ); } @@ -64,17 +64,17 @@ private function initialize() } /** - * Get json encoder/decoder + * Get serializer * - * @return JsonInterface + * @return SerializerInterface * @deprecated */ - private function getJson() + private function getSerializer() { - if ($this->json === null) { - $this->json = \Magento\Framework\App\ObjectManager::getInstance() - ->get(JsonInterface::class); + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); } - return $this->json; + return $this->serializer; } } From 8578eeca4c1daac899bc18f427488dd6a9728fc0 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 18 Oct 2016 15:30:16 -0500 Subject: [PATCH 058/144] MAGETWO-59444: Create serializer interface and json class in framework - Refactor all the use cases --- .../Magento/Framework/App/Test/Unit/Route/ConfigTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 77dd4479e3198..2d30c9ae48893 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -138,8 +138,8 @@ public function testGetRouteByFrontNameNoCache() ); $this->_cacheMock->expects($this->once()) - ->method('save'); -// ->with(json_encode($routes), 'scope::RoutesConfig'); + ->method('save') + ->with(json_encode($routes), 'scope::RoutesConfig'); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope')); From ae70dda245692f629b05bae98e461f4314ace001 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 19 Oct 2016 11:21:58 -0500 Subject: [PATCH 059/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - address code review comments --- .../ObjectManager/ConfigLoader/Compiled.php | 4 +- .../Framework/App/ObjectManagerFactory.php | 5 +- .../Unit/ObjectManager/ConfigCacheTest.php | 13 ++-- .../Unit/ObjectManager/ConfigLoaderTest.php | 16 ++-- .../Config/ConfigOptionsListConstants.php | 4 +- .../Interception/Definition/Compiled.php | 2 - .../Test/Unit/Config/ConfigTest.php | 20 +++-- .../Test/Unit/PluginList/PluginListTest.php | 14 +--- .../ObjectManager/Definition/Compiled.php | 34 ++++++++- .../Definition/Compiled/Json.php | 47 ------------ .../ObjectManager/DefinitionFactory.php | 23 +++--- .../ObjectManager/Relations/Compiled.php | 2 - .../Unit/Definition/Compiled/JsonTest.php | 68 ----------------- .../Test/Unit/Definition/CompiledStub.php | 25 ------- .../Test/Unit/Definition/CompiledTest.php | 73 +++++++++++++------ .../Test/Unit/DefinitionFactoryTest.php | 9 +-- .../Reflection/Test/Unit/MethodsMapTest.php | 5 +- .../Magento/Setup/Model/ConfigGenerator.php | 12 +-- .../Magento/Setup/Model/ConfigOptionsList.php | 7 -- .../Di/Compiler/Config/Writer/Filesystem.php | 3 +- .../Test/Unit/Model/ConfigOptionsListTest.php | 30 ++++---- .../Test/Unit/Module/ConfigGeneratorTest.php | 8 -- 22 files changed, 154 insertions(+), 270 deletions(-) delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index ac55963655b87..d1dc30ee7c9ef 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -11,8 +11,6 @@ class Compiled implements ConfigLoaderInterface { - const FILE_EXTENSION = '.json'; - /** * Global config * @@ -46,7 +44,7 @@ public function load($area) public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . $diPath . '/' . $area . self::FILE_EXTENSION; + return BP . $diPath . '/' . $area . '.json'; } /** diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index f91c389b493fb..bfe94a9dccc64 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -10,7 +10,6 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem\DriverPool; use Magento\Framework\Interception\ObjectManager\ConfigInterface; -use Magento\Framework\ObjectManager\Definition\Compiled\Json; use Magento\Framework\App\ObjectManager\Environment; use Magento\Framework\Config\File\ConfigFilePool; use Magento\Framework\Code\GeneratedFiles; @@ -23,6 +22,7 @@ class ObjectManagerFactory { /** * Path to definitions format in deployment configuration + * @deprecated */ const CONFIG_PATH_DEFINITION_FORMAT = 'definition/format'; @@ -118,8 +118,7 @@ public function create(array $arguments) $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory( $this->driverPool->getDriver(DriverPool::FILE), $this->directoryList->getPath(DirectoryList::DI), - $this->directoryList->getPath(DirectoryList::GENERATION), - $deploymentConfig->get(self::CONFIG_PATH_DEFINITION_FORMAT, Json::MODE_NAME) + $this->directoryList->getPath(DirectoryList::GENERATION) ); $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions')); 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 315bfeeb51f79..21dce94bb6c73 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigCacheTest.php @@ -63,9 +63,8 @@ public function testGet($loadData, $expectedResult) ); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->with($loadData) + ->willReturn($expectedResult); $this->assertEquals($expectedResult, $this->configCache->get($key)); } @@ -73,7 +72,7 @@ public function getDataProvider() { return [ [false, false], - [json_encode(['some data']), ['some data']], + ['["some data"]', ['some data']], ]; } @@ -83,10 +82,8 @@ public function testSave() $config = ['config']; $this->serializerMock->expects($this->once()) ->method('serialize') - ->willReturnCallback(function ($data) { - return json_encode($data); - }); - $this->cacheFrontendMock->expects($this->once())->method('save')->with(json_encode($config), 'diConfig' . $key); + ->willReturn('["config"]'); + $this->cacheFrontendMock->expects($this->once())->method('save')->with('["config"]', '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 b5e7c46913949..bb048218a768a 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ObjectManager/ConfigLoaderTest.php @@ -88,6 +88,7 @@ protected function setUp() public function testLoadNotCached($area) { $configData = ['some' => 'config', 'data' => 'value']; + $serializedConfigData = '{"some":"config","data":"value"}'; $this->cacheMock->expects($this->once()) ->method('load') @@ -96,14 +97,13 @@ public function testLoadNotCached($area) $this->cacheMock->expects($this->once()) ->method('save') - ->with(json_encode($configData)); + ->with($serializedConfigData); $this->readerMock->expects($this->once())->method('read')->with($area)->will($this->returnValue($configData)); $this->serializerMock->expects($this->once()) ->method('serialize') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); + ->willReturn($serializedConfigData); + $this->serializerMock->expects($this->never())->method('unserialize'); $this->assertEquals($configData, $this->object->load($area)); @@ -126,18 +126,18 @@ public function loadDataProvider() public function testLoadCached() { $configData = ['some' => 'config', 'data' => 'value']; + $serializedConfigData = '{"some":"config","data":"value"}'; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn(json_encode($configData)); + ->willReturn($serializedConfigData); $this->cacheMock->expects($this->never()) ->method('save'); $this->readerMock->expects($this->never())->method('read'); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->with($serializedConfigData) + ->willReturn($configData); $this->serializerMock->expects($this->never())->method('serialize'); $this->assertEquals($configData, $this->object->load('testArea')); } diff --git a/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php b/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php index b6363520dc085..304174ac52d37 100644 --- a/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php +++ b/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php @@ -37,7 +37,6 @@ class ConfigOptionsListConstants */ const INPUT_KEY_ENCRYPTION_KEY = 'key'; const INPUT_KEY_SESSION_SAVE = 'session-save'; - const INPUT_KEY_DEFINITION_FORMAT = 'definition-format'; const INPUT_KEY_DB_HOST = 'db-host'; const INPUT_KEY_DB_NAME = 'db-name'; const INPUT_KEY_DB_USER = 'db-user'; @@ -51,6 +50,9 @@ class ConfigOptionsListConstants const INPUT_KEY_CACHE_HOSTS = 'http-cache-hosts'; /**#@-*/ + /** @deprecated */ + const INPUT_KEY_DEFINITION_FORMAT = 'definition-format'; + /**#@+ * Values for session-save */ diff --git a/lib/internal/Magento/Framework/Interception/Definition/Compiled.php b/lib/internal/Magento/Framework/Interception/Definition/Compiled.php index 959d1da1026ee..6fbe9c99dce86 100644 --- a/lib/internal/Magento/Framework/Interception/Definition/Compiled.php +++ b/lib/internal/Magento/Framework/Interception/Definition/Compiled.php @@ -11,8 +11,6 @@ class Compiled implements DefinitionInterface { - const FILE_NAME = 'plugins.json'; - /** * List of plugin definitions * 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 d51db8bc36469..db0218d00afc1 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -148,10 +148,8 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type, $entit $this->relationsMock->expects($this->any())->method('getParents')->will($this->returnValue($entityParents)); $this->serializerMock->expects($this->once()) - ->method('serialize') - ->willReturnCallback(function ($data) { - return json_encode($data); - }); + ->method('serialize'); + $this->serializerMock->expects($this->never())->method('unserialize'); $model = $this->objectManagerHelper->getObject( @@ -188,17 +186,23 @@ public function testHasPluginsWhenDataIsCached($expectedResult, $type) ]; $this->readerMock->expects($this->never())->method('read'); $this->cacheMock->expects($this->never())->method('save'); + $serializedValue = '{"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemContainer":true,' + . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\Item":true,' + . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\Item\\Enhanced":true,' + . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemContainer\\Enhanced":true,' + . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemContainer\\Proxy":true,' + . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemProxy":false,' + . '"virtual_custom_item":true}'; $this->cacheMock->expects($this->any()) ->method('load') ->with($cacheId) - ->will($this->returnValue(json_encode($interceptionData))); + ->will($this->returnValue($serializedValue)); $this->serializerMock->expects($this->never())->method('serialize'); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->with($serializedValue) + ->willReturn($interceptionData); $model = $this->objectManagerHelper->getObject( \Magento\Framework\Interception\Config\Config::class, 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 e162cd37c789b..dd0d2f68ac6ad 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -238,12 +238,7 @@ public function testLoadScopedDataNotCached() ->method('getCurrentScope') ->will($this->returnValue('scope')); $this->serializerMock->expects($this->once()) - ->method('serialize') - ->willReturnCallback( - function ($data) { - return json_encode($data); - } - ); + ->method('serialize'); $this->serializerMock->expects($this->never()) ->method('unserialize'); $this->cacheMock->expects($this->once()) @@ -279,18 +274,17 @@ public function testLoadScopedDataCached() ->will($this->returnValue('scope')); $data = [['key'], ['key'], ['key']]; + $serializedData = '[["key"],["key"],["key"]]'; $this->serializerMock->expects($this->never()) ->method('serialize'); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->willReturn($data); $this->cacheMock->expects($this->once()) ->method('load') ->with('global|scope|interception') - ->will($this->returnValue(json_encode($data))); + ->willReturn($serializedData); $this->assertEquals(null, $this->object->getNext('Type', 'method')); } diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php index 2cb3b21211f23..db91618dccd23 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php @@ -5,10 +5,12 @@ */ namespace Magento\Framework\ObjectManager\Definition; +use Magento\Framework\Serialize\SerializerInterface; + /** * Compiled class definitions. Should be used for maximum performance in production. */ -abstract class Compiled implements \Magento\Framework\ObjectManager\DefinitionInterface +class Compiled implements \Magento\Framework\ObjectManager\DefinitionInterface { /** * Class definitions @@ -22,6 +24,16 @@ abstract class Compiled implements \Magento\Framework\ObjectManager\DefinitionIn */ protected $reader ; + /** + * @var SerializerInterface + */ + private $serializer; + + /** + * @var array + */ + private $_signatures; + /** * @param array $definitions * @param \Magento\Framework\Code\Reader\ClassReaderInterface $reader @@ -38,7 +50,10 @@ public function __construct(array $definitions, \Magento\Framework\Code\Reader\C * @param string $signature * @return mixed */ - abstract protected function _unpack($signature); + protected function _unpack($signature) + { + return $this->getSerializer()->unserialize($signature); + } /** * Get list of method parameters @@ -82,4 +97,19 @@ public function getClasses() { return array_keys($this->_definitions); } + + /** + * 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; + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php deleted file mode 100644 index f68e5203a23fe..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled/Json.php +++ /dev/null @@ -1,47 +0,0 @@ -getSerializer()->unserialize($signature); - } - - /** - * 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; - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index ef906cb945a42..4e9026eb7b20d 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -13,7 +13,6 @@ use Magento\Framework\Filesystem\DriverInterface; use Magento\Framework\Interception\Code\Generator as InterceptionGenerator; -use Magento\Framework\ObjectManager\Definition\Compiled\Json; use Magento\Framework\ObjectManager\Definition\Runtime; use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator; @@ -40,6 +39,7 @@ class DefinitionFactory * Format of definitions * * @var string + * @deprecated */ protected $_definitionFormat; @@ -55,9 +55,7 @@ class DefinitionFactory * * @var array */ - protected static $definitionClasses = [ - Json::MODE_NAME => \Magento\Framework\ObjectManager\Definition\Compiled\Json::class, - ]; + protected static $definitionClasses = \Magento\Framework\ObjectManager\Definition\Compiled::class; /** * @var \Magento\Framework\Code\Generator @@ -70,8 +68,12 @@ class DefinitionFactory * @param string $generationDir * @param string $definitionFormat */ - public function __construct(DriverInterface $filesystemDriver, $definitionDir, $generationDir, $definitionFormat) - { + public function __construct( + DriverInterface $filesystemDriver, + $definitionDir, + $generationDir, + $definitionFormat = null + ) { $this->_filesystemDriver = $filesystemDriver; $this->_definitionDir = $definitionDir; $this->_generationDir = $generationDir; @@ -90,7 +92,7 @@ public function createClassDefinition($definitions = false) if (is_string($definitions)) { $definitions = $this->_unpack($definitions); } - $definitionModel = self::$definitionClasses[$this->_definitionFormat]; + $definitionModel = self::$definitionClasses; $result = new $definitionModel($definitions); } else { $autoloader = new \Magento\Framework\Code\Generator\Autoloader($this->getCodeGenerator()); @@ -108,7 +110,7 @@ public function createClassDefinition($definitions = false) */ public function createPluginDefinition() { - $path = $this->_definitionDir . '/' . \Magento\Framework\Interception\Definition\Compiled::FILE_NAME; + $path = $this->_definitionDir . '/plugins.json'; if ($this->_filesystemDriver->isReadable($path)) { return new \Magento\Framework\Interception\Definition\Compiled( $this->_unpack($this->_filesystemDriver->fileGetContents($path)) @@ -125,7 +127,7 @@ public function createPluginDefinition() */ public function createRelations() { - $path = $this->_definitionDir . '/' . \Magento\Framework\ObjectManager\Relations\Compiled::FILE_NAME; + $path = $this->_definitionDir . '/relations.json'; if ($this->_filesystemDriver->isReadable($path)) { return new \Magento\Framework\ObjectManager\Relations\Compiled( $this->_unpack($this->_filesystemDriver->fileGetContents($path)) @@ -139,10 +141,11 @@ public function createRelations() * Gets supported definition formats * * @return array + * @deprecated */ public static function getSupportedFormats() { - return array_keys(self::$definitionClasses); + return []; } /** diff --git a/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php index 56163410f351d..71455dbf9acd4 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php @@ -9,8 +9,6 @@ class Compiled implements \Magento\Framework\ObjectManager\RelationsInterface { - const FILE_NAME = 'relations.json'; - /** * List of class relations * diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php deleted file mode 100644 index 4910eb0d2774a..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/Compiled/JsonTest.php +++ /dev/null @@ -1,68 +0,0 @@ -objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - } - - /** - * @param array $signatures - * @param array $definitions - * @param mixed $expected - * @dataProvider getParametersDataProvider - */ - public function testGetParametersWithoutDefinition($signatures, $definitions, $expected) - { - $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); - $this->assertEquals($expected, $model->getParameters('wonderful')); - } - - public function getParametersDataProvider() - { - $wonderfulSignature = new \stdClass(); - return [ - [ - [], - ['wonderful' => null], - null, - ], - [ - ['wonderfulClass' => $wonderfulSignature], - ['wonderful' => 'wonderfulClass'], - $wonderfulSignature, - ] - ]; - } - - public function testGetParametersWithUnpacking() - { - $checkString = 'code to pack'; - $signatures = ['wonderfulClass' => json_encode($checkString)]; - $definitions = ['wonderful' => 'wonderfulClass']; - $object = new \Magento\Framework\ObjectManager\Definition\Compiled\Json([$signatures, $definitions]); - $serializerMock = $this->getMock(SerializerInterface::class); - $serializerMock->expects($this->once()) - ->method('unserialize') - ->willReturnCallback(function ($data) { - return json_decode($data, true); - }); - $this->objectManagerHelper->setBackwardCompatibleProperty( - $object, - 'serializer', - $serializerMock - ); - $this->assertEquals($checkString, $object->getParameters('wonderful')); - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php deleted file mode 100644 index bb5826a7b17ec..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledStub.php +++ /dev/null @@ -1,25 +0,0 @@ -getMock( - \Magento\Framework\Code\Reader\ClassReader::class, - ['getConstructor'], - [], - '', - false - ); - $readerMock->expects($this->once()) - ->method('getConstructor') - ->with($className) - ->willReturn($undefinedDefinitionSignature); - $model = $objectManager->getObject( - \Magento\Framework\ObjectManager\Test\Unit\Definition\CompiledStub::class, + $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + } + + /** + * @param array $signatures + * @param array $definitions + * @param mixed $expected + * @dataProvider getParametersDataProvider + */ + public function testGetParametersWithoutDefinition($signatures, $definitions, $expected) + { + $model = new \Magento\Framework\ObjectManager\Definition\Compiled([$signatures, $definitions]); + $this->assertEquals($expected, $model->getParameters('wonderful')); + } + + public function getParametersDataProvider() + { + $wonderfulSignature = new \stdClass(); + return [ + [ + [], + ['wonderful' => null], + null, + ], [ - 'definitions' => [[], []], - 'reader' => $readerMock + ['wonderfulClass' => $wonderfulSignature], + ['wonderful' => 'wonderfulClass'], + $wonderfulSignature, ] + ]; + } + + public function testGetParametersWithUnpacking() + { + $checkString = 'code to pack'; + $signatures = ['wonderfulClass' => json_encode($checkString)]; + $definitions = ['wonderful' => 'wonderfulClass']; + $object = new \Magento\Framework\ObjectManager\Definition\Compiled([$signatures, $definitions]); + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->expects($this->once()) + ->method('unserialize') + ->willReturnCallback(function ($data) { + return json_decode($data, true); + }); + $this->objectManagerHelper->setBackwardCompatibleProperty( + $object, + 'serializer', + $serializerMock ); - $this->assertEquals($undefinedDefinitionSignature, $model->getParameters($className)); + $this->assertEquals($checkString, $object->getParameters('wonderful')); } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index 3051a671a4e77..3861d274b5243 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -6,7 +6,7 @@ namespace Magento\Framework\ObjectManager\Test\Unit; -use Magento\Framework\ObjectManager\Definition\Compiled\Json; +use Magento\Framework\ObjectManager\Definition\Compiled; class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { @@ -27,7 +27,7 @@ class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->sampleContent = json_encode([1, 2, 3]); + $this->sampleContent = '[1,2,3]'; $this->filesystemDriverMock = $this->getMock( \Magento\Framework\Filesystem\Driver\File::class, [], @@ -38,15 +38,14 @@ protected function setUp() $this->model = new \Magento\Framework\ObjectManager\DefinitionFactory( $this->filesystemDriverMock, 'DefinitionDir', - 'GenerationDir', - Json::MODE_NAME + 'GenerationDir' ); } public function testCreateClassDefinitionFromString() { $this->assertInstanceOf( - \Magento\Framework\ObjectManager\Definition\Compiled\Json::class, + \Magento\Framework\ObjectManager\Definition\Compiled::class, $this->model->createClassDefinition($this->sampleContent) ); } diff --git a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php index 4fa07f938facf..35af774c7ba45 100644 --- a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php +++ b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php @@ -87,10 +87,7 @@ public function testGetMethodReturnType() public function testGetMethodsMap() { $this->serializerMock->expects($this->once()) - ->method('serialize') - ->willReturnCallback(function ($data) { - return json_encode($data); - }); + ->method('serialize'); $methodsMap = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); $this->assertEquals( [ diff --git a/setup/src/Magento/Setup/Model/ConfigGenerator.php b/setup/src/Magento/Setup/Model/ConfigGenerator.php index 140f5a739889c..27b3999985743 100644 --- a/setup/src/Magento/Setup/Model/ConfigGenerator.php +++ b/setup/src/Magento/Setup/Model/ConfigGenerator.php @@ -116,19 +116,11 @@ public function createSessionConfig(array $data) * * @param array $data * @return ConfigData + * @deprecated */ public function createDefinitionsConfig(array $data) { - $configData = new ConfigData(ConfigFilePool::APP_ENV); - - if (!empty($data[ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT])) { - $configData->set( - ObjectManagerFactory::CONFIG_PATH_DEFINITION_FORMAT, - $data[ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT] - ); - } - - return $configData; + return null; } /** diff --git a/setup/src/Magento/Setup/Model/ConfigOptionsList.php b/setup/src/Magento/Setup/Model/ConfigOptionsList.php index 4e8374aabb4e6..0c1419a73cb8e 100644 --- a/setup/src/Magento/Setup/Model/ConfigOptionsList.php +++ b/setup/src/Magento/Setup/Model/ConfigOptionsList.php @@ -72,13 +72,6 @@ public function getOptions() 'Session save handler', ConfigOptionsListConstants::SESSION_SAVE_FILES ), - new SelectConfigOption( - ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT, - SelectConfigOption::FRONTEND_WIZARD_SELECT, - DefinitionFactory::getSupportedFormats(), - ObjectManagerFactory::CONFIG_PATH_DEFINITION_FORMAT, - 'Type of definitions used by Object Manager' - ), new TextConfigOption( ConfigOptionsListConstants::INPUT_KEY_DB_HOST, TextConfigOption::FRONTEND_WIZARD_TEXT, 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 d384303d5e31a..add2b0adc8a11 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 @@ -45,8 +45,7 @@ public function write($key, array $config) $this->initialize(); file_put_contents( - $this->directoryList->getPath(DirectoryList::DI) . '/' . $key - . \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::FILE_EXTENSION, + \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($key), $this->getSerializer()->serialize($config) ); } diff --git a/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsListTest.php b/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsListTest.php index 1e37d351d8f03..eaab4d28d7620 100644 --- a/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsListTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsListTest.php @@ -48,32 +48,30 @@ public function testGetOptions() $this->assertSame('Encryption key', $options[0]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\SelectConfigOption::class, $options[1]); $this->assertSame('Session save handler', $options[1]->getDescription()); - $this->assertInstanceOf(\Magento\Framework\Setup\Option\SelectConfigOption::class, $options[2]); - $this->assertSame('Type of definitions used by Object Manager', $options[2]->getDescription()); + $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[2]); + $this->assertSame('Database server host', $options[2]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[3]); - $this->assertSame('Database server host', $options[3]->getDescription()); + $this->assertSame('Database name', $options[3]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[4]); - $this->assertSame('Database name', $options[4]->getDescription()); + $this->assertSame('Database server username', $options[4]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[5]); - $this->assertSame('Database server username', $options[5]->getDescription()); + $this->assertSame('Database server engine', $options[5]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[6]); - $this->assertSame('Database server engine', $options[6]->getDescription()); + $this->assertSame('Database server password', $options[6]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[7]); - $this->assertSame('Database server password', $options[7]->getDescription()); + $this->assertSame('Database table prefix', $options[7]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[8]); - $this->assertSame('Database table prefix', $options[8]->getDescription()); + $this->assertSame('Database type', $options[8]->getDescription()); $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[9]); - $this->assertSame('Database type', $options[9]->getDescription()); - $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[10]); - $this->assertSame('Database initial set of commands', $options[10]->getDescription()); - $this->assertInstanceOf(\Magento\Framework\Setup\Option\FlagConfigOption::class, $options[11]); + $this->assertSame('Database initial set of commands', $options[9]->getDescription()); + $this->assertInstanceOf(\Magento\Framework\Setup\Option\FlagConfigOption::class, $options[10]); $this->assertSame( 'If specified, then db connection validation will be skipped', - $options[11]->getDescription() + $options[10]->getDescription() ); - $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[12]); - $this->assertSame('http Cache hosts', $options[12]->getDescription()); - $this->assertEquals(13, count($options)); + $this->assertInstanceOf(\Magento\Framework\Setup\Option\TextConfigOption::class, $options[11]); + $this->assertSame('http Cache hosts', $options[11]->getDescription()); + $this->assertEquals(12, count($options)); } public function testCreateOptions() diff --git a/setup/src/Magento/Setup/Test/Unit/Module/ConfigGeneratorTest.php b/setup/src/Magento/Setup/Test/Unit/Module/ConfigGeneratorTest.php index caca5509d1e14..238bf7ea3db9f 100644 --- a/setup/src/Magento/Setup/Test/Unit/Module/ConfigGeneratorTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Module/ConfigGeneratorTest.php @@ -66,14 +66,6 @@ public function testCreateSessionConfigWithoutInput() $this->assertEquals([], $returnValue->getData()); } - public function testCreateDefinitionsConfig() - { - $testData = [ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT => 'test-format']; - $returnValue = $this->configGeneratorObject->createDefinitionsConfig($testData); - $this->assertEquals(ConfigFilePool::APP_ENV, $returnValue->getFileKey()); - $this->assertEquals(['definition' => ['format' => 'test-format']], $returnValue->getData()); - } - public function testCreateDbConfig() { $testData = [ From fa7ffcad545986f173f2a39f8763faf63d1b8c2d Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 19 Oct 2016 11:57:08 -0500 Subject: [PATCH 060/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - fix build failures --- .../Review/Product/CollectionTest.php | 36 +++++++++++++------ .../Magento/Setup/Model/ConfigGenerator.php | 1 + .../Di/Compiler/Config/Writer/Filesystem.php | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php index b1a1b9a69c7ff..4a61406e8a139 100644 --- a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php +++ b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php @@ -10,6 +10,11 @@ */ class CollectionTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Review\Model\ResourceModel\Review\Product\Collection */ @@ -74,16 +79,27 @@ protected function setUp() false ); $fetchStrategy->expects($this->any())->method('fetchAll')->will($this->returnValue([])); - $this->model = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this)) - ->getObject( - \Magento\Review\Model\ResourceModel\Review\Product\Collection::class, - [ - 'universalFactory' => $universalFactory, - 'storeManager' => $storeManager, - 'eavConfig' => $eavConfig, - 'fetchStrategy' => $fetchStrategy - ] - ); + $productLimitationMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class + ); + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock + ]); + $this->model = $this->objectManager->getObject( + \Magento\Review\Model\ResourceModel\Review\Product\Collection::class, + [ + 'universalFactory' => $universalFactory, + 'storeManager' => $storeManager, + 'eavConfig' => $eavConfig, + 'fetchStrategy' => $fetchStrategy + ] + ); + } + + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); } /** diff --git a/setup/src/Magento/Setup/Model/ConfigGenerator.php b/setup/src/Magento/Setup/Model/ConfigGenerator.php index 27b3999985743..ce86ce4dd471f 100644 --- a/setup/src/Magento/Setup/Model/ConfigGenerator.php +++ b/setup/src/Magento/Setup/Model/ConfigGenerator.php @@ -117,6 +117,7 @@ public function createSessionConfig(array $data) * @param array $data * @return ConfigData * @deprecated + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function createDefinitionsConfig(array $data) { 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 add2b0adc8a11..3207e590adf97 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 @@ -45,7 +45,7 @@ public function write($key, array $config) $this->initialize(); file_put_contents( - \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($key), + $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.json', $this->getSerializer()->serialize($config) ); } From dddb34e52841860dc6b06ca36e4348d565a68258 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 19 Oct 2016 14:07:29 -0500 Subject: [PATCH 061/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../Test/Unit/Serializer/JsonTest.php | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php index 38fa7d2a66f7f..59e9eeed19027 100644 --- a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\Serialize\Test\Unit\Serializer; +use Magento\Framework\DataObject; use Magento\Framework\Serialize\Serializer\Json; class JsonTest extends \PHPUnit_Framework_TestCase @@ -22,24 +23,54 @@ protected function setUp() /** * @param null|bool|array $value - * @dataProvider serializeUnserializeDataProvider + * @dataProvider serializeDataProvider */ - public function testSerializeUnserialize($value) + public function testSerialize($value, $expected) { $this->assertEquals( - $this->json->unserialize($this->json->serialize($value)), - $value + $expected, + $this->json->serialize($value) ); } - public function serializeUnserializeDataProvider() + public function serializeDataProvider() { + $dataObject = new DataObject(['something']); return [ - [''], - ['string'], - [null], - [false], - [['a' => 'b']], + ['', '""'], + ['string', '"string"'], + [null, 'null'], + [false, 'false'], + [['a' => 'b', 'd' => 123], '{"a":"b","d":123}'], + [123, '123'], + [10.56, '10.56'], + [$dataObject, '{}'], + ]; + } + + /** + * @param null|bool|array $value + * @dataProvider unserializeDataProvider + */ + public function testUnserialize($value, $expected) + { + $this->assertEquals( + $expected, + $this->json->unserialize($value) + ); + } + + public function unserializeDataProvider() + { + return [ + ['""', ''], + ['"string"', 'string'], + ['null', null], + ['false', false], + ['{"a":"b","d":123}', ['a' => 'b', 'd' => 123]], + ['123', 123], + ['10.56', 10.56], + ['{}', []], ]; } } From e576927775af3e37923fc0519ebaf31b6ec6564f Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 20 Oct 2016 11:29:57 -0500 Subject: [PATCH 062/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Magento/Backend/Model/Session/Quote.php | 7 -- .../Test/Unit/Model/Session/QuoteTest.php | 21 ---- .../ResourceModel/Product/Collection.php | 20 +++- .../Unit/Model/ProductTypes/ConfigTest.php | 56 +++++++++-- .../ResourceModel/Product/CollectionTest.php | 54 +++++++++-- .../Product/Link/Product/CollectionTest.php | 34 ++++++- .../Product/Option/CollectionTest.php | 31 +++++- .../CatalogRule/Test/Unit/Model/RuleTest.php | 25 ++++- .../ResourceModel/Advanced/CollectionTest.php | 39 +++++++- .../ResourceModel/Fulltext/CollectionTest.php | 35 ++++++- .../Test/Unit/Model/RuleTest.php | 25 ++++- .../ShippingInformationManagementTest.php | 26 ++--- .../Cron/Test/Unit/Model/Config/DataTest.php | 31 ++++-- .../Test/Unit/Model/Address/ConfigTest.php | 31 ++++-- .../Country/Postcode/Config/DataTest.php | 33 +++++-- .../Import/Product/Type/DownloadableTest.php | 25 ++++- .../Model/Entity/Attribute/ConfigTest.php | 31 ++++-- .../Test/Unit/Model/Export/ConfigTest.php | 95 +++++++++++-------- .../Test/Unit/Model/Import/ConfigTest.php | 81 +++++++++------- .../Test/Unit/Model/Config/DataTest.php | 31 ++++-- .../Unit/Model/Quote/Item/RepositoryTest.php | 13 +-- .../Model/ShippingAddressManagementTest.php | 14 +-- .../Review/Product/CollectionTest.php | 36 ++++++- .../Sales/Test/Unit/Model/Config/DataTest.php | 31 ++++-- .../Unit/Model/ResourceModel/RuleTest.php | 25 ++++- .../SalesRule/Test/Unit/Model/RuleTest.php | 25 ++++- .../App/Test/Unit/Config/InitialTest.php | 45 +++++---- .../App/Test/Unit/Router/ActionListTest.php | 25 ++++- .../Config/Test/Unit/Data/ScopedTest.php | 27 +++--- .../Framework/Config/Test/Unit/DataTest.php | 33 +++++-- .../Test/Unit/Config/ConfigTest.php | 25 ++++- .../Mview/Test/Unit/Config/DataTest.php | 33 +++++-- .../Unit/Helper/ObjectManager.php | 38 -------- 33 files changed, 781 insertions(+), 320 deletions(-) diff --git a/app/code/Magento/Backend/Model/Session/Quote.php b/app/code/Magento/Backend/Model/Session/Quote.php index 12a4d4d138f53..6ca269488294a 100644 --- a/app/code/Magento/Backend/Model/Session/Quote.php +++ b/app/code/Magento/Backend/Model/Session/Quote.php @@ -7,8 +7,6 @@ use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Api\GroupManagementInterface; -use Magento\Framework\App\ObjectManager; -use Magento\Quote\Api\CartManagementInterface; /** * Adminhtml quote session @@ -81,11 +79,6 @@ class Quote extends \Magento\Framework\Session\SessionManager */ protected $quoteFactory; - /** - * @var \Magento\Quote\Api\CartManagementInterface; - */ - private $cartManagement; - /** * @param \Magento\Framework\App\Request\Http $request * @param \Magento\Framework\Session\SidResolverInterface $sidResolver diff --git a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php index f6ddb41abf583..99d7fdfab21de 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php @@ -97,11 +97,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase */ protected $quoteFactoryMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $cartManagementMock; - /** * Set up * @@ -203,13 +198,6 @@ protected function setUp() ); $this->quoteFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteFactory::class, ['create'], [], '', false); - $this->cartManagementMock = $this->getMock( - \Magento\Quote\Api\CartManagementInterface::class, - [], - [], - '', - false - ); $this->quote = $this->getMock( \Magento\Backend\Model\Session\Quote::class, @@ -232,15 +220,6 @@ protected function setUp() 'quoteFactory' => $this->quoteFactoryMock ] ); - - $this->objectManager->mockObjectManager([ - \Magento\Quote\Api\CartManagementInterface::class => $this->cartManagementMock - ]); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index c24460981afbf..e4af92baf8571 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -260,6 +260,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac */ private $metadataPool; + /** + * @var \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory + */ + private $productLimitationFiltersFactory; + /** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Psr\Log\LoggerInterface $logger @@ -316,7 +321,7 @@ public function __construct( $this->_resourceHelper = $resourceHelper; $this->dateTime = $dateTime; $this->_groupManagement = $groupManagement; - $this->_productLimitationFilters = $this->createLimitationFilters(); + $this->_productLimitationFilters = $this->getProductLimitationFactory()->create(); parent::__construct( $entityFactory, $logger, @@ -2336,11 +2341,16 @@ public function getPricesCount() } /** - * @return Collection\ProductLimitation + * @deprecated + * @return \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory */ - private function createLimitationFilters() + private function getProductLimitationFactory() { - return \Magento\Framework\App\ObjectManager::getInstance() - ->create(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class); + if (null === $this->productLimitationFiltersFactory) { + $this->productLimitationFiltersFactory = ObjectManager::getInstance()->get( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory::class + ); + } + return $this->productLimitationFiltersFactory; } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 44ef53a34d0ef..1842b8470c43a 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -28,7 +28,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase private $serializerMock; /** - * @var \Magento\Catalog\Model\ProductTypes\Config|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\ProductTypes\Config */ private $config; @@ -44,14 +44,16 @@ protected function setUp() ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } /** @@ -67,7 +69,15 @@ public function testGetType($value, $expected) $this->serializerMock->method('unserialize') ->willReturn($value); - $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + + $this->config = $this->objectManager->getObject( + \Magento\Catalog\Model\ProductTypes\Config::class, + [ + 'reader' => $this->readerMock, + 'cache' => $this->cacheMock, + 'cacheId' => 'cache_id', + ] + ); $this->assertEquals($expected, $this->config->getType('global')); } @@ -87,7 +97,15 @@ public function testGetAll() ->willReturn(json_encode('"types":["Expected Data"]]')); $this->serializerMock->method('unserialize') ->willReturn(['types' => $expected]); - $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + + $this->config = $this->objectManager->getObject( + \Magento\Catalog\Model\ProductTypes\Config::class, + [ + 'reader' => $this->readerMock, + 'cache' => $this->cacheMock, + 'cacheId' => 'cache_id', + ] + ); $this->assertEquals($expected, $this->config->getAll()); } @@ -98,8 +116,34 @@ public function testIsProductSet() ->willReturn(''); $this->serializerMock->method('unserialize') ->willReturn([]); - $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + $this->config = $this->objectManager->getObject( + \Magento\Catalog\Model\ProductTypes\Config::class, + [ + 'reader' => $this->readerMock, + 'cache' => $this->cacheMock, + 'cacheId' => 'cache_id', + ] + ); $this->assertEquals(false, $this->config->isProductSet('typeId')); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php index 5f78d3dba0f60..b2ebc56e09397 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -150,12 +152,14 @@ protected function setUp() $productLimitationMock = $this->getMock( \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class ); - $this->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock, - \Magento\Catalog\Model\ResourceModel\Product\Gallery::class => $this->galleryResourceMock, - \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, - \Magento\Catalog\Model\Product\Gallery\ReadHandler::class => $this->galleryReadHandlerMock - ]); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( + [ + ProductLimitationFactory::class => $productLimitationFactoryMock, + ] + ); $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Collection::class, [ @@ -182,11 +186,28 @@ protected function setUp() ] ); $this->collection->setConnection($this->connectionMock); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'mediaGalleryResource', + $this->galleryResourceMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'metadataPool', + $this->metadataPoolMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'productGalleryReadHandler', + $this->galleryReadHandlerMock + ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testAddProductCategoriesFilter() @@ -255,4 +276,23 @@ public function testAddMediaGalleryData() $this->assertSame($this->collection, $this->collection->addMediaGalleryData()); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php index 11eb542773d59..93e5224387c5a 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php @@ -5,6 +5,9 @@ */ namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Link\Product; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) @@ -131,10 +134,10 @@ function ($store) { $this->timezoneInterfaceMock = $this->getMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class); $this->sessionMock = $this->getMock(\Magento\Customer\Model\Session::class, [], [], '', false); $this->dateTimeMock = $this->getMock(\Magento\Framework\Stdlib\DateTime::class); - $this->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ]); + $productLimitationFactory = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactory->method('create') + ->willReturn($this->getMock(ProductLimitation::class)); + $this->mockObjectManager([ProductLimitationFactory::class => $productLimitationFactory]); $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class, @@ -163,7 +166,9 @@ function ($store) { protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testSetProduct() @@ -176,4 +181,23 @@ public function testSetProduct() $this->collection->setProduct($product); $this->assertEquals(33, $this->collection->getStoreId()); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php index 0aa45ddb7fb4a..33da480ba1a80 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php @@ -150,9 +150,8 @@ protected function setUp() $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->selectMock->expects($this->exactly(2))->method('join'); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, - \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class => $this->joinProcessor ]); $this->collection = new Collection( @@ -165,15 +164,41 @@ protected function setUp() null, $this->resourceMock ); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'joinProcessor', + $this->joinProcessor + ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testReset() { $this->collection->reset(); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index f9a8ead3abff9..22252d218f50c 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -124,7 +124,7 @@ protected function setUp() false ); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\Api\ExtensionAttributesFactory::class => $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), \Magento\Framework\Api\AttributeValueFactory::class => @@ -145,7 +145,9 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } /** @@ -370,4 +372,23 @@ public function testGetConditionsFieldSetId() $expectedResult = 'form_namerule_conditions_fieldset_100'; $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName)); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php index 0a7363d1821af..a205d23ee2ac9 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php @@ -7,6 +7,7 @@ use Magento\Catalog\Model\Product; use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * Tests Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection @@ -70,10 +71,17 @@ protected function setUp() ); $this->search = $this->getMock(\Magento\Search\Api\SearchInterface::class, [], [], '', false); - $this->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ]); + $productLimitationMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class + ); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( + [ + ProductLimitationFactory::class => $productLimitationFactoryMock, + ] + ); $this->advancedCollection = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class, @@ -91,7 +99,9 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testLoadWithFilterNoFilters() @@ -157,4 +167,23 @@ protected function getCriteriaBuilder() ->getMock(); return $criteriaBuilder; } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php index 0b762646cc7f1..ae820a9d3aa8c 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php @@ -8,6 +8,7 @@ use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest; use Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -76,10 +77,15 @@ protected function setUp() $this->criteriaBuilder = $this->getCriteriaBuilder(); $this->filterBuilder = $this->getFilterBuilder(); - $this->objectManager->mockObjectManager( + $productLimitationMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class + ); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( [ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) + ProductLimitationFactory::class => $productLimitationFactoryMock, ] ); @@ -114,7 +120,9 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } /** @@ -222,4 +230,23 @@ protected function createFilter() ->getMock(); return $filter; } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index 4a3443b6d28f4..5312dadf13c85 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -30,7 +30,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\Api\ExtensionAttributesFactory::class => $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), \Magento\Framework\Api\AttributeValueFactory::class => @@ -47,7 +47,9 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testGetConditionsInstance() @@ -64,4 +66,23 @@ public function testGetActionsInstance() { $this->assertNull($this->rule->getActionsInstance()); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php index 7a5438ba17a5d..c0649bef17f0a 100644 --- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php @@ -181,12 +181,6 @@ protected function setUp() $this->shippingFactoryMock = $this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false); - $this->objectManager->mockObjectManager([ - \Magento\Quote\Model\ShippingAssignmentFactory::class => $this->shippingAssignmentFactoryMock, - \Magento\Quote\Api\Data\CartExtensionFactory::class => $this->cartExtensionFactoryMock, - \Magento\Quote\Model\ShippingFactory::class => $this->shippingFactoryMock, - ]); - $this->model = new \Magento\Checkout\Model\ShippingInformationManagement( $this->paymentMethodManagementMock, $this->paymentDetailsFactoryMock, @@ -198,11 +192,21 @@ protected function setUp() $this->scopeConfigMock, $this->totalsCollectorMock ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); + $this->objectManager->setBackwardCompatibleProperty( + $this->model, + 'shippingAssignmentFactory', + $this->shippingAssignmentFactoryMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->model, + 'cartExtensionFactory', + $this->cartExtensionFactoryMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->model, + 'shippingFactory', + $this->shippingFactoryMock + ); } /** diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 5eecaa147fcbd..4e78c15c4f373 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -7,19 +7,30 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - - protected function setUp() + protected function tearDown() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } - protected function tearDown() + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -53,7 +64,7 @@ public function testGetJobs() $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] ); diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index 40a754f78ec2f..d9499812cead9 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -7,11 +7,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -59,7 +54,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false); $this->_scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); @@ -104,7 +98,7 @@ protected function setUp() ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); @@ -125,7 +119,28 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetStore() diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 2ae230da35c22..4581c088e4bba 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -7,11 +7,6 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Directory\Model\Country\Postcode\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ @@ -29,7 +24,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock( \Magento\Directory\Model\Country\Postcode\Config\Reader::class, [], @@ -45,7 +39,7 @@ protected function setUp() false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } @@ -63,8 +57,29 @@ public function testGet() $this->assertEquals($expected, $configData->get()); } - public function tearDown() + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } } diff --git a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php index f3d5d4f5a9db0..592be58c9b4a5 100644 --- a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php +++ b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php @@ -710,7 +710,7 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) ->getMock(\Magento\Framework\EntityManager\MetadataPool::class, ['getLinkField'], [], '', false); $metadataPoolMock->expects($this->any())->method('getMetadata')->willReturnSelf(); - $this->objectManagerHelper->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\EntityManager\MetadataPool::class => $metadataPoolMock ]); @@ -739,7 +739,28 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) protected function tearDown() { parent::tearDown(); - $this->objectManagerHelper->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index b111bf8c72a16..90ac040af5be7 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -11,11 +11,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Eav\Model\Entity\Attribute\Config */ @@ -48,7 +43,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_attribute = $this->getMock(\Magento\Eav\Model\Entity\Attribute::class, [], [], '', false); $this->_entityType = $this->getMock(\Magento\Eav\Model\Entity\Type::class, [], [], '', false); $this->_readerMock = $this->getMock( @@ -69,7 +63,7 @@ protected function setUp() $serializerMock->method('unserialize') ->willReturn([]); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] ); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( @@ -81,7 +75,28 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetLockedFieldsEmpty() diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index 04cb6bbcab687..d00642d86e9b9 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -8,19 +8,14 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + * @var \Magento\ImportExport\Model\Export\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - private $objectManager; + protected $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_configScopeMock; + protected $cacheMock; /** * @var \Magento\Framework\Serialize\SerializerInterface @@ -30,33 +25,53 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $_cacheId = 'some_id'; + protected $cacheId = 'some_id'; /** * @var \Magento\ImportExport\Model\Export\Config */ - protected $_model; + protected $model; protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_readerMock = $this->getMock( + $this->readerMock = $this->getMock( \Magento\ImportExport\Model\Export\Config\Reader::class, [], [], '', false ); - $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -66,22 +81,22 @@ protected function tearDown() */ public function testGetEntities($value, $expected) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); - $this->_model = new \Magento\ImportExport\Model\Export\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); + $this->model = new \Magento\ImportExport\Model\Export\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expected, $this->_model->getEntities('entities')); + $this->assertEquals($expected, $this->model->getEntities('entities')); } public function getEntitiesDataProvider() @@ -100,22 +115,22 @@ public function getEntitiesDataProvider() */ public function testGetEntityTypes($configData, $entity, $expectedResult) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); - $this->_model = new \Magento\ImportExport\Model\Export\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); + $this->model = new \Magento\ImportExport\Model\Export\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expectedResult, $this->_model->getEntityTypes($entity)); + $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity)); } public function getEntityTypesDataProvider() @@ -153,22 +168,22 @@ public function getEntityTypesDataProvider() */ public function testGetFileFormats($value, $expected) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); - $this->_model = new \Magento\ImportExport\Model\Export\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); + $this->model = new \Magento\ImportExport\Model\Export\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expected, $this->_model->getFileFormats('fileFormats')); + $this->assertEquals($expected, $this->model->getFileFormats('fileFormats')); } public function getFileFormatsDataProvider() diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php index 2e182c4e35562..c3f9f6c81cf67 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php @@ -8,19 +8,14 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + * @var \Magento\ImportExport\Model\Import\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - private $objectManager; + protected $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_configScopeMock; + protected $cacheMock; /** * @var \Magento\Framework\Serialize\SerializerInterface @@ -30,33 +25,53 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $_cacheId = 'some_id'; + protected $cacheId = 'some_id'; /** * @var \Magento\ImportExport\Model\Import\Config */ - protected $_model; + protected $model; protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_readerMock = $this->getMock( + $this->readerMock = $this->getMock( \Magento\ImportExport\Model\Import\Config\Reader::class, [], [], '', false ); - $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } - public function tearDown() + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -66,22 +81,22 @@ public function tearDown() */ public function testGetEntities($value, $expected) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); - $this->_model = new \Magento\ImportExport\Model\Import\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); + $this->model = new \Magento\ImportExport\Model\Import\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expected, $this->_model->getEntities('entities')); + $this->assertEquals($expected, $this->model->getEntities('entities')); } public function getEntitiesDataProvider() @@ -100,22 +115,22 @@ public function getEntitiesDataProvider() */ public function testGetEntityTypes($configData, $entity, $expectedResult) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); - $this->_model = new \Magento\ImportExport\Model\Import\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); + $this->model = new \Magento\ImportExport\Model\Import\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expectedResult, $this->_model->getEntityTypes($entity)); + $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity)); } public function getEntityTypesDataProvider() 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 6747025eed2a8..9096bb8af1b82 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -7,11 +7,6 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Indexer\Model\Config\Data */ @@ -49,7 +44,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reader = $this->getMock(\Magento\Framework\Indexer\Config\Reader::class, ['read'], [], '', false); $this->cache = $this->getMockForAbstractClass( \Magento\Framework\Config\CacheInterface::class, @@ -68,14 +62,35 @@ protected function setUp() false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructorWithCache() diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php index 0929cc16fb3ec..c6e5241282cd0 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php @@ -106,9 +106,6 @@ protected function setUp() '', false ); - $this->objectManager->mockObjectManager([ - \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class => $this->optionsProcessorMock - ]); $this->repository = new \Magento\Quote\Model\Quote\Item\Repository( $this->quoteRepositoryMock, @@ -116,11 +113,11 @@ protected function setUp() $this->itemDataFactoryMock, ['custom_options' => $this->customOptionProcessor] ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); + $this->objectManager->setBackwardCompatibleProperty( + $this->repository, + 'cartItemOptionsProcessor', + $this->optionsProcessorMock + ); } /** diff --git a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php index 0b820cdfca035..d8e65de847457 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php @@ -104,10 +104,6 @@ protected function setUp() '', false ); - $this->objectManager->mockObjectManager([ - \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class - => $this->amountErrorMessageMock - ]); $this->service = $this->objectManager->getObject( \Magento\Quote\Model\ShippingAddressManagement::class, @@ -120,11 +116,11 @@ protected function setUp() 'addressRepository' => $this->addressRepository ] ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); + $this->objectManager->setBackwardCompatibleProperty( + $this->service, + 'minimumAmountErrorMessage', + $this->amountErrorMessageMock + ); } /** diff --git a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php index 4a61406e8a139..d2a959e0a3a00 100644 --- a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php +++ b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Review\Test\Unit\Model\ResourceModel\Review\Product; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -82,10 +84,15 @@ protected function setUp() $productLimitationMock = $this->getMock( \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class ); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( + [ + ProductLimitationFactory::class => $productLimitationFactoryMock, + ] + ); $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock - ]); $this->model = $this->objectManager->getObject( \Magento\Review\Model\ResourceModel\Review\Product\Collection::class, [ @@ -99,7 +106,28 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index ed001a14c9924..cfb2f63c99fec 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -7,11 +7,6 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -29,7 +24,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMockBuilder( \Magento\Sales\Model\Config\Reader::class )->disableOriginalConstructor()->getMock(); @@ -37,14 +31,35 @@ protected function setUp() \Magento\Framework\App\Cache\Type\Config::class )->disableOriginalConstructor()->getMock(); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGet() diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php index b492d59f56895..c0de06df83087 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -139,7 +139,7 @@ protected function setUp() ] ); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class => $associatedEntitiesMap ]); @@ -155,7 +155,28 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index adf98a1528cd8..d7f58dce73790 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -61,7 +61,7 @@ protected function setUp() ->setMethods(['create']) ->getMock(); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\Api\ExtensionAttributesFactory::class => $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), \Magento\Framework\Api\AttributeValueFactory::class => @@ -80,7 +80,28 @@ protected function setUp() protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testLoadCouponCode() diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index ab200ea88ef17..2d7f61da267a0 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -17,11 +17,6 @@ class InitialTest extends \PHPUnit_Framework_TestCase */ private $config; - /** - * @var \Magento\Framework\App\Config\Initial\Reader|\PHPUnit_Framework_MockObject_MockObject - */ - private $readerMock; - /** * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ @@ -42,13 +37,6 @@ class InitialTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->readerMock = $this->getMock( - \Magento\Framework\App\Config\Initial\Reader::class, - [], - [], - '', - false - ); $this->cacheMock = $this->getMock( \Magento\Framework\App\Cache\Type\Config::class, [], @@ -64,19 +52,42 @@ protected function setUp() $serializerMock->method('unserialize') ->willReturn($this->data); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] ); - $this->config = new \Magento\Framework\App\Config\Initial( - $this->readerMock, - $this->cacheMock + $this->config = $this->objectManager->getObject( + \Magento\Framework\App\Config\Initial::class, + [ + 'cache' => $this->cacheMock, + ] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 993faa1dcd679..744e293029f0e 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -50,14 +50,35 @@ protected function setUp() false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructActionsCached() 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 8af81fe5dc608..2b0a677b51848 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -43,23 +43,22 @@ protected function setUp() $this->_readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - $this->_model = new \Magento\Framework\Config\Data\Scoped( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheMock, - 'tag' + $this->_model = $this->objectManager->getObject( + \Magento\Framework\Config\Data\Scoped::class, + [ + 'reader' => $this->_readerMock, + 'configScope' => $this->_configScopeMock, + 'cache' => $this->_cacheMock, + 'cacheId' => 'tag', + ] + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->_model, + 'serializer', + $this->serializerMock ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); } /** diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index b04257713389f..d527f1b0da4b5 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -10,12 +10,7 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - - /** + /** * @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject */ private $readerMock; @@ -32,16 +27,36 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]); + $this->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetConfigNotCached() 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 db0218d00afc1..7ff809810cc08 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -77,12 +77,33 @@ protected function setUp() ); $this->serializerMock = $this->getMock(SerializerInterface::class); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->objectManagerHelper->mockObjectManager([SerializerInterface::class => $this->serializerMock]); + $this->mockObjectManager([SerializerInterface::class => $this->serializerMock]); } protected function tearDown() { - $this->objectManagerHelper->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index ce9a97b7ac90a..8b8b3e737fe95 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -7,11 +7,6 @@ class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Framework\Mview\Config\Data */ @@ -49,7 +44,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reader = $this->getMock(\Magento\Framework\Mview\Config\Reader::class, ['read'], [], '', false); $this->cache = $this->getMockForAbstractClass( \Magento\Framework\Config\CacheInterface::class, @@ -71,14 +65,35 @@ protected function setUp() ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } - public function tearDown() + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructorWithCache() diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php index 91ecf8424748f..16493899d7d4e 100644 --- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php +++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php @@ -342,42 +342,4 @@ public function setBackwardCompatibleProperty($object, $propertyName, $propertyV $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($object, $propertyValue); } - - /** - * Mock application object manager to return configured dependencies. Can be used in the case when need to add a - * new dependency that is used in the constructor keeping backwards compatibility - * - * $dependencies = [\Magento\Framework\Json\JsonInterface::class => $jsonMock] - * - * @param array $dependencies - * @return void - */ - public function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->_testObject->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->_testObject->any()) - ->method('getInstance') - ->willReturnSelf(); - $objectManagerMock->expects($this->_testObject->any()) - ->method('get') - ->will($this->_testObject->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - - /** - * Unset mocked object manager, must be used to restore - * \Magento\Framework\App\ObjectManager::_instance after mockObjectManager called - * - * @return void - */ - public function restoreObjectManager() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } } From ba58324f67aea91261a8d6d100d3a5951752b55a Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 20 Oct 2016 12:06:08 -0500 Subject: [PATCH 063/144] MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework - address code review comments --- .../Reflection/Test/Unit/MethodsMapTest.php | 72 ++++++++++--------- .../Test/Unit/ServiceInputProcessorTest.php | 8 +-- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php index 35af774c7ba45..67a372c4d33ca 100644 --- a/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php +++ b/lib/internal/Magento/Framework/Reflection/Test/Unit/MethodsMapTest.php @@ -86,44 +86,46 @@ public function testGetMethodReturnType() public function testGetMethodsMap() { + $data = [ + 'getMethodReturnType' => [ + 'type' => 'string', + 'isRequired' => true, + 'description' => null, + 'parameterCount' => 2, + ], + 'getMethodsMap' => [ + 'type' => 'array', + 'isRequired' => true, + 'description' => "
 Service methods' reflection data stored in cache as 'methodName' => "
+                    . "'returnType' ex. [ 'create' => '\Magento\Customer\Api\Data\Customer', 'validatePassword' "
+                    . "=> 'boolean' ] 
", + 'parameterCount' => 1, + ], + 'getMethodParams' => [ + 'type' => 'array', + 'isRequired' => true, + 'description' => null, + 'parameterCount' => 2 + ], + 'isMethodValidForDataField' => [ + 'type' => 'bool', + 'isRequired' => true, + 'description' => null, + 'parameterCount' => 2, + ], + 'isMethodReturnValueRequired' => [ + 'type' => 'bool', + 'isRequired' => true, + 'description' => null, + 'parameterCount' => 2, + ], + ]; $this->serializerMock->expects($this->once()) - ->method('serialize'); + ->method('serialize') + ->with($data); $methodsMap = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); $this->assertEquals( - [ - 'getMethodReturnType' => [ - 'type' => 'string', - 'isRequired' => true, - 'description' => null, - 'parameterCount' => 2, - ], - 'getMethodsMap' => [ - 'type' => 'array', - 'isRequired' => true, - 'description' => "
 Service methods' reflection data stored in cache as 'methodName' => "
-                        . "'returnType' ex. [ 'create' => '\Magento\Customer\Api\Data\Customer', 'validatePassword' "
-                        . "=> 'boolean' ] 
", - 'parameterCount' => 1, - ], - 'getMethodParams' => [ - 'type' => 'array', - 'isRequired' => true, - 'description' => null, - 'parameterCount' => 2 - ], - 'isMethodValidForDataField' => [ - 'type' => 'bool', - 'isRequired' => true, - 'description' => null, - 'parameterCount' => 2, - ], - 'isMethodReturnValueRequired' => [ - 'type' => 'bool', - 'isRequired' => true, - 'description' => null, - 'parameterCount' => 2, - ], - ], + $data, $methodsMap ); } diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php index 4892c96a22058..658755d38a4e8 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php @@ -100,13 +100,9 @@ function () use ($objectManager) { ); $serializerMock = $this->getMock(SerializerInterface::class); $serializerMock->method('serialize') - ->willReturnCallback(function ($data) { - return json_encode($data); - }); + ->willReturn('serializedData'); $serializerMock->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->willReturn('unserializedData'); $objectManager->setBackwardCompatibleProperty( $this->methodsMap, 'serializer', From 6637704316f519294336024fa0bdedb57a7b7470 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 20 Oct 2016 12:13:59 -0500 Subject: [PATCH 064/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../testsuite/Magento/Framework/Interception/AbstractPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php index 2e3711c3be0b6..43b962851c3bf 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php @@ -81,7 +81,7 @@ public function setUpInterceptionConfig($pluginConfig) \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => $config, \Magento\Framework\ObjectManager\DefinitionInterface::class => $definitions, \Magento\Framework\Interception\DefinitionInterface::class => $interceptionDefinitions, - \Magento\Framework\Serialize\SerializerInterface::class => $json, + \Magento\Framework\Serialize\SerializerInterface::class => $json, ]; $this->_objectManager = new \Magento\Framework\ObjectManager\ObjectManager( $factory, From 382e4c9d8d810e3c494de4322ec25eafb70f8490 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 20 Oct 2016 15:16:59 -0500 Subject: [PATCH 065/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index d00642d86e9b9..9e025c4a11e2d 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -18,7 +18,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected $cacheMock; /** - * @var \Magento\Framework\Serialize\SerializerInterface + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $serializerMock; From bfd1d867ed24cb580f33ebc6f585a04a20507e9e Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 20 Oct 2016 16:51:14 -0500 Subject: [PATCH 066/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../Framework/Serialize/Test/Unit/Serializer/JsonTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php index 59e9eeed19027..88e06d89e3763 100644 --- a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php @@ -22,7 +22,8 @@ protected function setUp() } /** - * @param null|bool|array $value + * @param string|int|float|bool|array|null $value + * @param string $expected * @dataProvider serializeDataProvider */ public function testSerialize($value, $expected) @@ -49,7 +50,8 @@ public function serializeDataProvider() } /** - * @param null|bool|array $value + * @param string $value + * @param string|int|float|bool|array|null $expected * @dataProvider unserializeDataProvider */ public function testUnserialize($value, $expected) From 9abe24189c2cc8aac9c50c930d99d14797e7e0fc Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 21 Oct 2016 08:48:32 -0500 Subject: [PATCH 067/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../Source/CountryofmanufactureTest.php | 21 ++++---- .../Directory/Test/Unit/Block/DataTest.php | 8 +-- .../Unit/Model/DataObjectProcessorTest.php | 9 ++-- .../App/Test/Unit/Route/ConfigTest.php | 53 +++++++++---------- 4 files changed, 41 insertions(+), 50 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php index 8c0e090bfe941..9ae3f94924855 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Source/CountryofmanufactureTest.php @@ -32,6 +32,11 @@ class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture */ private $countryOfManufacture; + /** + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; + protected function setUp() { $this->storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); @@ -46,19 +51,11 @@ protected function setUp() ] ); - $serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); - $serializerMock->method('serialize') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $serializerMock->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + $this->serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); $this->objectManagerHelper->setBackwardCompatibleProperty( $this->countryOfManufacture, 'serializer', - $serializerMock + $this->serializerMock ); } @@ -78,7 +75,9 @@ public function testGetAllOptions($cachedDataSrl, $cachedDataUnsrl) ->method('load') ->with($this->equalTo('COUNTRYOFMANUFACTURE_SELECT_STORE_store_code')) ->will($this->returnValue($cachedDataSrl)); - + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->willReturn($cachedDataUnsrl); $this->assertEquals($cachedDataUnsrl, $this->countryOfManufacture->getAllOptions()); } diff --git a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php index 6b41140240a30..5aabfe04708bf 100644 --- a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php @@ -80,13 +80,9 @@ protected function setUp() $serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); $serializerMock->method('serialize') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); + ->willReturn('serializedData'); $serializerMock->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->willReturn(['unserializedData']); $objectManagerHelper->setBackwardCompatibleProperty( $this->block, 'serializer', diff --git a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php index 8c62cfcbc09d4..0565832932a3f 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/DataObjectProcessorTest.php @@ -34,13 +34,10 @@ protected function setup() ); $serializerMock = $this->getMock(SerializerInterface::class); $serializerMock->method('serialize') - ->willReturnCallback(function ($data) { - return json_encode($data); - }); + ->willReturn('serializedData'); $serializerMock->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + ->willReturn(['unserializedData']); + $objectManager->setBackwardCompatibleProperty( $methodsMapProcessor, 'serializer', diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 2d30c9ae48893..05bbf68daf5af 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -32,6 +32,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ protected $_areaList; + /** + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; + protected function setUp() { $this->_readerMock = $this->getMock(\Magento\Framework\App\Route\Config\Reader::class, [], [], '', false); @@ -51,16 +56,8 @@ protected function setUp() 'areaList' => $this->_areaList ] ); - $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $objectManager->setBackwardCompatibleProperty($this->_config, 'serializer', $serializerMock); - $serializerMock->method('serialize') - ->willReturnCallback(function ($string) { - return json_encode($string); - }); - $serializerMock->method('unserialize') - ->willReturnCallback(function ($string) { - return json_decode($string, true); - }); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $objectManager->setBackwardCompatibleProperty($this->_config, 'serializer', $this->serializerMock); } public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() @@ -74,14 +71,14 @@ public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() public function testGetRouteByFrontName() { + $data = ['routerCode' => ['frontName' => 'routerName']]; + $serializedData = json_encode($data); $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn(json_encode(['routerCode' => ['frontName' => 'routerName']])); - - $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName')); - - // check internal caching in $this->_routes array + ->willReturn($serializedData); + $this->serializerMock->method('unserialize') + ->willReturn($data); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName')); } @@ -90,11 +87,9 @@ public function testGetRouteByFrontNameNoRoutes() $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn(json_encode([])); - - $this->assertFalse($this->_config->getRouteByFrontName('routerName')); - - // check caching in $this->_routes array + ->willReturn('[]'); + $this->serializerMock->method('unserialize') + ->willReturn([]); $this->assertFalse($this->_config->getRouteByFrontName('routerName')); } @@ -117,6 +112,8 @@ public function testGetRouteByFrontNameNoCache() ], ]; + $serializedData = json_encode($routes); + $this->_readerMock->expects( $this->once() )->method( @@ -137,24 +134,26 @@ public function testGetRouteByFrontNameNoCache() $this->returnValue('default_router') ); + $this->serializerMock->method('serialize') + ->willReturn($serializedData); + $this->_cacheMock->expects($this->once()) ->method('save') - ->with(json_encode($routes), 'scope::RoutesConfig'); - - $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope')); + ->with($serializedData, 'scope::RoutesConfig'); - // check caching in $this->_routes array $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope')); } public function testGetModulesByFrontName() { + $data = ['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]; + $serializedData = json_encode($data); $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn( - json_encode(['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]) - ); + ->willReturn($serializedData); + $this->serializerMock->method('unserialize') + ->willReturn($data); $this->assertEquals(['Module1'], $this->_config->getModulesByFrontName('routerName')); } } From e4cf56c9ccec8b8aa1fc75383134860b4a5347df Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 21 Oct 2016 09:28:35 -0500 Subject: [PATCH 068/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../Directory/Test/Unit/Block/DataTest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php index 5aabfe04708bf..fce1e50b04931 100644 --- a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php @@ -53,6 +53,9 @@ class DataTest extends \PHPUnit_Framework_TestCase /** @var LayoutInterface |\PHPUnit_Framework_MockObject_MockObject */ private $layoutMock; + /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; + protected function setUp() { $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); @@ -78,15 +81,11 @@ protected function setUp() ] ); - $serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); - $serializerMock->method('serialize') - ->willReturn('serializedData'); - $serializerMock->method('unserialize') - ->willReturn(['unserializedData']); + $this->serializerMock = $this->getMock(SerializerInterface::class, [], [], '', false); $objectManagerHelper->setBackwardCompatibleProperty( $this->block, 'serializer', - $serializerMock + $this->serializerMock ); } @@ -171,13 +170,16 @@ public function testGetCountryHtmlSelect( ->method('getCode') ->willReturn($storeCode); + $this->serializerMock->method('serialize') + ->willReturn('serializedData'); + $this->cacheTypeConfigMock->expects($this->once()) ->method('load') ->with('DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) ->willReturn(false); $this->cacheTypeConfigMock->expects($this->once()) ->method('save') - ->with(json_encode($options), 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) + ->with('serializedData', 'DIRECTORY_COUNTRY_SELECT_STORE_' . $storeCode) ->willReturnSelf(); $this->scopeConfigMock->expects($this->once()) From 214d7fa22ce2e54bda2427ab4e66cbd4d6bce3d9 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 21 Oct 2016 09:35:04 -0500 Subject: [PATCH 069/144] MAGETWO-59444: Create serializer interface and json class in framework - Fix static test failure --- .../Test/Unit/Model/ShippingInformationManagementTest.php | 3 +++ .../Magento/Customer/Test/Unit/Model/Address/ConfigTest.php | 3 +++ app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php | 3 +++ 3 files changed, 9 insertions(+) diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php index c0649bef17f0a..c19abf318aaf1 100644 --- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php @@ -106,6 +106,9 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase */ private $shippingMock; + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index d9499812cead9..9209a96101cfe 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -5,6 +5,9 @@ */ namespace Magento\Customer\Test\Unit\Model\Address; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class ConfigTest extends \PHPUnit_Framework_TestCase { /** diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index d7f58dce73790..c17c6dfe98bc1 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -5,6 +5,9 @@ */ namespace Magento\SalesRule\Test\Unit\Model; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class RuleTest extends \PHPUnit_Framework_TestCase { /** From 920b4309c91626ced16db52bcda3d6baea2dade1 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 21 Oct 2016 15:46:42 -0500 Subject: [PATCH 070/144] MAGETWO-59923: Configuration in \Magento\Indexer\Model\Config\Data contains objects --- .../Paypal/Model/Config/Rules/Converter.php | 2 +- .../Magento/Indexer/Model/Config/DataTest.php | 68 +++++++++++++++++++ .../Indexer/Model/Config/_files/result.php | 4 +- .../Framework/Indexer/Config/Converter.php | 2 +- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php diff --git a/app/code/Magento/Paypal/Model/Config/Rules/Converter.php b/app/code/Magento/Paypal/Model/Config/Rules/Converter.php index d65078212d89f..d14bc6597599c 100644 --- a/app/code/Magento/Paypal/Model/Config/Rules/Converter.php +++ b/app/code/Magento/Paypal/Model/Config/Rules/Converter.php @@ -85,7 +85,7 @@ protected function createPredicate(\DOMElement $node) if ($this->hasNodeElement($child)) { $result = [ 'name' => $child->getAttribute('name'), - 'message' => __($child->getAttribute('message')), + 'message' => __($child->getAttribute('message'))->__toString(), 'event' => $child->getAttribute('event'), 'argument' => $this->createArgument($child), ]; diff --git a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php new file mode 100644 index 0000000000000..673933c69143c --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php @@ -0,0 +1,68 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + } + + public function testConstructor() + { + $pathFiles = __DIR__ . '/_files'; + $expectedResult = require $pathFiles . '/result.php'; + $path = $pathFiles . '/indexer.xml'; + $fileResolverMock = $this->getMock(\Magento\Framework\Config\FileResolverInterface::class); + $fileIterator = $this->objectManager->create( + \Magento\Framework\Config\FileIterator::class, + [ + 'paths' => [$path], + ] + ); + $fileResolverMock->method('get') + ->willReturn($fileIterator); + $this->cleanAllCache(); + + $reader = $this->objectManager->create( + \Magento\Framework\Indexer\Config\Reader::class, + [ + 'fileResolver' => $fileResolverMock, + ] + ); + $model = $this->objectManager->create( + \Magento\Indexer\Model\Config\Data::class, + [ + 'reader' => $reader, + ] + ); + $this->assertEquals($expectedResult['catalogsearch_fulltext'], $model->get('catalogsearch_fulltext')); + $model2 = $this->objectManager->create( + \Magento\Indexer\Model\Config\Data::class, + [ + 'reader' => $reader, + ] + ); + $this->assertEquals($expectedResult['catalogsearch_fulltext'], $model2->get('catalogsearch_fulltext')); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} diff --git a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php index fc3d466dd499b..aed5bbfd99df1 100644 --- a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php +++ b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php @@ -12,8 +12,8 @@ 'primary' => 'first', 'view_id' => 'catalogsearch_fulltext', 'action_class' => \Magento\CatalogSearch\Model\Indexer\Fulltext::class, - 'title' => __('Catalog Search'), - 'description' => __('Rebuild Catalog product fulltext search index'), + 'title' => __('Catalog Search')->__toString(), + 'description' => __('Rebuild Catalog product fulltext search index')->__toString(), 'fieldsets' => [ [ diff --git a/lib/internal/Magento/Framework/Indexer/Config/Converter.php b/lib/internal/Magento/Framework/Indexer/Config/Converter.php index b8b17b185a1f2..14b3ebaacc827 100644 --- a/lib/internal/Magento/Framework/Indexer/Config/Converter.php +++ b/lib/internal/Magento/Framework/Indexer/Config/Converter.php @@ -212,7 +212,7 @@ protected function getTranslatedNodeValue(\DOMNode $node) { $value = $node->nodeValue; if ($this->getAttributeValue($node, 'translate') == 'true') { - $value = new \Magento\Framework\Phrase($value); + $value = (new \Magento\Framework\Phrase($value))->__toString(); } return $value; } From 00363d017abff16665387088c30acd37315408a1 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 21 Oct 2016 16:32:08 -0500 Subject: [PATCH 071/144] 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 9096bb8af1b82..7ab42a5b914bb 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 @@ private function mockObjectManager($dependencies) 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 018e21f86a782..4b4be9cfc5310 100644 --- a/lib/internal/Magento/Framework/App/Config/ScopePool.php +++ b/lib/internal/Magento/Framework/App/Config/ScopePool.php @@ -7,6 +7,7 @@ use Magento\Framework\App\RequestInterface; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -168,9 +169,8 @@ protected function _getScopeCode($scopeType, $scopeCode) */ 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 dbb3bed895697..4e685e3472ef8 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 @@ public function save(array $config, $key) /** * 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 2cfa2b006ff1e..2770443b6d7e2 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 @@ public function load($area) } /** - * 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 d1dc30ee7c9ef..f39904988328d 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 @@ public function load($area) 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 @@ public function load($area) 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 dbfc9d2b9f502..c31059a146224 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 @@ public function get($module, $area, $namespace, $action) /** * 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 06f35c94a4661..b0465f28e24f1 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 @@ public function testGetScopeConfigNotCached($scopeType, $scope, array $data) ->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 21dce94bb6c73..7f05857f7cc81 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 @@ public function getDataProvider() { return [ [false, false], - ['["some data"]', ['some data']], + ['serialized data', ['some data']], ]; } @@ -80,10 +80,11 @@ public function testSave() { $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 bb048218a768a..90dba092f054e 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 @@ protected function setUp() 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 @@ public function testLoadNotCached($area) $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 @@ public function loadDataProvider() 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 2b0a677b51848..f07eaad289fb3 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 @@ public function getConfigByPathDataProvider() public function testGetScopeSwitchingWithNonCachedData() { $testValue = ['some' => 'testValue']; - $jsonString = '{"some":"testValue"}'; + $serializedData = 'serialized data'; /** change current area */ $this->_configScopeMock->expects( @@ -137,12 +137,12 @@ public function testGetScopeSwitchingWithNonCachedData() $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 @@ public function testGetScopeSwitchingWithNonCachedData() public function testGetScopeSwitchingWithCachedData() { $testValue = ['some' => 'testValue']; - $jsonString = '{"some":"testValue"}'; + $serializedData = 'serialized data'; /** change current area */ $this->_configScopeMock->expects( @@ -166,14 +166,14 @@ public function testGetScopeSwitchingWithCachedData() ); $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 a536a8c759432..25039b5e09c83 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 @@ private function getSerializer() { 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 5d67a9077b2cf..e3ee36ac3c7a8 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\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 @@ private function filterPlugins(array &$plugins) } /** - * Returns logger instance + * Get logger * * @deprecated * @return \Psr\Log\LoggerInterface @@ -387,4 +389,18 @@ private function getLogger() } 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 7ff809810cc08..22c935b176d3c 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 @@ protected function setUp() ); $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 dd0d2f68ac6ad..f70af781df643 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 @@ public function testLoadScopedDataCached() ->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 95331616b5b9f..aa1fd86dbf7cf 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 11dd0aeff1b63..4cc51652fdb74 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 @@ public function testExtendWithCacheMock() $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 e636fc79818af..5af8fb7f71b6b 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 0000000000000..ef400e43e3c0e --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php @@ -0,0 +1,40 @@ +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 0000000000000..9a9252029d0b6 --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php @@ -0,0 +1,48 @@ +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 971bd6980abb1..2dbf7436aa60f 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 9d4785915a6dd..cfd3e81ca6b09 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 3207e590adf97..33203df3e7249 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 @@ public function write($key, array $config) file_put_contents( $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.json', - $this->getSerializer()->serialize($config) + serialize($config) ); } @@ -61,19 +55,4 @@ private function initialize() 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; - } } From 9b7be0eabb13c6d3c1623dca3922696f0db01216 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 21 Oct 2016 16:41:33 -0500 Subject: [PATCH 072/144] MAGETWO-59764: Create Serialize implementation of SerializerInterface Creating Serialize implementation --- .../ObjectManager/ConfigLoader/Compiled.php | 25 +++++++++++++++++-- .../Di/Compiler/Config/Writer/Filesystem.php | 24 +++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index f39904988328d..a16c172537c9e 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -8,6 +8,8 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\ObjectManager\ConfigLoaderInterface; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; class Compiled implements ConfigLoaderInterface { @@ -18,6 +20,11 @@ class Compiled implements ConfigLoaderInterface */ private $configCache = []; + /** + * @var SerializerInterface + */ + private $serializer; + /** * {inheritdoc} */ @@ -26,7 +33,7 @@ public function load($area) if (isset($this->configCache[$area])) { return $this->configCache[$area]; } - $this->configCache[$area] = unserialize(\file_get_contents(self::getFilePath($area))); + $this->configCache[$area] = $this->getSerializer()->unserialize(\file_get_contents(self::getFilePath($area))); return $this->configCache[$area]; } @@ -39,6 +46,20 @@ public function load($area) public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . '/' . $diPath . '/' . $area . '.json'; + return BP . $diPath . '/' . $area . '.json'; + } + + /** + * Get serializer + * + * @return SerializerInterface + * @deprecated + */ + private function getSerializer() + { + if (null === $this->serializer) { + $this->serializer = new \Magento\Framework\Serialize\Serializer\Serialize(); + } + return $this->serializer; } } 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 33203df3e7249..3e2620fc20861 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 @@ -9,6 +9,8 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Setup\Module\Di\Compiler\Config\WriterInterface; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; class Filesystem implements WriterInterface { @@ -17,6 +19,11 @@ class Filesystem implements WriterInterface */ private $directoryList; + /** + * @var SerializerInterface + */ + private $serializer; + /** * Constructor * @@ -40,7 +47,7 @@ public function write($key, array $config) file_put_contents( $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.json', - serialize($config) + $this->getSerializer()->serialize($config) ); } @@ -55,4 +62,19 @@ private function initialize() mkdir($this->directoryList->getPath(DirectoryList::DI)); } } + + /** + * Get serializer + * + * @return SerializerInterface + * @deprecated + */ + private function getSerializer() + { + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(Serialize::class); + } + return $this->serializer; + } } From a598228a59bb0f07991b597e5fd4eaf5a5567c40 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 21 Oct 2016 16:42:59 -0500 Subject: [PATCH 073/144] MAGETWO-59764: Create Serialize implementation of SerializerInterface Creating Serialize implementation --- .../Magento/Framework/App/Test/Unit/Router/ActionListTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 744e293029f0e..28764c86b35a2 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -51,7 +51,7 @@ protected function setUp() ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] + [\Magento\Framework\Serialize\Serializer\Serialize::class => $this->serializerMock] ); } From 9a928e867aacb58a8265aaf13884d9b797a0ab84 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 21 Oct 2016 17:00:51 -0500 Subject: [PATCH 074/144] MAGETWO-59764: Create Serialize implementation of SerializerInterface Creating Serialize implementation --- .../Framework/App/ObjectManager/ConfigLoader/Compiled.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index a16c172537c9e..26ef880e151ff 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -58,7 +58,7 @@ public static function getFilePath($area) private function getSerializer() { if (null === $this->serializer) { - $this->serializer = new \Magento\Framework\Serialize\Serializer\Serialize(); + $this->serializer = new Serialize(); } return $this->serializer; } From cfa471f9146168647b56e33c3ed4bc62f30cb3d1 Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 24 Oct 2016 08:10:01 -0500 Subject: [PATCH 075/144] MAGETWO-59444: Create serializer interface and json class in framework - Address code review comments --- .../Unit/Model/ProductTypes/ConfigTest.php | 12 ++++++---- .../Directory/Test/Unit/Block/DataTest.php | 3 ++- .../Test/Unit/Model/Config/DataTest.php | 3 ++- .../Sales/Test/Unit/Model/Config/DataTest.php | 3 ++- .../App/Test/Unit/Config/ScopePoolTest.php | 6 +++-- .../App/Test/Unit/Route/ConfigTest.php | 24 ++++++++++++------- .../Config/Test/Unit/Data/ScopedTest.php | 6 +++-- .../Test/Unit/Config/ConfigTest.php | 8 +------ 8 files changed, 38 insertions(+), 27 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 1842b8470c43a..fd1a5fa862c07 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -65,9 +65,11 @@ public function testGetType($value, $expected) { $this->cacheMock->expects($this->any()) ->method('load') - ->willReturn(json_encode($value)); + ->willReturn('serializedData'); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') ->willReturn($value); $this->config = $this->objectManager->getObject( @@ -95,7 +97,8 @@ public function testGetAll() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(json_encode('"types":["Expected Data"]]')); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturn(['types' => $expected]); $this->config = $this->objectManager->getObject( @@ -114,7 +117,8 @@ public function testIsProductSet() $this->cacheMock->expects($this->once()) ->method('load') ->willReturn(''); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturn([]); $this->config = $this->objectManager->getObject( diff --git a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php index fce1e50b04931..8bad1c1efee37 100644 --- a/app/code/Magento/Directory/Test/Unit/Block/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Block/DataTest.php @@ -170,7 +170,8 @@ public function testGetCountryHtmlSelect( ->method('getCode') ->willReturn($storeCode); - $this->serializerMock->method('serialize') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturn('serializedData'); $this->cacheTypeConfigMock->expects($this->once()) 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 7ab42a5b914bb..40cfeaaa75411 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -102,7 +102,8 @@ public function testConstructorWithCache() ->with($this->cacheId) ->willReturn($serializedData); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->with($serializedData) ->willReturn($this->indexers); diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index cfb2f63c99fec..606efadb6ef52 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -68,7 +68,8 @@ public function testGet() $this->_cacheMock->expects($this->once()) ->method('load'); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturn($expected); $configData = new \Magento\Sales\Model\Config\Data($this->_readerMock, $this->_cacheMock); 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 b0465f28e24f1..252efab07e8d0 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php @@ -114,7 +114,8 @@ public function testGetScopeConfigNotCached($scopeType, $scope, array $data) ->with('testScope') ->willReturn($data); $serializedData = 'serialized data'; - $this->serializerMock->method('serialize') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->with($data) ->willReturn($serializedData); $this->_cache->expects($this->once()) @@ -168,7 +169,8 @@ public function testGetScopeConfigCached($scopeType, $scope, array $data, $cache ->method('load') ->with($cacheKey) ->willReturn($cachedData); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->willReturn($data); $configData = $this->getMock(\Magento\Framework\App\Config\Data::class, [], [], '', false); $this->_dataFactory->expects($this->once()) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php index 05bbf68daf5af..fe6f8d05114c5 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Route/ConfigTest.php @@ -72,12 +72,13 @@ public function testGetRouteFrontNameIfCacheIfRouterIdNotExist() public function testGetRouteByFrontName() { $data = ['routerCode' => ['frontName' => 'routerName']]; - $serializedData = json_encode($data); $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn($serializedData); - $this->serializerMock->method('unserialize') + ->willReturn('serializedData'); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') ->willReturn($data); $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName')); } @@ -87,8 +88,10 @@ public function testGetRouteByFrontNameNoRoutes() $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn('[]'); - $this->serializerMock->method('unserialize') + ->willReturn('serializedData'); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') ->willReturn([]); $this->assertFalse($this->_config->getRouteByFrontName('routerName')); } @@ -134,7 +137,8 @@ public function testGetRouteByFrontNameNoCache() $this->returnValue('default_router') ); - $this->serializerMock->method('serialize') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->willReturn($serializedData); $this->_cacheMock->expects($this->once()) @@ -147,12 +151,14 @@ public function testGetRouteByFrontNameNoCache() public function testGetModulesByFrontName() { $data = ['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]]; - $serializedData = json_encode($data); + $this->_cacheMock->expects($this->once()) ->method('load') ->with('areaCode::RoutesConfig') - ->willReturn($serializedData); - $this->serializerMock->method('unserialize') + ->willReturn('serializedData'); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') ->willReturn($data); $this->assertEquals(['Module1'], $this->_config->getModulesByFrontName('routerName')); } 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 f07eaad289fb3..6e89bf9d50ab0 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -135,7 +135,8 @@ public function testGetScopeSwitchingWithNonCachedData() $this->returnValue($testValue) ); - $this->serializerMock->method('serialize') + $this->serializerMock->expects($this->once()) + ->method('serialize') ->with($testValue) ->willReturn($serializedData); @@ -165,7 +166,8 @@ public function testGetScopeSwitchingWithCachedData() $this->returnValue('adminhtml') ); - $this->serializerMock->method('unserialize') + $this->serializerMock->expects($this->once()) + ->method('unserialize') ->with($serializedData) ->willReturn($testValue); 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 22c935b176d3c..7a2682d007959 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -208,13 +208,7 @@ public function testHasPluginsWhenDataIsCached($expectedResult, $type) ]; $this->readerMock->expects($this->never())->method('read'); $this->cacheMock->expects($this->never())->method('save'); - $serializedValue = '{"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemContainer":true,' - . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\Item":true,' - . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\Item\\Enhanced":true,' - . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemContainer\\Enhanced":true,' - . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemContainer\\Proxy":true,' - . '"Magento\\Framework\\Interception\\Test\\Unit\\Custom\\Module\\Model\\ItemProxy":false,' - . '"virtual_custom_item":true}'; + $serializedValue = 'serializedData'; $this->cacheMock->expects($this->any()) ->method('load') ->with($cacheId) From f0fd791c48ab43b78ce6bdea56c6a631dbf2ce7d Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 24 Oct 2016 14:55:39 -0500 Subject: [PATCH 076/144] MAGETWO-59923: Configuration in \Magento\Indexer\Model\Config\Data contains objects - revert 920b430 --- .../Paypal/Model/Config/Rules/Converter.php | 2 +- .../Magento/Indexer/Model/Config/DataTest.php | 68 ------------------- .../Indexer/Model/Config/_files/result.php | 4 +- .../Framework/Indexer/Config/Converter.php | 2 +- 4 files changed, 4 insertions(+), 72 deletions(-) delete mode 100644 dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php diff --git a/app/code/Magento/Paypal/Model/Config/Rules/Converter.php b/app/code/Magento/Paypal/Model/Config/Rules/Converter.php index d14bc6597599c..d65078212d89f 100644 --- a/app/code/Magento/Paypal/Model/Config/Rules/Converter.php +++ b/app/code/Magento/Paypal/Model/Config/Rules/Converter.php @@ -85,7 +85,7 @@ protected function createPredicate(\DOMElement $node) if ($this->hasNodeElement($child)) { $result = [ 'name' => $child->getAttribute('name'), - 'message' => __($child->getAttribute('message'))->__toString(), + 'message' => __($child->getAttribute('message')), 'event' => $child->getAttribute('event'), 'argument' => $this->createArgument($child), ]; diff --git a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php deleted file mode 100644 index 673933c69143c..0000000000000 --- a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/DataTest.php +++ /dev/null @@ -1,68 +0,0 @@ -objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - - } - - public function testConstructor() - { - $pathFiles = __DIR__ . '/_files'; - $expectedResult = require $pathFiles . '/result.php'; - $path = $pathFiles . '/indexer.xml'; - $fileResolverMock = $this->getMock(\Magento\Framework\Config\FileResolverInterface::class); - $fileIterator = $this->objectManager->create( - \Magento\Framework\Config\FileIterator::class, - [ - 'paths' => [$path], - ] - ); - $fileResolverMock->method('get') - ->willReturn($fileIterator); - $this->cleanAllCache(); - - $reader = $this->objectManager->create( - \Magento\Framework\Indexer\Config\Reader::class, - [ - 'fileResolver' => $fileResolverMock, - ] - ); - $model = $this->objectManager->create( - \Magento\Indexer\Model\Config\Data::class, - [ - 'reader' => $reader, - ] - ); - $this->assertEquals($expectedResult['catalogsearch_fulltext'], $model->get('catalogsearch_fulltext')); - $model2 = $this->objectManager->create( - \Magento\Indexer\Model\Config\Data::class, - [ - 'reader' => $reader, - ] - ); - $this->assertEquals($expectedResult['catalogsearch_fulltext'], $model2->get('catalogsearch_fulltext')); - } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } -} diff --git a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php index aed5bbfd99df1..fc3d466dd499b 100644 --- a/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php +++ b/dev/tests/integration/testsuite/Magento/Indexer/Model/Config/_files/result.php @@ -12,8 +12,8 @@ 'primary' => 'first', 'view_id' => 'catalogsearch_fulltext', 'action_class' => \Magento\CatalogSearch\Model\Indexer\Fulltext::class, - 'title' => __('Catalog Search')->__toString(), - 'description' => __('Rebuild Catalog product fulltext search index')->__toString(), + 'title' => __('Catalog Search'), + 'description' => __('Rebuild Catalog product fulltext search index'), 'fieldsets' => [ [ diff --git a/lib/internal/Magento/Framework/Indexer/Config/Converter.php b/lib/internal/Magento/Framework/Indexer/Config/Converter.php index 14b3ebaacc827..b8b17b185a1f2 100644 --- a/lib/internal/Magento/Framework/Indexer/Config/Converter.php +++ b/lib/internal/Magento/Framework/Indexer/Config/Converter.php @@ -212,7 +212,7 @@ protected function getTranslatedNodeValue(\DOMNode $node) { $value = $node->nodeValue; if ($this->getAttributeValue($node, 'translate') == 'true') { - $value = (new \Magento\Framework\Phrase($value))->__toString(); + $value = new \Magento\Framework\Phrase($value); } return $value; } From fbb5fbf5584cea674f8c33b0af890588a7cbf22c Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 24 Oct 2016 18:31:06 -0500 Subject: [PATCH 077/144] MAGETWO-59923: Configuration in \Magento\Indexer\Model\Config\Data contains objects --- .../Magento/Indexer/Model/Config/Data.php | 23 ++++++++++-- .../Test/Unit/Model/Config/DataTest.php | 35 +++---------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index 68a390a8c06b7..94e9079957a22 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -5,6 +5,9 @@ */ namespace Magento\Indexer\Model\Config; +use Magento\Framework\Serialize\Serializer\Serialize; +use Magento\Framework\Serialize\SerializerInterface; + class Data extends \Magento\Framework\Config\Data { /** @@ -22,13 +25,14 @@ public function __construct( \Magento\Framework\Indexer\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection, - $cacheId = 'indexer_config' + $cacheId = 'indexer_config', + SerializerInterface $serializer = null ) { $this->stateCollection = $stateCollection; $isCacheExists = $cache->test($cacheId); - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); if (!$isCacheExists) { $this->deleteNonexistentStates(); @@ -49,4 +53,19 @@ protected function deleteNonexistentStates() } } } + + /** + * Get serializer + * + * @return \Magento\Framework\Serialize\SerializerInterface + * @deprecated + */ + protected function getSerializer() + { + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(Serialize::class); + } + return $this->serializer; + } } 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 40cfeaaa75411..e16c21b8f1125 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -62,35 +62,6 @@ protected function setUp() false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructorWithCache() @@ -113,7 +84,8 @@ public function testConstructorWithCache() $this->reader, $this->cache, $this->stateCollection, - $this->cacheId + $this->cacheId, + $this->serializerMock ); } @@ -152,7 +124,8 @@ public function testConstructorWithoutCache() $this->reader, $this->cache, $this->stateCollection, - $this->cacheId + $this->cacheId, + $this->serializerMock ); } } From fdb56647071b434040a9301af5e81c1cafb522a4 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 24 Oct 2016 20:31:09 -0500 Subject: [PATCH 078/144] MAGETWO-59764: Create Serialize implementation of SerializerInterface Creating Serialize implementation --- .../Serialize/Serializer/Serialize.php | 5 ++ .../Test/Unit/Serializer/SerializeTest.php | 49 ++++++++++++++----- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php b/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php index ef400e43e3c0e..3d2dc66e502ef 100644 --- a/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php @@ -7,6 +7,11 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Less secure than Json implementation, but gives higher performance on big arrays. Does not unserialize objects on + * PHP 7. Using this implementation directly is discouraged as it may lead to security vulnerabilities, especially on + * older versions of PHP + */ class Serialize implements SerializerInterface { /** diff --git a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php index 9a9252029d0b6..874647b5d705f 100644 --- a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php @@ -24,25 +24,48 @@ protected function setUp() } /** - * @param string|int|bool|array|null $value - * @dataProvider serializeUnserializeDataProvider + * @param string|int|float|bool|array|null $value + * @param string $serializedValue + * @dataProvider serializeDataProvider */ - public function testSerializeUnserialize($value) + public function testSerialize($value, $serializedValue) { - $this->assertEquals( - $this->serialize->unserialize($this->serialize->serialize($value)), - $value - ); + $this->assertEquals($serializedValue, $this->serialize->serialize($value)); } - public function serializeUnserializeDataProvider() + public function serializeDataProvider() { return [ - ['string'], - [''], - [null], - [false], - [['a' => 'b']], + ['string', 's:6:"string";'], + ['', 's:0:"";'], + [10, 'i:10;'], + [10.5, 'd:10.5;'], + [null, 'N;'], + [false, 'b:0;'], + [['foo' => 'bar'], 'a:1:{s:3:"foo";s:3:"bar";}'], + ]; + } + + /** + * @param string $serializedValue + * @param string|int|float|bool|array|null $value + * @dataProvider unserializeDataProvider + */ + public function testUnserialize($serializedValue, $value) + { + $this->assertEquals($value, $this->serialize->unserialize($serializedValue)); + } + + public function unserializeDataProvider() + { + return [ + ['s:6:"string";', 'string'], + ['s:0:"";', ''], + ['i:10;', 10], + ['d:10.5;', 10.5], + ['N;', null], + ['b:0;', false], + ['a:1:{s:3:"foo";s:3:"bar";}', ['foo' => 'bar']], ]; } } From 011e2eacc0e642751c2f4f53ecf1f24e7e60c01a Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 24 Oct 2016 22:53:14 -0500 Subject: [PATCH 079/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Catalog/Model/ProductTypes/Config.php | 12 ++- .../ResourceModel/Product/Collection.php | 47 ++++----- .../Product/Link/Product/Collection.php | 96 +------------------ .../Product/Option/Collection.php | 21 ++-- .../Unit/Model/ProductTypes/ConfigTest.php | 32 +------ .../ResourceModel/Product/CollectionTest.php | 40 +------- .../Product/Link/Product/CollectionTest.php | 34 +------ .../Product/Option/CollectionTest.php | 33 +------ .../Import/Product/Type/AbstractType.php | 12 ++- app/code/Magento/CatalogRule/Model/Rule.php | 15 ++- .../CatalogRule/Test/Unit/Model/RuleTest.php | 48 ++++------ .../ResourceModel/Advanced/Collection.php | 20 +++- .../ResourceModel/Fulltext/Collection.php | 19 +++- .../ResourceModel/Advanced/CollectionTest.php | 32 +------ .../ResourceModel/Fulltext/CollectionTest.php | 27 +----- app/code/Magento/CatalogWidget/Model/Rule.php | 15 ++- .../Test/Unit/Model/RuleTest.php | 32 ------- app/code/Magento/Cron/Model/Config/Data.php | 18 ++-- .../Cron/Test/Unit/Model/Config/DataTest.php | 32 +------ .../Magento/Customer/Model/Address/Config.php | 11 ++- .../Test/Unit/Model/Address/ConfigTest.php | 33 +------ .../Model/Country/Postcode/Config/Data.php | 12 ++- .../Country/Postcode/Config/DataTest.php | 35 +------ .../Import/Product/Type/Downloadable.php | 11 ++- .../Import/Product/Type/DownloadableTest.php | 34 +------ .../Eav/Model/Entity/Attribute/Config.php | 13 ++- .../Model/Entity/Attribute/ConfigTest.php | 34 +------ .../ImportExport/Model/Export/Config.php | 12 ++- .../ImportExport/Model/Import/Config.php | 12 ++- .../Test/Unit/Model/Export/ConfigTest.php | 46 ++------- .../Test/Unit/Model/Import/ConfigTest.php | 35 +------ .../Review/Product/Collection.php | 17 +++- .../Review/Product/CollectionTest.php | 34 +------ app/code/Magento/Rule/Model/AbstractModel.php | 14 ++- app/code/Magento/Sales/Model/Config/Data.php | 12 ++- .../Sales/Test/Unit/Model/Config/DataTest.php | 44 +++------ app/code/Magento/SalesRule/Model/Rule.php | 15 ++- .../SalesRule/Test/Unit/Model/RuleTest.php | 33 ------- .../Magento/Framework/App/Config/Initial.php | 29 ++---- .../Framework/App/Router/ActionList.php | 27 ++---- .../App/Test/Unit/Config/InitialTest.php | 31 +----- .../App/Test/Unit/Router/ActionListTest.php | 30 +----- .../Magento/Framework/Config/Data.php | 9 +- .../Framework/Config/Test/Unit/DataTest.php | 36 ++----- .../Framework/Interception/Config/Config.php | 28 ++---- .../Test/Unit/Config/ConfigTest.php | 29 +----- .../Magento/Framework/Mview/Config/Data.php | 12 ++- .../Mview/Test/Unit/Config/DataTest.php | 35 +------ 48 files changed, 336 insertions(+), 972 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ProductTypes/Config.php b/app/code/Magento/Catalog/Model/ProductTypes/Config.php index a80692cfaf945..d6f88d3bf4640 100644 --- a/app/code/Magento/Catalog/Model/ProductTypes/Config.php +++ b/app/code/Magento/Catalog/Model/ProductTypes/Config.php @@ -5,19 +5,25 @@ */ namespace Magento\Catalog\Model\ProductTypes; +use Magento\Framework\Serialize\SerializerInterface; + class Config extends \Magento\Framework\Config\Data implements \Magento\Catalog\Model\ProductTypes\ConfigInterface { /** - * @param \Magento\Catalog\Model\ProductTypes\Config\Reader $reader + * Config constructor + * + * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Catalog\Model\ProductTypes\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = 'product_types_config' + $cacheId = 'product_types_config', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index e4af92baf8571..55b04163ae110 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -17,6 +17,7 @@ use Magento\Framework\EntityManager\MetadataPool; use Magento\Store\Model\Store; use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * Product collection @@ -258,7 +259,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac /** * @var MetadataPool */ - private $metadataPool; + protected $metadataPool; /** * @var \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory @@ -266,6 +267,8 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac private $productLimitationFiltersFactory; /** + * Collection constructor + * * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy @@ -285,7 +288,9 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param GroupManagementInterface $groupManagement - * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection + * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection + * @param ProductLimitationFactory|null $productLimitationFactory + * @param MetadataPool|null $metadataPool * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -309,7 +314,9 @@ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, GroupManagementInterface $groupManagement, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null + \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, + ProductLimitationFactory $productLimitationFactory = null, + MetadataPool $metadataPool = null ) { $this->moduleManager = $moduleManager; $this->_catalogProductFlatState = $catalogProductFlatState; @@ -321,7 +328,11 @@ public function __construct( $this->_resourceHelper = $resourceHelper; $this->dateTime = $dateTime; $this->_groupManagement = $groupManagement; - $this->_productLimitationFilters = $this->getProductLimitationFactory()->create(); + $productLimitationFactory = $productLimitationFactory ?: ObjectManager::getInstance()->get( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory::class + ); + $this->_productLimitationFilters = $productLimitationFactory->create(); + $this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class); parent::__construct( $entityFactory, $logger, @@ -2186,7 +2197,7 @@ public function addMediaGalleryData() ); $mediaGalleries = []; - $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); $items = $this->getItems(); $select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) { @@ -2206,18 +2217,6 @@ public function addMediaGalleryData() return $this; } - /** - * Get MetadataPool instance - * @return MetadataPool - */ - private function getMetadataPool() - { - if (!$this->metadataPool) { - $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class); - } - return $this->metadataPool; - } - /** * Retrieve GalleryReadHandler * @@ -2339,18 +2338,4 @@ public function getPricesCount() return $this->_pricesCount; } - - /** - * @deprecated - * @return \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory - */ - private function getProductLimitationFactory() - { - if (null === $this->productLimitationFiltersFactory) { - $this->productLimitationFiltersFactory = ObjectManager::getInstance()->get( - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory::class - ); - } - return $this->productLimitationFiltersFactory; - } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php index 239f94e1286b8..a71faa9144066 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php @@ -6,9 +6,6 @@ namespace Magento\Catalog\Model\ResourceModel\Product\Link\Product; use Magento\Catalog\Api\Data\ProductInterface; -use Magento\Customer\Api\GroupManagementInterface; -use Magento\Framework\App\ObjectManager; -use Magento\Framework\EntityManager\MetadataPool; /** * Catalog product linked products collection @@ -53,80 +50,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection */ protected $_hasLinkFilter = false; - /** - * @var MetadataPool - */ - private $metadataPool; - - /** - * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory - * @param \Psr\Log\LoggerInterface $logger - * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy - * @param \Magento\Framework\Event\ManagerInterface $eventManager - * @param \Magento\Eav\Model\Config $eavConfig - * @param \Magento\Framework\App\ResourceConnection $resource - * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory - * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper - * @param \Magento\Framework\Validator\UniversalFactory $universalFactory - * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Magento\Framework\Module\Manager $moduleManager - * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory - * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl - * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate - * @param \Magento\Customer\Model\Session $customerSession - * @param \Magento\Framework\Stdlib\DateTime $dateTime - * @param GroupManagementInterface $groupManagement - * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection - * @SuppressWarnings(PHPMD.ExcessiveParameterList) - */ - public function __construct( - \Magento\Framework\Data\Collection\EntityFactory $entityFactory, - \Psr\Log\LoggerInterface $logger, - \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, - \Magento\Framework\Event\ManagerInterface $eventManager, - \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\App\ResourceConnection $resource, - \Magento\Eav\Model\EntityFactory $eavEntityFactory, - \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper, - \Magento\Framework\Validator\UniversalFactory $universalFactory, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Framework\Module\Manager $moduleManager, - \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState, - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, - \Magento\Catalog\Model\ResourceModel\Url $catalogUrl, - \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, - \Magento\Customer\Model\Session $customerSession, - \Magento\Framework\Stdlib\DateTime $dateTime, - GroupManagementInterface $groupManagement, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null - ) { - parent::__construct( - $entityFactory, - $logger, - $fetchStrategy, - $eventManager, - $eavConfig, - $resource, - $eavEntityFactory, - $resourceHelper, - $universalFactory, - $storeManager, - $moduleManager, - $catalogProductFlatState, - $scopeConfig, - $productOptionFactory, - $catalogUrl, - $localeDate, - $customerSession, - $dateTime, - $groupManagement, - $connection - ); - } - /** * Declare link model and initialize type attributes join * @@ -219,7 +142,7 @@ public function addProductFilter($products) if (!is_array($products)) { $products = [$products]; } - $identifierField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getIdentifierField(); + $identifierField = $this->metadataPool->getMetadata(ProductInterface::class)->getIdentifierField(); $this->getSelect()->where("product_entity_table.$identifierField IN (?)", $products); $this->_hasLinkFilter = true; } @@ -279,7 +202,7 @@ protected function _joinLinks() $connection->quoteInto('links.link_type_id = ?', $this->_linkTypeId), ]; $joinType = 'join'; - $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); if ($this->getProduct() && $this->getProduct()->getId()) { $linkFieldId = $this->getProduct()->getData( $linkField @@ -422,19 +345,6 @@ public function addLinkAttributeToFilter($code, $condition) return $this; } - /** - * Get MetadataPool instance - * @return MetadataPool - * @deprecated - */ - private function getMetadataPool() - { - if (!$this->metadataPool) { - $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class); - } - return $this->metadataPool; - } - /** * Join Product To Links * @return void @@ -442,7 +352,7 @@ private function getMetadataPool() private function joinProductsToLinks() { if ($this->_hasLinkFilter) { - $metaDataPool = $this->getMetadataPool()->getMetadata(ProductInterface::class); + $metaDataPool = $this->metadataPool->getMetadata(ProductInterface::class); $linkField = $metaDataPool->getLinkField(); $entityTable = $metaDataPool->getEntityTable(); $this->getSelect() diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php index 2d50ead9d56d9..68280d5a1d597 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Collection.php @@ -7,6 +7,7 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface; +use Magento\Framework\EntityManager\MetadataPool; /** * Catalog product options collection @@ -49,6 +50,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource + * @param MetadataPool $metadataPool * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -59,10 +61,13 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $optionValueCollectionFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, - \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null + \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null, + MetadataPool $metadataPool = null ) { $this->_optionValueCollectionFactory = $optionValueCollectionFactory; $this->_storeManager = $storeManager; + $this->metadataPool = $metadataPool ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\EntityManager\MetadataPool::class); parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource); } @@ -248,7 +253,7 @@ protected function _initSelect() ['cpe' => $this->getTable('catalog_product_entity')], sprintf( 'cpe.%s = main_table.product_id', - $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField() + $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField() ), [] ); @@ -318,18 +323,6 @@ public function reset() return $this->_reset(); } - /** - * @return \Magento\Framework\EntityManager\MetadataPool - */ - private function getMetadataPool() - { - if (null === $this->metadataPool) { - $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\EntityManager\MetadataPool::class); - } - return $this->metadataPool; - } - /** * @return JoinProcessorInterface */ diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index fd1a5fa862c07..852eb11c5cfb9 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -44,16 +44,6 @@ protected function setUp() ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); } /** @@ -78,6 +68,7 @@ public function testGetType($value, $expected) 'reader' => $this->readerMock, 'cache' => $this->cacheMock, 'cacheId' => 'cache_id', + 'serializer' => $this->serializerMock, ] ); $this->assertEquals($expected, $this->config->getType('global')); @@ -107,6 +98,7 @@ public function testGetAll() 'reader' => $this->readerMock, 'cache' => $this->cacheMock, 'cacheId' => 'cache_id', + 'serializer' => $this->serializerMock, ] ); $this->assertEquals($expected, $this->config->getAll()); @@ -127,27 +119,9 @@ public function testIsProductSet() 'reader' => $this->readerMock, 'cache' => $this->cacheMock, 'cacheId' => 'cache_id', + 'serializer' => $this->serializerMock, ] ); $this->assertEquals(false, $this->config->isProductSet('typeId')); } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php index b2ebc56e09397..f19b2c15c8ab2 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php @@ -155,11 +155,6 @@ protected function setUp() $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); $productLimitationFactoryMock->method('create') ->willReturn($productLimitationMock); - $this->mockObjectManager( - [ - ProductLimitationFactory::class => $productLimitationFactoryMock, - ] - ); $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Collection::class, [ @@ -182,7 +177,9 @@ protected function setUp() 'customerSession' => $customerSession, 'dateTime' => $dateTime, 'groupManagement' => $groupManagement, - 'connection' => $this->connectionMock + 'connection' => $this->connectionMock, + 'productLimitationFactory' => $productLimitationFactoryMock, + 'metadataPool' => $this->metadataPoolMock, ] ); $this->collection->setConnection($this->connectionMock); @@ -191,11 +188,6 @@ protected function setUp() 'mediaGalleryResource', $this->galleryResourceMock ); - $this->objectManager->setBackwardCompatibleProperty( - $this->collection, - 'metadataPool', - $this->metadataPoolMock - ); $this->objectManager->setBackwardCompatibleProperty( $this->collection, 'productGalleryReadHandler', @@ -203,13 +195,6 @@ protected function setUp() ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - public function testAddProductCategoriesFilter() { $condition = ['in' => [1, 2]]; @@ -276,23 +261,4 @@ public function testAddMediaGalleryData() $this->assertSame($this->collection, $this->collection->addMediaGalleryData()); } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php index 93e5224387c5a..fe244d01eea80 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php @@ -134,10 +134,9 @@ function ($store) { $this->timezoneInterfaceMock = $this->getMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class); $this->sessionMock = $this->getMock(\Magento\Customer\Model\Session::class, [], [], '', false); $this->dateTimeMock = $this->getMock(\Magento\Framework\Stdlib\DateTime::class); - $productLimitationFactory = $this->getMock(ProductLimitationFactory::class, ['create']); - $productLimitationFactory->method('create') + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') ->willReturn($this->getMock(ProductLimitation::class)); - $this->mockObjectManager([ProductLimitationFactory::class => $productLimitationFactory]); $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class, @@ -159,18 +158,12 @@ function ($store) { 'catalogUrl' => $this->urlMock, 'localeDate' => $this->timezoneInterfaceMock, 'customerSession' => $this->sessionMock, - 'dateTime' => $this->dateTimeMock + 'dateTime' => $this->dateTimeMock, + 'productLimitationFactory' => $productLimitationFactoryMock, ] ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - public function testSetProduct() { /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ @@ -181,23 +174,4 @@ public function testSetProduct() $this->collection->setProduct($product); $this->assertEquals(33, $this->collection->getStoreId()); } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php index 33da480ba1a80..36afda6287fb9 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php @@ -150,10 +150,6 @@ protected function setUp() $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->selectMock->expects($this->exactly(2))->method('join'); - $this->mockObjectManager([ - \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, - ]); - $this->collection = new Collection( $this->entityFactoryMock, $this->loggerMock, @@ -162,7 +158,8 @@ protected function setUp() $this->optionsFactoryMock, $this->storeManagerMock, null, - $this->resourceMock + $this->resourceMock, + $this->metadataPoolMock ); $this->objectManager->setBackwardCompatibleProperty( $this->collection, @@ -171,34 +168,8 @@ protected function setUp() ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - public function testReset() { $this->collection->reset(); } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 0d19b9fcbedc8..2cb808cc3c3a7 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -8,6 +8,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface; use Magento\CatalogImportExport\Model\Import\Product; +use Magento\Framework\EntityManager\MetadataPool; /** * Import entity abstract product type model @@ -142,22 +143,27 @@ abstract class AbstractType private $productEntityLinkField; /** + * AbstractType constructor + * * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac - * @param \Magento\Framework\App\ResourceConnection $resource + * @param ResourceConnection $resource * @param array $params + * @param MetadataPool|null $metadataPool * @throws \Magento\Framework\Exception\LocalizedException */ public function __construct( \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac, \Magento\Framework\App\ResourceConnection $resource, - array $params + array $params, + MetadataPool $metadataPool = null ) { $this->_attrSetColFac = $attrSetColFac; $this->_prodAttrColFac = $prodAttrColFac; $this->_resource = $resource; $this->_connection = $resource->getConnection(); + $this->metadataPool = $metadataPool ?: $this->getMetadataPool(); if ($this->isSuitable()) { if (!isset($params[0]) || !isset($params[1]) @@ -563,7 +569,7 @@ protected function getMetadataPool() protected function getProductEntityLinkField() { if (!$this->productEntityLinkField) { - $this->productEntityLinkField = $this->getMetadataPool() + $this->productEntityLinkField = $this->metadataPool ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class) ->getLinkField(); } diff --git a/app/code/Magento/CatalogRule/Model/Rule.php b/app/code/Magento/CatalogRule/Model/Rule.php index 0da658be4cc21..4e8b95607de4b 100644 --- a/app/code/Magento/CatalogRule/Model/Rule.php +++ b/app/code/Magento/CatalogRule/Model/Rule.php @@ -7,6 +7,8 @@ use Magento\Catalog\Model\Product; use Magento\CatalogRule\Api\Data\RuleInterface; +use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\Api\ExtensionAttributesFactory; use Magento\Framework\DataObject\IdentityInterface; /** @@ -137,7 +139,8 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I protected $ruleConditionConverter; /** - * Rule constructor. + * Rule constructor + * * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Data\FormFactory $formFactory @@ -157,6 +160,8 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, I * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param array $relatedCacheTypes * @param array $data + * @param ExtensionAttributesFactory|null $extensionFactory + * @param AttributeValueFactory|null $customAttributeFactory * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -179,7 +184,9 @@ public function __construct( \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $relatedCacheTypes = [], - array $data = [] + array $data = [], + ExtensionAttributesFactory $extensionFactory = null, + AttributeValueFactory $customAttributeFactory = null ) { $this->_productCollectionFactory = $productCollectionFactory; $this->_storeManager = $storeManager; @@ -201,7 +208,9 @@ public function __construct( $localeDate, $resource, $resourceCollection, - $data + $data, + $extensionFactory, + $customAttributeFactory ); } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index 22252d218f50c..b3f35dbe98e0d 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -124,12 +124,20 @@ protected function setUp() false ); - $this->mockObjectManager([ - \Magento\Framework\Api\ExtensionAttributesFactory::class => - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), - \Magento\Framework\Api\AttributeValueFactory::class => - $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ]); + $extensionFactoryMock = $this->getMock( + \Magento\Framework\Api\ExtensionAttributesFactory::class, + [], + [], + '', + false + ); + $attributeValueFactoryMock = $this->getMock( + \Magento\Framework\Api\AttributeValueFactory::class, + [], + [], + '', + false + ); $this->rule = $this->objectManager->getObject( \Magento\CatalogRule\Model\Rule::class, @@ -139,17 +147,12 @@ protected function setUp() 'ruleProductProcessor' => $this->_ruleProductProcessor, 'productCollectionFactory' => $this->_productCollectionFactory, 'resourceIterator' => $this->_resourceIterator, + 'extensionFactory' => $extensionFactoryMock, + 'customAttributeFactory' => $attributeValueFactoryMock, ] ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - /** * @dataProvider dataProviderCallbackValidateProduct * @param bool $validate @@ -372,23 +375,4 @@ public function testGetConditionsFieldSetId() $expectedResult = 'form_namerule_conditions_fieldset_100'; $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName)); } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php index a8ada4d409f61..6b5db58acfd89 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php @@ -9,10 +9,12 @@ use Magento\Framework\Api\FilterBuilder; use Magento\Framework\Api\Search\SearchCriteriaBuilder; use Magento\Framework\Api\Search\SearchResultFactory; +use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage; use Magento\Framework\Search\Request\EmptyRequestDataException; use Magento\Framework\Search\Request\NonExistingRequestNameException; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * Collection Advanced @@ -54,7 +56,8 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection private $filterBuilder; /** - * Collection constructor. + * Collection constructor + * * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy @@ -77,8 +80,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\CatalogSearch\Model\Advanced\Request\Builder $requestBuilder * @param \Magento\Search\Model\SearchEngine $searchEngine * @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory - * @param \Zend_Db_Adapter_Abstract $connection - * @param SearchResultFactory $searchResultFactory + * @param null $connection + * @param SearchResultFactory|null $searchResultFactory + * @param ProductLimitationFactory|null $productLimitationFactory + * @param MetadataPool|null $metadataPool + * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -105,7 +111,9 @@ public function __construct( \Magento\Search\Model\SearchEngine $searchEngine, \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory, $connection = null, - SearchResultFactory $searchResultFactory = null + SearchResultFactory $searchResultFactory = null, + ProductLimitationFactory $productLimitationFactory = null, + MetadataPool $metadataPool = null ) { $this->requestBuilder = $requestBuilder; $this->searchEngine = $searchEngine; @@ -134,7 +142,9 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $connection + $connection, + $productLimitationFactory, + $metadataPool ); } diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php index 8a9e2b1917407..bc7568a471160 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php @@ -7,6 +7,7 @@ use Magento\CatalogSearch\Model\Search\RequestGenerator; use Magento\Framework\DB\Select; +use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Exception\StateException; use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage; use Magento\Framework\Search\Response\QueryResponse; @@ -15,6 +16,7 @@ use Magento\Framework\Api\Search\SearchResultFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\App\ObjectManager; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * Fulltext Collection @@ -94,6 +96,8 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection private $filterBuilder; /** + * Collection constructor + * * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy @@ -117,9 +121,12 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Framework\Search\Request\Builder $requestBuilder * @param \Magento\Search\Model\SearchEngine $searchEngine * @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory - * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection + * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection * @param string $searchRequestName - * @param SearchResultFactory $searchResultFactory + * @param SearchResultFactory|null $searchResultFactory + * @param ProductLimitationFactory|null $productLimitationFactory + * @param MetadataPool|null $metadataPool + * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -148,7 +155,9 @@ public function __construct( \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, $searchRequestName = 'catalog_view_container', - SearchResultFactory $searchResultFactory = null + SearchResultFactory $searchResultFactory = null, + ProductLimitationFactory $productLimitationFactory = null, + MetadataPool $metadataPool = null ) { $this->queryFactory = $catalogSearchData; if ($searchResultFactory === null) { @@ -175,7 +184,9 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $connection + $connection, + $productLimitationFactory, + $metadataPool ); $this->requestBuilder = $requestBuilder; $this->searchEngine = $searchEngine; diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php index a205d23ee2ac9..4b04e7d66ab56 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php @@ -77,11 +77,6 @@ protected function setUp() $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); $productLimitationFactoryMock->method('create') ->willReturn($productLimitationMock); - $this->mockObjectManager( - [ - ProductLimitationFactory::class => $productLimitationFactoryMock, - ] - ); $this->advancedCollection = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class, @@ -93,17 +88,11 @@ protected function setUp() 'filterBuilder' => $this->filterBuilder, 'temporaryStorageFactory' => $this->temporaryStorageFactory, 'search' => $this->search, + 'productLimitationFactory' => $productLimitationFactoryMock, ] ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - public function testLoadWithFilterNoFilters() { $this->advancedCollection->loadWithFilter(); @@ -167,23 +156,4 @@ protected function getCriteriaBuilder() ->getMock(); return $criteriaBuilder; } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php index ae820a9d3aa8c..5e56a25356359 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php @@ -83,11 +83,6 @@ protected function setUp() $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); $productLimitationFactoryMock->method('create') ->willReturn($productLimitationMock); - $this->mockObjectManager( - [ - ProductLimitationFactory::class => $productLimitationFactoryMock, - ] - ); $this->temporaryStorage = $this->getMockBuilder(\Magento\Framework\Search\Adapter\Mysql\TemporaryStorage::class) ->disableOriginalConstructor() @@ -106,7 +101,8 @@ protected function setUp() 'storeManager' => $this->storeManager, 'universalFactory' => $this->universalFactory, 'scopeConfig' => $this->scopeConfig, - 'temporaryStorageFactory' => $temporaryStorageFactory + 'temporaryStorageFactory' => $temporaryStorageFactory, + 'productLimitationFactory' => $productLimitationFactoryMock ] ); @@ -230,23 +226,4 @@ protected function createFilter() ->getMock(); return $filter; } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogWidget/Model/Rule.php b/app/code/Magento/CatalogWidget/Model/Rule.php index 8258d3f1a8b4f..3975ce1c683df 100644 --- a/app/code/Magento/CatalogWidget/Model/Rule.php +++ b/app/code/Magento/CatalogWidget/Model/Rule.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ namespace Magento\CatalogWidget\Model; +use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\Api\ExtensionAttributesFactory; /** @@ -17,6 +19,8 @@ class Rule extends \Magento\Rule\Model\AbstractModel protected $conditionsFactory; /** + * Rule constructor + * * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Data\FormFactory $formFactory @@ -25,6 +29,9 @@ class Rule extends \Magento\Rule\Model\AbstractModel * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param array $data + * @param ExtensionAttributesFactory|null $extensionFactory + * @param AttributeValueFactory|null $customAttributeFactory + * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -35,7 +42,9 @@ public function __construct( \Magento\CatalogWidget\Model\Rule\Condition\CombineFactory $conditionsFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + ExtensionAttributesFactory $extensionFactory = null, + AttributeValueFactory $customAttributeFactory = null ) { $this->conditionsFactory = $conditionsFactory; parent::__construct( @@ -45,7 +54,9 @@ public function __construct( $localeDate, $resource, $resourceCollection, - $data + $data, + $extensionFactory, + $customAttributeFactory ); } diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index 5312dadf13c85..42d8570f0472a 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -30,13 +30,6 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->mockObjectManager([ - \Magento\Framework\Api\ExtensionAttributesFactory::class => - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), - \Magento\Framework\Api\AttributeValueFactory::class => - $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ]); - $this->rule = $this->objectManager->getObject( \Magento\CatalogWidget\Model\Rule::class, [ @@ -45,13 +38,6 @@ protected function setUp() ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - public function testGetConditionsInstance() { $condition = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\Combine::class) @@ -67,22 +53,4 @@ public function testGetActionsInstance() $this->assertNull($this->rule->getActionsInstance()); } - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/Cron/Model/Config/Data.php b/app/code/Magento/Cron/Model/Config/Data.php index d09b830d1b539..3edc2e6237de8 100644 --- a/app/code/Magento/Cron/Model/Config/Data.php +++ b/app/code/Magento/Cron/Model/Config/Data.php @@ -9,23 +9,27 @@ */ namespace Magento\Cron\Model\Config; +use Magento\Framework\Serialize\SerializerInterface; + class Data extends \Magento\Framework\Config\Data { /** - * Initialize parameters + * Data constructor * - * @param \Magento\Cron\Model\Config\Reader\Xml $reader - * @param \Magento\Framework\Config\CacheInterface $cache - * @param \Magento\Cron\Model\Config\Reader\Db $dbReader - * @param string $cacheId + * @param Reader\Xml $reader + * @param \Magento\Framework\Config\CacheInterface $cache + * @param Reader\Db $dbReader + * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Cron\Model\Config\Reader\Xml $reader, \Magento\Framework\Config\CacheInterface $cache, \Magento\Cron\Model\Config\Reader\Db $dbReader, - $cacheId = 'crontab_config_cache' + $cacheId = 'crontab_config_cache', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); $this->merge($dbReader->get()); } diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 4e78c15c4f373..e25fdfe71d2d1 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -7,32 +7,6 @@ class DataTest extends \PHPUnit_Framework_TestCase { - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - /** * Testing return jobs from different sources (DB, XML) */ @@ -64,14 +38,10 @@ public function testGetJobs() $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] - ); - $serializerMock->method('unserialize') ->willReturn($jobs); - $configData = new \Magento\Cron\Model\Config\Data($reader, $cache, $dbReader, 'test_cache_id'); + $configData = new \Magento\Cron\Model\Config\Data($reader, $cache, $dbReader, 'test_cache_id', $serializerMock); $expected = [ 'job1' => ['schedule' => '* * * * *', 'instance' => 'JobModel1', 'method' => 'method1'], diff --git a/app/code/Magento/Customer/Model/Address/Config.php b/app/code/Magento/Customer/Model/Address/Config.php index cd76fd253f499..f53f9277cfa4f 100644 --- a/app/code/Magento/Customer/Model/Address/Config.php +++ b/app/code/Magento/Customer/Model/Address/Config.php @@ -7,6 +7,7 @@ use Magento\Framework\Config\Data as ConfigData; use Magento\Framework\DataObject; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Store\Model\ScopeInterface; /** @@ -60,12 +61,15 @@ class Config extends ConfigData protected $_scopeConfig; /** - * @param \Magento\Customer\Model\Address\Config\Reader $reader + * Config constructor + * + * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Customer\Helper\Address $addressHelper * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Customer\Model\Address\Config\Reader $reader, @@ -73,9 +77,10 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Customer\Helper\Address $addressHelper, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - $cacheId = 'address_format' + $cacheId = 'address_format', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); $this->_storeManager = $storeManager; $this->_addressHelper = $addressHelper; $this->_scopeConfig = $scopeConfig; diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index 9209a96101cfe..ff028e2f7340b 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -101,10 +101,6 @@ protected function setUp() ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - $this->serializerMock->method('serialize') ->willReturn(json_encode($fixtureConfigData)); $this->serializerMock->method('unserialize') @@ -116,36 +112,11 @@ protected function setUp() $this->_storeManagerMock, $this->_addressHelperMock, $this->_scopeConfigMock, - $this->_cacheId + $this->_cacheId, + $this->serializerMock ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - public function testGetStore() { $this->assertEquals($this->_storeMock, $this->_model->getStore()); diff --git a/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php b/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php index 879eccd987919..10a27b6a1b367 100644 --- a/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php +++ b/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php @@ -5,16 +5,22 @@ */ namespace Magento\Directory\Model\Country\Postcode\Config; +use Magento\Framework\Serialize\SerializerInterface; + class Data extends \Magento\Framework\Config\Data { /** - * @param \Magento\Directory\Model\Country\Postcode\Config\Reader $reader + * Data constructor + * + * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache + * @param SerializerInterface $serializer */ public function __construct( \Magento\Directory\Model\Country\Postcode\Config\Reader $reader, - \Magento\Framework\Config\CacheInterface $cache + \Magento\Framework\Config\CacheInterface $cache, + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, 'country_postcodes'); + parent::__construct($reader, $cache, 'country_postcodes', $serializer); } } diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 4581c088e4bba..20444b6080add 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -39,9 +39,6 @@ protected function setUp() false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); } public function testGet() @@ -53,33 +50,11 @@ public function testGet() $this->serializerMock->expects($this->once()) ->method('unserialize') ->willReturn($expected); - $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data($this->readerMock, $this->cacheMock); + $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data( + $this->readerMock, + $this->cacheMock, + $this->serializerMock + ); $this->assertEquals($expected, $configData->get()); } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } diff --git a/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php b/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php index a641093f54540..c84245fa0b4a4 100644 --- a/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php +++ b/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php @@ -7,7 +7,7 @@ */ namespace Magento\DownloadableImportExport\Model\Import\Product\Type; -use Magento\CatalogImportExport\Model\Import\Product; +use Magento\Framework\EntityManager\MetadataPool; use \Magento\Store\Model\Store; /** @@ -244,7 +244,7 @@ class Downloadable extends \Magento\CatalogImportExport\Model\Import\Product\Typ protected $downloadableHelper; /** - * Constructor + * Downloadable constructor * * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac @@ -252,7 +252,7 @@ class Downloadable extends \Magento\CatalogImportExport\Model\Import\Product\Typ * @param array $params * @param \Magento\DownloadableImportExport\Helper\Uploader $uploaderHelper * @param \Magento\DownloadableImportExport\Helper\Data $downloadableHelper - * @throws \Magento\Framework\Exception\LocalizedException + * @param MetadataPool $metadataPool */ public function __construct( \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac, @@ -260,9 +260,10 @@ public function __construct( \Magento\Framework\App\ResourceConnection $resource, array $params, \Magento\DownloadableImportExport\Helper\Uploader $uploaderHelper, - \Magento\DownloadableImportExport\Helper\Data $downloadableHelper + \Magento\DownloadableImportExport\Helper\Data $downloadableHelper, + MetadataPool $metadataPool ) { - parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params); + parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params, $metadataPool); $this->parameters = $this->_entityModel->getParameters(); $this->_resource = $resource; $this->connection = $resource->getConnection('write'); diff --git a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php index 592be58c9b4a5..c4799bcc42ae9 100644 --- a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php +++ b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php @@ -710,10 +710,6 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) ->getMock(\Magento\Framework\EntityManager\MetadataPool::class, ['getLinkField'], [], '', false); $metadataPoolMock->expects($this->any())->method('getMetadata')->willReturnSelf(); - $this->mockObjectManager([ - \Magento\Framework\EntityManager\MetadataPool::class => $metadataPoolMock - ]); - $this->downloadableModelMock = $this->objectManagerHelper->getObject( \Magento\DownloadableImportExport\Model\Import\Product\Type\Downloadable::class, [ @@ -722,7 +718,8 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) 'resource' => $this->resourceMock, 'params' => $this->paramsArray, 'uploaderHelper' => $this->uploaderHelper, - 'downloadableHelper' => $this->downloadableHelper + 'downloadableHelper' => $this->downloadableHelper, + 'metadataPool' => $metadataPoolMock, ] ); $this->entityModelMock->expects($this->once())->method('getNewSku')->will($this->returnValue($newSku)); @@ -736,33 +733,6 @@ public function testSetUploaderDirFalse($newSku, $bunch, $allowImport) $this->downloadableModelMock->saveData(); } - protected function tearDown() - { - parent::tearDown(); - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - /** * Data for methods testSetUploaderDirFalse, testSetDestDirFalse, testDirWithoutPermissions * diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Config.php b/app/code/Magento/Eav/Model/Entity/Attribute/Config.php index ae5c2f5e2339f..9dbba7c5bac4e 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Config.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Config.php @@ -5,20 +5,25 @@ */ namespace Magento\Eav\Model\Entity\Attribute; +use Magento\Framework\Serialize\SerializerInterface; + class Config extends \Magento\Framework\Config\Data { /** - * @param \Magento\Eav\Model\Entity\Attribute\Config\Reader $reader + * Config constructor + * + * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId - * @codeCoverageIgnore + * @param SerializerInterface $serializer */ public function __construct( \Magento\Eav\Model\Entity\Attribute\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = "eav_attributes" + $cacheId = "eav_attributes", + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); } /** diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index 90ac040af5be7..5ae74a16b27a9 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -60,45 +60,17 @@ protected function setUp() ->willReturn(''); $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $serializerMock->method('unserialize') ->willReturn([]); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] - ); + $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( $this->_readerMock, $this->_cacheMock, - $this->_cacheId + $this->_cacheId, + $serializerMock ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - public function testGetLockedFieldsEmpty() { $this->_entityType->expects($this->once())->method('getEntityTypeCode')->will($this->returnValue('test_code')); diff --git a/app/code/Magento/ImportExport/Model/Export/Config.php b/app/code/Magento/ImportExport/Model/Export/Config.php index 267a7e6c7a9e1..375aa7fa468b8 100644 --- a/app/code/Magento/ImportExport/Model/Export/Config.php +++ b/app/code/Magento/ImportExport/Model/Export/Config.php @@ -5,19 +5,25 @@ */ namespace Magento\ImportExport\Model\Export; +use Magento\Framework\Serialize\SerializerInterface; + class Config extends \Magento\Framework\Config\Data implements \Magento\ImportExport\Model\Export\ConfigInterface { /** - * @param \Magento\ImportExport\Model\Export\Config\Reader $reader + * Config constructor + * + * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\ImportExport\Model\Export\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = 'export_config_cache' + $cacheId = 'export_config_cache', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); } /** diff --git a/app/code/Magento/ImportExport/Model/Import/Config.php b/app/code/Magento/ImportExport/Model/Import/Config.php index 0652dd03d9ff4..88a607c0ca0d5 100644 --- a/app/code/Magento/ImportExport/Model/Import/Config.php +++ b/app/code/Magento/ImportExport/Model/Import/Config.php @@ -5,19 +5,25 @@ */ namespace Magento\ImportExport\Model\Import; +use Magento\Framework\Serialize\SerializerInterface; + class Config extends \Magento\Framework\Config\Data implements \Magento\ImportExport\Model\Import\ConfigInterface { /** - * @param \Magento\ImportExport\Model\Import\Config\Reader $reader + * Config constructor + * + * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\ImportExport\Model\Import\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = 'import_config_cache' + $cacheId = 'import_config_cache', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); } /** diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index 9e025c4a11e2d..0b1e542f85b9d 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -10,12 +10,12 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\ImportExport\Model\Export\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $readerMock; + private $readerMock; /** * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; /** * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject @@ -25,12 +25,12 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $cacheId = 'some_id'; + private $cacheId = 'some_id'; /** * @var \Magento\ImportExport\Model\Export\Config */ - protected $model; + private $model; protected function setUp() { @@ -43,35 +43,6 @@ protected function setUp() ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -94,7 +65,8 @@ public function testGetEntities($value, $expected) $this->model = new \Magento\ImportExport\Model\Export\Config( $this->readerMock, $this->cacheMock, - $this->cacheId + $this->cacheId, + $this->serializerMock ); $this->assertEquals($expected, $this->model->getEntities('entities')); } @@ -128,7 +100,8 @@ public function testGetEntityTypes($configData, $entity, $expectedResult) $this->model = new \Magento\ImportExport\Model\Export\Config( $this->readerMock, $this->cacheMock, - $this->cacheId + $this->cacheId, + $this->serializerMock ); $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity)); } @@ -181,7 +154,8 @@ public function testGetFileFormats($value, $expected) $this->model = new \Magento\ImportExport\Model\Export\Config( $this->readerMock, $this->cacheMock, - $this->cacheId + $this->cacheId, + $this->serializerMock ); $this->assertEquals($expected, $this->model->getFileFormats('fileFormats')); } diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php index c3f9f6c81cf67..60cd1f50238a4 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php @@ -43,35 +43,6 @@ protected function setUp() ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -94,7 +65,8 @@ public function testGetEntities($value, $expected) $this->model = new \Magento\ImportExport\Model\Import\Config( $this->readerMock, $this->cacheMock, - $this->cacheId + $this->cacheId, + $this->serializerMock ); $this->assertEquals($expected, $this->model->getEntities('entities')); } @@ -128,7 +100,8 @@ public function testGetEntityTypes($configData, $entity, $expectedResult) $this->model = new \Magento\ImportExport\Model\Import\Config( $this->readerMock, $this->cacheMock, - $this->cacheId + $this->cacheId, + $this->serializerMock ); $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity)); } diff --git a/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php b/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php index f4945508fe722..f066bb53b51ac 100644 --- a/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php +++ b/app/code/Magento/Review/Model/ResourceModel/Review/Product/Collection.php @@ -7,6 +7,8 @@ use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Framework\DB\Select; +use Magento\Framework\EntityManager\MetadataPool; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * Review Product Collection @@ -59,7 +61,8 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection protected $_voteFactory; /** - * Collection constructor. + * Collection constructor + * * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy @@ -81,7 +84,9 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement * @param \Magento\Review\Model\RatingFactory $ratingFactory * @param \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory - * @param mixed $connection + * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection + * @param ProductLimitationFactory|null $productLimitationFactory + * @param MetadataPool|null $metadataPool * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -107,7 +112,9 @@ public function __construct( \Magento\Customer\Api\GroupManagementInterface $groupManagement, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null + \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, + ProductLimitationFactory $productLimitationFactory = null, + MetadataPool $metadataPool = null ) { $this->_ratingFactory = $ratingFactory; $this->_voteFactory = $voteFactory; @@ -131,7 +138,9 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $connection + $connection, + $productLimitationFactory, + $metadataPool ); } diff --git a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php index d2a959e0a3a00..8c51535a7f6ab 100644 --- a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php +++ b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php @@ -87,11 +87,6 @@ protected function setUp() $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); $productLimitationFactoryMock->method('create') ->willReturn($productLimitationMock); - $this->mockObjectManager( - [ - ProductLimitationFactory::class => $productLimitationFactoryMock, - ] - ); $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->model = $this->objectManager->getObject( \Magento\Review\Model\ResourceModel\Review\Product\Collection::class, @@ -99,37 +94,12 @@ protected function setUp() 'universalFactory' => $universalFactory, 'storeManager' => $storeManager, 'eavConfig' => $eavConfig, - 'fetchStrategy' => $fetchStrategy + 'fetchStrategy' => $fetchStrategy, + 'productLimitationFactory' => $productLimitationFactoryMock ] ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - /** * @dataProvider addAttributeToFilterDataProvider * @param $attribute diff --git a/app/code/Magento/Rule/Model/AbstractModel.php b/app/code/Magento/Rule/Model/AbstractModel.php index 45f0c092c4aec..9924abf1a060c 100644 --- a/app/code/Magento/Rule/Model/AbstractModel.php +++ b/app/code/Magento/Rule/Model/AbstractModel.php @@ -5,6 +5,8 @@ */ namespace Magento\Rule\Model; +use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\Api\ExtensionAttributesFactory; /** * Abstract Rule entity data model @@ -76,7 +78,7 @@ abstract public function getActionsInstance(); protected $_localeDate; /** - * AbstractModel constructor. + * AbstractModel constructor * * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry @@ -85,6 +87,8 @@ abstract public function getActionsInstance(); * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param array $data + * @param ExtensionAttributesFactory|null $extensionFactory + * @param AttributeValueFactory|null $customAttributeFactory */ public function __construct( \Magento\Framework\Model\Context $context, @@ -93,15 +97,17 @@ public function __construct( \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + ExtensionAttributesFactory $extensionFactory = null, + AttributeValueFactory $customAttributeFactory = null ) { $this->_formFactory = $formFactory; $this->_localeDate = $localeDate; parent::__construct( $context, $registry, - $this->getExtensionFactory(), - $this->getCustomAttributeFactory(), + $extensionFactory ?: $this->getExtensionFactory(), + $customAttributeFactory ?: $this->getCustomAttributeFactory(), $resource, $resourceCollection, $data diff --git a/app/code/Magento/Sales/Model/Config/Data.php b/app/code/Magento/Sales/Model/Config/Data.php index 6d3d3a2ac1234..ddcb95efdfda6 100644 --- a/app/code/Magento/Sales/Model/Config/Data.php +++ b/app/code/Magento/Sales/Model/Config/Data.php @@ -9,18 +9,24 @@ */ namespace Magento\Sales\Model\Config; +use Magento\Framework\Serialize\SerializerInterface; + class Data extends \Magento\Framework\Config\Data { /** - * @param \Magento\Sales\Model\Config\Reader $reader + * Data constructor + * + * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Sales\Model\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = 'sales_totals_config_cache' + $cacheId = 'sales_totals_config_cache', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); } } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index 606efadb6ef52..0c342ff115eaf 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -7,6 +7,11 @@ class DataTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -24,6 +29,7 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMockBuilder( \Magento\Sales\Model\Config\Reader::class )->disableOriginalConstructor()->getMock(); @@ -31,35 +37,6 @@ protected function setUp() \Magento\Framework\App\Cache\Type\Config::class )->disableOriginalConstructor()->getMock(); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGet() @@ -72,7 +49,14 @@ public function testGet() ->method('unserialize') ->willReturn($expected); - $configData = new \Magento\Sales\Model\Config\Data($this->_readerMock, $this->_cacheMock); + $configData = $this->objectManager->getObject( + \Magento\Sales\Model\Config\Data::class, + [ + 'reader' => $this->_readerMock, + 'cache' => $this->_cacheMock, + 'serializer' => $this->serializerMock, + ] + ); $this->assertEquals($expected, $configData->get()); } diff --git a/app/code/Magento/SalesRule/Model/Rule.php b/app/code/Magento/SalesRule/Model/Rule.php index 3253c040ac9f9..033c0ebd5be90 100644 --- a/app/code/Magento/SalesRule/Model/Rule.php +++ b/app/code/Magento/SalesRule/Model/Rule.php @@ -5,6 +5,8 @@ */ namespace Magento\SalesRule\Model; +use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\Api\ExtensionAttributesFactory; use Magento\Quote\Model\Quote\Address; /** @@ -171,6 +173,8 @@ class Rule extends \Magento\Rule\Model\AbstractModel protected $_storeManager; /** + * Rule constructor + * * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Data\FormFactory $formFactory @@ -184,6 +188,9 @@ class Rule extends \Magento\Rule\Model\AbstractModel * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param array $data + * @param ExtensionAttributesFactory|null $extensionFactory + * @param AttributeValueFactory|null $customAttributeFactory + * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -199,7 +206,9 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + ExtensionAttributesFactory $extensionFactory = null, + AttributeValueFactory $customAttributeFactory = null ) { $this->_couponFactory = $couponFactory; $this->_codegenFactory = $codegenFactory; @@ -214,7 +223,9 @@ public function __construct( $localeDate, $resource, $resourceCollection, - $data + $data, + $extensionFactory, + $customAttributeFactory ); } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index c17c6dfe98bc1..b5b33fb8dc39e 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -64,13 +64,6 @@ protected function setUp() ->setMethods(['create']) ->getMock(); - $this->mockObjectManager([ - \Magento\Framework\Api\ExtensionAttributesFactory::class => - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), - \Magento\Framework\Api\AttributeValueFactory::class => - $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ]); - $this->model = $this->objectManager->getObject( \Magento\SalesRule\Model\Rule::class, [ @@ -81,32 +74,6 @@ protected function setUp() ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - public function testLoadCouponCode() { $this->coupon->expects($this->once()) diff --git a/lib/internal/Magento/Framework/App/Config/Initial.php b/lib/internal/Magento/Framework/App/Config/Initial.php index 84fd2bfa52d70..5669041ee98d2 100644 --- a/lib/internal/Magento/Framework/App/Config/Initial.php +++ b/lib/internal/Magento/Framework/App/Config/Initial.php @@ -37,19 +37,25 @@ class Initial private $serializer; /** - * @param \Magento\Framework\App\Config\Initial\Reader $reader + * Initial constructor + * + * @param Initial\Reader $reader * @param \Magento\Framework\App\Cache\Type\Config $cache + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\App\Config\Initial\Reader $reader, - \Magento\Framework\App\Cache\Type\Config $cache + \Magento\Framework\App\Cache\Type\Config $cache, + SerializerInterface $serializer = null ) { + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); $data = $cache->load(self::CACHE_ID); if (!$data) { $data = $reader->read(); - $cache->save($this->getSerializer()->serialize($data), self::CACHE_ID); + $cache->save($this->serializer->serialize($data), self::CACHE_ID); } else { - $data = $this->getSerializer()->unserialize($data); + $data = $this->serializer->unserialize($data); } $this->_data = $data['data']; $this->_metadata = $data['metadata']; @@ -82,19 +88,4 @@ public function getMetadata() { return $this->_metadata; } - - /** - * 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; - } } diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index c31059a146224..daadfee942793 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -43,27 +43,32 @@ class ActionList private $serializer; /** + * ActionList constructor + * * @param \Magento\Framework\Config\CacheInterface $cache * @param ModuleReader $moduleReader - * @param string $actionInterface + * @param $actionInterface * @param string $cacheKey * @param array $reservedWords + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Config\CacheInterface $cache, ModuleReader $moduleReader, $actionInterface = \Magento\Framework\App\ActionInterface::class, $cacheKey = 'app_action_list', - $reservedWords = [] + $reservedWords = [], + SerializerInterface $serializer = null ) { $this->reservedWords = array_merge($reservedWords, $this->reservedWords); $this->actionInterface = $actionInterface; + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class); $data = $cache->load($cacheKey); if (!$data) { $this->actions = $moduleReader->getActionFiles(); - $cache->save($this->getSerializer()->serialize($this->actions), $cacheKey); + $cache->save($this->serializer->serialize($this->actions), $cacheKey); } else { - $this->actions = $this->getSerializer()->unserialize($data); + $this->actions = $this->serializer->unserialize($data); } } @@ -99,18 +104,4 @@ public function get($module, $area, $namespace, $action) } return null; } - - /** - * Get serializer - * - * @return SerializerInterface - * @deprecated - */ - private 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/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index 2d7f61da267a0..941a670b1b44c 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -52,44 +52,15 @@ protected function setUp() $serializerMock->method('unserialize') ->willReturn($this->data); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] - ); - $this->config = $this->objectManager->getObject( \Magento\Framework\App\Config\Initial::class, [ 'cache' => $this->cacheMock, + 'serializer' => $serializerMock, ] ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - /** * @param string $scope * @param array $expected diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 28764c86b35a2..bef703e99a33e 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -50,35 +50,6 @@ protected function setUp() false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\Serializer\Serialize::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructActionsCached() @@ -203,6 +174,7 @@ private function createActionListInstance() [ 'cache' => $this->cacheMock, 'moduleReader' => $this->readerMock, + 'serializer' => $this->serializerMock, ] ); } diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 7598596dc9f04..5801de6dbaa01 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -70,20 +70,23 @@ class Data implements \Magento\Framework\Config\DataInterface protected $serializer; /** - * Constructor + * Data constructor * * @param ReaderInterface $reader * @param CacheInterface $cache - * @param string $cacheId + * @param $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( ReaderInterface $reader, CacheInterface $cache, - $cacheId + $cacheId, + SerializerInterface $serializer = null ) { $this->reader = $reader; $this->cache = $cache; $this->cacheId = $cacheId; + $this->serializer = $serializer ?: $this->getSerializer(); $this->initData(); } diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index d527f1b0da4b5..5ad20aead09bd 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -30,33 +30,6 @@ protected function setUp() $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetConfigNotCached() @@ -75,7 +48,8 @@ public function testGetConfigNotCached() $config = new \Magento\Framework\Config\Data( $this->readerMock, $this->cacheMock, - $cacheId + $cacheId, + $this->serializerMock ); $this->assertEquals($data, $config->get()); $this->assertEquals('b', $config->get('a')); @@ -100,7 +74,8 @@ public function testGetConfigCached() $config = new \Magento\Framework\Config\Data( $this->readerMock, $this->cacheMock, - $cacheId + $cacheId, + $this->serializerMock ); $this->assertEquals($data, $config->get()); $this->assertEquals('b', $config->get('a')); @@ -123,7 +98,8 @@ public function testReset() $config = new \Magento\Framework\Config\Data( $this->readerMock, $this->cacheMock, - $cacheId + $cacheId, + $this->serializerMock ); $config->reset(); } diff --git a/lib/internal/Magento/Framework/Interception/Config/Config.php b/lib/internal/Magento/Framework/Interception/Config/Config.php index 25039b5e09c83..0b30b2619c069 100644 --- a/lib/internal/Magento/Framework/Interception/Config/Config.php +++ b/lib/internal/Magento/Framework/Interception/Config/Config.php @@ -79,6 +79,8 @@ class Config implements \Magento\Framework\Interception\ConfigInterface private $serializer; /** + * Config constructor + * * @param \Magento\Framework\Config\ReaderInterface $reader * @param \Magento\Framework\Config\ScopeListInterface $scopeList * @param \Magento\Framework\Cache\FrontendInterface $cache @@ -86,6 +88,7 @@ class Config implements \Magento\Framework\Interception\ConfigInterface * @param \Magento\Framework\Interception\ObjectManager\ConfigInterface $omConfig * @param \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Config\ReaderInterface $reader, @@ -94,7 +97,8 @@ public function __construct( \Magento\Framework\ObjectManager\RelationsInterface $relations, \Magento\Framework\Interception\ObjectManager\ConfigInterface $omConfig, \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions, - $cacheId = 'interception' + $cacheId = 'interception', + SerializerInterface $serializer = null ) { $this->_omConfig = $omConfig; $this->_relations = $relations; @@ -103,10 +107,11 @@ public function __construct( $this->_cacheId = $cacheId; $this->_reader = $reader; $this->_scopeList = $scopeList; - + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(Serialize::class); $intercepted = $this->_cache->load($this->_cacheId); if ($intercepted !== false) { - $this->_intercepted = $this->getSerializer()->unserialize($intercepted); + $this->_intercepted = $this->serializer->unserialize($intercepted); } else { $this->initialize($this->_classDefinitions->getClasses()); } @@ -137,7 +142,7 @@ public function initialize($classDefinitions = []) foreach ($classDefinitions as $class) { $this->hasPlugins($class); } - $this->_cache->save($this->getSerializer()->serialize($this->_intercepted), $this->_cacheId); + $this->_cache->save($this->serializer->serialize($this->_intercepted), $this->_cacheId); } /** @@ -183,19 +188,4 @@ public function hasPlugins($type) } return $this->_inheritInterception($type); } - - /** - * Get serializer - * - * @return SerializerInterface - * @deprecated - */ - private function getSerializer() - { - if ($this->serializer === null) { - $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 7a2682d007959..c858bcb8812c4 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -78,33 +78,6 @@ protected function setUp() ); $this->serializerMock = $this->getMock(SerializerInterface::class); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->mockObjectManager([Serialize::class => $this->serializerMock]); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -183,6 +156,7 @@ public function testHasPluginsWhenDataIsNotCached($expectedResult, $type, $entit 'relations' => $this->relationsMock, 'omConfig' => $this->omConfigMock, 'classDefinitions' => $this->definitionMock, + 'serializer' => $this->serializerMock, ] ); @@ -232,6 +206,7 @@ public function testHasPluginsWhenDataIsCached($expectedResult, $type) 'omConfig' => $this->omConfigMock, 'classDefinitions' => $this->definitionMock, 'cacheId' => $cacheId, + 'serializer' => $this->serializerMock, ] ); diff --git a/lib/internal/Magento/Framework/Mview/Config/Data.php b/lib/internal/Magento/Framework/Mview/Config/Data.php index d2e4ee91bea2d..d65270be09ea0 100644 --- a/lib/internal/Magento/Framework/Mview/Config/Data.php +++ b/lib/internal/Magento/Framework/Mview/Config/Data.php @@ -5,6 +5,8 @@ */ namespace Magento\Framework\Mview\Config; +use Magento\Framework\Serialize\SerializerInterface; + class Data extends \Magento\Framework\Config\Data { /** @@ -13,22 +15,26 @@ class Data extends \Magento\Framework\Config\Data protected $stateCollection; /** - * @param \Magento\Framework\Mview\Config\Reader $reader + * Data constructor + * + * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Framework\Mview\View\State\CollectionInterface $stateCollection * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Mview\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, \Magento\Framework\Mview\View\State\CollectionInterface $stateCollection, - $cacheId = 'mview_config' + $cacheId = 'mview_config', + SerializerInterface $serializer = null ) { $this->stateCollection = $stateCollection; $isCacheExists = $cache->test($cacheId); - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); if (!$isCacheExists) { $this->deleteNonexistentStates(); diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index 8b8b3e737fe95..777c099d9b838 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -65,35 +65,6 @@ protected function setUp() ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructorWithCache() @@ -113,7 +84,8 @@ public function testConstructorWithCache() $this->reader, $this->cache, $this->stateCollection, - $this->cacheId + $this->cacheId, + $this->serializerMock ); } @@ -152,7 +124,8 @@ public function testConstructorWithoutCache() $this->reader, $this->cache, $this->stateCollection, - $this->cacheId + $this->cacheId, + $this->serializerMock ); } } From e8c9ddeae91d605f2e9818e4d9115819c62e61b7 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 04:10:12 -0500 Subject: [PATCH 080/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../ResourceModel/Product/Collection.php | 5 -- .../Type/Configurable/Product/Collection.php | 78 ------------------- 2 files changed, 83 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index 55b04163ae110..3be5e3afdc86e 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -261,11 +261,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac */ protected $metadataPool; - /** - * @var \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory - */ - private $productLimitationFiltersFactory; - /** * Collection constructor * diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php index 5216f13355970..e512c511a871a 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php @@ -25,84 +25,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection */ protected $_linkTable; - /** - * @var MetadataPool - */ - private $metadataPool; - - /** - * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory - * @param \Psr\Log\LoggerInterface $logger - * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy - * @param \Magento\Framework\Event\ManagerInterface $eventManager - * @param \Magento\Eav\Model\Config $eavConfig - * @param \Magento\Framework\App\ResourceConnection $resource - * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory - * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper - * @param \Magento\Framework\Validator\UniversalFactory $universalFactory - * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Magento\Framework\Module\Manager $moduleManager - * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory - * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl - * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate - * @param \Magento\Customer\Model\Session $customerSession - * @param \Magento\Framework\Stdlib\DateTime $dateTime - * @param GroupManagementInterface $groupManagement - * @param MetadataPool $metadataPool - * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection - * - * @SuppressWarnings(PHPMD.ExcessiveParameterList) - */ - public function __construct( - \Magento\Framework\Data\Collection\EntityFactory $entityFactory, - \Psr\Log\LoggerInterface $logger, - \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, - \Magento\Framework\Event\ManagerInterface $eventManager, - \Magento\Eav\Model\Config $eavConfig, - \Magento\Framework\App\ResourceConnection $resource, - \Magento\Eav\Model\EntityFactory $eavEntityFactory, - \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper, - \Magento\Framework\Validator\UniversalFactory $universalFactory, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Framework\Module\Manager $moduleManager, - \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState, - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, - \Magento\Catalog\Model\ResourceModel\Url $catalogUrl, - \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, - \Magento\Customer\Model\Session $customerSession, - \Magento\Framework\Stdlib\DateTime $dateTime, - GroupManagementInterface $groupManagement, - MetadataPool $metadataPool, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null - ) { - $this->metadataPool = $metadataPool; - parent::__construct( - $entityFactory, - $logger, - $fetchStrategy, - $eventManager, - $eavConfig, - $resource, - $eavEntityFactory, - $resourceHelper, - $universalFactory, - $storeManager, - $moduleManager, - $catalogProductFlatState, - $scopeConfig, - $productOptionFactory, - $catalogUrl, - $localeDate, - $customerSession, - $dateTime, - $groupManagement, - $connection - ); - } - /** * Assign link table name * From 949ac03b48c22046d124d1c9ecbe1c3d9eb98fdb Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 04:17:10 -0500 Subject: [PATCH 081/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Model/Import/Product/Type/Downloadable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php b/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php index c84245fa0b4a4..a34276ab5f4c6 100644 --- a/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php +++ b/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php @@ -261,7 +261,7 @@ public function __construct( array $params, \Magento\DownloadableImportExport\Helper\Uploader $uploaderHelper, \Magento\DownloadableImportExport\Helper\Data $downloadableHelper, - MetadataPool $metadataPool + MetadataPool $metadataPool = null ) { parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params, $metadataPool); $this->parameters = $this->_entityModel->getParameters(); From 2d727724b63e75bdc1c645db23380b1814e46a60 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 04:34:22 -0500 Subject: [PATCH 082/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Model/Import/Product/Type/Bundle.php | 32 ------------------- .../Import/Product/Type/AbstractType.php | 10 +++--- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php index 2d0f1264b6b8a..6a99c02af9da6 100644 --- a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php +++ b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php @@ -9,7 +9,6 @@ namespace Magento\BundleImportExport\Model\Import\Product\Type; use \Magento\Bundle\Model\Product\Price as BundlePrice; -use \Magento\BundleImportExport\Model\Export\RowCustomizer; use \Magento\Catalog\Model\Product\Type\AbstractType; /** @@ -55,20 +54,6 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst */ const SELECTION_PRICE_TYPE_PERCENT = 1; - /** - * Instance of database adapter. - * - * @var \Magento\Framework\DB\Adapter\AdapterInterface - */ - protected $connection; - - /** - * Instance of application resource. - * - * @var \Magento\Framework\App\ResourceConnection - */ - protected $_resource; - /** * Array of cached options. * @@ -144,23 +129,6 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst 'multiselect' => 'multi', ]; - /** - * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac - * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac - * @param \Magento\Framework\App\ResourceConnection $resource - * @param array $params - */ - public function __construct( - \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac, - \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac, - \Magento\Framework\App\ResourceConnection $resource, - array $params - ) { - parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params); - $this->_resource = $resource; - $this->connection = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION); - } - /** * Parse selections. * diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 2cb808cc3c3a7..a5f9960969389 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -126,7 +126,7 @@ abstract class AbstractType /** * @var \Magento\Framework\DB\Adapter\AdapterInterface */ - protected $connection; + protected $_connection; /** * Product metadata pool @@ -162,7 +162,7 @@ public function __construct( $this->_attrSetColFac = $attrSetColFac; $this->_prodAttrColFac = $prodAttrColFac; $this->_resource = $resource; - $this->_connection = $resource->getConnection(); + $this->connection = $resource->getConnection(); $this->metadataPool = $metadataPool ?: $this->getMetadataPool(); if ($this->isSuitable()) { if (!isset($params[0]) @@ -252,8 +252,8 @@ protected function _initAttributes() { // temporary storage for attributes' parameters to avoid double querying inside the loop $entityId = $this->_entityModel->getEntityTypeId(); - $entityAttributes = $this->_connection->fetchAll( - $this->_connection->select()->from( + $entityAttributes = $this->connection->fetchAll( + $this->connection->select()->from( ['attr' => $this->_resource->getTableName('eav_entity_attribute')], ['attr.attribute_id'] )->joinLeft( @@ -261,7 +261,7 @@ protected function _initAttributes() 'set.attribute_set_id = attr.attribute_set_id', ['set.attribute_set_name'] )->where( - $this->_connection->quoteInto('attr.entity_type_id IN (?)', $entityId) + $this->connection->quoteInto('attr.entity_type_id IN (?)', $entityId) ) ); $absentKeys = []; From 963fe8b8bd19cc23ea9d166680a75e08f9a3f8bb Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 04:43:10 -0500 Subject: [PATCH 083/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Model/ResourceModel/Product/Link/Product/Collection.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php index a71faa9144066..3fbd6f4822293 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php @@ -356,7 +356,9 @@ private function joinProductsToLinks() $linkField = $metaDataPool->getLinkField(); $entityTable = $metaDataPool->getEntityTable(); $this->getSelect() - ->join(['product_entity_table' => $entityTable], "links.product_id = product_entity_table.$linkField", []); + ->join( + ['product_entity_table' => $entityTable], "links.product_id = product_entity_table.$linkField", [] + ); } } } From d4f191df551b8d64170c543eaf487275df4ece1a Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 05:21:27 -0500 Subject: [PATCH 084/144] MAGETWO-59923: Configuration in \Magento\Indexer\Model\Config\Data contains objects --- app/code/Magento/Indexer/Model/Config/Data.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index 94e9079957a22..d91462674933b 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -5,7 +5,6 @@ */ namespace Magento\Indexer\Model\Config; -use Magento\Framework\Serialize\Serializer\Serialize; use Magento\Framework\Serialize\SerializerInterface; class Data extends \Magento\Framework\Config\Data @@ -53,19 +52,4 @@ protected function deleteNonexistentStates() } } } - - /** - * Get serializer - * - * @return \Magento\Framework\Serialize\SerializerInterface - * @deprecated - */ - protected function getSerializer() - { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(Serialize::class); - } - return $this->serializer; - } } From a8945752637df371e55a54db22c4c33782e1faad Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 09:43:36 -0500 Subject: [PATCH 085/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Model/ResourceModel/Product/Link/Product/Collection.php | 4 +++- .../Model/Import/Product/Type/AbstractType.php | 2 +- app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php | 1 - app/code/Magento/Indexer/Model/Config/Data.php | 3 +++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php index 3fbd6f4822293..6c9dfb6ce635a 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php @@ -357,7 +357,9 @@ private function joinProductsToLinks() $entityTable = $metaDataPool->getEntityTable(); $this->getSelect() ->join( - ['product_entity_table' => $entityTable], "links.product_id = product_entity_table.$linkField", [] + ['product_entity_table' => $entityTable], + "links.product_id = product_entity_table.$linkField", + [] ); } } diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index a5f9960969389..ac39cea10cbe3 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -126,7 +126,7 @@ abstract class AbstractType /** * @var \Magento\Framework\DB\Adapter\AdapterInterface */ - protected $_connection; + protected $connection; /** * Product metadata pool diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index 42d8570f0472a..a6468cc77a47f 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -52,5 +52,4 @@ public function testGetActionsInstance() { $this->assertNull($this->rule->getActionsInstance()); } - } diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index d91462674933b..703c875d1ef4e 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -15,10 +15,13 @@ class Data extends \Magento\Framework\Config\Data protected $stateCollection; /** + * Data constructor + * * @param \Magento\Framework\Indexer\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Indexer\Config\Reader $reader, From 613d4de5a49ad1f9a1b50a8365cebd2caca0e664 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 10:03:03 -0500 Subject: [PATCH 086/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- app/code/Magento/BundleImportExport/composer.json | 1 - app/code/Magento/CatalogWidget/Model/Rule.php | 2 +- app/code/Magento/Rule/Model/AbstractModel.php | 2 +- lib/internal/Magento/Framework/App/Router/ActionList.php | 7 ++++++- lib/internal/Magento/Framework/Config/Data.php | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/BundleImportExport/composer.json b/app/code/Magento/BundleImportExport/composer.json index 3d7900d98287c..d2a754a43cfd4 100644 --- a/app/code/Magento/BundleImportExport/composer.json +++ b/app/code/Magento/BundleImportExport/composer.json @@ -7,7 +7,6 @@ "magento/module-import-export": "100.2.*", "magento/module-catalog-import-export": "100.2.*", "magento/module-bundle": "100.2.*", - "magento/module-eav": "100.2.*", "magento/framework": "100.2.*" }, "type": "magento2-module", diff --git a/app/code/Magento/CatalogWidget/Model/Rule.php b/app/code/Magento/CatalogWidget/Model/Rule.php index 3975ce1c683df..06bc8cba74dd6 100644 --- a/app/code/Magento/CatalogWidget/Model/Rule.php +++ b/app/code/Magento/CatalogWidget/Model/Rule.php @@ -4,10 +4,10 @@ * See COPYING.txt for license details. */ namespace Magento\CatalogWidget\Model; + use Magento\Framework\Api\AttributeValueFactory; use Magento\Framework\Api\ExtensionAttributesFactory; - /** * Class Rule */ diff --git a/app/code/Magento/Rule/Model/AbstractModel.php b/app/code/Magento/Rule/Model/AbstractModel.php index 9924abf1a060c..73e3db2c8a358 100644 --- a/app/code/Magento/Rule/Model/AbstractModel.php +++ b/app/code/Magento/Rule/Model/AbstractModel.php @@ -3,8 +3,8 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Rule\Model; + use Magento\Framework\Api\AttributeValueFactory; use Magento\Framework\Api\ExtensionAttributesFactory; diff --git a/lib/internal/Magento/Framework/App/Router/ActionList.php b/lib/internal/Magento/Framework/App/Router/ActionList.php index daadfee942793..ec46154b2553a 100644 --- a/lib/internal/Magento/Framework/App/Router/ActionList.php +++ b/lib/internal/Magento/Framework/App/Router/ActionList.php @@ -42,12 +42,17 @@ class ActionList */ private $serializer; + /** + * @var string + */ + private $actionInterface; + /** * ActionList constructor * * @param \Magento\Framework\Config\CacheInterface $cache * @param ModuleReader $moduleReader - * @param $actionInterface + * @param string $actionInterface * @param string $cacheKey * @param array $reservedWords * @param SerializerInterface|null $serializer diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 5801de6dbaa01..557f79cfb2791 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -74,7 +74,7 @@ class Data implements \Magento\Framework\Config\DataInterface * * @param ReaderInterface $reader * @param CacheInterface $cache - * @param $cacheId + * @param string $cacheId * @param SerializerInterface|null $serializer */ public function __construct( From 592e59199d632f611058a47408003a5bae76ea40 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 10:21:30 -0500 Subject: [PATCH 087/144] MAGETWO-59444: Create serializer interface and json class in framework --- lib/internal/Magento/Framework/Serialize/Serializer/Json.php | 4 ++++ .../Magento/Framework/Serialize/SerializerInterface.php | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lib/internal/Magento/Framework/Serialize/Serializer/Json.php b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php index 009fee5e3639f..bf7a34c21fbdd 100644 --- a/lib/internal/Magento/Framework/Serialize/Serializer/Json.php +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php @@ -7,6 +7,10 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Class for serializing data to json string and unserializing json string to data + * + */ class Json implements SerializerInterface { /** diff --git a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php index 1dc70da80f394..f7a15b31a826e 100644 --- a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php +++ b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\Serialize; +/** + * Interface for serializing + */ interface SerializerInterface { /** From 70ae2aefcdbe4a3e2ba2888f5860b86e7426c7fb Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 12:29:13 -0500 Subject: [PATCH 088/144] MAGETWO-59441: Refactor Module_Theme --- app/code/Magento/Theme/Model/Design.php | 16 +++++++++++++--- .../testsuite/Magento/Theme/Model/DesignTest.php | 9 +++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Theme/Model/Design.php b/app/code/Magento/Theme/Model/Design.php index 39527afc0250d..55941cfbcacb6 100644 --- a/app/code/Magento/Theme/Model/Design.php +++ b/app/code/Magento/Theme/Model/Design.php @@ -6,9 +6,11 @@ namespace Magento\Theme\Model; use Magento\Framework\App\DesignInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Model\AbstractModel; use Magento\Framework\Model\ResourceModel\AbstractResource; use Magento\Framework\DataObject\IdentityInterface; +use Magento\Framework\Serialize\SerializerInterface; /** * Design settings change model @@ -55,6 +57,11 @@ class Design extends AbstractModel implements IdentityInterface, DesignInterface */ protected $_dateTime; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry @@ -63,6 +70,7 @@ class Design extends AbstractModel implements IdentityInterface, DesignInterface * @param AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\Model\Context $context, @@ -71,10 +79,12 @@ public function __construct( \Magento\Framework\Stdlib\DateTime $dateTime, AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + SerializerInterface $serializer = null ) { $this->_localeDate = $localeDate; $this->_dateTime = $dateTime; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); parent::__construct($context, $registry, $resource, $resourceCollection, $data); } @@ -108,9 +118,9 @@ public function loadChange($storeId, $date = null) if (!$result) { $result = []; } - $this->_cacheManager->save(serialize($result), $changeCacheId, [self::CACHE_TAG], 86400); + $this->_cacheManager->save($this->serializer->serialize($result), $changeCacheId, [self::CACHE_TAG], 86400); } else { - $result = unserialize($result); + $result = $this->serializer->unserialize($result); } if ($result) { diff --git a/dev/tests/integration/testsuite/Magento/Theme/Model/DesignTest.php b/dev/tests/integration/testsuite/Magento/Theme/Model/DesignTest.php index 6cd9746daca33..f450f1b3d0cbe 100644 --- a/dev/tests/integration/testsuite/Magento/Theme/Model/DesignTest.php +++ b/dev/tests/integration/testsuite/Magento/Theme/Model/DesignTest.php @@ -5,6 +5,9 @@ */ namespace Magento\Theme\Model; +use Magento\Backend\Block\Widget\Grid\Serializer; +use Magento\Framework\Serialize\SerializerInterface; + class DesignTest extends \PHPUnit_Framework_TestCase { /** @@ -121,7 +124,8 @@ public function testLoadChangeCache() )->load( $cacheId ); - $cachedDesign = unserialize($cachedDesign); + $serializer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(SerializerInterface::class); + $cachedDesign = $serializer->unserialize($cachedDesign); $this->assertInternalType('array', $cachedDesign); $this->assertArrayHasKey('design', $cachedDesign); @@ -139,7 +143,8 @@ public function testLoadChangeCache() )->load( $cacheId ); - $cachedDesign = unserialize($cachedDesign); + + $cachedDesign = $serializer->unserialize($cachedDesign); $this->assertTrue(is_array($cachedDesign)); $this->assertEquals($cachedDesign['design'], $design->getDesign()); From f7c6465f96ae9e6c4c2dc45ba76d976a5d249a60 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 13:33:03 -0500 Subject: [PATCH 089/144] MAGETWO-59442: Refactor Module_Rss --- app/code/Magento/Rss/Model/Rss.php | 21 +++++++++++--- .../Magento/Rss/Test/Unit/Model/RssTest.php | 29 +++++++++++++------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Rss/Model/Rss.php b/app/code/Magento/Rss/Model/Rss.php index 0f228cb9a21c2..5d78c499cacf7 100644 --- a/app/code/Magento/Rss/Model/Rss.php +++ b/app/code/Magento/Rss/Model/Rss.php @@ -5,7 +5,9 @@ */ namespace Magento\Rss\Model; +use Magento\Framework\App\ObjectManager; use Magento\Framework\App\Rss\DataProviderInterface; +use Magento\Framework\Serialize\SerializerInterface; /** * Auth session model @@ -25,11 +27,22 @@ class Rss protected $cache; /** + * @var SerializerInterface + */ + private $serializer; + + /** + * Rss constructor + * * @param \Magento\Framework\App\CacheInterface $cache + * @param SerializerInterface|null $serializer */ - public function __construct(\Magento\Framework\App\CacheInterface $cache) - { + public function __construct( + \Magento\Framework\App\CacheInterface $cache, + SerializerInterface $serializer = null + ) { $this->cache = $cache; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -46,14 +59,14 @@ public function getFeeds() } if ($cache) { - return unserialize($cache); + return $this->serializer->unserialize($cache); } $data = $this->dataProvider->getRssData(); if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) { $this->cache->save( - serialize($data), + $this->serializer->serialize($data), $this->dataProvider->getCacheKey(), ['rss'], $this->dataProvider->getCacheLifetime() diff --git a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php index 7a12c4818e71a..c6da22778532f 100644 --- a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php +++ b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php @@ -6,6 +6,7 @@ namespace Magento\Rss\Test\Unit\Model; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; class RssTest extends \PHPUnit_Framework_TestCase @@ -40,17 +41,23 @@ class RssTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheInterface; + private $cacheMock; + + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; protected function setUp() { - $this->cacheInterface = $this->getMock(\Magento\Framework\App\CacheInterface::class); - + $this->cacheMock = $this->getMock(\Magento\Framework\App\CacheInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->rss = $this->objectManagerHelper->getObject( \Magento\Rss\Model\Rss::class, [ - 'cache' => $this->cacheInterface + 'cache' => $this->cacheMock, + 'serializer' => $this->serializerMock ] ); } @@ -64,8 +71,9 @@ public function testGetFeeds() $this->rss->setDataProvider($dataProvider); - $this->cacheInterface->expects($this->once())->method('load')->will($this->returnValue(false)); - $this->cacheInterface->expects($this->once())->method('save')->will($this->returnValue(true)); + $this->cacheMock->expects($this->once())->method('load')->will($this->returnValue(false)); + $this->cacheMock->expects($this->once())->method('save')->will($this->returnValue(true)); + $this->serializerMock->expects($this->once())->method('serialize')->willReturn('serializedData'); $this->assertEquals($this->feedData, $this->rss->getFeeds()); } @@ -79,9 +87,12 @@ public function testGetFeedsWithCache() $this->rss->setDataProvider($dataProvider); - $this->cacheInterface->expects($this->once())->method('load') - ->will($this->returnValue(serialize($this->feedData))); - $this->cacheInterface->expects($this->never())->method('save'); + $this->cacheMock->expects($this->once())->method('load') + ->will($this->returnValue('serializedData')); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->willReturn($this->feedData); + $this->cacheMock->expects($this->never())->method('save'); $this->assertEquals($this->feedData, $this->rss->getFeeds()); } From 38d58f8362be0cab32e44ae8121b84f1765dc520 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 14:02:56 -0500 Subject: [PATCH 090/144] MAGETWO-59443: Refactor Framework_App --- .../Magento/Framework/App/Cache/TypeList.php | 17 ++++++-- .../App/Test/Unit/Cache/TypeListTest.php | 40 ++++++++++++++++--- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Cache/TypeList.php b/lib/internal/Magento/Framework/App/Cache/TypeList.php index 1fae9652810c8..38516b7174d6f 100644 --- a/lib/internal/Magento/Framework/App/Cache/TypeList.php +++ b/lib/internal/Magento/Framework/App/Cache/TypeList.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\App\Cache; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; + class TypeList implements TypeListInterface { const INVALIDATED_TYPES = 'core_cache_invalidate'; @@ -29,22 +32,30 @@ class TypeList implements TypeListInterface */ protected $_cache; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param \Magento\Framework\Cache\ConfigInterface $config * @param StateInterface $cacheState * @param InstanceFactory $factory * @param \Magento\Framework\App\CacheInterface $cache + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\Cache\ConfigInterface $config, StateInterface $cacheState, InstanceFactory $factory, - \Magento\Framework\App\CacheInterface $cache + \Magento\Framework\App\CacheInterface $cache, + SerializerInterface $serializer = null ) { $this->_config = $config; $this->_factory = $factory; $this->_cacheState = $cacheState; $this->_cache = $cache; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -72,7 +83,7 @@ protected function _getInvalidatedTypes() { $types = $this->_cache->load(self::INVALIDATED_TYPES); if ($types) { - $types = unserialize($types); + $types = $this->serializer->unserialize($types); } else { $types = []; } @@ -87,7 +98,7 @@ protected function _getInvalidatedTypes() */ protected function _saveInvalidatedTypes($types) { - $this->_cache->save(serialize($types), self::INVALIDATED_TYPES); + $this->_cache->save($this->serializer->serialize($types), self::INVALIDATED_TYPES); } /** diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Cache/TypeListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Cache/TypeListTest.php index a6074f6c45740..d185219becefe 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Cache/TypeListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Cache/TypeListTest.php @@ -7,6 +7,7 @@ namespace Magento\Framework\App\Test\Unit\Cache; use \Magento\Framework\App\Cache\TypeList; +use Magento\Framework\Serialize\SerializerInterface; /** * Test class for \Magento\Framework\App\Cache\TypeList @@ -48,6 +49,11 @@ class TypeListTest extends \PHPUnit_Framework_TestCase */ const CACHE_TYPE = \Magento\Framework\Cache\FrontendInterface::class; + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; + protected function setUp() { $this->_typesArray = [ @@ -85,6 +91,7 @@ protected function setUp() '', false ); + $this->serializerMock = $this->getMock(SerializerInterface::class); $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_typeList = $objectHelper->getObject( @@ -93,7 +100,8 @@ protected function setUp() 'config' => $this->_config, 'cacheState' => $cacheState, 'factory' => $factory, - 'cache' => $this->_cache + 'cache' => $this->_cache, + 'serializer' => $this->serializerMock, ] ); } @@ -118,8 +126,12 @@ public function testGetInvalidated() { $expectation = [self::TYPE_KEY => $this->_getPreparedType()]; $this->_cache->expects($this->once())->method('load')->with(TypeList::INVALIDATED_TYPES)->will( - $this->returnValue(serialize($this->_typesArray)) + $this->returnValue('serializedData') ); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') + ->willReturn($this->_typesArray); $this->assertEquals($expectation, $this->_typeList->getInvalidated()); } @@ -132,8 +144,12 @@ public function testInvalidate() $expectedInvalidated = [ self::TYPE_KEY => 1, ]; + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($expectedInvalidated) + ->willReturn('serializedData'); $this->_cache->expects($this->once())->method('save')->with( - serialize($expectedInvalidated), + 'serializedData', TypeList::INVALIDATED_TYPES ); $this->_typeList->invalidate(self::TYPE_KEY); @@ -147,8 +163,12 @@ public function testInvalidateList() $expectedInvalidated = [ self::TYPE_KEY => 1, ]; + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($expectedInvalidated) + ->willReturn('serializedData'); $this->_cache->expects($this->once())->method('save')->with( - serialize($expectedInvalidated), + 'serializedData', TypeList::INVALIDATED_TYPES ); $this->_typeList->invalidate([self::TYPE_KEY]); @@ -156,15 +176,23 @@ public function testInvalidateList() public function testCleanType() { + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') + ->willReturn($this->_typesArray); $this->_cache->expects($this->once())->method('load')->with(TypeList::INVALIDATED_TYPES)->will( - $this->returnValue(serialize($this->_typesArray)) + $this->returnValue('serializedData') ); $this->_config->expects($this->once())->method('getType')->with(self::TYPE_KEY)->will( $this->returnValue(['instance' => self::CACHE_TYPE]) ); unset($this->_typesArray[self::TYPE_KEY]); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($this->_typesArray) + ->willReturn('serializedData'); $this->_cache->expects($this->once())->method('save')->with( - serialize($this->_typesArray), + 'serializedData', TypeList::INVALIDATED_TYPES ); $this->_typeList->cleanType(self::TYPE_KEY); From da1b7ec3363727796fc2f0cd8a07152dadccf1e5 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 14:19:15 -0500 Subject: [PATCH 091/144] MAGETWO-59441: Refactor Module_Theme --- .../Theme/Test/Unit/Model/DesignTest.php | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/app/code/Magento/Theme/Test/Unit/Model/DesignTest.php b/app/code/Magento/Theme/Test/Unit/Model/DesignTest.php index dc11a4c46eda5..67f86575a8afd 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/DesignTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/DesignTest.php @@ -9,6 +9,7 @@ */ namespace Magento\Theme\Test\Unit\Model; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Theme\Model\Design; class DesignTest extends \PHPUnit_Framework_TestCase @@ -23,11 +24,6 @@ class DesignTest extends \PHPUnit_Framework_TestCase */ protected $cacheManager; - /** - * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject - */ - protected $registry; - /** * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -44,18 +40,15 @@ class DesignTest extends \PHPUnit_Framework_TestCase protected $resource; /** - * @var \Magento\Framework\Data\Collection\AbstractDb|\PHPUnit_Framework_MockObject_MockObject + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $resourceCollection; + private $serializerMock; protected function setUp() { $context = $this->getMockBuilder(\Magento\Framework\Model\Context::class) ->disableOriginalConstructor() ->getMock(); - $this->registry = $this->getMockBuilder( - \Magento\Framework\Registry::class - )->disableOriginalConstructor()->getMock(); $this->localeDate = $this->getMockBuilder( \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class )->getMock(); @@ -65,25 +58,24 @@ protected function setUp() $this->resource = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Design::class) ->disableOriginalConstructor() ->getMock(); - $this->resourceCollection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Design\Collection::class) - ->disableOriginalConstructor() - ->getMock(); $this->cacheManager = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)->getMock(); $context->expects($this->any()) ->method('getCacheManager') ->willReturn($this->cacheManager); - /** - * @var $context \Magento\Framework\Model\Context - */ - $this->model = new Design( - $context, - $this->registry, - $this->localeDate, - $this->dateTime, - $this->resource, - $this->resourceCollection + $this->serializerMock = $this->getMock(SerializerInterface::class); + + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->model = $objectManager->getObject( + Design::class, + [ + 'context' => $context, + 'localeDate' => $this->localeDate, + 'dateTime' => $this->dateTime, + 'resource' => $this->resource, + 'serializer' => $this->serializerMock, + ] ); } @@ -119,9 +111,12 @@ public function testLoadChange() ->method('loadChange') ->with($storeId, $date) ->willReturn(false); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->willReturn('serializedData'); $this->cacheManager->expects($this->once()) ->method('save') - ->with(serialize([]), $cacheId, [Design::CACHE_TAG], 86400) + ->with('serializedData', $cacheId, [Design::CACHE_TAG], 86400) ->willReturnSelf(); $this->assertInstanceOf(get_class($this->model), $this->model->loadChange($storeId)); @@ -151,9 +146,16 @@ public function testLoadChangeFromCache() $this->cacheManager->expects($this->once()) ->method('load') ->with($cacheId) - ->willReturn(serialize(['test' => 'data'])); - - $this->assertInstanceOf(get_class($this->model), $this->model->loadChange($storeId)); + ->willReturn('serializedData'); + $data = ['test' => 'data']; + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with('serializedData') + ->willReturn($data); + + $change = $this->model->loadChange($storeId); + $this->assertInstanceOf(get_class($this->model), $change); + $this->assertEquals($data, $change->getData()); } /** From 9b353c940e69118cb90499af839a83201945814d Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 25 Oct 2016 14:37:50 -0500 Subject: [PATCH 092/144] MAGETWO-59444: Create serializer interface and json class in framework --- .../ShippingInformationManagementTest.php | 58 +++---------------- 1 file changed, 8 insertions(+), 50 deletions(-) diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php index c19abf318aaf1..402a0c8228356 100644 --- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php @@ -36,26 +36,6 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase */ protected $quoteRepositoryMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $addressValidatorMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $loggerMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $addressRepositoryMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $scopeConfigMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -66,11 +46,6 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase */ protected $quoteMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $totalsCollectorMock; - /** * @var \Magento\Checkout\Model\ShippingInformationManagement */ @@ -106,9 +81,6 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase */ private $shippingMock; - /** - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - */ protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); @@ -122,18 +94,6 @@ protected function setUp() ); $this->cartTotalsRepositoryMock = $this->getMock(\Magento\Quote\Api\CartTotalRepositoryInterface::class); $this->quoteRepositoryMock = $this->getMock(\Magento\Quote\Api\CartRepositoryInterface::class); - $this->addressValidatorMock = $this->getMock( - \Magento\Quote\Model\QuoteAddressValidator::class, - [], - [], - '', - false - ); - $this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); - $this->addressRepositoryMock = $this->getMock(\Magento\Customer\Api\AddressRepositoryInterface::class); - $this->scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); - $this->totalsCollectorMock = - $this->getMock(\Magento\Quote\Model\Quote\TotalsCollector::class, [], [], '', false); $this->shippingAddressMock = $this->getMock( \Magento\Quote\Model\Quote\Address::class, [ @@ -184,16 +144,14 @@ protected function setUp() $this->shippingFactoryMock = $this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false); - $this->model = new \Magento\Checkout\Model\ShippingInformationManagement( - $this->paymentMethodManagementMock, - $this->paymentDetailsFactoryMock, - $this->cartTotalsRepositoryMock, - $this->quoteRepositoryMock, - $this->addressValidatorMock, - $this->loggerMock, - $this->addressRepositoryMock, - $this->scopeConfigMock, - $this->totalsCollectorMock + $this->model = $this->objectManager->getObject( + \Magento\Checkout\Model\ShippingInformationManagement::class, + [ + 'paymentMethodManagement' => $this->paymentMethodManagementMock, + 'paymentDetailsFactory' => $this->paymentDetailsFactoryMock, + 'cartTotalsRepository' => $this->cartTotalsRepositoryMock, + 'quoteRepository' => $this->quoteRepositoryMock, + ] ); $this->objectManager->setBackwardCompatibleProperty( $this->model, From 1243ad8a9e1db72b04eb9e5b6ca37f4f8c41f1ca Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 25 Oct 2016 15:36:12 -0500 Subject: [PATCH 093/144] MAGETWO-59439: Refactor Module_Catalog - refactored the classes --- .../Plugin/Model/ResourceModel/Config.php | 21 ++++-- .../Plugin/Model/ResourceModel/ConfigTest.php | 64 ++++++++++++++++--- .../Product/Form/Modifier/Categories.php | 14 +++- 3 files changed, 81 insertions(+), 18 deletions(-) diff --git a/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Config.php b/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Config.php index 699feef76c67e..69c693f4fd01a 100644 --- a/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Config.php +++ b/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Config.php @@ -5,6 +5,9 @@ */ namespace Magento\Catalog\Plugin\Model\ResourceModel; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; + class Config { /**#@+ @@ -20,16 +23,24 @@ class Config /** @var bool|null */ protected $isCacheEnabled = null; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param \Magento\Framework\App\CacheInterface $cache * @param \Magento\Framework\App\Cache\StateInterface $cacheState + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\App\CacheInterface $cache, - \Magento\Framework\App\Cache\StateInterface $cacheState + \Magento\Framework\App\Cache\StateInterface $cacheState, + SerializerInterface $serializer = null ) { $this->cache = $cache; $this->isCacheEnabled = $cacheState->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER); + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -43,12 +54,12 @@ public function aroundGetAttributesUsedInListing( ) { $cacheId = self::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID . $config->getEntityTypeId() . '_' . $config->getStoreId(); if ($this->isCacheEnabled && ($attributes = $this->cache->load($cacheId))) { - return unserialize($attributes); + return $this->serializer->unserialize($attributes); } $attributes = $proceed(); if ($this->isCacheEnabled) { $this->cache->save( - serialize($attributes), + $this->serializer->serialize($attributes), $cacheId, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, @@ -71,12 +82,12 @@ public function aroundGetAttributesUsedForSortBy( $cacheId = self::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID . $config->getEntityTypeId() . '_' . $config->getStoreId(); if ($this->isCacheEnabled && ($attributes = $this->cache->load($cacheId))) { - return unserialize($attributes); + return $this->serializer->unserialize($attributes); } $attributes = $proceed(); if ($this->isCacheEnabled) { $this->cache->save( - serialize($attributes), + $this->serializer->serialize($attributes), $cacheId, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, diff --git a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php index 01f9964f2d83e..24d2f1fcd0f92 100644 --- a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php @@ -6,27 +6,46 @@ namespace Magento\Catalog\Test\Unit\Plugin\Model\ResourceModel; +use Magento\Catalog\Plugin\Model\ResourceModel\Config; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class ConfigTest extends \PHPUnit_Framework_TestCase { - /** @var \Magento\Catalog\Plugin\Model\ResourceModel\Config */ - protected $config; + /** @var Config */ + private $config; /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cache; + private $cache; /** @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheState; + private $cacheState; + + /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializer; /** @var \Magento\Catalog\Model\ResourceModel\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $subject; + private $subject; + + /** + * @var Config + */ + private $object; protected function setUp() { $this->cache = $this->getMock(\Magento\Framework\App\CacheInterface::class); $this->cacheState = $this->getMock(\Magento\Framework\App\Cache\StateInterface::class); + $this->serializer = $this->getMock(SerializerInterface::class); $this->subject = $this->getMock(\Magento\Catalog\Model\ResourceModel\Config::class, [], [], '', false); + $this->object = (new ObjectManager($this))->getObject( + Config::class, + [ + 'cache' => $this->cache, + 'cacheState' => $this->cacheState, + 'serializer' => $this->serializer, + ] + ); } public function testGetAttributesUsedInListingOnCacheDisabled() @@ -47,12 +66,17 @@ public function testGetAttributesUsedInListingFromCache() $entityTypeId = 'type'; $storeId = 'store'; $attributes = ['attributes']; + $serializedAttributes = '["attributes"]'; $this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId); $this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId); $cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID . $entityTypeId . '_' . $storeId; - $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(serialize($attributes)); + $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn($serializedAttributes); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedAttributes) + ->willReturn($attributes); $this->assertEquals( $attributes, @@ -68,14 +92,21 @@ public function testGetAttributesUsedInListingWithCacheSave() $entityTypeId = 'type'; $storeId = 'store'; $attributes = ['attributes']; + $serializedAttributes = '["attributes"]'; $this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId); $this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId); $cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID . $entityTypeId . '_' . $storeId; $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(false); + $this->serializer->expects($this->never()) + ->method('unserialize'); + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($attributes) + ->willReturn($serializedAttributes); $this->cache->expects($this->any())->method('save')->with( - serialize($attributes), + $serializedAttributes, $cacheId, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, @@ -110,11 +141,16 @@ public function testGetAttributesUsedForSortByFromCache() $entityTypeId = 'type'; $storeId = 'store'; $attributes = ['attributes']; + $serializedAttributes = '["attributes"]'; $this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId); $this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId); $cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID . $entityTypeId . '_' . $storeId; - $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(serialize($attributes)); + $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn($serializedAttributes); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedAttributes) + ->willReturn($attributes); $this->assertEquals( $attributes, @@ -130,13 +166,20 @@ public function testGetAttributesUsedForSortByWithCacheSave() $entityTypeId = 'type'; $storeId = 'store'; $attributes = ['attributes']; + $serializedAttributes = '["attributes"]'; $this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId); $this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId); $cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID . $entityTypeId . '_' . $storeId; $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(false); + $this->serializer->expects($this->never()) + ->method('unserialize'); + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($attributes) + ->willReturn($serializedAttributes); $this->cache->expects($this->any())->method('save')->with( - serialize($attributes), + $serializedAttributes, $cacheId, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, @@ -165,7 +208,8 @@ protected function getConfig($cacheEnabledFlag) \Magento\Catalog\Plugin\Model\ResourceModel\Config::class, [ 'cache' => $this->cache, - 'cacheState' => $this->cacheState + 'cacheState' => $this->cacheState, + 'serializer' => $this->serializer, ] ); } diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php index 0e46b5899851f..b05bd229a832d 100644 --- a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php +++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php @@ -11,6 +11,7 @@ use Magento\Framework\App\CacheInterface; use Magento\Framework\DB\Helper as DbHelper; use Magento\Catalog\Model\Category as CategoryModel; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\UrlInterface; use Magento\Framework\Stdlib\ArrayManager; @@ -61,6 +62,10 @@ class Categories extends AbstractModifier * @var CacheInterface */ private $cacheManager; + /** + * @var SerializerInterface + */ + private $serializer; /** * @param LocatorInterface $locator @@ -68,19 +73,22 @@ class Categories extends AbstractModifier * @param DbHelper $dbHelper * @param UrlInterface $urlBuilder * @param ArrayManager $arrayManager + * @param SerializerInterface $serializer */ public function __construct( LocatorInterface $locator, CategoryCollectionFactory $categoryCollectionFactory, DbHelper $dbHelper, UrlInterface $urlBuilder, - ArrayManager $arrayManager + ArrayManager $arrayManager, + SerializerInterface $serializer = null ) { $this->locator = $locator; $this->categoryCollectionFactory = $categoryCollectionFactory; $this->dbHelper = $dbHelper; $this->urlBuilder = $urlBuilder; $this->arrayManager = $arrayManager; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -286,7 +294,7 @@ protected function getCategoriesTree($filter = null) { $categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter); if ($categoryTree) { - return unserialize($categoryTree); + return $this->serializer->unserialize($categoryTree); } $storeId = $this->locator->getStore()->getId(); @@ -340,7 +348,7 @@ protected function getCategoriesTree($filter = null) } $this->getCacheManager()->save( - serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']), + $this->serializer->serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']), self::CATEGORY_TREE_ID . '_' . $filter, [ \Magento\Catalog\Model\Category::CACHE_TAG, From 59bb5fc078f6c367a54e1ddd7c7b88b60a71ebb7 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 25 Oct 2016 18:47:49 -0500 Subject: [PATCH 094/144] MAGETWO-59439: Refactor Module_Catalog - added an integration test for categories tree UI component --- .../Product/Form/Modifier/CategoriesTest.php | 43 +++++++++ .../Modifier/_files/expected_categories.php | 89 +++++++++++++++++++ .../_files/input_meta_for_categories.php | 56 ++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/expected_categories.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php new file mode 100644 index 0000000000000..b94cc197840ae --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php @@ -0,0 +1,43 @@ +get(\Magento\Framework\Registry::class); + /** @var $store \Magento\Store\Model\Store */ + $store = $objectManager->create(\Magento\Store\Model\Store::class); + $store->load('admin'); + $registry->register('current_store', $store); + $this->object = $objectManager->create(Categories::class); + } + + public function testModifyMeta() + { + $inputMeta = include __DIR__ . '/_files/input_meta_for_categories.php'; + $meta = $this->object->modifyMeta($inputMeta); + $expectedCategories = include __DIR__ . '/_files/expected_categories.php'; + $categoriesElement = $meta['product-details']['children']['container_category_ids']['children']['category_ids']; + $this->assertEquals($expectedCategories, $categoriesElement['arguments']['data']['config']['options']); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/expected_categories.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/expected_categories.php new file mode 100644 index 0000000000000..ed30676e16b07 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/expected_categories.php @@ -0,0 +1,89 @@ + + [ + 'value' => '2', + 'is_active' => '1', + 'label' => 'Default Category', + 'optgroup' => + [ + 0 => + [ + 'value' => '3', + 'is_active' => '1', + 'label' => 'Category 1', + 'optgroup' => + [ + 0 => + [ + 'value' => '4', + 'is_active' => '1', + 'label' => 'Category 1.1', + 'optgroup' => + [ + 0 => + [ + 'value' => '5', + 'is_active' => '1', + 'label' => 'Category 1.1.1', + ], + ], + ], + 1 => + [ + 'value' => '13', + 'is_active' => '1', + 'label' => 'Category 1.2', + ], + ], + ], + 1 => + [ + 'value' => '6', + 'is_active' => '1', + 'label' => 'Category 2', + ], + 2 => + [ + 'value' => '7', + 'is_active' => '1', + 'label' => 'Movable', + ], + 3 => + [ + 'value' => '8', + 'is_active' => '0', + 'label' => 'Inactive', + ], + 4 => + [ + 'value' => '9', + 'is_active' => '1', + 'label' => 'Movable Position 1', + ], + 5 => + [ + 'value' => '10', + 'is_active' => '1', + 'label' => 'Movable Position 2', + ], + 6 => + [ + 'value' => '11', + 'is_active' => '1', + 'label' => 'Movable Position 3', + ], + 7 => + [ + 'value' => '12', + 'is_active' => '1', + 'label' => 'Category 12', + ], + ], + ], +]; diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php new file mode 100644 index 0000000000000..d494ed8e95d97 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php @@ -0,0 +1,56 @@ + + [ + 'children' => + ['container_category_ids' => + [ + 'arguments' => + [ + 'data' => + [ + 'config' => + [ + 'formElement' => 'container', + 'componentType' => 'container', + 'breakLine' => false, + 'label' => 'Categories', + 'required' => '0', + 'sortOrder' => 70, + ], + ], + ], + 'children' => + [ + 'category_ids' => + [ + 'arguments' => + [ + 'data' => + [ + 'config' => + [ + 'dataType' => 'text', + 'formElement' => 'input', + 'visible' => '1', + 'required' => '0', + 'notice' => NULL, + 'default' => NULL, + 'label' => 'Categories', + 'code' => 'category_ids', + 'source' => 'product-details', + 'scopeLabel' => '[GLOBAL]', + 'globalScope' => true, + 'sortOrder' => 70, + 'componentType' => 'field', + ], + ], + ], + ], + ], + ]]]]; From 388d1b2ba7d5edf2a820e9b279c6d5d5dad49078 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 25 Oct 2016 18:57:03 -0500 Subject: [PATCH 095/144] MAGETWO-59439: Refactor Module_Catalog - fixed code style --- .../Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php index b05bd229a832d..0ee761646616e 100644 --- a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php +++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php @@ -62,6 +62,7 @@ class Categories extends AbstractModifier * @var CacheInterface */ private $cacheManager; + /** * @var SerializerInterface */ From 0cc8dad3113a988cf4d57e6a9d14fa0040edd166 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 25 Oct 2016 21:52:51 -0500 Subject: [PATCH 096/144] MAGETWO-59439: Refactor Module_Catalog - fixed code style --- .../Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php | 3 --- .../Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php | 4 +--- .../Form/Modifier/_files/input_meta_for_categories.php | 4 ++-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php index 24d2f1fcd0f92..52e010fdd4e29 100644 --- a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php @@ -12,9 +12,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase { - /** @var Config */ - private $config; - /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ private $cache; diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php index b94cc197840ae..33d419440d4a1 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php @@ -4,9 +4,7 @@ * See COPYING.txt for license details. */ -namespace dev\tests\integration\testsuite\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier; - -use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories; +namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier; /** * @magentoAppArea adminhtml diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php index d494ed8e95d97..f43e9c916fcd2 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/_files/input_meta_for_categories.php @@ -39,8 +39,8 @@ 'formElement' => 'input', 'visible' => '1', 'required' => '0', - 'notice' => NULL, - 'default' => NULL, + 'notice' => null, + 'default' => null, 'label' => 'Categories', 'code' => 'category_ids', 'source' => 'product-details', From e99c4a2a76d2f7ac325527bf972366765e11a40e Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 10:45:31 -0500 Subject: [PATCH 097/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../ResourceModel/Product/Collection.php | 14 ++++++- .../Product/Link/Product/Collection.php | 6 +-- .../ResourceModel/Advanced/Collection.php | 4 +- .../Type/Configurable/Product/Collection.php | 2 +- .../SalesRule/Model/ResourceModel/Rule.php | 31 ++++++-------- .../Unit/Model/ResourceModel/RuleTest.php | 41 ++++--------------- 6 files changed, 39 insertions(+), 59 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index 3be5e3afdc86e..645901829eee5 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -259,7 +259,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac /** * @var MetadataPool */ - protected $metadataPool; + private $metadataPool; /** * Collection constructor @@ -2192,7 +2192,7 @@ public function addMediaGalleryData() ); $mediaGalleries = []; - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $items = $this->getItems(); $select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) { @@ -2212,6 +2212,16 @@ public function addMediaGalleryData() return $this; } + /** + * Get MetadataPool instance + * + * @return MetadataPool + */ + public function getMetadataPool() + { + return $this->metadataPool; + } + /** * Retrieve GalleryReadHandler * diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php index 6c9dfb6ce635a..0759eaae264a4 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php @@ -142,7 +142,7 @@ public function addProductFilter($products) if (!is_array($products)) { $products = [$products]; } - $identifierField = $this->metadataPool->getMetadata(ProductInterface::class)->getIdentifierField(); + $identifierField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getIdentifierField(); $this->getSelect()->where("product_entity_table.$identifierField IN (?)", $products); $this->_hasLinkFilter = true; } @@ -202,7 +202,7 @@ protected function _joinLinks() $connection->quoteInto('links.link_type_id = ?', $this->_linkTypeId), ]; $joinType = 'join'; - $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); if ($this->getProduct() && $this->getProduct()->getId()) { $linkFieldId = $this->getProduct()->getData( $linkField @@ -352,7 +352,7 @@ public function addLinkAttributeToFilter($code, $condition) private function joinProductsToLinks() { if ($this->_hasLinkFilter) { - $metaDataPool = $this->metadataPool->getMetadata(ProductInterface::class); + $metaDataPool = $this->getMetadataPool()->getMetadata(ProductInterface::class); $linkField = $metaDataPool->getLinkField(); $entityTable = $metaDataPool->getEntityTable(); $this->getSelect() diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php index 6b5db58acfd89..f63e06efc18d5 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Advanced/Collection.php @@ -80,7 +80,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\CatalogSearch\Model\Advanced\Request\Builder $requestBuilder * @param \Magento\Search\Model\SearchEngine $searchEngine * @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory - * @param null $connection + * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection * @param SearchResultFactory|null $searchResultFactory * @param ProductLimitationFactory|null $productLimitationFactory * @param MetadataPool|null $metadataPool @@ -110,7 +110,7 @@ public function __construct( \Magento\CatalogSearch\Model\Advanced\Request\Builder $requestBuilder, \Magento\Search\Model\SearchEngine $searchEngine, \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory, - $connection = null, + \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, SearchResultFactory $searchResultFactory = null, ProductLimitationFactory $productLimitationFactory = null, MetadataPool $metadataPool = null diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php index e512c511a871a..c60b1e0d0e9e4 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php @@ -61,7 +61,7 @@ protected function _initSelect() */ public function setProductFilter($product) { - $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); $this->getSelect()->where('link_table.parent_id = ?', $product->getData($metadata->getLinkField())); return $this; diff --git a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php index 78cc3e1c39c27..789a29e2f7d77 100644 --- a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php +++ b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php @@ -5,12 +5,11 @@ */ namespace Magento\SalesRule\Model\ResourceModel; +use Magento\Framework\App\ObjectManager; use \Magento\SalesRule\Model\Rule as SalesRule; use Magento\Framework\Model\AbstractModel; -use Magento\Framework\DB\Select; use Magento\Rule\Model\ResourceModel\AbstractResource; use Magento\Framework\EntityManager\EntityManager; -use Magento\SalesRule\Api\Data\RuleInterface; /** * Sales Rule resource model @@ -51,21 +50,31 @@ class Rule extends AbstractResource */ protected $entityManager; + /** + * @var \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap + */ + private $associatedEntitiesMapInstance; + /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Stdlib\StringUtils $string * @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon * @param string $connectionName + * @param \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap|null $associatedEntityMapInstance */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\StringUtils $string, \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon, - $connectionName = null + $connectionName = null, + \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap $associatedEntityMapInstance = null ) { $this->string = $string; $this->_resourceCoupon = $resourceCoupon; - $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap(); + $this->associatedEntitiesMapInstance = $associatedEntityMapInstance ?: ObjectManager::getInstance()->get( + \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class + ); + $this->_associatedEntitiesMap = $this->associatedEntitiesMapInstance->getData(); parent::__construct($context, $connectionName); } @@ -380,20 +389,6 @@ public function delete(AbstractModel $object) return $this; } - /** - * @return array - * @deprecated - */ - private function getAssociatedEntitiesMap() - { - if (!$this->_associatedEntitiesMap) { - $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class) - ->getData(); - } - return $this->_associatedEntitiesMap; - } - /** * @return \Magento\Framework\EntityManager\EntityManager * @deprecated diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php index c0de06df83087..fa3fa2c69fc70 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -5,8 +5,6 @@ */ namespace Magento\SalesRule\Test\Unit\Model\ResourceModel; -use Magento\SalesRule\Api\Data\RuleInterface; - /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -121,7 +119,13 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $associatedEntitiesMap = $this->getMock(\Magento\Framework\DataObject::class, [], [], '', false); + $associatedEntitiesMap = $this->getMock( + \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class, + ['getData'], + [], + '', + false + ); $associatedEntitiesMap->expects($this->once()) ->method('getData') ->willReturn( @@ -139,46 +143,17 @@ protected function setUp() ] ); - $this->mockObjectManager([ - \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class => $associatedEntitiesMap - ]); - $this->model = $this->objectManager->getObject( \Magento\SalesRule\Model\ResourceModel\Rule::class, [ 'context' => $context, 'connectionName' => $connectionName, 'entityManager' => $this->entityManager, + 'associatedEntityMapInstance' => $associatedEntitiesMap ] ); } - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } - - /** - * Mock application object manager to return configured dependencies. - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - /** * test load */ From 80b0c1b45a6307be27de994ba8380512f05ff692 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 11:25:45 -0500 Subject: [PATCH 098/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../Catalog/Model/ResourceModel/Product/Collection.php | 10 +++++----- .../ResourceModel/Product/Link/Product/Collection.php | 8 +++----- .../Product/Type/Configurable/Product/Collection.php | 6 +----- .../Magento/SalesRule/Model/ResourceModel/Rule.php | 9 ++------- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index 645901829eee5..1cf18fc998ef9 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -2192,7 +2192,7 @@ public function addMediaGalleryData() ); $mediaGalleries = []; - $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getProductEntityMetadata()->getLinkField(); $items = $this->getItems(); $select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) { @@ -2213,13 +2213,13 @@ public function addMediaGalleryData() } /** - * Get MetadataPool instance + * Get product entity metadata * - * @return MetadataPool + * @return \Magento\Framework\EntityManager\EntityMetadataInterface */ - public function getMetadataPool() + public function getProductEntityMetadata() { - return $this->metadataPool; + return $this->metadataPool->getMetadata(ProductInterface::class); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php index 0759eaae264a4..116f454cd5e75 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Link/Product/Collection.php @@ -5,8 +5,6 @@ */ namespace Magento\Catalog\Model\ResourceModel\Product\Link\Product; -use Magento\Catalog\Api\Data\ProductInterface; - /** * Catalog product linked products collection * @@ -142,7 +140,7 @@ public function addProductFilter($products) if (!is_array($products)) { $products = [$products]; } - $identifierField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getIdentifierField(); + $identifierField = $this->getProductEntityMetadata()->getIdentifierField(); $this->getSelect()->where("product_entity_table.$identifierField IN (?)", $products); $this->_hasLinkFilter = true; } @@ -202,7 +200,7 @@ protected function _joinLinks() $connection->quoteInto('links.link_type_id = ?', $this->_linkTypeId), ]; $joinType = 'join'; - $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); + $linkField = $this->getProductEntityMetadata()->getLinkField(); if ($this->getProduct() && $this->getProduct()->getId()) { $linkFieldId = $this->getProduct()->getData( $linkField @@ -352,7 +350,7 @@ public function addLinkAttributeToFilter($code, $condition) private function joinProductsToLinks() { if ($this->_hasLinkFilter) { - $metaDataPool = $this->getMetadataPool()->getMetadata(ProductInterface::class); + $metaDataPool = $this->getProductEntityMetadata(); $linkField = $metaDataPool->getLinkField(); $entityTable = $metaDataPool->getEntityTable(); $this->getSelect() diff --git a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php index c60b1e0d0e9e4..44a4d468cd097 100644 --- a/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php +++ b/app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable/Product/Collection.php @@ -7,10 +7,6 @@ */ namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product; -use Magento\Customer\Api\GroupManagementInterface; -use Magento\Framework\EntityManager\MetadataPool; -use Magento\Catalog\Api\Data\ProductInterface; - /** * Class Collection * @@ -61,7 +57,7 @@ protected function _initSelect() */ public function setProductFilter($product) { - $metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); + $metadata = $this->getProductEntityMetadata(); $this->getSelect()->where('link_table.parent_id = ?', $product->getData($metadata->getLinkField())); return $this; diff --git a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php index 789a29e2f7d77..1aabdbd65317e 100644 --- a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php +++ b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php @@ -50,11 +50,6 @@ class Rule extends AbstractResource */ protected $entityManager; - /** - * @var \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap - */ - private $associatedEntitiesMapInstance; - /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Stdlib\StringUtils $string @@ -71,10 +66,10 @@ public function __construct( ) { $this->string = $string; $this->_resourceCoupon = $resourceCoupon; - $this->associatedEntitiesMapInstance = $associatedEntityMapInstance ?: ObjectManager::getInstance()->get( + $associatedEntitiesMapInstance = $associatedEntityMapInstance ?: ObjectManager::getInstance()->get( \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class ); - $this->_associatedEntitiesMap = $this->associatedEntitiesMapInstance->getData(); + $this->_associatedEntitiesMap = $associatedEntitiesMapInstance->getData(); parent::__construct($context, $connectionName); } From 3fddeb1e4adbacf00f173f034328009238fc9626 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 12:07:36 -0500 Subject: [PATCH 099/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- app/code/Magento/SalesRule/Model/ResourceModel/Rule.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php index 1aabdbd65317e..0398508eccf3e 100644 --- a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php +++ b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php @@ -55,14 +55,14 @@ class Rule extends AbstractResource * @param \Magento\Framework\Stdlib\StringUtils $string * @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon * @param string $connectionName - * @param \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap|null $associatedEntityMapInstance + * @param \Magento\Framework\DataObject|null $associatedEntityMapInstance */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\StringUtils $string, \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon, $connectionName = null, - \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap $associatedEntityMapInstance = null + \Magento\Framework\DataObject $associatedEntityMapInstance = null ) { $this->string = $string; $this->_resourceCoupon = $resourceCoupon; From ac2d0145f1d6931469be97335a683c2d21323624 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 12:16:03 -0500 Subject: [PATCH 100/144] MAGETWO-59876: Eliminate mockObjectManager and restoreObjectManager methods in \Magento\Framework\TestFramework\Unit\Helper\ObjectManager --- .../SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php index fa3fa2c69fc70..763e77f94f7a2 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -120,7 +120,7 @@ protected function setUp() ->getMock(); $associatedEntitiesMap = $this->getMock( - \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class, + \Magento\Framework\DataObject::class, ['getData'], [], '', From b6a669d4078f95c9d5c3622af36abeb4891b06ba Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 12:33:06 -0500 Subject: [PATCH 101/144] MAGETWO-59442: Refactor Module_Rss --- app/code/Magento/Rss/Test/Unit/Model/RssTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php index c6da22778532f..21674fa5937bb 100644 --- a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php +++ b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php @@ -73,7 +73,10 @@ public function testGetFeeds() $this->cacheMock->expects($this->once())->method('load')->will($this->returnValue(false)); $this->cacheMock->expects($this->once())->method('save')->will($this->returnValue(true)); - $this->serializerMock->expects($this->once())->method('serialize')->willReturn('serializedData'); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($this->feedData) + ->willReturn('serializedData'); $this->assertEquals($this->feedData, $this->rss->getFeeds()); } @@ -91,6 +94,7 @@ public function testGetFeedsWithCache() ->will($this->returnValue('serializedData')); $this->serializerMock->expects($this->once()) ->method('unserialize') + ->with('serializedData') ->willReturn($this->feedData); $this->cacheMock->expects($this->never())->method('save'); From 710b93dd87796c6869ed83395f922b9e2888038c Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Wed, 26 Oct 2016 16:23:47 -0500 Subject: [PATCH 102/144] MAGETWO-59439: Refactor Module_Catalog - added cache cleaner to Integration testing framework --- .../Plugin/Model/ResourceModel/ConfigTest.php | 13 ----- .../TestFramework/Helper/CacheCleaner.php | 49 +++++++++++++++++++ .../Source/CountryofmanufactureTest.php | 21 ++------ .../Product/Form/Modifier/CategoriesTest.php | 3 ++ .../Magento/Directory/Block/DataTest.php | 31 ++++-------- .../Framework/App/Config/InitialTest.php | 16 ++---- .../App/ObjectManager/ConfigLoaderTest.php | 21 ++------ .../Framework/App/Route/ConfigTest.php | 15 +----- .../Framework/Reflection/MethodsMapTest.php | 23 +++------ .../Magento/Framework/TranslateTest.php | 47 +++++++----------- .../Config/Provider/TemplateTest.php | 17 +------ .../Magento/Store/Model/StoreResolverTest.php | 22 +++------ 12 files changed, 110 insertions(+), 168 deletions(-) create mode 100644 dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php diff --git a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php index 52e010fdd4e29..b6f0dcf52bb1e 100644 --- a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php @@ -24,25 +24,12 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Catalog\Model\ResourceModel\Config|\PHPUnit_Framework_MockObject_MockObject */ private $subject; - /** - * @var Config - */ - private $object; - protected function setUp() { $this->cache = $this->getMock(\Magento\Framework\App\CacheInterface::class); $this->cacheState = $this->getMock(\Magento\Framework\App\Cache\StateInterface::class); $this->serializer = $this->getMock(SerializerInterface::class); $this->subject = $this->getMock(\Magento\Catalog\Model\ResourceModel\Config::class, [], [], '', false); - $this->object = (new ObjectManager($this))->getObject( - Config::class, - [ - 'cache' => $this->cache, - 'cacheState' => $this->cacheState, - 'serializer' => $this->serializer, - ] - ); } public function testGetAttributesUsedInListingOnCacheDisabled() diff --git a/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php b/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php new file mode 100644 index 0000000000000..61ecad9e89b50 --- /dev/null +++ b/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php @@ -0,0 +1,49 @@ +get($cacheType)->getBackend()->clean(); + } + } + + /** + * Clean all cache + */ + public static function cleanAll() + { + $cachePool = self::getCachePool(); + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } + + /** + * Get cache pool + * + * @return Pool + */ + private static function getCachePool() + { + return Bootstrap::getObjectManager() + ->get(Pool::class); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php index ed33a6ff6da82..34fd3b678a086 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Source/CountryofmanufactureTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Catalog\Model\Product\Attribute\Source; +use Magento\TestFramework\Helper\CacheCleaner; + class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase { /** @@ -12,32 +14,19 @@ class CountryofmanufactureTest extends \PHPUnit_Framework_TestCase */ private $model; - /** @var \Magento\TestFramework\ObjectManager */ - private $objectManager; - protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $this->model = $this->objectManager->create( + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->model = $objectManager->create( \Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture::class ); } public function testGetAllOptions() { - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $allOptions = $this->model->getAllOptions(); $cachedAllOptions = $this->model->getAllOptions(); $this->assertEquals($allOptions, $cachedAllOptions); } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php index 33d419440d4a1..1bf360c4c72e9 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php @@ -6,6 +6,8 @@ namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier; +use Magento\TestFramework\Helper\CacheCleaner; + /** * @magentoAppArea adminhtml * @magentoDataFixture Magento/Catalog/_files/categories.php @@ -33,6 +35,7 @@ protected function setUp() public function testModifyMeta() { $inputMeta = include __DIR__ . '/_files/input_meta_for_categories.php'; + CacheCleaner::cleanAll(); $meta = $this->object->modifyMeta($inputMeta); $expectedCategories = include __DIR__ . '/_files/expected_categories.php'; $categoriesElement = $meta['product-details']['children']['container_category_ids']['children']['category_ids']; diff --git a/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php b/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php index 8f367163b3ab5..b0c08ed1792fc 100644 --- a/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php +++ b/dev/tests/integration/testsuite/Magento/Directory/Block/DataTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Directory\Block; +use Magento\TestFramework\Helper\CacheCleaner; + class DataTest extends \PHPUnit_Framework_TestCase { /** @@ -12,38 +14,25 @@ class DataTest extends \PHPUnit_Framework_TestCase */ private $block; - /** @var \Magento\TestFramework\ObjectManager */ - private $objectManager; - protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $this->block = $this->objectManager->get(\Magento\Directory\Block\Data::class); + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->block = $objectManager->get(\Magento\Directory\Block\Data::class); } public function testGetCountryHtmlSelect() { - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $result = $this->block->getCountryHtmlSelect(); - $result2 = $this->block->getCountryHtmlSelect(); - $this->assertEquals($result, $result2); + $resultTwo = $this->block->getCountryHtmlSelect(); + $this->assertEquals($result, $resultTwo); } public function testGetRegionHtmlSelect() { - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $result = $this->block->getRegionHtmlSelect(); - $result2 = $this->block->getRegionHtmlSelect(); - $this->assertEquals($result, $result2); - } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } + $resultTwo = $this->block->getRegionHtmlSelect(); + $this->assertEquals($result, $resultTwo); } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php index 4d47909dee909..a98704a130b3b 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Config/InitialTest.php @@ -5,9 +5,9 @@ */ namespace Magento\Framework\App\Config; +use Magento\TestFramework\Helper\CacheCleaner; use Magento\TestFramework\ObjectManager; use Magento\TestFramework\Helper\Bootstrap; -use Magento\Framework\App\Cache\Frontend\Pool; use Magento\Framework\App\Config\Initial as Config; class InitialTest extends \PHPUnit_Framework_TestCase @@ -24,7 +24,7 @@ protected function setUp() public function testGetMetadata() { - $this->cleanCache(); + CacheCleaner::cleanAll(); $this->assertEquals( $this->objectManager->create(Config::class)->getMetadata(), $this->objectManager->create(Config::class)->getMetadata() @@ -37,7 +37,7 @@ public function testGetMetadata() */ public function testGetData($scope) { - $this->cleanCache(); + CacheCleaner::cleanAll(); $this->assertEquals( $this->objectManager->create(Config::class)->getData($scope), $this->objectManager->create(Config::class)->getData($scope) @@ -52,14 +52,4 @@ public function getDataDataProvider() ['websites|default'] ]; } - - private function cleanCache() - { - /** @var Pool $cachePool */ - $cachePool = $this->objectManager->get(Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php index 74a3aee56ba28..30e9c09e9b4c5 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/App/ObjectManager/ConfigLoaderTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Framework\App\ObjectManager; +use Magento\TestFramework\Helper\CacheCleaner; + class ConfigLoaderTest extends \PHPUnit_Framework_TestCase { /** @@ -12,33 +14,20 @@ class ConfigLoaderTest extends \PHPUnit_Framework_TestCase */ private $object; - /** @var \Magento\TestFramework\ObjectManager */ - private $objectManager; - protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $this->object = $this->objectManager->create( + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->object = $objectManager->create( \Magento\Framework\App\ObjectManager\ConfigLoader::class ); } public function testLoad() { - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $data = $this->object->load('global'); $this->assertNotEmpty($data); $cachedData = $this->object->load('global'); $this->assertEquals($data, $cachedData); } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php index af99b7226bc75..d0c4b562952ba 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/App/Route/ConfigTest.php @@ -6,8 +6,7 @@ namespace Magento\Framework\App\Route; use Magento\TestFramework\Helper\Bootstrap; -use Magento\Framework\App\Cache\Frontend\Pool; -use Magento\Framework\App\Route\Config; +use Magento\TestFramework\Helper\CacheCleaner; use Magento\TestFramework\ObjectManager; class ConfigTest extends \PHPUnit_Framework_TestCase @@ -29,7 +28,7 @@ protected function setUp() */ public function testGetRouteFrontName($route, $scope) { - $this->cleanCache(); + CacheCleaner::cleanAll(); $this->assertEquals( $this->objectManager->create(Config::class)->getRouteFrontName($route, $scope), $this->objectManager->create(Config::class)->getRouteFrontName($route, $scope) @@ -43,14 +42,4 @@ public function getRouteFrontNameDataProvider() ['catalog', 'frontend'], ]; } - - private function cleanCache() - { - /** @var Pool $cachePool */ - $cachePool = $this->objectManager->get(Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php b/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php index ecb815836625f..992b6a59382e7 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Reflection/MethodsMapTest.php @@ -7,25 +7,24 @@ */ namespace Magento\Framework\Reflection; +use Magento\TestFramework\Helper\CacheCleaner; + class MethodsMapTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\Framework\Reflection\MethodsMap */ private $object; - /** @var \Magento\TestFramework\ObjectManager */ - private $objectManager; - protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $this->object = $this->objectManager->create( + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->object = $objectManager->create( \Magento\Framework\Reflection\MethodsMap::class ); } public function testGetMethodsMap() { - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $data = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); $this->assertArrayHasKey('getMethodsMap', $data); $cachedData = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class); @@ -34,7 +33,7 @@ public function testGetMethodsMap() public function testGetMethodParams() { - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $data = $this->object->getMethodParams( \Magento\Framework\Reflection\MethodsMap::class, 'getMethodParams' @@ -46,14 +45,4 @@ public function testGetMethodParams() ); $this->assertEquals($data, $cachedData); } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php b/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php index 277716e63ac8f..78035c7b26491 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php @@ -6,6 +6,7 @@ namespace Magento\Framework; use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\Helper\CacheCleaner; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -15,9 +16,6 @@ class TranslateTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Framework\Translate */ private $translate; - /** @var \Magento\TestFramework\ObjectManager */ - private $objectManager; - protected function setUp() { /** @var \Magento\Framework\View\FileSystem $viewFileSystem */ @@ -41,11 +39,12 @@ protected function setUp() $viewFileSystem->expects($this->any())->method('getDesignTheme')->will($this->returnValue($theme)); - $this->objectManager = Bootstrap::getObjectManager(); - $this->objectManager->addSharedInstance($viewFileSystem, \Magento\Framework\View\FileSystem::class); + /** @var \Magento\TestFramework\ObjectManager $objectManager */ + $objectManager = Bootstrap::getObjectManager(); + $objectManager->addSharedInstance($viewFileSystem, \Magento\Framework\View\FileSystem::class); /** @var $moduleReader \Magento\Framework\Module\Dir\Reader */ - $moduleReader = $this->objectManager->get(\Magento\Framework\Module\Dir\Reader::class); + $moduleReader = $objectManager->get(\Magento\Framework\Module\Dir\Reader::class); $moduleReader->setModuleDir( 'Magento_Store', 'i18n', @@ -62,33 +61,33 @@ protected function setUp() \Magento\Theme\Model\View\Design::class, ['getDesignTheme'], [ - $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class), - $this->objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class), - $this->objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class), - $this->objectManager->get(\Magento\Theme\Model\ThemeFactory::class), - $this->objectManager->get(\Magento\Framework\ObjectManagerInterface::class), - $this->objectManager->get(\Magento\Framework\App\State::class), + $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class), + $objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class), + $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class), + $objectManager->get(\Magento\Theme\Model\ThemeFactory::class), + $objectManager->get(\Magento\Framework\ObjectManagerInterface::class), + $objectManager->get(\Magento\Framework\App\State::class), ['frontend' => 'Test/default'] ] ); $designModel->expects($this->any())->method('getDesignTheme')->will($this->returnValue($theme)); - $this->objectManager->addSharedInstance($designModel, \Magento\Theme\Model\View\Design\Proxy::class); + $objectManager->addSharedInstance($designModel, \Magento\Theme\Model\View\Design\Proxy::class); - $this->translate = $this->objectManager->create(\Magento\Framework\Translate::class); - $this->objectManager->addSharedInstance($this->translate, \Magento\Framework\Translate::class); - $this->objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Composite::class); - $this->objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class); + $this->translate = $objectManager->create(\Magento\Framework\Translate::class); + $objectManager->addSharedInstance($this->translate, \Magento\Framework\Translate::class); + $objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Composite::class); + $objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class); \Magento\Framework\Phrase::setRenderer( - $this->objectManager->get(\Magento\Framework\Phrase\RendererInterface::class) + $objectManager->get(\Magento\Framework\Phrase\RendererInterface::class) ); } public function testLoadData() { $data = $this->translate->loadData(null, true)->getData(); - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $this->translate->loadData()->getData(); $dataCached = $this->translate->loadData()->getData(); $this->assertEquals($data, $dataCached); @@ -117,14 +116,4 @@ public function translateDataProvider() ['Design value to translate', 'Design translated value'] ]; } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php index 5d0476832a984..d2e5ab7215ff0 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Element/UiComponent/Config/Provider/TemplateTest.php @@ -7,7 +7,7 @@ namespace Magento\Framework\View\Element\UiComponent\Config\Provider; use \Magento\TestFramework\Helper\Bootstrap; -use Magento\Framework\App\Cache\Frontend\Pool; +use Magento\TestFramework\Helper\CacheCleaner; /** * @magentoComponentsDir Magento/Framework/View/_files/UiComponent/theme @@ -49,7 +49,7 @@ public function testGetTemplate() \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('adminhtml'); $this->objectManager->get(\Magento\Framework\View\DesignInterface::class) ->setDesignTheme('FrameworkViewUiComponent/default'); - $this->cleanCache(); + CacheCleaner::cleanAll(); $resultOne = $this->model->getTemplate('test.xml'); $resultTwo = $this->model->getTemplate('test.xml'); @@ -58,19 +58,6 @@ public function testGetTemplate() $this->assertXmlStringEqualsXmlString($expected, $resultTwo); } - /** - * Clean application cache - */ - protected function cleanCache() - { - /** @var Pool $cachePool */ - $cachePool = $this->objectManager->get(Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } - /** * Register themes in the fixture folder */ diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php index e175fead09cae..b2aa803bc0105 100644 --- a/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php +++ b/dev/tests/integration/testsuite/Magento/Store/Model/StoreResolverTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Store\Model; +use Magento\TestFramework\Helper\CacheCleaner; + class StoreResolverTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\TestFramework\ObjectManager */ @@ -23,23 +25,13 @@ public function testGetStoreData() $methodReadStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'readStoresData'); $methodReadStoresData->setAccessible(true); - $storeResover = $this->objectManager->get(\Magento\Store\Model\StoreResolver::class); + $storeResolver = $this->objectManager->get(\Magento\Store\Model\StoreResolver::class); - $storesDataRead = $methodReadStoresData->invoke($storeResover); - $this->cleanAllCache(); - $storesData = $methodGetStoresData->invoke($storeResover); - $storesDataCached = $methodGetStoresData->invoke($storeResover); + $storesDataRead = $methodReadStoresData->invoke($storeResolver); + CacheCleaner::cleanAll(); + $storesData = $methodGetStoresData->invoke($storeResolver); + $storesDataCached = $methodGetStoresData->invoke($storeResolver); $this->assertEquals($storesDataRead, $storesData); $this->assertEquals($storesDataRead, $storesDataCached); } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } From fa2711b25617ccf4cd82c13e104bf4e227da72bf Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Wed, 26 Oct 2016 17:15:31 -0500 Subject: [PATCH 103/144] MAGETWO-59439: Refactor Module_Catalog - fixed code style --- .../framework/Magento/TestFramework/Helper/CacheCleaner.php | 1 + 1 file changed, 1 insertion(+) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php b/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php index 61ecad9e89b50..5d7748f0b1fd9 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Helper/CacheCleaner.php @@ -5,6 +5,7 @@ */ namespace Magento\TestFramework\Helper; + use Magento\Framework\App\Cache\Frontend\Pool; /** From 0e3715dd3e2b03801e163948af5222d2d54c1b9c Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 20:21:09 -0500 Subject: [PATCH 104/144] MAGETWO-59442: Refactor Module_Rss --- app/code/Magento/Rss/Model/Rss.php | 5 ----- app/code/Magento/Rss/Test/Unit/Model/RssTest.php | 9 +++++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Rss/Model/Rss.php b/app/code/Magento/Rss/Model/Rss.php index 5d78c499cacf7..af716613bd261 100644 --- a/app/code/Magento/Rss/Model/Rss.php +++ b/app/code/Magento/Rss/Model/Rss.php @@ -9,11 +9,6 @@ use Magento\Framework\App\Rss\DataProviderInterface; use Magento\Framework\Serialize\SerializerInterface; -/** - * Auth session model - * - * @author Magento Core Team - */ class Rss { /** diff --git a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php index 21674fa5937bb..61ae691fc7ea9 100644 --- a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php +++ b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php @@ -71,7 +71,10 @@ public function testGetFeeds() $this->rss->setDataProvider($dataProvider); - $this->cacheMock->expects($this->once())->method('load')->will($this->returnValue(false)); + $this->cacheMock->expects($this->once()) + ->method('load') + ->with('cache_key') + ->will($this->returnValue(false)); $this->cacheMock->expects($this->once())->method('save')->will($this->returnValue(true)); $this->serializerMock->expects($this->once()) ->method('serialize') @@ -90,7 +93,9 @@ public function testGetFeedsWithCache() $this->rss->setDataProvider($dataProvider); - $this->cacheMock->expects($this->once())->method('load') + $this->cacheMock->expects($this->once()) + ->method('load') + ->with('cache_key') ->will($this->returnValue('serializedData')); $this->serializerMock->expects($this->once()) ->method('unserialize') From 393be373918e27124f59123c989b8c634ca9abe1 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 26 Oct 2016 20:25:40 -0500 Subject: [PATCH 105/144] MAGETWO-59442: Refactor Module_Rss --- app/code/Magento/Rss/Test/Unit/Model/RssTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php index 61ae691fc7ea9..0c5eb303935ff 100644 --- a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php +++ b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php @@ -75,7 +75,10 @@ public function testGetFeeds() ->method('load') ->with('cache_key') ->will($this->returnValue(false)); - $this->cacheMock->expects($this->once())->method('save')->will($this->returnValue(true)); + $this->cacheMock->expects($this->once()) + ->method('save') + ->with('serializedData') + ->will($this->returnValue(true)); $this->serializerMock->expects($this->once()) ->method('serialize') ->with($this->feedData) From e74f3f29ac475f491c295a0be44d1bafab5094ca Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Thu, 27 Oct 2016 09:35:19 -0500 Subject: [PATCH 106/144] MAGETWO-59439: Refactor Module_Catalog - added assertions for cached data --- .../Product/Form/Modifier/CategoriesTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php index 1bf360c4c72e9..b01d1e6d38655 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php @@ -35,9 +35,15 @@ protected function setUp() public function testModifyMeta() { $inputMeta = include __DIR__ . '/_files/input_meta_for_categories.php'; - CacheCleaner::cleanAll(); - $meta = $this->object->modifyMeta($inputMeta); $expectedCategories = include __DIR__ . '/_files/expected_categories.php'; + CacheCleaner::cleanAll(); + $this->assertCategoriesInMeta($expectedCategories, $this->object->modifyMeta($inputMeta)); + // Verify cached data + $this->assertCategoriesInMeta($expectedCategories, $this->object->modifyMeta($inputMeta)); + } + + private function assertCategoriesInMeta(array $expectedCategories, array $meta) + { $categoriesElement = $meta['product-details']['children']['container_category_ids']['children']['category_ids']; $this->assertEquals($expectedCategories, $categoriesElement['arguments']['data']['config']['options']); } From 79be4b306f8f0059cbdba455f293978c9f67a495 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 27 Oct 2016 16:45:43 -0500 Subject: [PATCH 107/144] MAGETWO-59440: Refactor Module_Eav Introducing SerializerInterface --- app/code/Magento/Eav/Model/Config.php | 23 +- .../ResourceModel/Entity/Attribute/Set.php | 31 ++- .../Model/ResourceModel/Entity/Attribute.php | 63 ++++-- .../Eav/Test/Unit/Model/ConfigTest.php | 204 +++++++++++++++--- .../Entity/Attribute/SetTest.php | 141 ++++++------ .../ResourceModel/Entity/AttributeTest.php | 197 +++++++++++------ .../Magento/Eav/Model/ConfigTest.php | 49 +++++ 7 files changed, 503 insertions(+), 205 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php diff --git a/app/code/Magento/Eav/Model/Config.php b/app/code/Magento/Eav/Model/Config.php index 36be7733dcd75..89d0bc93c8007 100644 --- a/app/code/Magento/Eav/Model/Config.php +++ b/app/code/Magento/Eav/Model/Config.php @@ -6,6 +6,8 @@ namespace Magento\Eav\Model; use Magento\Eav\Model\Entity\Type; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\App\ObjectManager; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -92,6 +94,11 @@ class Config */ protected $_universalFactory; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param \Magento\Framework\App\CacheInterface $cache * @param \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory @@ -105,13 +112,15 @@ public function __construct( \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory, \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory, \Magento\Framework\App\Cache\StateInterface $cacheState, - \Magento\Framework\Validator\UniversalFactory $universalFactory + \Magento\Framework\Validator\UniversalFactory $universalFactory, + SerializerInterface $serializer = null ) { $this->_cache = $cache; $this->_entityTypeFactory = $entityTypeFactory; $this->entityTypeCollectionFactory = $entityTypeCollectionFactory; $this->_cacheState = $cacheState; $this->_universalFactory = $universalFactory; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -278,7 +287,7 @@ protected function _initEntityTypes() \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]); if ($this->isCacheEnabled() && ($cache = $this->_cache->load(self::ENTITIES_CACHE_ID))) { - $this->_entityTypeData = unserialize($cache); + $this->_entityTypeData = $this->serializer->unserialize($cache); foreach ($this->_entityTypeData as $typeCode => $data) { $typeId = $data['entity_type_id']; $this->_addEntityTypeReference($typeId, $typeCode); @@ -302,7 +311,7 @@ protected function _initEntityTypes() if ($this->isCacheEnabled()) { $this->_cache->save( - serialize($this->_entityTypeData), + $this->serializer->serialize($this->_entityTypeData), self::ENTITIES_CACHE_ID, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, @@ -372,7 +381,7 @@ protected function _initAttributes($entityType) } $cacheKey = self::ATTRIBUTES_CACHE_ID . $entityTypeCode; if ($this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))) { - $attributes = unserialize($attributes); + $attributes = $this->serializer->unserialize($attributes); if ($attributes) { foreach ($attributes as $attribute) { $this->_createAttribute($entityType, $attribute); @@ -402,7 +411,7 @@ protected function _initAttributes($entityType) } if ($this->isCacheEnabled()) { $this->_cache->save( - serialize($this->_attributeData[$entityTypeCode]), + $this->serializer->serialize($this->_attributeData[$entityTypeCode]), $cacheKey, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, @@ -487,7 +496,7 @@ public function getEntityAttributeCodes($entityType, $object = null) } if ($this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))) { - $this->_attributeCodes[$cacheKey] = unserialize($attributes); + $this->_attributeCodes[$cacheKey] = $this->serializer->unserialize($attributes); return $this->_attributeCodes[$cacheKey]; } @@ -514,7 +523,7 @@ public function getEntityAttributeCodes($entityType, $object = null) $this->_attributeCodes[$cacheKey] = $attributes; if ($this->isCacheEnabled()) { $this->_cache->save( - serialize($attributes), + $this->serializer->serialize($attributes), $cacheKey, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, diff --git a/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Set.php b/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Set.php index 4e6e45f5d2270..b3e7bf2bc3925 100644 --- a/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Set.php +++ b/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Set.php @@ -5,11 +5,8 @@ */ namespace Magento\Eav\Model\ResourceModel\Entity\Attribute; -/** - * Eav attribute set resource model - * - * @author Magento Core Team - */ +use Magento\Framework\Serialize\SerializerInterface; + class Set extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { /** @@ -27,6 +24,11 @@ class Set extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ protected $eavConfig; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param GroupFactory $attrGroupFactory @@ -152,7 +154,7 @@ public function getSetInfo(array $attributeIds, $setId = null) $cacheKey = self::ATTRIBUTES_CACHE_ID . $setId; if ($this->eavConfig->isCacheEnabled() && ($cache = $this->eavConfig->getCache()->load($cacheKey))) { - $setInfoData = unserialize($cache); + $setInfoData = $this->getSerializer()->unserialize($cache); } else { $attributeSetData = $this->fetchAttributeSetData($setId); @@ -168,7 +170,7 @@ public function getSetInfo(array $attributeIds, $setId = null) if ($this->eavConfig->isCacheEnabled()) { $this->eavConfig->getCache()->save( - serialize($setInfoData), + $this->getSerializer()->serialize($setInfoData), $cacheKey, [ \Magento\Eav\Model\Cache\Type::CACHE_TAG, @@ -233,4 +235,19 @@ protected function fetchAttributeSetData($setId = null) } return $connection->fetchAll($select, $bind); } + + /** + * Get serializer + * + * @return SerializerInterface + * @deprecated + */ + private function getSerializer() + { + if (null === $this->serializer) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(SerializerInterface::class); + } + return $this->serializer; + } } diff --git a/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php b/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php index c76449b1223ed..3452d452ac490 100644 --- a/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php +++ b/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php @@ -5,6 +5,13 @@ */ namespace Magento\Eav\Plugin\Model\ResourceModel\Entity; +use Magento\Framework\App\CacheInterface; +use Magento\Framework\App\Cache\StateInterface; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Eav\Model\ResourceModel\Entity\Attribute as AttributeResource; +use Magento\Eav\Model\Cache\Type; +use Magento\Eav\Model\Entity\Attribute as EntityAttribute; + class Attribute { /** @@ -12,52 +19,74 @@ class Attribute */ const STORE_LABEL_ATTRIBUTE = 'EAV_STORE_LABEL_ATTRIBUTE'; - /** @var \Magento\Framework\App\CacheInterface */ - protected $cache; + /** + * @var CacheInterface + */ + private $cache; + + /** + * @var StateInterface + */ + private $cacheState; - /** @var bool|null */ - protected $isCacheEnabled = null; + /** + * @var SerializerInterface + */ + private $serializer; /** - * @param \Magento\Framework\App\CacheInterface $cache - * @param \Magento\Framework\App\Cache\StateInterface $cacheState + * @param CacheInterface $cache + * @param StateInterface $cacheState + * @param SerializerInterface $serializer * @codeCoverageIgnore */ public function __construct( - \Magento\Framework\App\CacheInterface $cache, - \Magento\Framework\App\Cache\StateInterface $cacheState + CacheInterface $cache, + StateInterface $cacheState, + SerializerInterface $serializer ) { $this->cache = $cache; - $this->isCacheEnabled = $cacheState->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER); + $this->serializer = $serializer; + $this->cacheState = $cacheState;; } /** - * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $subject + * @param AttributeResource $subject * @param callable $proceed * @param int $attributeId * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGetStoreLabelsByAttributeId( - \Magento\Eav\Model\ResourceModel\Entity\Attribute $subject, + AttributeResource $subject, \Closure $proceed, $attributeId ) { $cacheId = self::STORE_LABEL_ATTRIBUTE . $attributeId; - if ($this->isCacheEnabled && ($storeLabels = $this->cache->load($cacheId))) { - return unserialize($storeLabels); + if ($this->isCacheEnabled() && ($storeLabels = $this->cache->load($cacheId))) { + return $this->serializer->unserialize($storeLabels); } $storeLabels = $proceed($attributeId); - if ($this->isCacheEnabled) { + if ($this->isCacheEnabled()) { $this->cache->save( - serialize($storeLabels), + $this->serializer->serialize($storeLabels), $cacheId, [ - \Magento\Eav\Model\Cache\Type::CACHE_TAG, - \Magento\Eav\Model\Entity\Attribute::CACHE_TAG + Type::CACHE_TAG, + EntityAttribute::CACHE_TAG ] ); } return $storeLabels; } + + /** + * Check if cache is enabled + * + * @return bool + */ + private function isCacheEnabled() + { + return $this->cacheState->isEnabled(Type::TYPE_IDENTIFIER); + } } diff --git a/app/code/Magento/Eav/Test/Unit/Model/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/ConfigTest.php index f5fad571f205c..ff1e186de604a 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/ConfigTest.php @@ -3,11 +3,15 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Eav\Test\Unit\Model; use Magento\Framework\DataObject; use Magento\Eav\Model\Config; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Eav\Model\Entity\Type; +use Magento\Eav\Model\Cache\Type as Cache; +use Magento\Eav\Model\Entity\Attribute; +use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection; class ConfigTest extends \PHPUnit_Framework_TestCase { @@ -34,19 +38,26 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $stateMock; + protected $cacheStateMock; /** * @var \Magento\Framework\Validator\UniversalFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $universalFactoryMock; + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; + + /** + * @var Type|\PHPUnit_Framework_MockObject_MockObject + */ + private $typeMock; + protected function setUp() { - $this->cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class) - ->disableOriginalConstructor() - ->setMethods(['load', 'getFrontend', 'save', 'remove', 'clean']) - ->getMock(); + $this->cacheMock = $this->getMock(\Magento\Framework\App\CacheInterface::class); $this->typeFactoryMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\TypeFactory::class) ->setMethods(['create']) ->disableOriginalConstructor() @@ -56,35 +67,44 @@ protected function setUp() ->setMethods(['create']) ->disableOriginalConstructor() ->getMock(); - $this->stateMock = $this->getMockBuilder(\Magento\Framework\App\Cache\StateInterface::class) - ->setMethods(['isEnabled', 'setEnabled', 'persist']) - ->disableOriginalConstructor() - ->getMock(); + $this->cacheStateMock = $this->getMock(\Magento\Framework\App\Cache\StateInterface::class); $this->universalFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\UniversalFactory::class) ->setMethods(['create']) ->disableOriginalConstructor() ->getMock(); + $this->serializerMock = $this->getMock(SerializerInterface::class); + + $this->typeMock = $this->getMock(Type::class, [], [], '', false); + $this->config = new Config( $this->cacheMock, $this->typeFactoryMock, $this->collectionFactoryMock, - $this->stateMock, - $this->universalFactoryMock + $this->cacheStateMock, + $this->universalFactoryMock, + $this->serializerMock ); } /** * @param boolean $cacheEnabled * @param int $loadCalls - * @param string $cachedValue + * @param int $cachedValue + * @param int $unserializeCalls * @dataProvider getAttributeCacheDataProvider * @return void */ - public function testGetAttributeCache($cacheEnabled, $loadCalls, $cachedValue) + public function testGetAttributeCache($cacheEnabled, $loadCalls, $unserializeCalls, $cachedValue) { + $attributeData = [ + [ + 'attribute_code' => 'attribute_code_1', + 'attribute_id' => 1 + ] + ]; $attributeCollectionMock = $this->getMockBuilder( - \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class + Collection::class )->disableOriginalConstructor() ->setMethods(['getData', 'setEntityTypeFilter']) ->getMock(); @@ -96,29 +116,40 @@ public function testGetAttributeCache($cacheEnabled, $loadCalls, $cachedValue) ->expects($this->any()) ->method('getData') ->willReturn([]); - $entityAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class) + $entityAttributeMock = $this->getMockBuilder(Attribute::class) ->setMethods(['setData']) ->disableOriginalConstructor() ->getMock(); $factoryCalls = [ - [\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class, [], $attributeCollectionMock], - [\Magento\Eav\Model\Entity\Attribute::class, [], $entityAttributeMock], + [ + Collection::class, + [], + $attributeCollectionMock + ], + [ + Attribute::class, + [], + $entityAttributeMock + ], ]; - $this->stateMock + $this->cacheStateMock ->expects($this->atLeastOnce()) ->method('isEnabled') - ->with(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER) + ->with(Cache::TYPE_IDENTIFIER) ->willReturn($cacheEnabled); $this->cacheMock ->expects($this->exactly($loadCalls)) ->method('load') ->with(Config::ATTRIBUTES_CACHE_ID) ->willReturn($cachedValue); + $this->serializerMock + ->expects($this->exactly($unserializeCalls)) + ->method('unserialize') + ->with($cachedValue) + ->willReturn($attributeData); - $collectionStub = new DataObject([ - ['entity_type_code' => 'type_code_1', 'entity_type_id' => 1], - ]); + $collectionStub = new DataObject([$attributeData]); $this->collectionFactoryMock ->expects($this->any()) ->method('create') @@ -134,7 +165,7 @@ public function testGetAttributeCache($cacheEnabled, $loadCalls, $cachedValue) ->method('create') ->will($this->returnValueMap($factoryCalls)); - $entityType = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class) + $entityType = $this->getMockBuilder(Type::class) ->setMethods(['getEntity', 'setData', 'getData']) ->disableOriginalConstructor() ->getMock(); @@ -151,38 +182,145 @@ public function getAttributeCacheDataProvider() 'cache-disabled' => [ false, 0, + 0, false, ], 'cache-miss' => [ true, 1, + 0, false, ], 'cached' => [ true, 1, - serialize( - [ - ['attribute_code' => 'attribute_code_1', 'attribute_id' => 1], - ] - ), + 1, + 'attribute serialzied data', ], ]; } public function testClear() { - $this->cacheMock - ->expects($this->once()) + $this->cacheMock->expects($this->once()) ->method('clean') ->with( $this->equalTo( [ - \Magento\Eav\Model\Cache\Type::CACHE_TAG, - \Magento\Eav\Model\Entity\Attribute::CACHE_TAG, + Cache::CACHE_TAG, + Attribute::CACHE_TAG, ] ) ); $this->config->clear(); } + + public function testGetEntityTypeInstanceOfTypePassed() + { + $this->assertEquals( + $this->typeMock, + $this->config->getEntityType($this->typeMock) + ); + } + + public function testGetEntityTypeCacheExists() + { + $entityTypeCode = 'catalog_product'; + $data = [ + $entityTypeCode => [ + 'entity_type_id' => 1 + ] + ]; + $serializedData = 'serialized data'; + $this->cacheStateMock->expects($this->once()) + ->method('isEnabled') + ->with(Cache::TYPE_IDENTIFIER) + ->willReturn(true); + $this->cacheMock->expects($this->once()) + ->method('load') + ->with(Config::ENTITIES_CACHE_ID) + ->willReturn($serializedData); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with($serializedData) + ->willReturn($data); + $this->typeMock->expects($this->exactly(2)) + ->method('getId') + ->willReturn($data[$entityTypeCode]['entity_type_id']); + $this->typeMock->expects($this->once()) + ->method('getEntityTypeCode') + ->willReturn($entityTypeCode); + $this->typeFactoryMock->expects($this->once()) + ->method('create') + ->with(['data' => $data[$entityTypeCode]]) + ->willReturn($this->typeMock); + $this->assertInstanceOf( + Type::class, + $this->config->getEntityType($entityTypeCode) + ); + } + + public function testGetEntityTypeCacheDoesNotExist() + { + $entityTypeCode = 'catalog_product'; + $collectionData = [ + [ + 'entity_type_id' => 1, + 'entity_type_code' => $entityTypeCode + ] + ]; + $data = [ + $entityTypeCode => [ + 'entity_type_id' => 1, + 'entity_type_code' => $entityTypeCode, + 'attribute_model' => Attribute::class + ] + ]; + $serializedData = 'serialized data'; + $this->cacheStateMock->expects($this->once()) + ->method('isEnabled') + ->with(Cache::TYPE_IDENTIFIER) + ->willReturn(true); + $this->cacheMock->expects($this->once()) + ->method('load') + ->with(Config::ENTITIES_CACHE_ID) + ->willReturn(false); + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $attributeCollectionMock = $this->getMock(Collection::class, [], [], '', false); + $this->collectionFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($attributeCollectionMock); + $attributeCollectionMock->expects($this->once()) + ->method('getData') + ->willReturn($collectionData); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($data) + ->willReturn($serializedData); + $this->cacheMock->expects($this->once()) + ->method('save') + ->with( + $serializedData, + Config::ENTITIES_CACHE_ID, + [ + Cache::CACHE_TAG, + Attribute::CACHE_TAG + ] + ); + $this->typeMock->expects($this->exactly(2)) + ->method('getId') + ->willReturn($data[$entityTypeCode]['entity_type_id']); + $this->typeMock->expects($this->once()) + ->method('getEntityTypeCode') + ->willReturn($entityTypeCode); + $this->typeFactoryMock->expects($this->once()) + ->method('create') + ->with(['data' => $data[$entityTypeCode]]) + ->willReturn($this->typeMock); + $this->assertInstanceOf( + Type::class, + $this->config->getEntityType($entityTypeCode) + ); + } } diff --git a/app/code/Magento/Eav/Test/Unit/Model/ResourceModel/Entity/Attribute/SetTest.php b/app/code/Magento/Eav/Test/Unit/Model/ResourceModel/Entity/Attribute/SetTest.php index a380b14595eb2..e00a8ee97648c 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/ResourceModel/Entity/Attribute/SetTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/ResourceModel/Entity/Attribute/SetTest.php @@ -1,14 +1,14 @@ resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class) ->disableOriginalConstructor() ->setMethods(['getConnection', 'getTableName']) @@ -81,31 +87,28 @@ protected function setUp() ->setMethods(['isCacheEnabled', 'getEntityType', 'getCache']) ->disableOriginalConstructor() ->getMock(); - $this->model = $this->getMock( + + $this->serializerMock = $this->getMock(SerializerInterface::class); + + $attributeGroupFactoryMock = $this->getMock( + \Magento\Eav\Model\ResourceModel\Entity\Attribute\GroupFactory::class, + [], + [], + '', + false + ); + + $this->model = $objectManager->getObject( \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set::class, [ - 'beginTransaction', - 'getMainTable', - 'getIdFieldName', - '_afterDelete', - 'commit', - 'rollBack', - '__wakeup' - ], - [ - $contextMock, - $this->getMock( - \Magento\Eav\Model\ResourceModel\Entity\Attribute\GroupFactory::class, - [], - [], - '', - false - ), - $this->eavConfigMock - ], - '', - true + 'context' => $contextMock, + 'attrGroupFactory' => $attributeGroupFactoryMock, + 'eavConfig' => $this->eavConfigMock + ] ); + + $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $this->serializerMock); + $this->typeMock = $this->getMock(\Magento\Eav\Model\Entity\Type::class, [], [], '', false); $this->objectMock = $this->getMock( \Magento\Framework\Model\AbstractModel::class, @@ -123,7 +126,6 @@ protected function setUp() '', false ); - } /** @@ -182,6 +184,22 @@ public function testBeforeDelete() */ public function testGetSetInfoCacheMiss() { + $serializedData = 'serialized data'; + $setElement = [ + 10000 => [ + 'group_id' => 10, + 'group_sort' => 100, + 'sort' => 1000 + ] + ]; + $setData = [ + 1 => $setElement, + 2 => [], + 3 => [] + ]; + $cached = [ + 1 => $setElement + ]; $cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class) ->disableOriginalConstructor() ->setMethods(['load', 'save', 'getFrontend', 'remove', 'clean']) @@ -192,21 +210,15 @@ public function testGetSetInfoCacheMiss() ->method('load') ->with($cacheKey) ->willReturn(false); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($cached) + ->willReturn($serializedData); $cacheMock ->expects($this->once()) ->method('save') ->with( - serialize( - [ - 1 => [ - 10000 => [ - 'group_id' => 10, - 'group_sort' => 100, - 'sort' => 1000 - ] - ] - ] - ), + $serializedData, $cacheKey, [\Magento\Eav\Model\Cache\Type::CACHE_TAG, \Magento\Eav\Model\Entity\Attribute::CACHE_TAG] ); @@ -242,17 +254,7 @@ public function testGetSetInfoCacheMiss() $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($connectionMock); $this->resourceMock->expects($this->any())->method('getTableName')->willReturn('_TABLE_'); $this->assertEquals( - [ - 1 => [ - 10000 => [ - 'group_id' => 10, - 'group_sort' => 100, - 'sort' => 1000 - ] - ], - 2 => [], - 3 => [] - ], + $setData, $this->model->getSetInfo([1, 2, 3], 1) ); } @@ -262,42 +264,41 @@ public function testGetSetInfoCacheMiss() */ public function testGetSetInfoCacheHit() { - $cached = [ - 1 => [ - 10000 => [ - 'group_id' => 10, - 'group_sort' => 100, - 'sort' => 1000 - ] + $setElement = [ + 10000 => [ + 'group_id' => 10, + 'group_sort' => 100, + 'sort' => 1000 ] ]; - + $setData = [ + 1 => $setElement, + 2 => [], + 3 => [] + ]; + $cached = [ + 1 => $setElement + ]; + $serializedData = 'serialized data'; $this->resourceMock->expects($this->never())->method('getConnection'); $this->eavConfigMock->expects($this->any())->method('isCacheEnabled')->willReturn(true); $cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class) ->disableOriginalConstructor() ->setMethods(['load', 'save', 'getFrontend', 'remove', 'clean']) ->getMock(); - $cacheMock - ->expects($this->once()) + $cacheMock->expects($this->once()) ->method('load') ->with(Set::ATTRIBUTES_CACHE_ID . 1) - ->willReturn(serialize($cached)); + ->willReturn($serializedData); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with($serializedData) + ->willReturn($cached); $this->eavConfigMock->expects($this->any())->method('getCache')->willReturn($cacheMock); $this->assertEquals( - [ - 1 => [ - 10000 => [ - 'group_id' => 10, - 'group_sort' => 100, - 'sort' => 1000 - ] - ], - 2 => [], - 3 => [] - ], + $setData, $this->model->getSetInfo([1, 2, 3], 1) ); } diff --git a/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php b/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php index 7d4561259cf88..67ea1912ccf8e 100644 --- a/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php +++ b/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php @@ -3,111 +3,166 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Eav\Test\Unit\Plugin\Model\ResourceModel\Entity; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\App\CacheInterface; +use Magento\Framework\App\Cache\StateInterface; +use Magento\Eav\Model\ResourceModel\Entity\Attribute as AttributeResource; +use Magento\Eav\Plugin\Model\ResourceModel\Entity\Attribute as AttributeResourcePlugin; +use Magento\Eav\Model\Cache\Type; +use Magento\Eav\Model\Entity\Attribute; class AttributeTest extends \PHPUnit_Framework_TestCase { - /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cache; + /** + * @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheMock; - /** @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cacheState; + /** + * @var StateInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheStateMock; + + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; + + /** + * @var AttributeResource|\PHPUnit_Framework_MockObject_MockObject + */ + private $attributeResourceMock; - /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute|\PHPUnit_Framework_MockObject_MockObject */ - protected $subject; + /** + * @var AttributeResourcePlugin + */ + private $attributeResourcePlugin; protected function setUp() { - $this->cache = $this->getMock(\Magento\Framework\App\CacheInterface::class); - $this->cacheState = $this->getMock(\Magento\Framework\App\Cache\StateInterface::class); - $this->subject = $this->getMock(\Magento\Eav\Model\ResourceModel\Entity\Attribute::class, [], [], '', false); + $objectManager = new ObjectManager($this); + $this->cacheMock = $this->getMock(CacheInterface::class); + $this->cacheStateMock = $this->getMock(StateInterface::class); + $this->attributeResourceMock = $this->getMock(AttributeResource::class, [], [], '', false); + $this->serializerMock = $this->getMock(SerializerInterface::class); + $this->attributeResourcePlugin = $objectManager->getObject( + AttributeResourcePlugin::class, + [ + 'cache' => $this->cacheMock, + 'cacheState' => $this->cacheStateMock, + 'serializer' => $this->serializerMock + ] + ); } - public function testGetStoreLabelsByAttributeIdOnCacheDisabled() + public function testAroundGetStoreLabelsByAttributeIdCacheIsDisabled() { - $this->cache->expects($this->never())->method('load'); + $attributeId = 1; + $this->cacheMock->expects($this->never()) + ->method('load'); + $this->cacheStateMock->expects($this->exactly(2)) + ->method('isEnabled') + ->with(Type::TYPE_IDENTIFIER) + ->willReturn(false); - $this->assertEquals( - 'attributeId', - $this->getAttribute(false)->aroundGetStoreLabelsByAttributeId( - $this->subject, - $this->mockPluginProceed('attributeId'), - 'attributeId' - ) + $isProceedCalled = false; + // @SuppressWarnings(PHPMD.UnusedFormalParameter) + $proceed = function($attributeId) use (&$isProceedCalled) { + $isProceedCalled = true; + }; + + $this->attributeResourcePlugin->aroundGetStoreLabelsByAttributeId( + $this->attributeResourceMock, + $proceed, + $attributeId ); + $this->assertTrue($isProceedCalled); } - public function testGetStoreLabelsByAttributeIdFromCache() + public function testAroundGetStoreLabelsByAttributeIdCacheExists() { $attributeId = 1; - $attributes = ['k' => 'v']; - $cacheId = \Magento\Eav\Plugin\Model\ResourceModel\Entity\Attribute::STORE_LABEL_ATTRIBUTE . $attributeId; - $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(serialize($attributes)); + $attributes = ['foo' => 'bar']; + $serializedAttributes = 'serialized attributes'; + $cacheId = AttributeResourcePlugin::STORE_LABEL_ATTRIBUTE . $attributeId; + $this->cacheStateMock->expects($this->once()) + ->method('isEnabled') + ->with(Type::TYPE_IDENTIFIER) + ->willReturn(true); + $this->cacheMock->expects($this->once()) + ->method('load') + ->with($cacheId) + ->willReturn($serializedAttributes); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with($serializedAttributes) + ->willReturn($attributes); + + $isProceedCalled = false; + // @SuppressWarnings(PHPMD.UnusedFormalParameter) + $proceed = function($attributeId) use (&$isProceedCalled) { + $isProceedCalled = true; + }; $this->assertEquals( $attributes, - $this->getAttribute(true)->aroundGetStoreLabelsByAttributeId( - $this->subject, - $this->mockPluginProceed(), + $this->attributeResourcePlugin->aroundGetStoreLabelsByAttributeId( + $this->attributeResourceMock, + $proceed, $attributeId ) ); + $this->assertFalse($isProceedCalled); } - public function testGetStoreLabelsByAttributeIdWithCacheSave() + public function testAroundGetStoreLabelsByAttributeIdCacheDoesNotExist() { $attributeId = 1; - $cacheId = \Magento\Eav\Plugin\Model\ResourceModel\Entity\Attribute::STORE_LABEL_ATTRIBUTE . $attributeId; - $this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(false); - $this->cache->expects($this->any())->method('save')->with( - serialize([$attributeId]), - $cacheId, - [ - \Magento\Eav\Model\Cache\Type::CACHE_TAG, - \Magento\Eav\Model\Entity\Attribute::CACHE_TAG - ] - ); + $attributes = ['foo' => 'bar']; + $serializedAttributes = 'serialized attributes'; + $cacheId = AttributeResourcePlugin::STORE_LABEL_ATTRIBUTE . $attributeId; + $this->cacheStateMock->expects($this->exactly(2)) + ->method('isEnabled') + ->with(Type::TYPE_IDENTIFIER) + ->willReturn(true); + $this->cacheMock->expects($this->once()) + ->method('load') + ->with($cacheId) + ->willReturn(false); + $this->serializerMock->expects($this->never()) + ->method('unserialize') + ->with($serializedAttributes) + ->willReturn($attributes); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($attributes) + ->willReturn($serializedAttributes); + $this->cacheMock->expects($this->once()) + ->method('save') + ->with( + $serializedAttributes, + $cacheId, + [ + Type::CACHE_TAG, + Attribute::CACHE_TAG + ] + ); + + // @SuppressWarnings(PHPMD.UnusedFormalParameter) + $proceed = function($attributeId) use ($attributes) { + return $attributes; + }; $this->assertEquals( - [$attributeId], - $this->getAttribute(true)->aroundGetStoreLabelsByAttributeId( - $this->subject, - $this->mockPluginProceed([$attributeId]), + $attributes, + $this->attributeResourcePlugin->aroundGetStoreLabelsByAttributeId( + $this->attributeResourceMock, + $proceed, $attributeId ) ); } - - /** - * @param bool $cacheEnabledFlag - * @return \Magento\Eav\Plugin\Model\ResourceModel\Entity\Attribute - */ - protected function getAttribute($cacheEnabledFlag) - { - $this->cacheState->expects($this->any())->method('isEnabled') - ->with(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER)->willReturn($cacheEnabledFlag); - return (new ObjectManager($this))->getObject( - \Magento\Eav\Plugin\Model\ResourceModel\Entity\Attribute::class, - [ - 'cache' => $this->cache, - 'cacheState' => $this->cacheState - ] - ); - } - - /** - * @param mixed $returnValue - * @return callable - */ - protected function mockPluginProceed($returnValue = null) - { - return function () use ($returnValue) { - return $returnValue; - }; - } } diff --git a/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php new file mode 100644 index 0000000000000..97b3cb4731771 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php @@ -0,0 +1,49 @@ +objectManager = Bootstrap::getObjectManager(); + $this->config = $this->objectManager->get(Config::class); + } + + public function testGetEntityAttributeCodes() + { + $entityType = 'catalog_product'; + $this->cleanAllCache(); + $this->assertEquals( + $this->config->getEntityAttributeCodes($entityType), + $this->config->getEntityAttributeCodes($entityType) + ); + } + + private function cleanAllCache() + { + /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ + $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); + /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ + foreach ($cachePool as $cacheType) { + $cacheType->getBackend()->clean(); + } + } +} From bf646df6705599ab742656949b26ebbb82582cce Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 27 Oct 2016 16:55:17 -0500 Subject: [PATCH 108/144] MAGETWO-59440: Refactor Module_Eav Introducing SerializerInterface --- .../testsuite/Magento/Eav/Model/ConfigTest.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php index 97b3cb4731771..4cb90c2c98c56 100644 --- a/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php @@ -8,6 +8,7 @@ use Magento\Eav\Model\Config; use Magento\TestFramework\ObjectManager; use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\Helper\CacheCleaner; class ConfigTest extends \PHPUnit_Framework_TestCase { @@ -30,20 +31,10 @@ protected function setUp() public function testGetEntityAttributeCodes() { $entityType = 'catalog_product'; - $this->cleanAllCache(); + CacheCleaner::cleanAll(); $this->assertEquals( $this->config->getEntityAttributeCodes($entityType), $this->config->getEntityAttributeCodes($entityType) ); } - - private function cleanAllCache() - { - /** @var \Magento\Framework\App\Cache\Frontend\Pool $cachePool */ - $cachePool = $this->objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class); - /** @var \Magento\Framework\Cache\FrontendInterface $cacheType */ - foreach ($cachePool as $cacheType) { - $cacheType->getBackend()->clean(); - } - } } From 45d7ddc6b93ed704e65e1fc5279ef494fc1b4ca0 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 27 Oct 2016 17:22:53 -0500 Subject: [PATCH 109/144] MAGETWO-59440: Refactor Module_Eav Introducing SerializerInterface --- app/code/Magento/Eav/Model/Config.php | 1 + .../Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Eav/Model/Config.php b/app/code/Magento/Eav/Model/Config.php index 89d0bc93c8007..0d0237a1f6732 100644 --- a/app/code/Magento/Eav/Model/Config.php +++ b/app/code/Magento/Eav/Model/Config.php @@ -105,6 +105,7 @@ class Config * @param \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory * @param \Magento\Framework\App\Cache\StateInterface $cacheState * @param \Magento\Framework\Validator\UniversalFactory $universalFactory + * @param SerializerInterface $serializer * @codeCoverageIgnore */ public function __construct( diff --git a/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php b/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php index 3452d452ac490..56bad8bf75e11 100644 --- a/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php +++ b/app/code/Magento/Eav/Plugin/Model/ResourceModel/Entity/Attribute.php @@ -47,7 +47,7 @@ public function __construct( ) { $this->cache = $cache; $this->serializer = $serializer; - $this->cacheState = $cacheState;; + $this->cacheState = $cacheState; } /** From 5b636d0b7dd63e64c9ff0a980ef0ffe2a16fe8aa Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 27 Oct 2016 17:50:41 -0500 Subject: [PATCH 110/144] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Fixing tests --- .../testsuite/Magento/Webapi/Model/ServiceMetadataTest.php | 7 ++++--- .../testsuite/Magento/Webapi/Model/Soap/ConfigTest.php | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php index 7bfbe08a34a09..8fac1c9b0e703 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php @@ -7,10 +7,11 @@ use Magento\TestFramework\Helper\Bootstrap; use Magento\Customer\Api\AccountManagementInterface; +use Magento\Framework\Exception\LocalizedException; class ServiceMetadataTest extends \PHPUnit_Framework_TestCase { - /**bootstrap.sh + /** * @var ServiceMetadata */ private $serviceMetadata; @@ -57,7 +58,7 @@ public function testGetServiceMetadata() ] ], 'throws' => [ - "\\Magento\\Framework\\Exception\\LocalizedException" + '\\' . LocalizedException::class ] ] ] @@ -106,7 +107,7 @@ public function testGetRouteMetadata() ] ], 'throws' => [ - "\\Magento\\Framework\\Exception\\LocalizedException" + '\\' . LocalizedException::class ] ] ] diff --git a/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php index 95743e9e10537..ff1634c60782a 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php @@ -8,6 +8,7 @@ use Magento\TestFramework\Helper\Bootstrap; use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Api\CustomerRepositoryInterface; +use Magento\Framework\Exception\LocalizedException; class ConfigTest extends \PHPUnit_Framework_TestCase { @@ -60,7 +61,7 @@ public function testGetRequestedSoapServices() ] ], 'throws' => [ - "\\Magento\\Framework\\Exception\\LocalizedException" + '\\' . LocalizedException::class ] ] ] From 7c4aadb5b09ad3905a99c68329dd750ccafcb7d2 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 27 Oct 2016 18:31:33 -0500 Subject: [PATCH 111/144] MAGETWO-59440: Refactor Module_Eav Fixing tests --- .../Plugin/Model/ResourceModel/Entity/AttributeTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php b/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php index 67ea1912ccf8e..3fa739d4096a8 100644 --- a/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php +++ b/app/code/Magento/Eav/Test/Unit/Plugin/Model/ResourceModel/Entity/AttributeTest.php @@ -70,7 +70,7 @@ public function testAroundGetStoreLabelsByAttributeIdCacheIsDisabled() $isProceedCalled = false; // @SuppressWarnings(PHPMD.UnusedFormalParameter) - $proceed = function($attributeId) use (&$isProceedCalled) { + $proceed = function ($attributeId) use (&$isProceedCalled) { $isProceedCalled = true; }; @@ -103,7 +103,7 @@ public function testAroundGetStoreLabelsByAttributeIdCacheExists() $isProceedCalled = false; // @SuppressWarnings(PHPMD.UnusedFormalParameter) - $proceed = function($attributeId) use (&$isProceedCalled) { + $proceed = function ($attributeId) use (&$isProceedCalled) { $isProceedCalled = true; }; @@ -152,7 +152,7 @@ public function testAroundGetStoreLabelsByAttributeIdCacheDoesNotExist() ); // @SuppressWarnings(PHPMD.UnusedFormalParameter) - $proceed = function($attributeId) use ($attributes) { + $proceed = function ($attributeId) use ($attributes) { return $attributes; }; From d196f666a1e1e3674fbbf1c6534f35690fdad2fb Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 27 Oct 2016 21:24:00 -0500 Subject: [PATCH 112/144] MAGETWO-59440: Refactor Module_Eav --- app/code/Magento/Catalog/Model/Config.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Config.php b/app/code/Magento/Catalog/Model/Config.php index dec29a925cc4d..70d11f2e282b4 100644 --- a/app/code/Magento/Catalog/Model/Config.php +++ b/app/code/Magento/Catalog/Model/Config.php @@ -7,6 +7,7 @@ // @codingStandardsIgnoreFile namespace Magento\Catalog\Model; +use Magento\Framework\Serialize\SerializerInterface; /** * @SuppressWarnings(PHPMD.LongVariable) @@ -132,6 +133,7 @@ class Config extends \Magento\Eav\Model\Config * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Eav\Model\Config $eavConfig + * @param SerializerInterface $serializer * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -147,7 +149,8 @@ public function __construct( \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Eav\Model\Config $eavConfig + \Magento\Eav\Model\Config $eavConfig, + SerializerInterface $serializer = null ) { $this->_scopeConfig = $scopeConfig; $this->_configFactory = $configFactory; @@ -157,7 +160,14 @@ public function __construct( $this->_storeManager = $storeManager; $this->_eavConfig = $eavConfig; - parent::__construct($cache, $entityTypeFactory, $entityTypeCollectionFactory, $cacheState, $universalFactory); + parent::__construct( + $cache, + $entityTypeFactory, + $entityTypeCollectionFactory, + $cacheState, + $universalFactory, + $serializer + ); } /** From ec668787347ccfe2c345a7e82f3e9205287d4a36 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 28 Oct 2016 10:34:02 -0500 Subject: [PATCH 113/144] MAGETWO-59440: Refactor Module_Eav Adding integration test for Magento\Catalog\Model\Config --- .../Magento/Catalog/Model/ConfigTest.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Model/ConfigTest.php diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ConfigTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ConfigTest.php new file mode 100644 index 0000000000000..178107e487e92 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ConfigTest.php @@ -0,0 +1,61 @@ +objectManager = Bootstrap::getObjectManager(); + $this->config = $this->objectManager->get(Config::class); + } + + public function testGetEntityAttributeCodes() + { + $entityType = 'catalog_product'; + CacheCleaner::cleanAll(); + $this->assertEquals( + $this->config->getEntityAttributeCodes($entityType), + $this->config->getEntityAttributeCodes($entityType) + ); + } + + public function testGetAttribute() + { + $entityType = 'catalog_product'; + $attributeCode = 'color'; + CacheCleaner::cleanAll(); + $this->assertEquals( + $this->config->getAttribute($entityType, $attributeCode), + $this->config->getAttribute($entityType, $attributeCode) + ); + } + + public function testGetEntityType() + { + $entityType = 'catalog_product'; + CacheCleaner::cleanAll(); + $this->assertEquals( + $this->config->getEntityType($entityType), + $this->config->getEntityType($entityType) + ); + } +} From 220835ac851f56ff38ff2c3b11448a1808dfa019 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 28 Oct 2016 17:53:37 -0500 Subject: [PATCH 114/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Removing CouplingBetweenObjects annotation --- lib/internal/Magento/Framework/Validator/Factory.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index b3d7147f42029..3b8608239be12 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -1,23 +1,20 @@ Date: Fri, 28 Oct 2016 17:55:36 -0500 Subject: [PATCH 115/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Removing CouplingBetweenObjects annotation --- lib/internal/Magento/Framework/Validator/Factory.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index 3b8608239be12..f646748b15840 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -82,8 +82,8 @@ protected function _initializeConfigList() $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); $this->cache->save($this->getSerializer()->serialize($this->_configFiles->toArray()), self::CACHE_KEY); } else { - $filesArray = $this->getSerializer()->unserialize($this->_configFiles); - $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray)); + $files = $this->getSerializer()->unserialize($this->_configFiles); + $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($files)); } } } @@ -161,8 +161,7 @@ public function createValidator($entityName, $groupName, array $builderConfig = private function getSerializer() { if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Serialize\SerializerInterface::class); + $this->serializer = $this->_objectManager->get(\Magento\Framework\Serialize\SerializerInterface::class); } return $this->serializer; } @@ -176,7 +175,7 @@ private function getSerializer() private function getFileIteratorFactory() { if ($this->fileIteratorFactory === null) { - $this->fileIteratorFactory = \Magento\Framework\App\ObjectManager::getInstance() + $this->fileIteratorFactory = $this->_objectManager ->get(\Magento\Framework\Config\FileIteratorFactory::class); } return $this->fileIteratorFactory; From 5691491cace5ce63e949821bc3f37e55cb9d5dde Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 28 Oct 2016 17:56:57 -0500 Subject: [PATCH 116/144] MAGETWO-58638: Refactor Framework_App, Framework_Config Renaming variable --- lib/internal/Magento/Framework/Validator/Factory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Validator/Factory.php b/lib/internal/Magento/Framework/Validator/Factory.php index f646748b15840..76633f48dfe0b 100644 --- a/lib/internal/Magento/Framework/Validator/Factory.php +++ b/lib/internal/Magento/Framework/Validator/Factory.php @@ -82,8 +82,8 @@ protected function _initializeConfigList() $this->_configFiles = $this->moduleReader->getConfigurationFiles('validation.xml'); $this->cache->save($this->getSerializer()->serialize($this->_configFiles->toArray()), self::CACHE_KEY); } else { - $files = $this->getSerializer()->unserialize($this->_configFiles); - $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($files)); + $filesArray = $this->getSerializer()->unserialize($this->_configFiles); + $this->_configFiles = $this->getFileIteratorFactory()->create(array_keys($filesArray)); } } } From 1cfec96ed584849fcbe84300cff40783c9c832aa Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 1 Nov 2016 14:54:12 -0500 Subject: [PATCH 117/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Introducing SerializerInterface, removing obsolete code --- .../ObjectManager/ConfigLoader/Compiled.php | 2 +- .../Framework/App/ObjectManagerFactory.php | 4 +- .../ObjectManager/Definition/Compiled.php | 2 +- .../ObjectManager/DefinitionFactory.php | 78 +++-------- .../Test/Unit/DefinitionFactoryTest.php | 122 ++++++------------ .../Di/Compiler/Config/Writer/Filesystem.php | 2 +- 6 files changed, 61 insertions(+), 149 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index 26ef880e151ff..8b92737752571 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -46,7 +46,7 @@ public function load($area) public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . $diPath . '/' . $area . '.json'; + return BP . $diPath . '/' . $area . '.ser'; } /** diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index bfe94a9dccc64..a16795ec6c9a6 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -117,7 +117,7 @@ public function create(array $arguments) $arguments = array_merge($deploymentConfig->get(), $arguments); $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory( $this->driverPool->getDriver(DriverPool::FILE), - $this->directoryList->getPath(DirectoryList::DI), + new \Magento\Framework\Serialize\Serializer\Json(), $this->directoryList->getPath(DirectoryList::GENERATION) ); @@ -127,7 +127,7 @@ public function create(array $arguments) /** @var EnvironmentFactory $envFactory */ $envFactory = new $this->envFactoryClassName($relations, $definitions); /** @var EnvironmentInterface $env */ - $env = $envFactory->createEnvironment(); + $env = $envFactory->createEnvironment(); /** @var ConfigInterface $diConfig */ $diConfig = $env->getDiConfig(); diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php index db91618dccd23..e2a4f0c34a2a8 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php @@ -22,7 +22,7 @@ class Compiled implements \Magento\Framework\ObjectManager\DefinitionInterface /** * @var \Magento\Framework\Code\Reader\ClassReaderInterface */ - protected $reader ; + protected $reader; /** * @var SerializerInterface diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index 4e9026eb7b20d..f818296c526d8 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -1,33 +1,22 @@ _filesystemDriver = $filesystemDriver; - $this->_definitionDir = $definitionDir; + $this->serializer = $serializer; $this->_generationDir = $generationDir; - $this->_definitionFormat = $definitionFormat; } /** * Create class definitions * * @param mixed $definitions - * @return Runtime + * @return Compiled|Runtime */ public function createClassDefinition($definitions = false) { @@ -110,14 +91,7 @@ public function createClassDefinition($definitions = false) */ public function createPluginDefinition() { - $path = $this->_definitionDir . '/plugins.json'; - if ($this->_filesystemDriver->isReadable($path)) { - return new \Magento\Framework\Interception\Definition\Compiled( - $this->_unpack($this->_filesystemDriver->fileGetContents($path)) - ); - } else { - return new \Magento\Framework\Interception\Definition\Runtime(); - } + return new \Magento\Framework\Interception\Definition\Runtime(); } /** @@ -127,25 +101,7 @@ public function createPluginDefinition() */ public function createRelations() { - $path = $this->_definitionDir . '/relations.json'; - if ($this->_filesystemDriver->isReadable($path)) { - return new \Magento\Framework\ObjectManager\Relations\Compiled( - $this->_unpack($this->_filesystemDriver->fileGetContents($path)) - ); - } else { - return new \Magento\Framework\ObjectManager\Relations\Runtime(); - } - } - - /** - * Gets supported definition formats - * - * @return array - * @deprecated - */ - public static function getSupportedFormats() - { - return []; + return new \Magento\Framework\ObjectManager\Relations\Runtime(); } /** @@ -156,7 +112,7 @@ public static function getSupportedFormats() */ protected function _unpack($definitions) { - return json_decode($definitions); + return $this->serializer->unserialize($definitions); } /** diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index 3861d274b5243..ca086c23aefe4 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -3,118 +3,74 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Framework\ObjectManager\Test\Unit; use Magento\Framework\ObjectManager\Definition\Compiled; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Filesystem\DriverInterface; +use Magento\Framework\ObjectManager\DefinitionFactory; +use Magento\Framework\ObjectManager\Definition\Runtime; class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\Filesystem\DriverInterface | \PHPUnit_Framework_MockObject_MockObject + * @var DriverInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $filesystemDriverMock; + private $filesystemDriverMock; /** - * @var \Magento\Framework\ObjectManager\DefinitionFactory + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $model; + private $serializerMock; /** - * @var string + * @var DefinitionFactory */ - protected $sampleContent; + private $definitionFactory; protected function setUp() { - $this->sampleContent = '[1,2,3]'; - $this->filesystemDriverMock = $this->getMock( - \Magento\Framework\Filesystem\Driver\File::class, - [], - [], - '', - false - ); - $this->model = new \Magento\Framework\ObjectManager\DefinitionFactory( + $this->filesystemDriverMock = $this->getMock(DriverInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); + $this->definitionFactory = new DefinitionFactory( $this->filesystemDriverMock, - 'DefinitionDir', - 'GenerationDir' + $this->serializerMock, + 'generation dir' ); } - public function testCreateClassDefinitionFromString() + public function testCreateClassDefinitionSerialized() { + $serializedDefinitions = 'serialized definitions'; + $definitions = [[], []]; + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with($serializedDefinitions) + ->willReturn($definitions); $this->assertInstanceOf( - \Magento\Framework\ObjectManager\Definition\Compiled::class, - $this->model->createClassDefinition($this->sampleContent) + Compiled::class, + $this->definitionFactory->createClassDefinition($serializedDefinitions) ); } - /** - * @param string $path - * @param string $callMethod - * @param string $expectedClass - * @dataProvider createPluginsAndRelationsReadableDataProvider - */ - public function testCreatePluginsAndRelationsReadable($path, $callMethod, $expectedClass) - { - $this->filesystemDriverMock->expects($this->once())->method('isReadable') - ->with($path) - ->will($this->returnValue(true)); - $this->filesystemDriverMock->expects($this->once())->method('fileGetContents') - ->with($path) - ->will($this->returnValue($this->sampleContent)); - $this->assertInstanceOf($expectedClass, $this->model->$callMethod()); - } - - public function createPluginsAndRelationsReadableDataProvider() + public function testCreateClassDefinitionArray() { - return [ - 'relations' => [ - 'DefinitionDir/relations.json', - 'createRelations', \Magento\Framework\ObjectManager\Relations\Compiled::class, - ], - 'plugins' => [ - 'DefinitionDir/plugins.json', - 'createPluginDefinition', \Magento\Framework\Interception\Definition\Compiled::class, - ], - ]; - } - - /** - * @param string $path - * @param string $callMethod - * @param string $expectedClass - * @dataProvider createPluginsAndRelationsNotReadableDataProvider - */ - public function testCreatePluginsAndRelationsNotReadable($path, $callMethod, $expectedClass) - { - $this->filesystemDriverMock->expects($this->once())->method('isReadable') - ->with($path) - ->will($this->returnValue(false)); - $this->assertInstanceOf($expectedClass, $this->model->$callMethod()); - } - - public function createPluginsAndRelationsNotReadableDataProvider() - { - return [ - 'relations' => [ - 'DefinitionDir/relations.json', - 'createRelations', \Magento\Framework\ObjectManager\Relations\Runtime::class, - ], - 'plugins' => [ - 'DefinitionDir/plugins.json', - 'createPluginDefinition', \Magento\Framework\Interception\Definition\Runtime::class, - ], - ]; + $definitions = [[], []]; + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->assertInstanceOf( + Compiled::class, + $this->definitionFactory->createClassDefinition($definitions) + ); } - public function testGetSupportedFormats() + public function testCreateClassDefinition() { - $actual = \Magento\Framework\ObjectManager\DefinitionFactory::getSupportedFormats(); - $this->assertInternalType('array', $actual); - foreach ($actual as $className) { - $this->assertInternalType('string', $className); - } + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->assertInstanceOf( + Runtime::class, + $this->definitionFactory->createClassDefinition() + ); } } 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 3e2620fc20861..342b26a22e32b 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 @@ -46,7 +46,7 @@ public function write($key, array $config) $this->initialize(); file_put_contents( - $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.json', + $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.ser', $this->getSerializer()->serialize($config) ); } From 96603ae3c9f19ee2d237453dfe3f193542422611 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 1 Nov 2016 16:01:49 -0500 Subject: [PATCH 118/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Introducing SerializerInterface, removing obsolete code --- .../App/ObjectManager/ConfigLoader/Compiled.php | 2 +- .../Framework/ObjectManager/DefinitionFactory.php | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index 8b92737752571..669c9f4121ac0 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -46,7 +46,7 @@ public function load($area) public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . $diPath . '/' . $area . '.ser'; + return BP . '/' . $diPath . '/' . $area . '.ser'; } /** diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index f818296c526d8..7d9c3ea5e0b21 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -11,6 +11,7 @@ use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator; use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\ObjectManager\Definition\Compiled; +use Magento\Framework\Code\Generator\Autoloader; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -31,11 +32,6 @@ class DefinitionFactory */ protected $_filesystemDriver; - /** - * @var string - */ - protected static $definitionClasses = Compiled::class; - /** * @var \Magento\Framework\Code\Generator */ @@ -65,7 +61,7 @@ public function __construct( * Create class definitions * * @param mixed $definitions - * @return Compiled|Runtime + * @return DefinitionInterface */ public function createClassDefinition($definitions = false) { @@ -73,10 +69,9 @@ public function createClassDefinition($definitions = false) if (is_string($definitions)) { $definitions = $this->_unpack($definitions); } - $definitionModel = self::$definitionClasses; - $result = new $definitionModel($definitions); + $result = new Compiled($definitions); } else { - $autoloader = new \Magento\Framework\Code\Generator\Autoloader($this->getCodeGenerator()); + $autoloader = new Autoloader($this->getCodeGenerator()); spl_autoload_register([$autoloader, 'load']); $result = new Runtime(); From 3cb01f17a1b201de8a89c90371e75c2cfdb667b9 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 1 Nov 2016 16:05:04 -0500 Subject: [PATCH 119/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Introducing SerializerInterface, removing obsolete code --- .../ObjectManager/Test/Unit/DefinitionFactoryTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index ca086c23aefe4..8ad1ac13a12cd 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -7,14 +7,14 @@ use Magento\Framework\ObjectManager\Definition\Compiled; use Magento\Framework\Serialize\SerializerInterface; -use Magento\Framework\Filesystem\DriverInterface; +use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\ObjectManager\DefinitionFactory; use Magento\Framework\ObjectManager\Definition\Runtime; class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var DriverInterface|\PHPUnit_Framework_MockObject_MockObject + * @var File|\PHPUnit_Framework_MockObject_MockObject */ private $filesystemDriverMock; @@ -30,7 +30,7 @@ class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->filesystemDriverMock = $this->getMock(DriverInterface::class); + $this->filesystemDriverMock = $this->getMock(File::class); $this->serializerMock = $this->getMock(SerializerInterface::class); $this->definitionFactory = new DefinitionFactory( $this->filesystemDriverMock, From 67918750a813ebfce65faf6c3d2ea0d939e2da6b Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 1 Nov 2016 16:38:20 -0500 Subject: [PATCH 120/144] MAGETWO-60357: Extra parameter passed to parent constructor in \Magento\Indexer\Model\Config\Data::__construct Removing extra parameter --- .../Magento/Indexer/Model/Config/Data.php | 22 ++++++++-- .../Test/Unit/Model/Config/DataTest.php | 43 ++++++++++++++++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index 703c875d1ef4e..7fdd3b326d5af 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -6,6 +6,7 @@ namespace Magento\Indexer\Model\Config; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; class Data extends \Magento\Framework\Config\Data { @@ -21,20 +22,18 @@ class Data extends \Magento\Framework\Config\Data * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection * @param string $cacheId - * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Indexer\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection, - $cacheId = 'indexer_config', - SerializerInterface $serializer = null + $cacheId = 'indexer_config' ) { $this->stateCollection = $stateCollection; $isCacheExists = $cache->test($cacheId); - parent::__construct($reader, $cache, $cacheId, $serializer); + parent::__construct($reader, $cache, $cacheId); if (!$isCacheExists) { $this->deleteNonexistentStates(); @@ -55,4 +54,19 @@ protected function deleteNonexistentStates() } } } + + /** + * Get serializer + * + * @return SerializerInterface + * @deprecated + */ + protected function getSerializer() + { + if ($this->serializer === null) { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() + ->get(Serialize::class); + } + return $this->serializer; + } } 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 e16c21b8f1125..fe66da6366e4d 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -5,6 +5,9 @@ */ namespace Magento\Indexer\Test\Unit\Model\Config; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; + class DataTest extends \PHPUnit_Framework_TestCase { /** @@ -38,7 +41,7 @@ class DataTest extends \PHPUnit_Framework_TestCase protected $indexers = ['indexer1' => [], 'indexer3' => []]; /** - * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $serializerMock; @@ -61,7 +64,14 @@ protected function setUp() '', false ); - $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); + } + + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testConstructorWithCache() @@ -80,12 +90,13 @@ public function testConstructorWithCache() $this->stateCollection->expects($this->never())->method('getItems'); + $this->mockObjectManager([Serialize::class => $this->serializerMock]); + $this->model = new \Magento\Indexer\Model\Config\Data( $this->reader, $this->cache, $this->stateCollection, - $this->cacheId, - $this->serializerMock + $this->cacheId ); } @@ -120,12 +131,32 @@ public function testConstructorWithoutCache() $this->stateCollection->expects($this->once())->method('getItems')->will($this->returnValue($states)); + $this->mockObjectManager([Serialize::class => $this->serializerMock]); + $this->model = new \Magento\Indexer\Model\Config\Data( $this->reader, $this->cache, $this->stateCollection, - $this->cacheId, - $this->serializerMock + $this->cacheId ); } + + /** + * Mock application object manager to return configured dependencies + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } From dfdc20b5562e6aaf7a5cd2638e8af2bf6b1b79a2 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 1 Nov 2016 17:26:44 -0500 Subject: [PATCH 121/144] MAGETWO-60357: Extra parameter passed to parent constructor in \Magento\Indexer\Model\Config\Data::__construct Reverting commit 67918750a813ebfce65faf6c3d2ea0d939e2da6b --- .../Magento/Indexer/Model/Config/Data.php | 22 ++-------- .../Test/Unit/Model/Config/DataTest.php | 43 +++---------------- 2 files changed, 10 insertions(+), 55 deletions(-) diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index 7fdd3b326d5af..703c875d1ef4e 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -6,7 +6,6 @@ namespace Magento\Indexer\Model\Config; use Magento\Framework\Serialize\SerializerInterface; -use Magento\Framework\Serialize\Serializer\Serialize; class Data extends \Magento\Framework\Config\Data { @@ -22,18 +21,20 @@ class Data extends \Magento\Framework\Config\Data * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Indexer\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection, - $cacheId = 'indexer_config' + $cacheId = 'indexer_config', + SerializerInterface $serializer = null ) { $this->stateCollection = $stateCollection; $isCacheExists = $cache->test($cacheId); - parent::__construct($reader, $cache, $cacheId); + parent::__construct($reader, $cache, $cacheId, $serializer); if (!$isCacheExists) { $this->deleteNonexistentStates(); @@ -54,19 +55,4 @@ protected function deleteNonexistentStates() } } } - - /** - * Get serializer - * - * @return SerializerInterface - * @deprecated - */ - protected function getSerializer() - { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(Serialize::class); - } - return $this->serializer; - } } 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 fe66da6366e4d..e16c21b8f1125 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -5,9 +5,6 @@ */ namespace Magento\Indexer\Test\Unit\Model\Config; -use Magento\Framework\Serialize\SerializerInterface; -use Magento\Framework\Serialize\Serializer\Serialize; - class DataTest extends \PHPUnit_Framework_TestCase { /** @@ -41,7 +38,7 @@ class DataTest extends \PHPUnit_Framework_TestCase protected $indexers = ['indexer1' => [], 'indexer3' => []]; /** - * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $serializerMock; @@ -64,14 +61,7 @@ protected function setUp() '', false ); - $this->serializerMock = $this->getMock(SerializerInterface::class); - } - - protected function tearDown() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); } public function testConstructorWithCache() @@ -90,13 +80,12 @@ public function testConstructorWithCache() $this->stateCollection->expects($this->never())->method('getItems'); - $this->mockObjectManager([Serialize::class => $this->serializerMock]); - $this->model = new \Magento\Indexer\Model\Config\Data( $this->reader, $this->cache, $this->stateCollection, - $this->cacheId + $this->cacheId, + $this->serializerMock ); } @@ -131,32 +120,12 @@ public function testConstructorWithoutCache() $this->stateCollection->expects($this->once())->method('getItems')->will($this->returnValue($states)); - $this->mockObjectManager([Serialize::class => $this->serializerMock]); - $this->model = new \Magento\Indexer\Model\Config\Data( $this->reader, $this->cache, $this->stateCollection, - $this->cacheId + $this->cacheId, + $this->serializerMock ); } - - /** - * Mock application object manager to return configured dependencies - * - * @param array $dependencies - * @return void - */ - private function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } } From a9ce69f1bf22681f294968eedebc7c5fcc944a36 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 11:00:27 -0500 Subject: [PATCH 122/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Removing obsolete code --- .../Framework/App/ObjectManagerFactory.php | 32 +-------- .../Interception/Definition/Compiled.php | 39 ----------- .../Test/Unit/Definition/CompiledTest.php | 24 ------- .../ObjectManager/DefinitionFactory.php | 33 ++------- .../ObjectManager/Relations/Compiled.php | 55 --------------- .../Test/Unit/Definition/CompiledTest.php | 68 ------------------- .../Test/Unit/DefinitionFactoryTest.php | 41 ++++------- .../Test/Unit/Relations/CompiledTest.php | 27 -------- 8 files changed, 17 insertions(+), 302 deletions(-) delete mode 100644 lib/internal/Magento/Framework/Interception/Definition/Compiled.php delete mode 100644 lib/internal/Magento/Framework/Interception/Test/Unit/Definition/CompiledTest.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledTest.php delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Relations/CompiledTest.php diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index a16795ec6c9a6..a67304d798609 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -117,11 +117,10 @@ public function create(array $arguments) $arguments = array_merge($deploymentConfig->get(), $arguments); $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory( $this->driverPool->getDriver(DriverPool::FILE), - new \Magento\Framework\Serialize\Serializer\Json(), $this->directoryList->getPath(DirectoryList::GENERATION) ); - $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions')); + $definitions = $definitionFactory->createClassDefinition(); $relations = $definitionFactory->createRelations(); /** @var EnvironmentFactory $envFactory */ @@ -287,33 +286,4 @@ protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, } return $configData; } - - /** - * Crete plugin list object - * - * @param \Magento\Framework\ObjectManagerInterface $objectManager - * @param \Magento\Framework\ObjectManager\RelationsInterface $relations - * @param \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory - * @param \Magento\Framework\ObjectManager\Config\Config $diConfig - * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions - * @return \Magento\Framework\Interception\PluginList\PluginList - */ - protected function _createPluginList( - \Magento\Framework\ObjectManagerInterface $objectManager, - \Magento\Framework\ObjectManager\RelationsInterface $relations, - \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory, - \Magento\Framework\ObjectManager\Config\Config $diConfig, - \Magento\Framework\ObjectManager\DefinitionInterface $definitions - ) { - return $objectManager->create( - \Magento\Framework\Interception\PluginList\PluginList::class, - [ - 'relations' => $relations, - 'definitions' => $definitionFactory->createPluginDefinition(), - 'omConfig' => $diConfig, - 'classDefinitions' => $definitions instanceof - \Magento\Framework\ObjectManager\Definition\Compiled ? $definitions : null - ] - ); - } } diff --git a/lib/internal/Magento/Framework/Interception/Definition/Compiled.php b/lib/internal/Magento/Framework/Interception/Definition/Compiled.php deleted file mode 100644 index 6fbe9c99dce86..0000000000000 --- a/lib/internal/Magento/Framework/Interception/Definition/Compiled.php +++ /dev/null @@ -1,39 +0,0 @@ -_definitions = $definitions; - } - - /** - * Retrieve list of methods - * - * @param string $type - * @return string[] - */ - public function getMethodList($type) - { - return $this->_definitions[$type]; - } -} diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/Definition/CompiledTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/Definition/CompiledTest.php deleted file mode 100644 index a64cd96b62966..0000000000000 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Definition/CompiledTest.php +++ /dev/null @@ -1,24 +0,0 @@ - 'definitions']; - - /** - * @covers \Magento\Framework\Interception\Definition\Compiled::getMethodList - * @covers \Magento\Framework\Interception\Definition\Compiled::__construct - */ - public function testGetMethodList() - { - $model = new \Magento\Framework\Interception\Definition\Compiled($this->_definitions); - $this->assertEquals('definitions', $model->getMethodList('type')); - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index 7d9c3ea5e0b21..79d398e3da079 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -10,7 +10,6 @@ use Magento\Framework\ObjectManager\Definition\Runtime; use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator; use Magento\Framework\Serialize\SerializerInterface; -use Magento\Framework\ObjectManager\Definition\Compiled; use Magento\Framework\Code\Generator\Autoloader; /** @@ -44,39 +43,26 @@ class DefinitionFactory /** * @param DriverInterface $filesystemDriver - * @param SerializerInterface $serializer * @param string $generationDir */ public function __construct( DriverInterface $filesystemDriver, - SerializerInterface $serializer, $generationDir ) { $this->_filesystemDriver = $filesystemDriver; - $this->serializer = $serializer; $this->_generationDir = $generationDir; } /** * Create class definitions * - * @param mixed $definitions * @return DefinitionInterface */ - public function createClassDefinition($definitions = false) + public function createClassDefinition() { - if ($definitions) { - if (is_string($definitions)) { - $definitions = $this->_unpack($definitions); - } - $result = new Compiled($definitions); - } else { - $autoloader = new Autoloader($this->getCodeGenerator()); - spl_autoload_register([$autoloader, 'load']); - - $result = new Runtime(); - } - return $result; + $autoloader = new Autoloader($this->getCodeGenerator()); + spl_autoload_register([$autoloader, 'load']); + return new Runtime(); } /** @@ -99,17 +85,6 @@ public function createRelations() return new \Magento\Framework\ObjectManager\Relations\Runtime(); } - /** - * Un-compress definitions - * - * @param string $definitions - * @return mixed - */ - protected function _unpack($definitions) - { - return $this->serializer->unserialize($definitions); - } - /** * Get existing code generator. Instantiate a new one if it does not exist yet. * diff --git a/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php deleted file mode 100644 index 71455dbf9acd4..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Relations/Compiled.php +++ /dev/null @@ -1,55 +0,0 @@ -_relations = $relations; - } - - /** - * Check whether requested type is available for read - * - * @param string $type - * @return bool - */ - public function has($type) - { - return isset($this->_relations[$type]); - } - - /** - * Retrieve parents for class - * - * @param string $type - * @return array - */ - public function getParents($type) - { - return $this->_relations[$type]; - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledTest.php deleted file mode 100644 index 49616e6765874..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Definition/CompiledTest.php +++ /dev/null @@ -1,68 +0,0 @@ -objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - } - - /** - * @param array $signatures - * @param array $definitions - * @param mixed $expected - * @dataProvider getParametersDataProvider - */ - public function testGetParametersWithoutDefinition($signatures, $definitions, $expected) - { - $model = new \Magento\Framework\ObjectManager\Definition\Compiled([$signatures, $definitions]); - $this->assertEquals($expected, $model->getParameters('wonderful')); - } - - public function getParametersDataProvider() - { - $wonderfulSignature = new \stdClass(); - return [ - [ - [], - ['wonderful' => null], - null, - ], - [ - ['wonderfulClass' => $wonderfulSignature], - ['wonderful' => 'wonderfulClass'], - $wonderfulSignature, - ] - ]; - } - - public function testGetParametersWithUnpacking() - { - $checkString = 'code to pack'; - $signatures = ['wonderfulClass' => json_encode($checkString)]; - $definitions = ['wonderful' => 'wonderfulClass']; - $object = new \Magento\Framework\ObjectManager\Definition\Compiled([$signatures, $definitions]); - $serializerMock = $this->getMock(SerializerInterface::class); - $serializerMock->expects($this->once()) - ->method('unserialize') - ->willReturnCallback(function ($data) { - return json_decode($data, true); - }); - $this->objectManagerHelper->setBackwardCompatibleProperty( - $object, - 'serializer', - $serializerMock - ); - $this->assertEquals($checkString, $object->getParameters('wonderful')); - } -} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index 8ad1ac13a12cd..99db458c11953 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -5,11 +5,11 @@ */ namespace Magento\Framework\ObjectManager\Test\Unit; -use Magento\Framework\ObjectManager\Definition\Compiled; -use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\ObjectManager\DefinitionFactory; -use Magento\Framework\ObjectManager\Definition\Runtime; +use Magento\Framework\ObjectManager\DefinitionInterface; +use Magento\Framework\Interception\DefinitionInterface as InterceptionDefinitionInterface; +use Magento\Framework\ObjectManager\RelationsInterface; class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { @@ -18,11 +18,6 @@ class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase */ private $filesystemDriverMock; - /** - * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - private $serializerMock; - /** * @var DefinitionFactory */ @@ -34,43 +29,31 @@ protected function setUp() $this->serializerMock = $this->getMock(SerializerInterface::class); $this->definitionFactory = new DefinitionFactory( $this->filesystemDriverMock, - $this->serializerMock, 'generation dir' ); } - public function testCreateClassDefinitionSerialized() + public function testCreateClassDefinition() { - $serializedDefinitions = 'serialized definitions'; - $definitions = [[], []]; - $this->serializerMock->expects($this->once()) - ->method('unserialize') - ->with($serializedDefinitions) - ->willReturn($definitions); $this->assertInstanceOf( - Compiled::class, - $this->definitionFactory->createClassDefinition($serializedDefinitions) + DefinitionInterface::class, + $this->definitionFactory->createClassDefinition() ); } - public function testCreateClassDefinitionArray() + public function testCreatePluginDefinition() { - $definitions = [[], []]; - $this->serializerMock->expects($this->never()) - ->method('unserialize'); $this->assertInstanceOf( - Compiled::class, - $this->definitionFactory->createClassDefinition($definitions) + InterceptionDefinitionInterface::class, + $this->definitionFactory->createPluginDefinition() ); } - public function testCreateClassDefinition() + public function testCreateRelations() { - $this->serializerMock->expects($this->never()) - ->method('unserialize'); $this->assertInstanceOf( - Runtime::class, - $this->definitionFactory->createClassDefinition() + RelationsInterface::class, + $this->definitionFactory->createRelations() ); } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Relations/CompiledTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Relations/CompiledTest.php deleted file mode 100644 index 336a798df100e..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Relations/CompiledTest.php +++ /dev/null @@ -1,27 +0,0 @@ - 'yes']; - - $model = new \Magento\Framework\ObjectManager\Relations\Compiled($relations); - $this->assertEquals(true, $model->has('amazing')); - $this->assertEquals(false, $model->has('fuzzy')); - } - - public function testGetParents() - { - $relations = ['amazing' => 'parents']; - - $model = new \Magento\Framework\ObjectManager\Relations\Compiled($relations); - $this->assertEquals('parents', $model->getParents('amazing')); - } -} From 47e7bb8aa9e5f799dc6ee75cc7bf5874274bb198 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 14:27:29 -0500 Subject: [PATCH 123/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Fixing tests --- .../Framework/App/ObjectManagerFactory.php | 28 +++++++++++++++++++ .../ObjectManager/DefinitionFactory.php | 5 ---- .../Test/Unit/DefinitionFactoryTest.php | 1 - 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index a67304d798609..d5f4e782fc93f 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -286,4 +286,32 @@ protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, } return $configData; } + + /** + * Crete plugin list object + * + * @param \Magento\Framework\ObjectManagerInterface $objectManager + * @param \Magento\Framework\ObjectManager\RelationsInterface $relations + * @param \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory + * @param \Magento\Framework\ObjectManager\Config\Config $diConfig + * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions + * @return \Magento\Framework\Interception\PluginList\PluginList + */ + protected function _createPluginList( + \Magento\Framework\ObjectManagerInterface $objectManager, + \Magento\Framework\ObjectManager\RelationsInterface $relations, + \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory, + \Magento\Framework\ObjectManager\Config\Config $diConfig, + \Magento\Framework\ObjectManager\DefinitionInterface $definitions + ) { + return $objectManager->create( + \Magento\Framework\Interception\PluginList\PluginList::class, + [ + 'relations' => $relations, + 'definitions' => $definitionFactory->createPluginDefinition(), + 'omConfig' => $diConfig, + 'classDefinitions' => null + ] + ); + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index 79d398e3da079..29431b570bc64 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -36,11 +36,6 @@ class DefinitionFactory */ protected $codeGenerator; - /** - * @var SerializerInterface - */ - private $serializer; - /** * @param DriverInterface $filesystemDriver * @param string $generationDir diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index 99db458c11953..8c587cec4351c 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -26,7 +26,6 @@ class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->filesystemDriverMock = $this->getMock(File::class); - $this->serializerMock = $this->getMock(SerializerInterface::class); $this->definitionFactory = new DefinitionFactory( $this->filesystemDriverMock, 'generation dir' From cc34832442336b62f6f63df43e0532057ad8fe7c Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 14:49:57 -0500 Subject: [PATCH 124/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Refactoring to use Serialize implementation --- .../Magento/Framework/ObjectManager/Config/Compiled.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index aa1fd86dbf7cf..5b979db102092 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -1,6 +1,5 @@ serializer === null) { + if (null === $this->serializer) { $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); + ->get(Serialize::class); } return $this->serializer; } From ff860672c02ca8afe6aa3fab0700d0eb744fb287 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 14:59:30 -0500 Subject: [PATCH 125/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Removing obsolete code --- .../Framework/App/ObjectManagerFactory.php | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index d5f4e782fc93f..a67304d798609 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -286,32 +286,4 @@ protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, } return $configData; } - - /** - * Crete plugin list object - * - * @param \Magento\Framework\ObjectManagerInterface $objectManager - * @param \Magento\Framework\ObjectManager\RelationsInterface $relations - * @param \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory - * @param \Magento\Framework\ObjectManager\Config\Config $diConfig - * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions - * @return \Magento\Framework\Interception\PluginList\PluginList - */ - protected function _createPluginList( - \Magento\Framework\ObjectManagerInterface $objectManager, - \Magento\Framework\ObjectManager\RelationsInterface $relations, - \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory, - \Magento\Framework\ObjectManager\Config\Config $diConfig, - \Magento\Framework\ObjectManager\DefinitionInterface $definitions - ) { - return $objectManager->create( - \Magento\Framework\Interception\PluginList\PluginList::class, - [ - 'relations' => $relations, - 'definitions' => $definitionFactory->createPluginDefinition(), - 'omConfig' => $diConfig, - 'classDefinitions' => null - ] - ); - } } From 0f5597d5010390cc0b30c5ee520db568e79022e0 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 15:27:47 -0500 Subject: [PATCH 126/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Removing obsolete code --- .../Magento/Framework/App/ObjectManagerFactory.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index a67304d798609..fc47cc5aca3d9 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -1,7 +1,5 @@ Date: Wed, 2 Nov 2016 16:49:01 -0500 Subject: [PATCH 127/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding SerializerInterface dependency in the constructor --- .../Catalog/Model/Attribute/Config/Data.php | 17 ++++++++++++---- .../Catalog/Model/ProductOptions/Config.php | 10 +++++++--- .../Magento/Cron/Model/Groups/Config/Data.php | 13 +++++++----- .../Eav/Model/Entity/Attribute/Config.php | 8 +++----- .../Email/Model/Template/Config/Data.php | 17 ++++++++++++---- .../Search/Model/SearchEngine/Config/Data.php | 12 ++++++----- .../Api/ExtensionAttribute/Config.php | 11 ++++++---- .../App/ResourceConnection/Config.php | 5 +++-- .../Framework/Communication/Config/Data.php | 20 +++++++++++-------- .../Framework/Search/Request/Config.php | 14 +++++++++---- .../Module/Di/Code/Generator/PluginList.php | 2 -- 11 files changed, 83 insertions(+), 46 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Attribute/Config/Data.php b/app/code/Magento/Catalog/Model/Attribute/Config/Data.php index 032970a7461b6..2644e1fe83b3a 100644 --- a/app/code/Magento/Catalog/Model/Attribute/Config/Data.php +++ b/app/code/Magento/Catalog/Model/Attribute/Config/Data.php @@ -1,22 +1,31 @@ Date: Wed, 2 Nov 2016 17:30:03 -0500 Subject: [PATCH 128/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding SerializerInterface dependency in the constructor --- .../Catalog/Model/Attribute/Config/Data.php | 7 +---- .../Catalog/Model/ProductTypes/Config.php | 4 +-- .../Config/Model/Config/Structure/Data.php | 8 ++++-- app/code/Magento/Cron/Model/Config/Data.php | 4 +-- .../Magento/Customer/Model/Address/Config.php | 2 +- .../Model/Country/Postcode/Config/Data.php | 8 +++--- .../Email/Model/Template/Config/Data.php | 7 +---- .../ImportExport/Model/Export/Config.php | 4 +-- .../ImportExport/Model/Import/Config.php | 4 +-- .../Magento/Indexer/Model/Config/Data.php | 4 +-- app/code/Magento/Sales/Model/Config/Data.php | 4 +-- .../TestFramework/Interception/PluginList.php | 11 +++++--- .../App/ResourceConnection/Config.php | 9 ++++--- .../Unit/ResourceConnection/ConfigTest.php | 3 +++ .../Magento/Framework/Cache/Config/Data.php | 13 +++++++--- .../Framework/Communication/Config/Data.php | 7 +---- .../Magento/Framework/Config/Data.php | 26 +++++-------------- .../Magento/Framework/Config/Data/Scoped.php | 8 +++++- .../Magento/Framework/Event/Config/Data.php | 12 +++++---- .../Interception/PluginList/PluginList.php | 4 +-- .../Magento/Framework/Mview/Config/Data.php | 4 +-- 21 files changed, 69 insertions(+), 84 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Attribute/Config/Data.php b/app/code/Magento/Catalog/Model/Attribute/Config/Data.php index 2644e1fe83b3a..08128376439a3 100644 --- a/app/code/Magento/Catalog/Model/Attribute/Config/Data.php +++ b/app/code/Magento/Catalog/Model/Attribute/Config/Data.php @@ -9,11 +9,6 @@ class Data extends \Magento\Framework\Config\Data { - /** - * Cache identifier - */ - const CACHE_ID = 'catalog_attributes'; - /** * @param \Magento\Catalog\Model\Attribute\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache @@ -23,7 +18,7 @@ class Data extends \Magento\Framework\Config\Data public function __construct( \Magento\Catalog\Model\Attribute\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = 'eav_attributes', + $cacheId = 'catalog_attributes', SerializerInterface $serializer = null ) { parent::__construct($reader, $cache, $cacheId, $serializer); diff --git a/app/code/Magento/Catalog/Model/ProductTypes/Config.php b/app/code/Magento/Catalog/Model/ProductTypes/Config.php index d6f88d3bf4640..7a5465e525bf7 100644 --- a/app/code/Magento/Catalog/Model/ProductTypes/Config.php +++ b/app/code/Magento/Catalog/Model/ProductTypes/Config.php @@ -10,11 +10,9 @@ class Config extends \Magento\Framework\Config\Data implements \Magento\Catalog\Model\ProductTypes\ConfigInterface { /** - * Config constructor - * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/app/code/Magento/Config/Model/Config/Structure/Data.php b/app/code/Magento/Config/Model/Config/Structure/Data.php index 414addf5b6f06..d785ce2b90861 100644 --- a/app/code/Magento/Config/Model/Config/Structure/Data.php +++ b/app/code/Magento/Config/Model/Config/Structure/Data.php @@ -5,6 +5,8 @@ */ namespace Magento\Config\Model\Config\Structure; +use Magento\Framework\Serialize\SerializerInterface; + class Data extends \Magento\Framework\Config\Data\Scoped { /** @@ -12,14 +14,16 @@ class Data extends \Magento\Framework\Config\Data\Scoped * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( Reader $reader, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Config\CacheInterface $cache, - $cacheId + $cacheId, + SerializerInterface $serializer = null ) { - parent::__construct($reader, $configScope, $cache, $cacheId); + parent::__construct($reader, $configScope, $cache, $cacheId, $serializer); } /** diff --git a/app/code/Magento/Cron/Model/Config/Data.php b/app/code/Magento/Cron/Model/Config/Data.php index 3edc2e6237de8..5b2f638609df7 100644 --- a/app/code/Magento/Cron/Model/Config/Data.php +++ b/app/code/Magento/Cron/Model/Config/Data.php @@ -14,12 +14,10 @@ class Data extends \Magento\Framework\Config\Data { /** - * Data constructor - * * @param Reader\Xml $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param Reader\Db $dbReader - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/app/code/Magento/Customer/Model/Address/Config.php b/app/code/Magento/Customer/Model/Address/Config.php index f53f9277cfa4f..dc2ceede663fb 100644 --- a/app/code/Magento/Customer/Model/Address/Config.php +++ b/app/code/Magento/Customer/Model/Address/Config.php @@ -68,7 +68,7 @@ class Config extends ConfigData * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Customer\Helper\Address $addressHelper * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php b/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php index 10a27b6a1b367..1a5241e0a11e5 100644 --- a/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php +++ b/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php @@ -10,17 +10,17 @@ class Data extends \Magento\Framework\Config\Data { /** - * Data constructor - * * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache - * @param SerializerInterface $serializer + * @param string|null $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Directory\Model\Country\Postcode\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, + $cacheId = 'country_postcodes', SerializerInterface $serializer = null ) { - parent::__construct($reader, $cache, 'country_postcodes', $serializer); + parent::__construct($reader, $cache, $cacheId, $serializer); } } diff --git a/app/code/Magento/Email/Model/Template/Config/Data.php b/app/code/Magento/Email/Model/Template/Config/Data.php index 182cd276771bb..e1d00e20e8d16 100644 --- a/app/code/Magento/Email/Model/Template/Config/Data.php +++ b/app/code/Magento/Email/Model/Template/Config/Data.php @@ -9,11 +9,6 @@ class Data extends \Magento\Framework\Config\Data { - /** - * Cache identifier - */ - const CACHE_ID = 'email_templates'; - /** * @param \Magento\Email\Model\Template\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache @@ -23,7 +18,7 @@ class Data extends \Magento\Framework\Config\Data public function __construct( \Magento\Email\Model\Template\Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, - $cacheId = self::CACHE_ID, + $cacheId = 'email_templates', SerializerInterface $serializer = null ) { parent::__construct($reader, $cache, $cacheId, $serializer); diff --git a/app/code/Magento/ImportExport/Model/Export/Config.php b/app/code/Magento/ImportExport/Model/Export/Config.php index 375aa7fa468b8..a2e1a02ffd306 100644 --- a/app/code/Magento/ImportExport/Model/Export/Config.php +++ b/app/code/Magento/ImportExport/Model/Export/Config.php @@ -10,11 +10,9 @@ class Config extends \Magento\Framework\Config\Data implements \Magento\ImportExport\Model\Export\ConfigInterface { /** - * Config constructor - * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/app/code/Magento/ImportExport/Model/Import/Config.php b/app/code/Magento/ImportExport/Model/Import/Config.php index 88a607c0ca0d5..826303785210a 100644 --- a/app/code/Magento/ImportExport/Model/Import/Config.php +++ b/app/code/Magento/ImportExport/Model/Import/Config.php @@ -10,11 +10,9 @@ class Config extends \Magento\Framework\Config\Data implements \Magento\ImportExport\Model\Import\ConfigInterface { /** - * Config constructor - * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index 703c875d1ef4e..2e1e433e4be25 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -15,12 +15,10 @@ class Data extends \Magento\Framework\Config\Data protected $stateCollection; /** - * Data constructor - * * @param \Magento\Framework\Indexer\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/app/code/Magento/Sales/Model/Config/Data.php b/app/code/Magento/Sales/Model/Config/Data.php index ddcb95efdfda6..0d44c627d5552 100644 --- a/app/code/Magento/Sales/Model/Config/Data.php +++ b/app/code/Magento/Sales/Model/Config/Data.php @@ -14,11 +14,9 @@ class Data extends \Magento\Framework\Config\Data { /** - * Data constructor - * * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache - * @param string $cacheId + * @param string|null $cacheId * @param SerializerInterface|null $serializer */ public function __construct( diff --git a/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php b/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php index 9e8901c91fd75..07c2c698eee00 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php @@ -5,6 +5,8 @@ */ namespace Magento\TestFramework\Interception; +use Magento\Framework\Serialize\SerializerInterface; + class PluginList extends \Magento\Framework\Interception\PluginList\PluginList { /** @@ -22,7 +24,8 @@ class PluginList extends \Magento\Framework\Interception\PluginList\PluginList * @param \Magento\Framework\ObjectManagerInterface $objectManager * @param \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions * @param array $scopePriorityScheme - * @param string $cacheId + * @param string|null $cacheId + * @param SerializerInterface|null $serializer * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -36,7 +39,8 @@ public function __construct( \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions, array $scopePriorityScheme, - $cacheId = 'plugins' + $cacheId = 'plugins', + SerializerInterface $serializer = null ) { parent::__construct( $reader, @@ -48,7 +52,8 @@ public function __construct( $objectManager, $classDefinitions, $scopePriorityScheme, - $cacheId + $cacheId, + $serializer ); $this->_originScopeScheme = $this->_scopePriorityScheme; } diff --git a/lib/internal/Magento/Framework/App/ResourceConnection/Config.php b/lib/internal/Magento/Framework/App/ResourceConnection/Config.php index c6b186e05a3df..da0b4d3b033e4 100644 --- a/lib/internal/Magento/Framework/App/ResourceConnection/Config.php +++ b/lib/internal/Magento/Framework/App/ResourceConnection/Config.php @@ -6,6 +6,7 @@ namespace Magento\Framework\App\ResourceConnection; use Magento\Framework\Config\ConfigOptionsListConstants; +use Magento\Framework\Serialize\SerializerInterface; /** * Resource configuration, uses application configuration to retrieve resource connection information @@ -24,7 +25,8 @@ class Config extends \Magento\Framework\Config\Data\Scoped implements ConfigInte * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig - * @param string $cacheId + * @param string|null $cacheId + * @param SerializerInterface|null $serializer * @throws \InvalidArgumentException */ public function __construct( @@ -32,9 +34,10 @@ public function __construct( \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Config\CacheInterface $cache, \Magento\Framework\App\DeploymentConfig $deploymentConfig, - $cacheId = 'resourcesCache' + $cacheId = 'resourcesCache', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $configScope, $cache, $cacheId); + parent::__construct($reader, $configScope, $cache, $cacheId, $serializer); $resource = $deploymentConfig->getConfigData(ConfigOptionsListConstants::KEY_RESOURCE); foreach ($resource as $resourceName => $resourceData) { diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php index fa96ef92f7b0b..62938aceae2cc 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php @@ -72,6 +72,9 @@ protected function setUp() ->with($jsonString) ->willReturn($this->resourcesConfig); + /** + * @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject $deploymentConfigMock + */ $deploymentConfigMock = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false); $deploymentConfigMock->expects($this->once()) ->method('getConfigData') diff --git a/lib/internal/Magento/Framework/Cache/Config/Data.php b/lib/internal/Magento/Framework/Cache/Config/Data.php index a1f203d9aa7bb..3a2ba6c8c0d05 100644 --- a/lib/internal/Magento/Framework/Cache/Config/Data.php +++ b/lib/internal/Magento/Framework/Cache/Config/Data.php @@ -1,12 +1,15 @@ reader = $reader; $this->cache = $cache; $this->cacheId = $cacheId; - $this->serializer = $serializer ?: $this->getSerializer(); + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); $this->initData(); } /** * Initialise data for configuration + * * @return void */ protected function initData() @@ -99,9 +99,9 @@ protected function initData() $data = $this->cache->load($this->cacheId); if (false === $data) { $data = $this->reader->read(); - $this->cache->save($this->getSerializer()->serialize($data), $this->cacheId, $this->cacheTags); + $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags); } else { - $data = $this->getSerializer()->unserialize($data); + $data = $this->serializer->unserialize($data); } $this->merge($data); @@ -144,25 +144,11 @@ public function get($path = null, $default = null) /** * Clear cache data + * * @return void */ public function reset() { $this->cache->remove($this->cacheId); } - - /** - * Get serializer - * - * @return \Magento\Framework\Serialize\SerializerInterface - * @deprecated - */ - protected function getSerializer() - { - if ($this->serializer === null) { - $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() - ->get(SerializerInterface::class); - } - return $this->serializer; - } } diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index b5f01a14335fa..644c2649e4982 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\Config\Data; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\App\ObjectManager; + class Scoped extends \Magento\Framework\Config\Data { /** @@ -56,17 +59,20 @@ class Scoped extends \Magento\Framework\Config\Data * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache * @param string $cacheId + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\Config\ReaderInterface $reader, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Config\CacheInterface $cache, - $cacheId + $cacheId, + SerializerInterface $serializer = null ) { $this->_reader = $reader; $this->_configScope = $configScope; $this->_cache = $cache; $this->_cacheId = $cacheId; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);; } /** diff --git a/lib/internal/Magento/Framework/Event/Config/Data.php b/lib/internal/Magento/Framework/Event/Config/Data.php index 7bd082e5d465b..d5858be832fee 100644 --- a/lib/internal/Magento/Framework/Event/Config/Data.php +++ b/lib/internal/Magento/Framework/Event/Config/Data.php @@ -1,12 +1,12 @@ Date: Wed, 2 Nov 2016 17:51:06 -0500 Subject: [PATCH 129/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding SerializerInterface dependency in the constructor --- .../Test/Unit/Model/Country/Postcode/Config/DataTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 20444b6080add..57c86ab58c91b 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -53,6 +53,7 @@ public function testGet() $configData = new \Magento\Directory\Model\Country\Postcode\Config\Data( $this->readerMock, $this->cacheMock, + 'country_postcodes', $this->serializerMock ); $this->assertEquals($expected, $configData->get()); From abbfc14d1de4b249d10cf71b39c50fc31550e393 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 18:04:10 -0500 Subject: [PATCH 130/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding SerializerInterface dependency in the constructor --- .../Unit/ResourceConnection/ConfigTest.php | 25 +++++++++++-------- .../Magento/Framework/Config/Data/Scoped.php | 4 +-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php index 62938aceae2cc..6382394353749 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php @@ -27,6 +27,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ private $readerMock; + /** + * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializerMock; + /** * @var array */ @@ -39,7 +44,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->scopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); @@ -50,7 +54,7 @@ protected function setUp() '', false ); - $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); $this->resourcesConfig = [ 'mainResourceName' => ['name' => 'mainResourceName', 'extends' => 'anotherResourceName'], @@ -64,12 +68,12 @@ protected function setUp() 'validResource' => ['connection' => 'validConnectionName'], ]; - $jsonString = json_encode($this->resourcesConfig); + $serializedData = 'serialized data'; $this->cacheMock->expects($this->any()) ->method('load') - ->willReturn($jsonString); - $serializerMock->method('unserialize') - ->with($jsonString) + ->willReturn($serializedData); + $this->serializerMock->method('unserialize') + ->with($serializedData) ->willReturn($this->resourcesConfig); /** @@ -86,15 +90,15 @@ protected function setUp() $this->scopeMock, $this->cacheMock, $deploymentConfigMock, - 'cacheId' + 'cacheId', + $this->serializerMock ); - $objectManager->setBackwardCompatibleProperty($this->config, 'serializer', $serializerMock); } /** - * @dataProvider getConnectionNameDataProvider * @param string $resourceName * @param string $connectionName + * @dataProvider getConnectionNameDataProvider */ public function testGetConnectionName($resourceName, $connectionName) { @@ -117,7 +121,8 @@ public function testExceptionConstructor() $this->scopeMock, $this->cacheMock, $deploymentConfigMock, - 'cacheId' + 'cacheId', + $this->serializerMock ); } diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 644c2649e4982..5e7bd441bbb29 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -104,12 +104,12 @@ protected function _loadScopedData() if (false == isset($this->_loadedScopes[$scopeCode])) { if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId)) ) { - $data = $this->getSerializer()->unserialize($data); + $data = $this->serializer->unserialize($data); } else { $data = $this->_reader->read($scopeCode); if ($scopeCode !== 'primary') { $this->_cache->save( - $this->getSerializer()->serialize($data), + $this->serializer->serialize($data), $scopeCode . '::' . $this->_cacheId ); } From b5e21241b871d674d3bf25f198ad2e32ee8c71d7 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Wed, 2 Nov 2016 21:15:52 -0500 Subject: [PATCH 131/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Fixing tests --- lib/internal/Magento/Framework/Config/Data/Scoped.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 5e7bd441bbb29..0171c97ff1e90 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -72,7 +72,7 @@ public function __construct( $this->_configScope = $configScope; $this->_cache = $cache; $this->_cacheId = $cacheId; - $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** From 2f3a64b411487af1ccaf59690227c30c483713ad Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 3 Nov 2016 09:36:11 -0500 Subject: [PATCH 132/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Refactoring tests --- app/code/Magento/Cron/Model/Config/Data.php | 4 ---- .../Magento/Cron/Model/Groups/Config/Data.php | 3 --- .../Magento/Customer/Model/Address/Config.php | 6 +----- .../Model/Customer/NotificationStorageTest.php | 6 +++--- app/code/Magento/Sales/Model/Config/Data.php | 4 ---- .../Api/ExtensionAttribute/Config.php | 3 +++ .../Magento/Framework/Config/Data/Scoped.php | 2 -- .../Config/Test/Unit/Data/ScopedTest.php | 6 +----- .../Framework/Config/Test/Unit/DataTest.php | 12 ++++++------ .../Framework/DataObject/Copy/Config/Data.php | 2 -- .../Test/Unit/PluginList/PluginListTest.php | 18 +++++++++--------- 11 files changed, 23 insertions(+), 43 deletions(-) diff --git a/app/code/Magento/Cron/Model/Config/Data.php b/app/code/Magento/Cron/Model/Config/Data.php index 5b2f638609df7..0e685aa910296 100644 --- a/app/code/Magento/Cron/Model/Config/Data.php +++ b/app/code/Magento/Cron/Model/Config/Data.php @@ -3,10 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -/** - * Prepare cron jobs data - */ namespace Magento\Cron\Model\Config; use Magento\Framework\Serialize\SerializerInterface; diff --git a/app/code/Magento/Cron/Model/Groups/Config/Data.php b/app/code/Magento/Cron/Model/Groups/Config/Data.php index 2f400c642ed33..5c69cf9667b35 100644 --- a/app/code/Magento/Cron/Model/Groups/Config/Data.php +++ b/app/code/Magento/Cron/Model/Groups/Config/Data.php @@ -7,9 +7,6 @@ use Magento\Framework\Serialize\SerializerInterface; -/** - * Prepare cron jobs data - */ class Data extends \Magento\Framework\Config\Data { /** diff --git a/app/code/Magento/Customer/Model/Address/Config.php b/app/code/Magento/Customer/Model/Address/Config.php index dc2ceede663fb..7170cf3d2383f 100644 --- a/app/code/Magento/Customer/Model/Address/Config.php +++ b/app/code/Magento/Customer/Model/Address/Config.php @@ -12,8 +12,6 @@ /** * Customer address config - * - * @author Magento Core Team */ class Config extends ConfigData { @@ -24,7 +22,7 @@ class Config extends ConfigData const DEFAULT_ADDRESS_FORMAT = 'oneline'; /** - * Customer Address Templates per store + * Customer address templates per store * * @var array */ @@ -61,8 +59,6 @@ class Config extends ConfigData protected $_scopeConfig; /** - * Config constructor - * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Store\Model\StoreManagerInterface $storeManager diff --git a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php index e75cacc0781d5..644d0a2d70122 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Customer/NotificationStorageTest.php @@ -45,15 +45,15 @@ public function testAdd() 'customer_id' => $customerId, 'notification_type' => $notificationType ]; - $jsonString = json_encode($data); + $serializedData = 'serialized data'; $this->serializerMock->expects($this->once()) ->method('serialize') ->with($data) - ->willReturn($jsonString); + ->willReturn($serializedData); $this->cacheMock->expects($this->once()) ->method('save') ->with( - $jsonString, + $serializedData, $this->getCacheKey($notificationType, $customerId) ); $this->notificationStorage->add($notificationType, $customerId); diff --git a/app/code/Magento/Sales/Model/Config/Data.php b/app/code/Magento/Sales/Model/Config/Data.php index 0d44c627d5552..bbd90a302f659 100644 --- a/app/code/Magento/Sales/Model/Config/Data.php +++ b/app/code/Magento/Sales/Model/Config/Data.php @@ -3,10 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -/** - * Sales configuration data container - */ namespace Magento\Sales\Model\Config; use Magento\Framework\Serialize\SerializerInterface; diff --git a/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php b/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php index 3c5dd0bf415d2..32d2b75ed5e54 100644 --- a/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php +++ b/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php @@ -14,6 +14,9 @@ */ class Config extends \Magento\Framework\Config\Data { + /** + * Cache identifier + */ const CACHE_ID = 'extension_attributes_config'; /** diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 0171c97ff1e90..55a354f0d2a67 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -53,8 +53,6 @@ class Scoped extends \Magento\Framework\Config\Data protected $_loadedScopes = []; /** - * Constructor - * * @param \Magento\Framework\Config\ReaderInterface $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache 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 6e89bf9d50ab0..607977b99bb42 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -52,13 +52,9 @@ protected function setUp() 'configScope' => $this->_configScopeMock, 'cache' => $this->_cacheMock, 'cacheId' => 'tag', + 'serializer' => $this->serializerMock ] ); - $this->objectManager->setBackwardCompatibleProperty( - $this->_model, - 'serializer', - $this->serializerMock - ); } /** diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index 5ad20aead09bd..c37d2108191b4 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -60,16 +60,16 @@ public function testGetConfigNotCached() public function testGetConfigCached() { $data = ['a' => 'b']; - $jsonString = '{"a":"b"}'; + $serializedData = '{"a":"b"}'; $cacheId = 'test'; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn($jsonString); + ->willReturn($serializedData); $this->readerMock->expects($this->never()) ->method('read'); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->with($jsonString) + ->with($serializedData) ->willReturn($data); $config = new \Magento\Framework\Config\Data( $this->readerMock, @@ -83,14 +83,14 @@ public function testGetConfigCached() public function testReset() { - $jsonString = ''; + $serializedData = ''; $cacheId = 'test'; $this->cacheMock->expects($this->once()) ->method('load') - ->willReturn($jsonString); + ->willReturn($serializedData); $this->serializerMock->expects($this->once()) ->method('unserialize') - ->with($jsonString) + ->with($serializedData) ->willReturn([]); $this->cacheMock->expects($this->once()) ->method('remove') diff --git a/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php b/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php index 61802637750c6..731eb9d685d51 100644 --- a/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php +++ b/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php @@ -1,7 +1,5 @@ getMock(\Magento\Framework\ObjectManagerInterface::class); $objectManagerMock->expects($this->any())->method('get')->will($this->returnArgument(0)); + $this->serializerMock = $this->getMock(SerializerInterface::class); $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); @@ -80,15 +85,10 @@ protected function setUp() 'objectManager' => $objectManagerMock, 'classDefinitions' => $definitions, 'scopePriorityScheme' => ['global'], - 'cacheId' => 'interception' + 'cacheId' => 'interception', + 'serializer' => $this->serializerMock ] ); - $this->serializerMock = $this->getMock(SerializerInterface::class); - $objectManagerHelper->setBackwardCompatibleProperty( - $this->object, - 'serializer', - $this->serializerMock - ); $this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); $objectManagerHelper->setBackwardCompatibleProperty( From 2891de9822009b25ef8c2c9807321e4dd84c72cb Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 3 Nov 2016 09:59:52 -0500 Subject: [PATCH 133/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding SerializerInterface dependency in the constructor --- app/code/Magento/Indexer/Model/Config/Data.php | 3 +++ lib/internal/Magento/Framework/Config/Data.php | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index 2e1e433e4be25..cf6ea584196ce 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -6,6 +6,8 @@ namespace Magento\Indexer\Model\Config; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Serialize\Serializer\Serialize; +use Magento\Framework\App\ObjectManager; class Data extends \Magento\Framework\Config\Data { @@ -29,6 +31,7 @@ public function __construct( SerializerInterface $serializer = null ) { $this->stateCollection = $stateCollection; + $serializer = $serializer ?: ObjectManager::getInstance()->get(Serialize::class); $isCacheExists = $cache->test($cacheId); diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index e3e09f0fca602..0723e40dbfef3 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -1,7 +1,5 @@ Date: Thu, 3 Nov 2016 10:32:44 -0500 Subject: [PATCH 134/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding SerializerInterface dependency in the constructor --- .../Interception/PluginList/PluginList.php | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index 3406a1395675f..f74213eaf4a06 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -92,7 +92,8 @@ class PluginList extends Scoped implements InterceptionPluginList * @param ObjectManagerInterface $objectManager * @param ClassDefinitions $classDefinitions * @param array $scopePriorityScheme - * @param string $cacheId + * @param string|null $cacheId + * @param SerializerInterface|null $serializer * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -105,9 +106,11 @@ public function __construct( ObjectManagerInterface $objectManager, ClassDefinitions $classDefinitions, array $scopePriorityScheme = ['global'], - $cacheId = 'plugins' + $cacheId = 'plugins', + SerializerInterface $serializer = null ) { - parent::__construct($reader, $configScope, $cache, $cacheId); + $serializer = $serializer ?: $objectManager->get(Serialize::class); + parent::__construct($reader, $configScope, $cache, $cacheId, $serializer); $this->_omConfig = $omConfig; $this->_relations = $relations; $this->_definitions = $definitions; @@ -277,7 +280,7 @@ protected function _loadScopedData() $cacheId = implode('|', $this->_scopePriorityScheme) . "|" . $this->_cacheId; $data = $this->_cache->load($cacheId); if ($data) { - list($this->_data, $this->_inherited, $this->_processed) = $this->getSerializer()->unserialize($data); + list($this->_data, $this->_inherited, $this->_processed) = $this->serializer->unserialize($data); foreach ($this->_scopePriorityScheme as $scope) { $this->_loadedScopes[$scope] = true; } @@ -311,7 +314,7 @@ protected function _loadScopedData() $this->_inheritPlugins($class); } $this->_cache->save( - $this->getSerializer()->serialize([$this->_data, $this->_inherited, $this->_processed]), + $this->serializer->serialize([$this->_data, $this->_inherited, $this->_processed]), $cacheId ); } @@ -389,18 +392,4 @@ private function getLogger() } 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; - } } From f39dab0463ade2c2d5d5143c1aec88b3b8ddd220 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 3 Nov 2016 12:27:38 -0500 Subject: [PATCH 135/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Fixing tests --- .../ObjectManager/Config/Compiled.php | 1 - .../ObjectManager/Config/CompiledTest.php | 142 +++++++++--------- 2 files changed, 74 insertions(+), 69 deletions(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index dd78957f01962..0db65f3b6b5fa 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -1,6 +1,5 @@ objectManagerHelper = new ObjectManagerHelper($this); - } + private $objectManager; /** - * @param array $initialData - * @param array $configuration - * @param array $expectedArguments - * @param array $expectedVirtualTypes - * @param array $expectedPreferences - * - * @dataProvider extendDataProvider + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - public function testExtend( - array $initialData, - array $configuration, - array $expectedArguments, - array $expectedVirtualTypes, - array $expectedPreferences - ) { - /** @var CompiledConfig $compiledConfig */ - $compiledConfig = $this->objectManagerHelper->getObject(CompiledConfig::class, ['data' => $initialData]); - $compiledConfig->extend($configuration); + private $serializerMock; - foreach ($expectedArguments as $type => $arguments) { - $this->assertEquals($arguments, $compiledConfig->getArguments($type)); - } - - $this->assertEquals($expectedVirtualTypes, $compiledConfig->getVirtualTypes()); - $this->assertEquals($expectedPreferences, $compiledConfig->getPreferences()); + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->serializerMock = $this->getMock(SerializerInterface::class); } - /** - * @return array - */ - public function extendDataProvider() + public function testExtend() { - return [ - [ - 'initialData' => [ - 'arguments' => [ - 'type1' => serialize(['argument1_1' => 'argumentValue1_1', 'argument1_2' => 'argumentValue1_2']) - ], - 'instanceTypes' => [ - 'instanceType1' => 'instanceTypeValue1', 'instanceType2' => 'instanceTypeValue2' - ], - 'preferences' => ['preference1' => 'preferenceValue1', 'preference2' => 'preferenceValue2'] - ], - 'configuration' => [ - 'arguments' => [ - 'type1' => serialize(['argument1_1' => 'newArgumentValue1_1']), - 'type2' => serialize(['argument2_1' => 'newArgumentValue2_1']) - ], - 'instanceTypes' => [ - 'instanceType2' => 'newInstanceTypeValue2', 'instanceType3' => 'newInstanceTypeValue3' - ], - 'preferences' => ['preference1' => 'newPreferenceValue1'] - ], - 'expectedArguments' => [ - 'type1' => ['argument1_1' => 'newArgumentValue1_1'], - 'type2' => ['argument2_1' => 'newArgumentValue2_1'] - ], - 'expectedVirtualTypes' => [ - 'instanceType1' => 'instanceTypeValue1', 'instanceType2' => 'newInstanceTypeValue2', - 'instanceType3' => 'newInstanceTypeValue3' - ], - 'expectedPreferences' => [ - 'preference1' => 'newPreferenceValue1', 'preference2' => 'preferenceValue2' - ] + $initialData = [ + 'arguments' => [ + 'type1' => 'initial serialized configuration for type1' + ], + 'instanceTypes' => [ + 'instanceType1' => 'instanceTypeValue1', + 'instanceType2' => 'instanceTypeValue2' + ], + 'preferences' => [ + 'preference1' => 'preferenceValue1', + 'preference2' => 'preferenceValue2' + ] + ]; + $configuration = [ + 'arguments' => [ + 'type1' => 'serialized configuration for type1', + 'type2' => 'serialized configuration for type2' + ], + 'instanceTypes' => [ + 'instanceType2' => 'newInstanceTypeValue2', + 'instanceType3' => 'newInstanceTypeValue3' + ], + 'preferences' => [ + 'preference1' => 'newPreferenceValue1' ] ]; + $expectedArguments = [ + 'type1' => [ + 'argument1_1' => 'newArgumentValue1_1' + ], + 'type2' => [ + 'argument2_1' => 'newArgumentValue2_1' + ] + ]; + $expectedVirtualTypes = [ + 'instanceType1' => 'instanceTypeValue1', + 'instanceType2' => 'newInstanceTypeValue2', + 'instanceType3' => 'newInstanceTypeValue3' + ]; + $expectedPreferences = [ + 'preference1' => 'newPreferenceValue1', + 'preference2' => 'preferenceValue2' + ]; + $this->serializerMock->expects($this->at(0)) + ->method('unserialize') + ->with($configuration['arguments']['type1']) + ->willReturn($expectedArguments['type1']); + $this->serializerMock->expects($this->at(1)) + ->method('unserialize') + ->with($configuration['arguments']['type2']) + ->willReturn($expectedArguments['type2']); + $compiled = $this->objectManager->getObject( + Compiled::class, + [ + 'data' => $initialData, + 'serializer' => $this->serializerMock + ] + ); + $compiled->extend($configuration); + foreach ($expectedArguments as $type => $arguments) { + $this->assertEquals($arguments, $compiled->getArguments($type)); + } + $this->assertEquals($expectedVirtualTypes, $compiled->getVirtualTypes()); + $this->assertEquals($expectedPreferences, $compiled->getPreferences()); } } From 9149476ad6c3d92dc78ed83cc9c0778d20347b0e Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 14:17:35 -0500 Subject: [PATCH 136/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Removing obsolete code --- .../ObjectManager/Definition/Compiled.php | 115 ------------------ 1 file changed, 115 deletions(-) delete mode 100644 lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php deleted file mode 100644 index e2a4f0c34a2a8..0000000000000 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php +++ /dev/null @@ -1,115 +0,0 @@ -_signatures, $this->_definitions) = $definitions; - $this->reader = $reader ?: new \Magento\Framework\Code\Reader\ClassReader(); - } - - /** - * Unpack signature - * - * @param string $signature - * @return mixed - */ - protected function _unpack($signature) - { - return $this->getSerializer()->unserialize($signature); - } - - /** - * Get list of method parameters - * - * Retrieve an ordered list of constructor parameters. - * Each value is an array with following entries: - * - * array( - * 0, // string: Parameter name - * 1, // string|null: Parameter type - * 2, // bool: whether this param is required - * 3, // mixed: default value - * ); - * - * @param string $className - * @return array|null - */ - public function getParameters($className) - { - // if the definition isn't found in the list gathered from the compiled file then using reflection to find it - if (!array_key_exists($className, $this->_definitions)) { - return $this->reader->getConstructor($className); - } - - $definition = $this->_definitions[$className]; - if ($definition !== null) { - if (is_string($this->_signatures[$definition])) { - $this->_signatures[$definition] = $this->_unpack($this->_signatures[$definition]); - } - return $this->_signatures[$definition]; - } - return null; - } - - /** - * Retrieve list of all classes covered with definitions - * - * @return array - */ - public function getClasses() - { - return array_keys($this->_definitions); - } - - /** - * 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; - } -} From c957071a0c4a20748a0f130100605ff9a2fd683e Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 14:28:13 -0500 Subject: [PATCH 137/144] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Adding removed method back --- .../Framework/App/ObjectManagerFactory.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index fc47cc5aca3d9..530c7c43599df 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -277,4 +277,34 @@ protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, } return $configData; } + + /** + * Crete plugin list object + * + * @param \Magento\Framework\ObjectManagerInterface $objectManager + * @param \Magento\Framework\ObjectManager\RelationsInterface $relations + * @param \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory + * @param \Magento\Framework\ObjectManager\Config\Config $diConfig + * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions + * @return \Magento\Framework\Interception\PluginList\PluginList + * @deprecated + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + protected function _createPluginList( + \Magento\Framework\ObjectManagerInterface $objectManager, + \Magento\Framework\ObjectManager\RelationsInterface $relations, + \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory, + \Magento\Framework\ObjectManager\Config\Config $diConfig, + \Magento\Framework\ObjectManager\DefinitionInterface $definitions + ) { + return $objectManager->create( + \Magento\Framework\Interception\PluginList\PluginList::class, + [ + 'relations' => $relations, + 'definitions' => $definitionFactory->createPluginDefinition(), + 'omConfig' => $diConfig, + 'classDefinitions' => null + ] + ); + } } From 47df808ef213e47d29c4ad1d9f3d0014fc797301 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 16:15:13 -0500 Subject: [PATCH 138/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding class and __constructor descriptions --- app/code/Magento/Catalog/Model/Attribute/Config/Data.php | 5 +++++ app/code/Magento/Catalog/Model/ProductOptions/Config.php | 5 +++++ app/code/Magento/Catalog/Model/ProductTypes/Config.php | 5 +++++ app/code/Magento/Config/Model/Config/Structure/Data.php | 5 +++++ app/code/Magento/Cron/Model/Config/Data.php | 5 +++++ app/code/Magento/Cron/Model/Groups/Config/Data.php | 5 +++++ app/code/Magento/Customer/Model/Address/Config.php | 7 ++++--- .../Directory/Model/Country/Postcode/Config/Data.php | 5 +++++ app/code/Magento/Eav/Model/Entity/Attribute/Config.php | 5 +++++ app/code/Magento/Email/Model/Template/Config/Data.php | 5 +++++ app/code/Magento/ImportExport/Model/Export/Config.php | 5 +++++ app/code/Magento/ImportExport/Model/Import/Config.php | 5 +++++ app/code/Magento/Indexer/Model/Config/Data.php | 7 +++++-- app/code/Magento/Sales/Model/Config/Data.php | 5 +++++ app/code/Magento/Search/Model/SearchEngine/Config/Data.php | 5 +++++ .../Magento/Framework/Api/ExtensionAttribute/Config.php | 2 ++ lib/internal/Magento/Framework/Cache/Config/Data.php | 2 ++ .../Magento/Framework/Communication/Config/Data.php | 5 +++++ lib/internal/Magento/Framework/Config/Data.php | 6 ++++-- lib/internal/Magento/Framework/Config/Data/Scoped.php | 5 +++++ .../Magento/Framework/DataObject/Copy/Config/Data.php | 3 +++ lib/internal/Magento/Framework/Event/Config/Data.php | 5 +++++ .../Framework/Interception/PluginList/PluginList.php | 2 ++ lib/internal/Magento/Framework/Mview/Config/Data.php | 5 +++++ .../Magento/Framework/ObjectManager/Config/Compiled.php | 5 +++++ lib/internal/Magento/Framework/Search/Request/Config.php | 5 +++++ .../Magento/Setup/Module/Di/Code/Generator/PluginList.php | 3 +++ 27 files changed, 120 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Attribute/Config/Data.php b/app/code/Magento/Catalog/Model/Attribute/Config/Data.php index 08128376439a3..1fac4e58c75c9 100644 --- a/app/code/Magento/Catalog/Model/Attribute/Config/Data.php +++ b/app/code/Magento/Catalog/Model/Attribute/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides catalog attributes configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param \Magento\Catalog\Model\Attribute\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Catalog/Model/ProductOptions/Config.php b/app/code/Magento/Catalog/Model/ProductOptions/Config.php index f78eb0e4f508b..fa828832bf4a7 100644 --- a/app/code/Magento/Catalog/Model/ProductOptions/Config.php +++ b/app/code/Magento/Catalog/Model/ProductOptions/Config.php @@ -7,10 +7,15 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides product options configuration + */ class Config extends \Magento\Framework\Config\Data implements \Magento\Catalog\Model\ProductOptions\ConfigInterface { /** + * Constructor + * * @param \Magento\Catalog\Model\ProductOptions\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Catalog/Model/ProductTypes/Config.php b/app/code/Magento/Catalog/Model/ProductTypes/Config.php index 7a5465e525bf7..f691e08a34b57 100644 --- a/app/code/Magento/Catalog/Model/ProductTypes/Config.php +++ b/app/code/Magento/Catalog/Model/ProductTypes/Config.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides product types configuration + */ class Config extends \Magento\Framework\Config\Data implements \Magento\Catalog\Model\ProductTypes\ConfigInterface { /** + * Constructor + * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Config/Model/Config/Structure/Data.php b/app/code/Magento/Config/Model/Config/Structure/Data.php index d785ce2b90861..6c926e7c1da1a 100644 --- a/app/code/Magento/Config/Model/Config/Structure/Data.php +++ b/app/code/Magento/Config/Model/Config/Structure/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides configuration + */ class Data extends \Magento\Framework\Config\Data\Scoped { /** + * Constructor + * * @param Reader $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache diff --git a/app/code/Magento/Cron/Model/Config/Data.php b/app/code/Magento/Cron/Model/Config/Data.php index 0e685aa910296..bcfaef37ece7b 100644 --- a/app/code/Magento/Cron/Model/Config/Data.php +++ b/app/code/Magento/Cron/Model/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides cron configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param Reader\Xml $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param Reader\Db $dbReader diff --git a/app/code/Magento/Cron/Model/Groups/Config/Data.php b/app/code/Magento/Cron/Model/Groups/Config/Data.php index 5c69cf9667b35..82c35abff22b0 100644 --- a/app/code/Magento/Cron/Model/Groups/Config/Data.php +++ b/app/code/Magento/Cron/Model/Groups/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides cron groups configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param \Magento\Cron\Model\Groups\Config\Reader\Xml $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Customer/Model/Address/Config.php b/app/code/Magento/Customer/Model/Address/Config.php index 7170cf3d2383f..18a043bc019bb 100644 --- a/app/code/Magento/Customer/Model/Address/Config.php +++ b/app/code/Magento/Customer/Model/Address/Config.php @@ -11,7 +11,7 @@ use Magento\Store\Model\ScopeInterface; /** - * Customer address config + * Customer address configuration */ class Config extends ConfigData { @@ -36,8 +36,7 @@ class Config extends ConfigData protected $_store = null; /** - * Default types per store - * Using for invalid code + * Default types per store, used for invalid code * * @var array */ @@ -59,6 +58,8 @@ class Config extends ConfigData protected $_scopeConfig; /** + * Constructor + * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Store\Model\StoreManagerInterface $storeManager diff --git a/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php b/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php index 1a5241e0a11e5..c24da536e779c 100644 --- a/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php +++ b/app/code/Magento/Directory/Model/Country/Postcode/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides country postcodes configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Config.php b/app/code/Magento/Eav/Model/Entity/Attribute/Config.php index d7c5edd848566..1bc5bba6d5e79 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Config.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Config.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides EAV attributes configuration + */ class Config extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Email/Model/Template/Config/Data.php b/app/code/Magento/Email/Model/Template/Config/Data.php index e1d00e20e8d16..1f6a4beb166e0 100644 --- a/app/code/Magento/Email/Model/Template/Config/Data.php +++ b/app/code/Magento/Email/Model/Template/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides email templates configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param \Magento\Email\Model\Template\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/ImportExport/Model/Export/Config.php b/app/code/Magento/ImportExport/Model/Export/Config.php index a2e1a02ffd306..2d7b2c7a3af25 100644 --- a/app/code/Magento/ImportExport/Model/Export/Config.php +++ b/app/code/Magento/ImportExport/Model/Export/Config.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides export configuration + */ class Config extends \Magento\Framework\Config\Data implements \Magento\ImportExport\Model\Export\ConfigInterface { /** + * Constructor + * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/ImportExport/Model/Import/Config.php b/app/code/Magento/ImportExport/Model/Import/Config.php index 826303785210a..a1ec492da3e96 100644 --- a/app/code/Magento/ImportExport/Model/Import/Config.php +++ b/app/code/Magento/ImportExport/Model/Import/Config.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides import configuration + */ class Config extends \Magento\Framework\Config\Data implements \Magento\ImportExport\Model\Import\ConfigInterface { /** + * Constructor + * * @param Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Indexer/Model/Config/Data.php b/app/code/Magento/Indexer/Model/Config/Data.php index cf6ea584196ce..3cedaa51ef4bb 100644 --- a/app/code/Magento/Indexer/Model/Config/Data.php +++ b/app/code/Magento/Indexer/Model/Config/Data.php @@ -6,9 +6,11 @@ namespace Magento\Indexer\Model\Config; use Magento\Framework\Serialize\SerializerInterface; -use Magento\Framework\Serialize\Serializer\Serialize; use Magento\Framework\App\ObjectManager; +/** + * Provides indexer configuration + */ class Data extends \Magento\Framework\Config\Data { /** @@ -17,6 +19,8 @@ class Data extends \Magento\Framework\Config\Data protected $stateCollection; /** + * Constructor + * * @param \Magento\Framework\Indexer\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection $stateCollection @@ -31,7 +35,6 @@ public function __construct( SerializerInterface $serializer = null ) { $this->stateCollection = $stateCollection; - $serializer = $serializer ?: ObjectManager::getInstance()->get(Serialize::class); $isCacheExists = $cache->test($cacheId); diff --git a/app/code/Magento/Sales/Model/Config/Data.php b/app/code/Magento/Sales/Model/Config/Data.php index bbd90a302f659..b6a1b43012f9e 100644 --- a/app/code/Magento/Sales/Model/Config/Data.php +++ b/app/code/Magento/Sales/Model/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides sales configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/app/code/Magento/Search/Model/SearchEngine/Config/Data.php b/app/code/Magento/Search/Model/SearchEngine/Config/Data.php index 213a92958ac0e..d128a9d50025d 100644 --- a/app/code/Magento/Search/Model/SearchEngine/Config/Data.php +++ b/app/code/Magento/Search/Model/SearchEngine/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides search engine configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param \Magento\Framework\Search\SearchEngine\Config\Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php b/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php index 32d2b75ed5e54..1dcfe02e56b3e 100644 --- a/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php +++ b/lib/internal/Magento/Framework/Api/ExtensionAttribute/Config.php @@ -20,6 +20,8 @@ class Config extends \Magento\Framework\Config\Data const CACHE_ID = 'extension_attributes_config'; /** + * Constructor + * * @param Reader $reader * @param CacheInterface $cache * @param string $cacheId|null diff --git a/lib/internal/Magento/Framework/Cache/Config/Data.php b/lib/internal/Magento/Framework/Cache/Config/Data.php index 3a2ba6c8c0d05..5909fff105e2b 100644 --- a/lib/internal/Magento/Framework/Cache/Config/Data.php +++ b/lib/internal/Magento/Framework/Cache/Config/Data.php @@ -20,6 +20,8 @@ class Data extends \Magento\Framework\Config\Data\Scoped protected $_scopePriorityScheme = ['global']; /** + * Constructor + * * @param \Magento\Framework\Cache\Config\Reader $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache diff --git a/lib/internal/Magento/Framework/Communication/Config/Data.php b/lib/internal/Magento/Framework/Communication/Config/Data.php index ea2965b1a44b3..29667100b6860 100644 --- a/lib/internal/Magento/Framework/Communication/Config/Data.php +++ b/lib/internal/Magento/Framework/Communication/Config/Data.php @@ -7,9 +7,14 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides communication configuration + */ class Data extends \Magento\Framework\Config\Data { /** + * Constructor + * * @param \Magento\Framework\Communication\Config\CompositeReader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 0723e40dbfef3..57b6769d3e559 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -16,14 +16,14 @@ class Data implements \Magento\Framework\Config\DataInterface { /** - * Configuration reader model + * Configuration reader * * @var ReaderInterface */ protected $_reader; /** - * Configuration cache model + * Configuration cache * * @var CacheInterface */ @@ -71,6 +71,8 @@ class Data implements \Magento\Framework\Config\DataInterface protected $serializer; /** + * Constructor + * * @param ReaderInterface $reader * @param CacheInterface $cache * @param string $cacheId diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 55a354f0d2a67..02379834c5e78 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -8,6 +8,9 @@ use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\App\ObjectManager; +/** + * Provides scoped configuration + */ class Scoped extends \Magento\Framework\Config\Data { /** @@ -53,6 +56,8 @@ class Scoped extends \Magento\Framework\Config\Data protected $_loadedScopes = []; /** + * Constructor + * * @param \Magento\Framework\Config\ReaderInterface $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache diff --git a/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php b/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php index 731eb9d685d51..5f2a1518d485a 100644 --- a/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php +++ b/lib/internal/Magento/Framework/DataObject/Copy/Config/Data.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\DataObject\Copy\Config; +/** + * Provides DataObject copier configuration + */ class Data extends \Magento\Framework\Config\Data { } diff --git a/lib/internal/Magento/Framework/Event/Config/Data.php b/lib/internal/Magento/Framework/Event/Config/Data.php index d5858be832fee..4b69e59793497 100644 --- a/lib/internal/Magento/Framework/Event/Config/Data.php +++ b/lib/internal/Magento/Framework/Event/Config/Data.php @@ -7,6 +7,9 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides event configuration + */ class Data extends \Magento\Framework\Config\Data\Scoped { /** @@ -17,6 +20,8 @@ class Data extends \Magento\Framework\Config\Data\Scoped protected $_scopePriorityScheme = ['global']; /** + * Constructor + * * @param \Magento\Framework\Event\Config\Reader $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index f74213eaf4a06..dc3ddea2c6fa8 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -83,6 +83,8 @@ class PluginList extends Scoped implements InterceptionPluginList private $logger; /** + * Constructor + * * @param ReaderInterface $reader * @param ScopeInterface $configScope * @param CacheInterface $cache diff --git a/lib/internal/Magento/Framework/Mview/Config/Data.php b/lib/internal/Magento/Framework/Mview/Config/Data.php index 0ddf85d5d5c85..fed3021a161ee 100644 --- a/lib/internal/Magento/Framework/Mview/Config/Data.php +++ b/lib/internal/Magento/Framework/Mview/Config/Data.php @@ -7,6 +7,9 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides materialized view configuration + */ class Data extends \Magento\Framework\Config\Data { /** @@ -15,6 +18,8 @@ class Data extends \Magento\Framework\Config\Data protected $stateCollection; /** + * Constructor + * * @param Reader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param \Magento\Framework\Mview\View\State\CollectionInterface $stateCollection diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index 0db65f3b6b5fa..0260af34ef108 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -11,6 +11,9 @@ use Magento\Framework\ObjectManager\ConfigCacheInterface; use Magento\Framework\ObjectManager\RelationsInterface; +/** + * Provides object manager configuration when in compiled mode + */ class Compiled implements ConfigInterface { /** @@ -34,6 +37,8 @@ class Compiled implements ConfigInterface private $serializer; /** + * Constructor + * * @param array $data */ public function __construct($data) diff --git a/lib/internal/Magento/Framework/Search/Request/Config.php b/lib/internal/Magento/Framework/Search/Request/Config.php index 4793c4b51c4d5..80a963af39b41 100644 --- a/lib/internal/Magento/Framework/Search/Request/Config.php +++ b/lib/internal/Magento/Framework/Search/Request/Config.php @@ -7,6 +7,9 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Provides search request configuration + */ class Config extends \Magento\Framework\Config\Data { /** @@ -15,6 +18,8 @@ class Config extends \Magento\Framework\Config\Data const CACHE_ID = 'request_declaration'; /** + * Constructor + * * @param \Magento\Framework\Search\Request\Config\FilesystemReader $reader * @param \Magento\Framework\Config\CacheInterface $cache * @param string|null $cacheId diff --git a/setup/src/Magento/Setup/Module/Di/Code/Generator/PluginList.php b/setup/src/Magento/Setup/Module/Di/Code/Generator/PluginList.php index 5853ae8a51cec..851bfa8c36313 100644 --- a/setup/src/Magento/Setup/Module/Di/Code/Generator/PluginList.php +++ b/setup/src/Magento/Setup/Module/Di/Code/Generator/PluginList.php @@ -7,6 +7,9 @@ use Magento\Framework\Interception; +/** + * Provides plugin list configuration + */ class PluginList extends Interception\PluginList\PluginList { /** From 601895025643eb040921230c2021008726d0c372 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 17:07:19 -0500 Subject: [PATCH 139/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding class and __constructor descriptions --- .../Magento/TestFramework/Interception/PluginList.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php b/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php index 07c2c698eee00..c696e362841a2 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php @@ -7,6 +7,9 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Plugin list configuration + */ class PluginList extends \Magento\Framework\Interception\PluginList\PluginList { /** @@ -15,6 +18,8 @@ class PluginList extends \Magento\Framework\Interception\PluginList\PluginList protected $_originScopeScheme = []; /** + * Constructor + * * @param \Magento\Framework\Config\ReaderInterface $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache @@ -26,7 +31,6 @@ class PluginList extends \Magento\Framework\Interception\PluginList\PluginList * @param array $scopePriorityScheme * @param string|null $cacheId * @param SerializerInterface|null $serializer - * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( From 8bac8b032bdd09ca5e849a2bdb3503f0111e99e8 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 17:08:15 -0500 Subject: [PATCH 140/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding class and __constructor descriptions --- .../framework/Magento/TestFramework/Interception/PluginList.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php b/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php index c696e362841a2..18cdc66cb3f2e 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php @@ -8,7 +8,7 @@ use Magento\Framework\Serialize\SerializerInterface; /** - * Plugin list configuration + * Provides plugin list configuration */ class PluginList extends \Magento\Framework\Interception\PluginList\PluginList { From 57b33ce6c83873e2da7492122f3470a5e6d7b2d3 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 17:14:02 -0500 Subject: [PATCH 141/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding class and __constructor descriptions --- dev/tests/integration/etc/install-config-mysql.php.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/etc/install-config-mysql.php.dist b/dev/tests/integration/etc/install-config-mysql.php.dist index 6fffcb22b5000..981d1bd073419 100644 --- a/dev/tests/integration/etc/install-config-mysql.php.dist +++ b/dev/tests/integration/etc/install-config-mysql.php.dist @@ -7,7 +7,7 @@ return [ 'db-host' => 'localhost', 'db-user' => 'root', - 'db-password' => '123123q', + 'db-password' => 'vagrant', 'db-name' => 'magento_integration_tests', 'db-prefix' => '', 'backend-frontname' => 'backend', From 2ee6dbfd3f5307734c626418e3ba7533db76ca36 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 4 Nov 2016 17:46:27 -0500 Subject: [PATCH 142/144] MAGETWO-60442: Add optional SerializerInterface dependency to child classes of \Magento\Framework\Config\Data Adding class and __constructor descriptions --- dev/tests/integration/etc/install-config-mysql.php.dist | 2 +- .../Magento/Framework/App/ResourceConnection/Config.php | 2 ++ lib/internal/Magento/Framework/Config/Data.php | 2 +- lib/internal/Magento/Framework/Config/Data/Scoped.php | 5 +++++ .../Framework/Interception/PluginList/PluginList.php | 9 +++++++-- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/dev/tests/integration/etc/install-config-mysql.php.dist b/dev/tests/integration/etc/install-config-mysql.php.dist index 981d1bd073419..6fffcb22b5000 100644 --- a/dev/tests/integration/etc/install-config-mysql.php.dist +++ b/dev/tests/integration/etc/install-config-mysql.php.dist @@ -7,7 +7,7 @@ return [ 'db-host' => 'localhost', 'db-user' => 'root', - 'db-password' => 'vagrant', + 'db-password' => '123123q', 'db-name' => 'magento_integration_tests', 'db-prefix' => '', 'backend-frontname' => 'backend', diff --git a/lib/internal/Magento/Framework/App/ResourceConnection/Config.php b/lib/internal/Magento/Framework/App/ResourceConnection/Config.php index da0b4d3b033e4..79500c4b1073e 100644 --- a/lib/internal/Magento/Framework/App/ResourceConnection/Config.php +++ b/lib/internal/Magento/Framework/App/ResourceConnection/Config.php @@ -21,6 +21,8 @@ class Config extends \Magento\Framework\Config\Data\Scoped implements ConfigInte protected $_connectionNames = []; /** + * Constructor + * * @param Config\Reader $reader * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Config\CacheInterface $cache diff --git a/lib/internal/Magento/Framework/Config/Data.php b/lib/internal/Magento/Framework/Config/Data.php index 57b6769d3e559..1169abafccd8f 100644 --- a/lib/internal/Magento/Framework/Config/Data.php +++ b/lib/internal/Magento/Framework/Config/Data.php @@ -68,7 +68,7 @@ class Data implements \Magento\Framework\Config\DataInterface /** * @var SerializerInterface */ - protected $serializer; + private $serializer; /** * Constructor diff --git a/lib/internal/Magento/Framework/Config/Data/Scoped.php b/lib/internal/Magento/Framework/Config/Data/Scoped.php index 02379834c5e78..f9c151e867b89 100644 --- a/lib/internal/Magento/Framework/Config/Data/Scoped.php +++ b/lib/internal/Magento/Framework/Config/Data/Scoped.php @@ -55,6 +55,11 @@ class Scoped extends \Magento\Framework\Config\Data */ protected $_loadedScopes = []; + /** + * @var SerializerInterface + */ + private $serializer; + /** * Constructor * diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index dc3ddea2c6fa8..eacd4e8493237 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -82,6 +82,11 @@ class PluginList extends Scoped implements InterceptionPluginList */ private $logger; + /** + * @var SerializerInterface + */ + private $serializer; + /** * Constructor * @@ -111,8 +116,8 @@ public function __construct( $cacheId = 'plugins', SerializerInterface $serializer = null ) { - $serializer = $serializer ?: $objectManager->get(Serialize::class); - parent::__construct($reader, $configScope, $cache, $cacheId, $serializer); + $this->serializer = $serializer ?: $objectManager->get(Serialize::class); + parent::__construct($reader, $configScope, $cache, $cacheId, $this->serializer); $this->_omConfig = $omConfig; $this->_relations = $relations; $this->_definitions = $definitions; From 9cd37983ee993ee024459e6dae3fc29285d91b49 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 30 Nov 2016 22:44:11 -0600 Subject: [PATCH 143/144] MAGETWO-58446: Remove uses of unserialize in usages of \Magento\Framework\Cache\FrontendInterface::load() --- app/code/Magento/Backend/Model/Url.php | 13 ++- .../Backend/Test/Unit/Model/UrlTest.php | 63 ++++------- .../Import/Product/Type/Configurable.php | 37 +++--- .../Import/Product/Type/ConfigurableTest.php | 9 +- .../Test/Unit/Model/Address/ConfigTest.php | 105 +++++++----------- .../Import/Product/Type/Downloadable.php | 1 - .../SalesRule/Test/Unit/Model/RuleTest.php | 12 +- .../Import/Product/Type/DownloadableTest.php | 2 - .../Test/Unit/Config/ConfigTest.php | 1 - .../Test/Unit/PluginList/PluginListTest.php | 28 +++-- 10 files changed, 111 insertions(+), 160 deletions(-) diff --git a/app/code/Magento/Backend/Model/Url.php b/app/code/Magento/Backend/Model/Url.php index c1aa03a457cac..f09c9c04ee6bb 100644 --- a/app/code/Magento/Backend/Model/Url.php +++ b/app/code/Magento/Backend/Model/Url.php @@ -5,6 +5,8 @@ */ namespace Magento\Backend\Model; +use Magento\Framework\Url\HostChecker; +use Magento\Framework\App\ObjectManager; /** * Class \Magento\Backend\Model\UrlInterface @@ -77,6 +79,8 @@ class Url extends \Magento\Framework\Url implements \Magento\Backend\Model\UrlIn protected $_scope; /** + * Constructor + * * @param \Magento\Framework\App\Route\ConfigInterface $routeConfig * @param \Magento\Framework\App\RequestInterface $request * @param \Magento\Framework\Url\SecurityInfoInterface $urlSecurityInfo @@ -96,7 +100,7 @@ class Url extends \Magento\Framework\Url implements \Magento\Backend\Model\UrlIn * @param \Magento\Store\Model\StoreFactory $storeFactory * @param \Magento\Framework\Data\Form\FormKey $formKey * @param array $data - * + * @param HostChecker|null $hostChecker * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -118,9 +122,11 @@ public function __construct( \Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Store\Model\StoreFactory $storeFactory, \Magento\Framework\Data\Form\FormKey $formKey, - array $data = [] + array $data = [], + HostChecker $hostChecker = null ) { $this->_encryptor = $encryptor; + $hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class); parent::__construct( $routeConfig, $request, @@ -133,7 +139,8 @@ public function __construct( $scopeConfig, $routeParamsPreprocessor, $scopeType, - $data + $data, + $hostChecker ); $this->_backendHelper = $backendHelper; $this->_menuConfig = $menuConfig; diff --git a/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php b/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php index 2bc3d86e74d69..4eda145156c6d 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php @@ -3,16 +3,13 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - -/** - * Test class for \Magento\Backend\Model\Url - */ namespace Magento\Backend\Test\Unit\Model; +use Magento\Framework\Url\HostChecker; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @codingStandardsIgnoreFile */ class UrlTest extends \PHPUnit_Framework_TestCase { @@ -21,10 +18,12 @@ class UrlTest extends \PHPUnit_Framework_TestCase */ protected $_model; + /** + * @var string + */ protected $_areaFrontName = 'backendArea'; /** - * Mock menu model * @var \PHPUnit_Framework_MockObject_MockObject */ protected $_menuMock; @@ -62,7 +61,7 @@ class UrlTest extends \PHPUnit_Framework_TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_paramsResolverMock; + protected $routeParamsResolverFactoryMock; /** * @var \Magento\Framework\Encryption\EncryptorInterface @@ -75,6 +74,7 @@ class UrlTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_menuMock = $this->getMock( \Magento\Backend\Model\Menu::class, [], @@ -141,25 +141,21 @@ protected function setUp() false, false ); - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_encryptor = $this->getMock(\Magento\Framework\Encryption\Encryptor::class, null, [], '', false); - $this->_paramsResolverMock = $this->getMock( + $routeParamsResolver = $this->getMock(\Magento\Framework\Url\RouteParamsResolver::class, [], [], '', false); + $this->routeParamsResolverFactoryMock = $this->getMock( \Magento\Framework\Url\RouteParamsResolverFactory::class, [], [], '', false ); - $this->_paramsResolverMock->expects( - $this->any() - )->method( - 'create' - )->will( - $this->returnValue( - $this->getMock(\Magento\Framework\Url\RouteParamsResolver::class, [], [], '', false) - ) - ); - $this->_model = $helper->getObject( + $this->routeParamsResolverFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($routeParamsResolver); + /** @var HostChecker|\PHPUnit_Framework_MockObject_MockObject $hostCheckerMock */ + $hostCheckerMock = $this->getMock(HostChecker::class, [], [], '', false); + $this->_model = $objectManager->getObject( \Magento\Backend\Model\Url::class, [ 'scopeConfig' => $this->_scopeConfigMock, @@ -168,31 +164,10 @@ protected function setUp() 'menuConfig' => $this->_menuConfigMock, 'authSession' => $this->_authSessionMock, 'encryptor' => $this->_encryptor, - 'routeParamsResolverFactory' => $this->_paramsResolverMock + 'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock, + 'hostChecker' => $hostCheckerMock ] ); - $this->_paramsResolverMock->expects( - $this->any() - )->method( - 'create' - )->will( - $this->returnValue( - $this->getMock(\Magento\Framework\Url\RouteParamsResolver::class, [], [], '', false) - ) - ); - $this->_model = $helper->getObject( - \Magento\Backend\Model\Url::class, - [ - 'scopeConfig' => $this->_scopeConfigMock, - 'backendHelper' => $helperMock, - 'formKey' => $this->_formKey, - 'menuConfig' => $this->_menuConfigMock, - 'authSession' => $this->_authSessionMock, - 'encryptor' => $this->_encryptor, - 'routeParamsResolverFactory' => $this->_paramsResolverMock - ] - ); - $this->_requestMock = $this->getMock(\Magento\Framework\App\Request\Http::class, [], [], '', false); $this->_model->setRequest($this->_requestMock); } @@ -262,7 +237,7 @@ public function testGetAreaFrontName() [ 'backendHelper' => $helperMock, 'authSession' => $this->_authSessionMock, - 'routeParamsResolverFactory' => $this->_paramsResolverMock + 'routeParamsResolverFactory' => $this->routeParamsResolverFactoryMock ] ); $urlModel->getAreaFrontName(); diff --git a/app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php b/app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php index 29e670e9f7f4f..9eedf22fbc1ae 100644 --- a/app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php +++ b/app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php @@ -12,6 +12,7 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\CatalogImportExport\Model\Import\Product as ImportProduct; +use Magento\Framework\EntityManager\MetadataPool; /** * Importing configurable products @@ -140,6 +141,7 @@ class Configurable extends \Magento\CatalogImportExport\Model\Import\Product\Typ * Instance of database adapter. * * @var \Magento\Framework\DB\Adapter\AdapterInterface + * @deprecated */ protected $_connection; @@ -200,6 +202,7 @@ class Configurable extends \Magento\CatalogImportExport\Model\Import\Product\Typ * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypesConfig * @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productColFac + * @param MetadataPool $metadataPool */ public function __construct( \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac, @@ -208,12 +211,12 @@ public function __construct( array $params, \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypesConfig, \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper, - \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productColFac + \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productColFac, + MetadataPool $metadataPool = null ) { - parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params); + parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params, $metadataPool); $this->_productTypesConfig = $productTypesConfig; $this->_resourceHelper = $resourceHelper; - $this->_resource = $resource; $this->_productColFac = $_productColFac; $this->_connection = $this->_entityModel->getConnection(); } @@ -379,14 +382,14 @@ protected function _loadSkuSuperDataForBunch(array $bunch) if (!empty($productIds)) { $mainTable = $this->_resource->getTableName('catalog_product_super_attribute'); $optionTable = $this->_resource->getTableName('eav_attribute_option'); - $select = $this->_connection->select()->from( + $select = $this->connection->select()->from( ['m' => $mainTable], ['product_id', 'attribute_id', 'product_super_attribute_id'] )->joinLeft( ['o' => $optionTable], - $this->_connection->quoteIdentifier( + $this->connection->quoteIdentifier( 'o.attribute_id' - ) . ' = ' . $this->_connection->quoteIdentifier( + ) . ' = ' . $this->connection->quoteIdentifier( 'o.attribute_id' ), ['option_id'] @@ -395,7 +398,7 @@ protected function _loadSkuSuperDataForBunch(array $bunch) $productIds ); - foreach ($this->_connection->fetchAll($select) as $row) { + foreach ($this->connection->fetchAll($select) as $row) { $attrId = $row['attribute_id']; $productId = $row['product_id']; if ($row['option_id']) { @@ -448,8 +451,8 @@ protected function _processSuperData() 'product_id' => $this->_productSuperData['assoc_entity_ids'][$assocId], 'parent_id' => $this->_productSuperData['product_id'], ]; - $subEntityId = $this->_connection->fetchOne( - $this->_connection->select()->from( + $subEntityId = $this->connection->fetchOne( + $this->connection->select()->from( ['cpe' => $this->_resource->getTableName('catalog_product_entity')], ['entity_id'] )->where($metadata->getLinkField() . ' = ?', $assocId) ); @@ -557,10 +560,10 @@ protected function _deleteData() && !empty($this->_productSuperData['product_id']) && !empty($this->_simpleIdsToDelete) ) { - $quoted = $this->_connection->quoteInto('IN (?)', [$this->_productSuperData['product_id']]); - $quotedChildren = $this->_connection->quoteInto('IN (?)', $this->_simpleIdsToDelete); - $this->_connection->delete($linkTable, "parent_id {$quoted} AND product_id {$quotedChildren}"); - $this->_connection->delete($relationTable, "parent_id {$quoted} AND child_id {$quotedChildren}"); + $quoted = $this->connection->quoteInto('IN (?)', [$this->_productSuperData['product_id']]); + $quotedChildren = $this->connection->quoteInto('IN (?)', $this->_simpleIdsToDelete); + $this->connection->delete($linkTable, "parent_id {$quoted} AND product_id {$quotedChildren}"); + $this->connection->delete($relationTable, "parent_id {$quoted} AND child_id {$quotedChildren}"); } return $this; } @@ -587,16 +590,16 @@ protected function _insertData() } } if ($mainData) { - $this->_connection->insertOnDuplicate($mainTable, $mainData); + $this->connection->insertOnDuplicate($mainTable, $mainData); } if ($this->_superAttributesData['labels']) { - $this->_connection->insertOnDuplicate($labelTable, $this->_superAttributesData['labels']); + $this->connection->insertOnDuplicate($labelTable, $this->_superAttributesData['labels']); } if ($this->_superAttributesData['super_link']) { - $this->_connection->insertOnDuplicate($linkTable, $this->_superAttributesData['super_link']); + $this->connection->insertOnDuplicate($linkTable, $this->_superAttributesData['super_link']); } if ($this->_superAttributesData['relation']) { - $this->_connection->insertOnDuplicate($relationTable, $this->_superAttributesData['relation']); + $this->connection->insertOnDuplicate($relationTable, $this->_superAttributesData['relation']); } return $this; } diff --git a/app/code/Magento/ConfigurableImportExport/Test/Unit/Model/Import/Product/Type/ConfigurableTest.php b/app/code/Magento/ConfigurableImportExport/Test/Unit/Model/Import/Product/Type/ConfigurableTest.php index 8ce7489db932e..4cc4e6648a0ef 100644 --- a/app/code/Magento/ConfigurableImportExport/Test/Unit/Model/Import/Product/Type/ConfigurableTest.php +++ b/app/code/Magento/ConfigurableImportExport/Test/Unit/Model/Import/Product/Type/ConfigurableTest.php @@ -312,15 +312,10 @@ protected function setUp() 'prodAttrColFac' => $this->attrCollectionFactory, 'params' => $this->params, 'resource' => $this->resource, - 'productColFac' => $this->productCollectionFactory + 'productColFac' => $this->productCollectionFactory, + 'metadataPool' => $metadataPoolMock, ] ); - $reflection = new \ReflectionClass( - \Magento\ConfigurableImportExport\Model\Import\Product\Type\Configurable::class - ); - $reflectionProperty = $reflection->getProperty('metadataPool'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($this->configurable, $metadataPoolMock); } /** diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index ff028e2f7340b..2b4a93084c7ac 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -5,140 +5,115 @@ */ namespace Magento\Customer\Test\Unit\Model\Address; -/** - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ class ConfigTest extends \PHPUnit_Framework_TestCase { /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_cacheMock; + protected $addressHelperMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_storeManagerMock; + protected $storeMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_addressHelperMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_storeMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_scopeConfigMock; - - /** - * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - private $serializerMock; + protected $scopeConfigMock; /** * @var \Magento\Customer\Model\Address\Config */ - protected $_model; - - /** - * @var string - */ - protected $_cacheId = 'cache_id'; + protected $model; protected function setUp() { - $this->_storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false); - $this->_scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); + $cacheId = 'cache_id'; + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false); + $this->scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); - $this->_readerMock = $this->getMock( + $readerMock = $this->getMock( \Magento\Customer\Model\Address\Config\Reader::class, [], [], '', false ); - $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->_storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManager::class, [], [], '', false); - $this->_storeManagerMock->expects( + $cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManager::class, [], [], '', false); + $storeManagerMock->expects( $this->once() )->method( 'getStore' )->will( - $this->returnValue($this->_storeMock) + $this->returnValue($this->storeMock) ); - $this->_addressHelperMock = $this->getMock(\Magento\Customer\Helper\Address::class, [], [], '', false); + $this->addressHelperMock = $this->getMock(\Magento\Customer\Helper\Address::class, [], [], '', false); - $this->_cacheMock->expects( + $cacheMock->expects( $this->once() )->method( 'load' )->with( - $this->_cacheId + $cacheId )->will( $this->returnValue(false) ); $fixtureConfigData = require __DIR__ . '/Config/_files/formats_merged.php'; - $this->_readerMock->expects($this->once())->method('read')->will($this->returnValue($fixtureConfigData)); + $readerMock->expects($this->once())->method('read')->will($this->returnValue($fixtureConfigData)); - $this->_cacheMock->expects($this->once()) + $cacheMock->expects($this->once()) ->method('save') ->with( json_encode($fixtureConfigData), - $this->_cacheId + $cacheId ); - $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->serializerMock->method('serialize') + $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $serializerMock->method('serialize') ->willReturn(json_encode($fixtureConfigData)); - $this->serializerMock->method('unserialize') + $serializerMock->method('unserialize') ->willReturn($fixtureConfigData); - $this->_model = new \Magento\Customer\Model\Address\Config( - $this->_readerMock, - $this->_cacheMock, - $this->_storeManagerMock, - $this->_addressHelperMock, - $this->_scopeConfigMock, - $this->_cacheId, - $this->serializerMock + $this->model = $objectManagerHelper->getObject( + \Magento\Customer\Model\Address\Config::class, + [ + 'reader' => $readerMock, + 'cache' => $cacheMock, + 'storeManager' => $storeManagerMock, + 'scopeConfig' => $this->scopeConfigMock, + 'cacheId' => $cacheId, + 'serializer' => $serializerMock, + 'addressHelper' => $this->addressHelperMock, + ] ); } public function testGetStore() { - $this->assertEquals($this->_storeMock, $this->_model->getStore()); + $this->assertEquals($this->storeMock, $this->model->getStore()); } public function testSetStore() { - $this->_model->setStore($this->_storeMock); - - //no call to $_storeManagerMock's method - $this->assertEquals($this->_storeMock, $this->_model->getStore()); + $this->model->setStore($this->storeMock); + $this->assertEquals($this->storeMock, $this->model->getStore()); } public function testGetFormats() { - $this->_storeMock->expects($this->once())->method('getId'); + $this->storeMock->expects($this->once())->method('getId'); - $this->_scopeConfigMock->expects($this->any())->method('getValue')->will($this->returnValue('someValue')); + $this->scopeConfigMock->expects($this->any())->method('getValue')->will($this->returnValue('someValue')); $rendererMock = $this->getMock(\Magento\Framework\DataObject::class); - $this->_addressHelperMock->expects( + $this->addressHelperMock->expects( $this->any() )->method( 'getRenderer' @@ -173,6 +148,6 @@ public function testGetFormats() ); $expectedResult = [$firstExpected, $secondExpected]; - $this->assertEquals($expectedResult, $this->_model->getFormats()); + $this->assertEquals($expectedResult, $this->model->getFormats()); } } diff --git a/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php b/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php index a34276ab5f4c6..7a447aa90eea8 100644 --- a/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php +++ b/app/code/Magento/DownloadableImportExport/Model/Import/Product/Type/Downloadable.php @@ -266,7 +266,6 @@ public function __construct( parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params, $metadataPool); $this->parameters = $this->_entityModel->getParameters(); $this->_resource = $resource; - $this->connection = $resource->getConnection('write'); $this->uploaderHelper = $uploaderHelper; $this->downloadableHelper = $downloadableHelper; } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index b5b33fb8dc39e..48cd2ab7c4850 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -5,16 +5,8 @@ */ namespace Magento\SalesRule\Test\Unit\Model; -/** - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ class RuleTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\SalesRule\Model\Rule */ @@ -37,7 +29,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->coupon = $this->getMockBuilder(\Magento\SalesRule\Model\Coupon::class) ->disableOriginalConstructor() @@ -64,7 +56,7 @@ protected function setUp() ->setMethods(['create']) ->getMock(); - $this->model = $this->objectManager->getObject( + $this->model = $objectManager->getObject( \Magento\SalesRule\Model\Rule::class, [ 'couponFactory' => $couponFactory, diff --git a/dev/tests/integration/testsuite/Magento/DownloadableImportExport/Model/Import/Product/Type/DownloadableTest.php b/dev/tests/integration/testsuite/Magento/DownloadableImportExport/Model/Import/Product/Type/DownloadableTest.php index 8db369facf97f..d25e040688237 100644 --- a/dev/tests/integration/testsuite/Magento/DownloadableImportExport/Model/Import/Product/Type/DownloadableTest.php +++ b/dev/tests/integration/testsuite/Magento/DownloadableImportExport/Model/Import/Product/Type/DownloadableTest.php @@ -5,9 +5,7 @@ */ namespace Magento\DownloadableImportExport\Model\Import\Product\Type; -use Magento\Framework\App\Bootstrap; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\ImportExport\Model\Import; /** * @magentoAppArea adminhtml 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 c858bcb8812c4..7d15d1029bda6 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -7,7 +7,6 @@ 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'; 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 15d1b55f7cfcd..11e8dabf6d924 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/PluginList/PluginListTest.php @@ -6,6 +6,7 @@ namespace Magento\Framework\Interception\Test\Unit\PluginList; use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\ObjectManagerInterface; require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; @@ -37,15 +38,20 @@ class PluginListTest extends \PHPUnit_Framework_TestCase */ private $cacheMock; + /** + * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $loggerMock; + /** * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $serializerMock; /** - * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject + * @var ObjectManagerInterface||\PHPUnit_Framework_MockObject_MockObject */ - private $loggerMock; + private $objectManagerMock; protected function setUp() { @@ -66,8 +72,10 @@ protected function setUp() $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('get')->will($this->returnArgument(0)); + $this->objectManagerMock = $this->getMock(ObjectManagerInterface::class); + $this->objectManagerMock->expects($this->any()) + ->method('get') + ->willReturnArgument(0); $this->serializerMock = $this->getMock(SerializerInterface::class); $definitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); @@ -82,7 +90,7 @@ protected function setUp() 'relations' => new \Magento\Framework\ObjectManager\Relations\Runtime(), 'omConfig' => $omConfigMock, 'definitions' => new \Magento\Framework\Interception\Definition\Runtime(), - 'objectManager' => $objectManagerMock, + 'objectManager' => $this->objectManagerMock, 'classDefinitions' => $definitions, 'scopePriorityScheme' => ['global'], 'cacheId' => 'interception', @@ -234,7 +242,7 @@ public function testInheritPluginsWithNonExistingClass() public function testLoadScopedDataNotCached() { - $this->configScopeMock->expects($this->exactly(2)) + $this->configScopeMock->expects($this->exactly(3)) ->method('getCurrentScope') ->will($this->returnValue('scope')); $this->serializerMock->expects($this->once()) @@ -295,23 +303,23 @@ public function testLoadScopedDataCached() */ public function testLoadScopeDataWithEmptyData() { - $this->_objectManagerMock->expects($this->any()) + $this->objectManagerMock->expects($this->any()) ->method('get') ->will($this->returnArgument(0)); - $this->_configScopeMock->expects($this->any()) + $this->configScopeMock->expects($this->any()) ->method('getCurrentScope') ->will($this->returnValue('emptyscope')); $this->assertEquals( [4 => ['simple_plugin']], - $this->_model->getNext( + $this->object->getNext( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'getName' ) ); $this->assertEquals( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, - $this->_model->getPlugin( + $this->object->getPlugin( \Magento\Framework\Interception\Test\Unit\Custom\Module\Model\Item::class, 'simple_plugin' ) From b072b985850a14eea813234742d5b12366f75a4b Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 1 Dec 2016 16:41:43 -0600 Subject: [PATCH 144/144] Merge remote-tracking branch 'upstream/develop' into pr # Conflicts: # dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php # lib/internal/Magento/Framework/App/Config/ScopePool.php # lib/internal/Magento/Framework/App/ResourceConnection/Config.php # lib/internal/Magento/Framework/App/Test/Unit/Config/ScopePoolTest.php # lib/internal/Magento/Framework/App/Test/Unit/ResourceConnection/ConfigTest.php --- .../Framework/App/Config/ScopePoolTest.php | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php deleted file mode 100644 index 8e000676c7a68..0000000000000 --- a/dev/tests/integration/testsuite/Magento/Framework/App/Config/ScopePoolTest.php +++ /dev/null @@ -1,49 +0,0 @@ -removeSharedInstance(ScopePool::class); - $this->scopePool = $objectManager->get(ScopePool::class); - } - - /** - * @param string $scopeType - * @param string $scopeCode - * @dataProvider getScopeDataProvider - */ - public function testGetScope($scopeType, $scopeCode = null) - { - $this->scopePool->clean(); - $this->assertEquals( - $this->scopePool->getScope($scopeType, $scopeCode), - $this->scopePool->getScope($scopeType, $scopeCode) - ); - } - - public function getScopeDataProvider() - { - return [ - ['default'], - ['stores', 'default'], - ['websites', 'default'] - ]; - } -}