From 5b5120b08290f7b02dbeab8661395f487f7b7c98 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Sep 2016 08:27:44 -0500 Subject: [PATCH 01/33] MAGETWO-58843: Create Json class --- app/etc/di.xml | 1 + lib/internal/Magento/Framework/Json/Json.php | 25 +++++++++++++++ .../Magento/Framework/Json/JsonInterface.php | 31 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lib/internal/Magento/Framework/Json/Json.php create mode 100644 lib/internal/Magento/Framework/Json/JsonInterface.php diff --git a/app/etc/di.xml b/app/etc/di.xml index 28f0d024c3ae2..157da1a49b470 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -154,6 +154,7 @@ + diff --git a/lib/internal/Magento/Framework/Json/Json.php b/lib/internal/Magento/Framework/Json/Json.php new file mode 100644 index 0000000000000..35d94e6ee0939 --- /dev/null +++ b/lib/internal/Magento/Framework/Json/Json.php @@ -0,0 +1,25 @@ + Date: Fri, 23 Sep 2016 10:14:06 -0500 Subject: [PATCH 02/33] MAGETWO-58843: Create Json class Adding unit test --- .../Framework/Json/Test/Unit/JsonTest.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php diff --git a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php new file mode 100644 index 0000000000000..6d21897c1052b --- /dev/null +++ b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php @@ -0,0 +1,48 @@ +json = new Json(); + } + + /** + * @param null|bool|array|\stdClass $value + * @param int $objectDecodeType + * @dataProvider encodeDecodeDataProvider + */ + public function testEncodeDecode($value, $objectDecodeType) + { + $this->assertEquals( + $this->json->decode($this->json->encode($value), $objectDecodeType), + $value + ); + } + + public function encodeDecodeDataProvider() + { + $object = new \stdClass(); + $object->a = 'b'; + return [ + ['', JsonInterface::TYPE_ARRAY], + [null, JsonInterface::TYPE_ARRAY], + [false, JsonInterface::TYPE_ARRAY], + [['a' => 'b'], JsonInterface::TYPE_ARRAY], + [$object, JsonInterface::TYPE_OBJECT] + ]; + } +} From 9e020abf4f6e53bbb4966e77cdcbc1948c7bcf0d Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 14:16:45 -0500 Subject: [PATCH 03/33] MAGETWO-58843: Create Json class Refactoring --- lib/internal/Magento/Framework/Json/JsonInterface.php | 9 ++++++++- .../Magento/Framework/Json/Test/Unit/JsonTest.php | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Json/JsonInterface.php b/lib/internal/Magento/Framework/Json/JsonInterface.php index af18965f7737c..3d9df3d89e7ac 100644 --- a/lib/internal/Magento/Framework/Json/JsonInterface.php +++ b/lib/internal/Magento/Framework/Json/JsonInterface.php @@ -7,12 +7,19 @@ interface JsonInterface { + /** + * Decode object as array + */ const TYPE_ARRAY = 1; + /** + * Decode object as object + */ const TYPE_OBJECT = 0; /** - * Encode $data into the JSON format + * Encode $data into the JSON format. Please see http://php.net/manual/en/function.json-encode.php for supported + * values for $option * * @param array|string $data * @param int $options diff --git a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php index 6d21897c1052b..596ef85e4b5f4 100644 --- a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php +++ b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php @@ -17,7 +17,8 @@ class JsonTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->json = new Json(); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->json = $objectManager->getObject(Json::class); } /** From 3977f5783d73dc53ab8fb72928ad951138771250 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Fri, 23 Sep 2016 15:35:01 -0500 Subject: [PATCH 04/33] MAGETWO-58843: Create Json class Refactoring --- lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php index 596ef85e4b5f4..7190e85c5dad5 100644 --- a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php +++ b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php @@ -3,7 +3,7 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Framework\Json\Test\Unit\Helper; +namespace Magento\Framework\Json\Test\Unit; use Magento\Framework\Json\JsonInterface; use Magento\Framework\Json\Json; From 363f1caa473d97b7752fadee9359ee3dd3571fb2 Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 17 Oct 2016 12:22:30 -0500 Subject: [PATCH 05/33] MAGETWO-59444: Create serializer interface and json class in framework --- app/etc/di.xml | 2 +- lib/internal/Magento/Framework/Json/Json.php | 25 ------------ .../Magento/Framework/Json/JsonInterface.php | 38 ------------------- .../Magento/Framework/Serialize/Json.php | 25 ++++++++++++ .../Serialize/SerializerInterface.php | 26 +++++++++++++ .../Test/Unit/JsonTest.php | 21 +++++----- 6 files changed, 62 insertions(+), 75 deletions(-) delete mode 100644 lib/internal/Magento/Framework/Json/Json.php delete mode 100644 lib/internal/Magento/Framework/Json/JsonInterface.php create mode 100644 lib/internal/Magento/Framework/Serialize/Json.php create mode 100644 lib/internal/Magento/Framework/Serialize/SerializerInterface.php rename lib/internal/Magento/Framework/{Json => Serialize}/Test/Unit/JsonTest.php (59%) diff --git a/app/etc/di.xml b/app/etc/di.xml index 157da1a49b470..2828a238c2671 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -154,7 +154,7 @@ - + diff --git a/lib/internal/Magento/Framework/Json/Json.php b/lib/internal/Magento/Framework/Json/Json.php deleted file mode 100644 index 35d94e6ee0939..0000000000000 --- a/lib/internal/Magento/Framework/Json/Json.php +++ /dev/null @@ -1,25 +0,0 @@ -assertEquals( - $this->json->decode($this->json->encode($value), $objectDecodeType), + $this->json->unserialize($this->json->serialize($value)), $value ); } @@ -39,11 +39,10 @@ public function encodeDecodeDataProvider() $object = new \stdClass(); $object->a = 'b'; return [ - ['', JsonInterface::TYPE_ARRAY], - [null, JsonInterface::TYPE_ARRAY], - [false, JsonInterface::TYPE_ARRAY], - [['a' => 'b'], JsonInterface::TYPE_ARRAY], - [$object, JsonInterface::TYPE_OBJECT] + [''], + [null], + [false], + [['a' => 'b']], ]; } } From 26319b888d94d8d963264db25395e756492965dc Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 17 Oct 2016 14:24:35 -0500 Subject: [PATCH 06/33] MAGETWO-59444: Create serializer interface and json class in framework --- lib/internal/Magento/Framework/Serialize/README.md | 5 +++++ .../Magento/Framework/Serialize/SerializerInterface.php | 9 ++++----- 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 lib/internal/Magento/Framework/Serialize/README.md diff --git a/lib/internal/Magento/Framework/Serialize/README.md b/lib/internal/Magento/Framework/Serialize/README.md new file mode 100644 index 0000000000000..f724a87bc3bba --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/README.md @@ -0,0 +1,5 @@ +# Serialize + +**Serialize** provides *SerializerInterface* and a few serializers to support different kinds of needs of serializing/unserializing of data. Here are list of serializers in this library: + + * **Json** (recommended) - 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 diff --git a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php index 0f8fc7149b79d..d3c856687f408 100644 --- a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php +++ b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php @@ -10,17 +10,16 @@ interface SerializerInterface /** * Serialize data into string * - * @param array|string $data - * @return string|bool + * @param string|integer|float|boolean|array|null $data + * @return string|boolean */ public function serialize($data); /** - * Unserialize the given string into array + * Unserialize the given string into data * * @param string $string - * @param int $objectDecodeType - * @return array + * @return string|integer|float|boolean|array|null */ public function unserialize($string); } From b583fe6730ba2f55f4eb9fed8edaaed6713cd6a1 Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 17 Oct 2016 15:19:21 -0500 Subject: [PATCH 07/33] MAGETWO-59444: Create serializer interface and json class in framework --- app/etc/di.xml | 2 +- .../Serialize/{ => Serializer}/Json.php | 4 +++- .../Test/Unit/{ => Serializer}/JsonTest.php | 17 +++++++---------- 3 files changed, 11 insertions(+), 12 deletions(-) rename lib/internal/Magento/Framework/Serialize/{ => Serializer}/Json.php (79%) rename lib/internal/Magento/Framework/Serialize/Test/Unit/{ => Serializer}/JsonTest.php (62%) diff --git a/app/etc/di.xml b/app/etc/di.xml index 2828a238c2671..2bd37564a597a 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -154,7 +154,7 @@ - + diff --git a/lib/internal/Magento/Framework/Serialize/Json.php b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php similarity index 79% rename from lib/internal/Magento/Framework/Serialize/Json.php rename to lib/internal/Magento/Framework/Serialize/Serializer/Json.php index 7b5f8a2006bbb..009fee5e3639f 100644 --- a/lib/internal/Magento/Framework/Serialize/Json.php +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php @@ -3,7 +3,9 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Framework\Serialize; +namespace Magento\Framework\Serialize\Serializer; + +use Magento\Framework\Serialize\SerializerInterface; class Json implements SerializerInterface { diff --git a/lib/internal/Magento/Framework/Serialize/Test/Unit/JsonTest.php b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php similarity index 62% rename from lib/internal/Magento/Framework/Serialize/Test/Unit/JsonTest.php rename to lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php index add2f60d44c6e..1f36b7b729e9b 100644 --- a/lib/internal/Magento/Framework/Serialize/Test/Unit/JsonTest.php +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php @@ -3,15 +3,14 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Framework\Serialize\Test\Unit; +namespace Magento\Framework\Serialize\Test\Unit\Serializer; -use Magento\Framework\Serialize\SerializerInterface; -use Magento\Framework\Serialize\Json; +use Magento\Framework\Serialize\Serializer\Json; class JsonTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\Serialize\Json + * @var \Magento\Framework\Serialize\Serializer\Json */ private $json; @@ -22,11 +21,11 @@ protected function setUp() } /** - * @param null|bool|array|\stdClass $value + * @param null|bool|array $value * @param int $objectDecodeType - * @dataProvider encodeDecodeDataProvider + * @dataProvider serializeUnserializeDataProvider */ - public function testEncodeDecode($value) + public function testSerializeUnserialize($value) { $this->assertEquals( $this->json->unserialize($this->json->serialize($value)), @@ -34,10 +33,8 @@ public function testEncodeDecode($value) ); } - public function encodeDecodeDataProvider() + public function serializeUnserializeDataProvider() { - $object = new \stdClass(); - $object->a = 'b'; return [ [''], [null], From 4efeb173baf6fbb3f098086ae0bdf470ad4f9ff4 Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 17 Oct 2016 18:30:48 -0500 Subject: [PATCH 08/33] MAGETWO-59444: Create serializer interface and json class in framework --- lib/internal/Magento/Framework/Serialize/README.md | 4 ++-- .../Magento/Framework/Serialize/SerializerInterface.php | 8 ++++---- .../Framework/Serialize/Test/Unit/Serializer/JsonTest.php | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Serialize/README.md b/lib/internal/Magento/Framework/Serialize/README.md index f724a87bc3bba..e636fc79818af 100644 --- a/lib/internal/Magento/Framework/Serialize/README.md +++ b/lib/internal/Magento/Framework/Serialize/README.md @@ -1,5 +1,5 @@ # Serialize -**Serialize** provides *SerializerInterface* and a few serializers to support different kinds of needs of serializing/unserializing of data. Here are list of serializers in this library: +**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: - * **Json** (recommended) - 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) - 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 diff --git a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php index d3c856687f408..1dc70da80f394 100644 --- a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php +++ b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php @@ -10,16 +10,16 @@ interface SerializerInterface /** * Serialize data into string * - * @param string|integer|float|boolean|array|null $data - * @return string|boolean + * @param string|int|float|bool|array|null $data + * @return string|bool */ public function serialize($data); /** - * Unserialize the given string into data + * Unserialize the given string * * @param string $string - * @return string|integer|float|boolean|array|null + * @return string|int|float|bool|array|null */ public function unserialize($string); } 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 1f36b7b729e9b..38fa7d2a66f7f 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,6 @@ protected function setUp() /** * @param null|bool|array $value - * @param int $objectDecodeType * @dataProvider serializeUnserializeDataProvider */ public function testSerializeUnserialize($value) @@ -37,6 +36,7 @@ public function serializeUnserializeDataProvider() { return [ [''], + ['string'], [null], [false], [['a' => 'b']], From 226ea7f5c095fff7212d2d188f4c315419cd1a6f Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 18 Oct 2016 12:13:05 -0500 Subject: [PATCH 09/33] MAGETWO-58691: Refactor Module_Quote, Module_Sales - fixed static test --- .../Magento/Test/Legacy/UnsecureFunctionsUsageTest.php | 3 ++- .../Magento/Test/Legacy/_files/security/blacklist.php | 5 +++-- .../Magento/Test/Legacy/_files/security/whitelist.txt | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php index 6b18eb2b71478..52972683319e8 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php @@ -114,7 +114,8 @@ function ($path) use ($directoriesToScan, $fileExtensions, $blackListFiles) { if (strpos($path, $directory) === 0) { if (preg_match($fileExtensions, $path)) { foreach ($blackListFiles as $blackListFile) { - if (preg_match($blackListFile, $path)) { + $blackListFile = preg_quote($blackListFile, '#'); + if (preg_match('#' . $blackListFile . '#', $path)) { return false; } } diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php index d120a4543b9dd..edcb6ab4d7fb9 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php @@ -4,6 +4,7 @@ * See COPYING.txt for license details. */ return [ - '/Test\/Unit/', - '/lib\/internal\/Magento\/Framework\/DB\/Adapter\/Pdo\/Mysql\.php/', + 'Test/Unit', + 'lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php', + 'lib/internal/Magento/Framework/Serialize/SerializerInterface.php', ]; diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/whitelist.txt b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/whitelist.txt index 2567475de6a03..e464d9713657f 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/whitelist.txt +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/whitelist.txt @@ -1,4 +1,5 @@ +# "Component Type" "Component Name" "Path Pattern" module * / library * / setup -pub \ No newline at end of file +pub From 9dfaec84c3107ddcb6b412bbb508dc755e6c1b19 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Tue, 18 Oct 2016 16:00:12 -0500 Subject: [PATCH 10/33] MAGETWO-58691: Refactor Module_Quote, Module_Sales - fixed static test to skip function declarations and calls to class methods --- .../Magento/Test/Legacy/UnsecureFunctionsUsageTest.php | 4 ++-- .../Magento/Test/Legacy/_files/security/blacklist.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php index 52972683319e8..5220eb43644fd 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php @@ -64,7 +64,7 @@ function ($fileName) { if ($regexp) { $matches = preg_grep( $regexp, - file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) + file($fileName) ); if (!empty($matches)) { foreach (array_keys($matches) as $line) { @@ -161,7 +161,7 @@ private function prepareRegexp(array $functions) } $regexArray = []; foreach ($functions as $function) { - $regexArray[] = '\b' . $function . '\b\('; + $regexArray[] = '(? Date: Wed, 19 Oct 2016 15:14:14 -0500 Subject: [PATCH 11/33] MAGETWO-58691: Refactor Module_Quote, Module_Sales - replaced native functions with SerializerInterface --- .../Magento/Quote/Model/QueryResolver.php | 15 +++- .../Model/Quote/Address/Total/Collector.php | 7 +- .../Test/Unit/Model/QueryResolverTest.php | 75 ++++++++++++------- .../Magento/Sales/Model/Config/Ordered.php | 15 +++- .../Sales/Model/Order/Total/Config/Base.php | 7 +- .../Model/Order/Total/Config/BaseTest.php | 32 ++++++-- 6 files changed, 105 insertions(+), 46 deletions(-) diff --git a/app/code/Magento/Quote/Model/QueryResolver.php b/app/code/Magento/Quote/Model/QueryResolver.php index cfc1480feb663..216e644b767a0 100644 --- a/app/code/Magento/Quote/Model/QueryResolver.php +++ b/app/code/Magento/Quote/Model/QueryResolver.php @@ -5,8 +5,10 @@ */ namespace Magento\Quote\Model; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\CacheInterface; use Magento\Framework\App\ResourceConnection\ConfigInterface; +use Magento\Framework\Serialize\SerializerInterface; class QueryResolver { @@ -36,20 +38,27 @@ class QueryResolver * @var array */ private $cacheTags = []; + /** + * @var SerializerInterface + */ + private $serializer; /** * @param ConfigInterface $config * @param CacheInterface $cache * @param string $cacheId + * @param SerializerInterface $serializer */ public function __construct( ConfigInterface $config, CacheInterface $cache, - $cacheId = 'connection_config_cache' + $cacheId = 'connection_config_cache', + SerializerInterface $serializer = null ) { $this->config = $config; $this->cache = $cache; $this->cacheId = $cacheId; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -75,9 +84,9 @@ protected function initData() if (false === $data) { $singleQuery = $this->config->getConnectionName('checkout_setup') == 'default' ? true : false; $data['checkout'] = $singleQuery; - $this->cache->save(serialize($data), $this->cacheId, $this->cacheTags); + $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags); } else { - $data = unserialize($data); + $data = $this->serializer->unserialize($data); } $this->merge($data); } diff --git a/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php b/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php index 8e62d1b942f1e..0b3dbc318b23c 100644 --- a/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php +++ b/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php @@ -4,6 +4,7 @@ * See COPYING.txt for license details. */ namespace Magento\Quote\Model\Quote\Address\Total; +use Magento\Framework\Serialize\SerializerInterface; /** * Address Total Collector model @@ -69,6 +70,7 @@ class Collector extends \Magento\Sales\Model\Config\Ordered * @param \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory * @param mixed $sourceData * @param mixed $store + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\App\Cache\Type\Config $configCacheType, @@ -78,11 +80,12 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory, $sourceData = null, - $store = null + $store = null, + SerializerInterface $serializer = null ) { $this->_scopeConfig = $scopeConfig; $this->_totalFactory = $totalFactory; - parent::__construct($configCacheType, $logger, $salesConfig, $sourceData); + parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer); $this->_store = $store ?: $storeManager->getStore(); $this->_initModels()->_initCollectors()->_initRetrievers(); } diff --git a/app/code/Magento/Quote/Test/Unit/Model/QueryResolverTest.php b/app/code/Magento/Quote/Test/Unit/Model/QueryResolverTest.php index a2075b1161fc2..eb6828afff4ed 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/QueryResolverTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/QueryResolverTest.php @@ -6,83 +6,100 @@ namespace Magento\Quote\Test\Unit\Model; +use Magento\Framework\Serialize\SerializerInterface; + class QueryResolverTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Quote\Model\QueryResolver */ - protected $quoteResolver; + private $quoteResolver; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $configMock; + private $configMock; /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $cacheMock; + private $cacheMock; + + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializer; protected function setUp() { $this->configMock = $this->getMock(\Magento\Framework\App\ResourceConnection\ConfigInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->serializer = $this->getMockForAbstractClass(SerializerInterface::class); $this->quoteResolver = new \Magento\Quote\Model\QueryResolver( $this->configMock, $this->cacheMock, - 'connection_config_cache' + 'connection_config_cache', + $this->serializer ); - } public function testIsSingleQueryWhenDataWereCached() { - $queryData['checkout'] = true; + $serializedData = '{"checkout":true}'; + $data = ['checkout' => true]; $this->cacheMock ->expects($this->once()) ->method('load') ->with('connection_config_cache') - ->willReturn(serialize($queryData)); + ->willReturn($serializedData); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedData) + ->willReturn($data); $this->assertTrue($this->quoteResolver->isSingleQuery()); } - public function testIsSingleQueryWhenDataNotCached() + /** + * @param string $connectionName + * @param bool $isSingleQuery + * + * @dataProvider isSingleQueryWhenDataNotCachedDataProvider + */ + public function testIsSingleQueryWhenDataNotCached($connectionName, $isSingleQuery) { - $queryData['checkout'] = true; + $data = ['checkout' => $isSingleQuery]; + $serializedData = '{"checkout":true}'; $this->cacheMock ->expects($this->once()) ->method('load') ->with('connection_config_cache') ->willReturn(false); + $this->serializer->expects($this->never()) + ->method('unserialize'); $this->configMock ->expects($this->once()) ->method('getConnectionName') ->with('checkout_setup') - ->willReturn('default'); + ->willReturn($connectionName); + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($data) + ->willReturn($serializedData); $this->cacheMock ->expects($this->once()) ->method('save') - ->with(serialize($queryData), 'connection_config_cache', []); - $this->assertTrue($this->quoteResolver->isSingleQuery()); + ->with($serializedData, 'connection_config_cache', []); + $this->assertEquals($isSingleQuery, $this->quoteResolver->isSingleQuery()); } - public function testIsSingleQueryWhenSeveralConnectionsExist() + /** + * @return array + */ + public function isSingleQueryWhenDataNotCachedDataProvider() { - $queryData['checkout'] = false; - $this->cacheMock - ->expects($this->once()) - ->method('load') - ->with('connection_config_cache') - ->willReturn(false); - $this->configMock - ->expects($this->once()) - ->method('getConnectionName') - ->with('checkout_setup') - ->willReturn('checkout'); - $this->cacheMock - ->expects($this->once()) - ->method('save') - ->with(serialize($queryData), 'connection_config_cache', []); - $this->assertFalse($this->quoteResolver->isSingleQuery()); + return [ + ['default', true], + ['checkout', false], + ]; } } diff --git a/app/code/Magento/Sales/Model/Config/Ordered.php b/app/code/Magento/Sales/Model/Config/Ordered.php index 7ea7d1f8cc5fa..c59bd097bf552 100644 --- a/app/code/Magento/Sales/Model/Config/Ordered.php +++ b/app/code/Magento/Sales/Model/Config/Ordered.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ namespace Magento\Sales\Model\Config; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; /** * Configuration class for ordered items @@ -68,23 +70,30 @@ abstract class Ordered extends \Magento\Framework\App\Config\Base * @var \Magento\Sales\Model\Config */ protected $_salesConfig; + /** + * @var SerializerInterface + */ + private $serializer; /** * @param \Magento\Framework\App\Cache\Type\Config $configCacheType * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Sales\Model\Config $salesConfig * @param \Magento\Framework\Simplexml\Element $sourceData + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\App\Cache\Type\Config $configCacheType, \Psr\Log\LoggerInterface $logger, \Magento\Sales\Model\Config $salesConfig, - $sourceData = null + $sourceData = null, + SerializerInterface $serializer = null ) { parent::__construct($sourceData); $this->_configCacheType = $configCacheType; $this->_logger = $logger; $this->_salesConfig = $salesConfig; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -179,11 +188,11 @@ protected function _initCollectors() $sortedCodes = []; $cachedData = $this->_configCacheType->load($this->_collectorsCacheKey); if ($cachedData) { - $sortedCodes = unserialize($cachedData); + $sortedCodes = $this->serializer->unserialize($cachedData); } if (!$sortedCodes) { $sortedCodes = $this->_getSortedCollectorCodes($this->_modelsConfig); - $this->_configCacheType->save(serialize($sortedCodes), $this->_collectorsCacheKey); + $this->_configCacheType->save($this->serializer->serialize($sortedCodes), $this->_collectorsCacheKey); } foreach ($sortedCodes as $code) { $this->_collectors[$code] = $this->_models[$code]; diff --git a/app/code/Magento/Sales/Model/Order/Total/Config/Base.php b/app/code/Magento/Sales/Model/Order/Total/Config/Base.php index 22c71f48b6f35..20647e34c4535 100644 --- a/app/code/Magento/Sales/Model/Order/Total/Config/Base.php +++ b/app/code/Magento/Sales/Model/Order/Total/Config/Base.php @@ -4,6 +4,7 @@ * See COPYING.txt for license details. */ namespace Magento\Sales\Model\Order\Total\Config; +use Magento\Framework\Serialize\SerializerInterface; /** * Configuration class for totals @@ -42,15 +43,17 @@ class Base extends \Magento\Sales\Model\Config\Ordered * @param \Magento\Sales\Model\Config $salesConfig * @param \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory * @param mixed $sourceData + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\App\Cache\Type\Config $configCacheType, \Psr\Log\LoggerInterface $logger, \Magento\Sales\Model\Config $salesConfig, \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory, - $sourceData = null + $sourceData = null, + SerializerInterface $serializer = null ) { - parent::__construct($configCacheType, $logger, $salesConfig, $sourceData); + parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer); $this->_orderTotalFactory = $orderTotalFactory; } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Total/Config/BaseTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Total/Config/BaseTest.php index ed44331f577d0..bd519e76585ba 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Total/Config/BaseTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Total/Config/BaseTest.php @@ -5,24 +5,28 @@ */ namespace Magento\Sales\Test\Unit\Model\Order\Total\Config; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class BaseTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\Sales\Model\Order\Total\Config\Base */ - protected $object; + private $object; + + /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializer; /** @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $configCacheType; + private $configCacheType; /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $logger; + private $logger; /** @var \Magento\Sales\Model\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $salesConfig; + private $salesConfig; /** @var \Magento\Sales\Model\Order\TotalFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $orderTotalFactory; + private $orderTotalFactory; protected function setUp() { @@ -30,6 +34,7 @@ protected function setUp() $this->logger = $this->getMock(\Psr\Log\LoggerInterface::class); $this->salesConfig = $this->getMock(\Magento\Sales\Model\Config::class, [], [], '', false); $this->orderTotalFactory = $this->getMock(\Magento\Sales\Model\Order\TotalFactory::class, [], [], '', false); + $this->serializer = $this->getMockForAbstractClass(SerializerInterface::class); $objectManager = new ObjectManager($this); $this->object = $objectManager->getObject( @@ -39,6 +44,7 @@ protected function setUp() 'logger' => $this->logger, 'salesConfig' => $this->salesConfig, 'orderTotalFactory' => $this->orderTotalFactory, + 'serializer' => $this->serializer, ] ); } @@ -59,8 +65,14 @@ public function testGetTotalModels() ->with(\Magento\Sales\Model\Order\Total\AbstractTotal::class) ->will($this->returnValue($total)); + $sortedCodes = ['other_code', 'some_code']; + $serializedCodes = '["other_code", "some_code"]'; + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($sortedCodes) + ->willReturn($serializedCodes); $this->configCacheType->expects($this->once())->method('save') - ->with('a:2:{i:0;s:10:"other_code";i:1;s:9:"some_code";}', 'sorted_collectors'); + ->with($serializedCodes, 'sorted_collectors'); $this->assertSame( ['other_code' => $total, 'some_code' => $total], @@ -106,8 +118,14 @@ public function testGetTotalUnserializeCachedCollectorCodes() ->with(\Magento\Sales\Model\Order\Total\AbstractTotal::class) ->will($this->returnValue($total)); + $sortedCodes = ['other_code', 'some_code']; + $serializedCodes = '["other_code", "some_code"]'; $this->configCacheType->expects($this->once())->method('load')->with('sorted_collectors') - ->will($this->returnValue('a:2:{i:0;s:10:"other_code";i:1;s:9:"some_code";}')); + ->will($this->returnValue($serializedCodes)); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedCodes) + ->willReturn($sortedCodes); $this->configCacheType->expects($this->never())->method('save'); $this->assertSame( From 912bbc053f69c7e8112769841d102c3531b35739 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Wed, 19 Oct 2016 15:19:50 -0500 Subject: [PATCH 12/33] MAGETWO-58691: Refactor Module_Quote, Module_Sales - fixed child class and code style --- app/code/Magento/Sales/Model/Config/Ordered.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Sales/Model/Config/Ordered.php b/app/code/Magento/Sales/Model/Config/Ordered.php index c59bd097bf552..01e70b4845c45 100644 --- a/app/code/Magento/Sales/Model/Config/Ordered.php +++ b/app/code/Magento/Sales/Model/Config/Ordered.php @@ -70,6 +70,7 @@ abstract class Ordered extends \Magento\Framework\App\Config\Base * @var \Magento\Sales\Model\Config */ protected $_salesConfig; + /** * @var SerializerInterface */ From ae76c0c8fa61d247decd32c2feea7718c63d7f3e Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Wed, 19 Oct 2016 15:52:33 -0500 Subject: [PATCH 13/33] MAGETWO-58691: Refactor Module_Quote, Module_Sales - fixed code style --- app/code/Magento/Quote/Model/QueryResolver.php | 1 + app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php | 1 + app/code/Magento/Sales/Model/Config/Ordered.php | 1 + app/code/Magento/Sales/Model/Order/Total/Config/Base.php | 1 + 4 files changed, 4 insertions(+) diff --git a/app/code/Magento/Quote/Model/QueryResolver.php b/app/code/Magento/Quote/Model/QueryResolver.php index 216e644b767a0..04d83bab85b2b 100644 --- a/app/code/Magento/Quote/Model/QueryResolver.php +++ b/app/code/Magento/Quote/Model/QueryResolver.php @@ -38,6 +38,7 @@ class QueryResolver * @var array */ private $cacheTags = []; + /** * @var SerializerInterface */ diff --git a/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php b/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php index 0b3dbc318b23c..d430bf3acc6cd 100644 --- a/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php +++ b/app/code/Magento/Quote/Model/Quote/Address/Total/Collector.php @@ -4,6 +4,7 @@ * See COPYING.txt for license details. */ namespace Magento\Quote\Model\Quote\Address\Total; + use Magento\Framework\Serialize\SerializerInterface; /** diff --git a/app/code/Magento/Sales/Model/Config/Ordered.php b/app/code/Magento/Sales/Model/Config/Ordered.php index 01e70b4845c45..806a7b522c189 100644 --- a/app/code/Magento/Sales/Model/Config/Ordered.php +++ b/app/code/Magento/Sales/Model/Config/Ordered.php @@ -4,6 +4,7 @@ * See COPYING.txt for license details. */ namespace Magento\Sales\Model\Config; + use Magento\Framework\App\ObjectManager; use Magento\Framework\Serialize\SerializerInterface; diff --git a/app/code/Magento/Sales/Model/Order/Total/Config/Base.php b/app/code/Magento/Sales/Model/Order/Total/Config/Base.php index 20647e34c4535..d96591118b822 100644 --- a/app/code/Magento/Sales/Model/Order/Total/Config/Base.php +++ b/app/code/Magento/Sales/Model/Order/Total/Config/Base.php @@ -4,6 +4,7 @@ * See COPYING.txt for license details. */ namespace Magento\Sales\Model\Order\Total\Config; + use Magento\Framework\Serialize\SerializerInterface; /** From 3894d3100863fd237d306b84a7ed49fc27d2d88c Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Wed, 19 Oct 2016 17:20:45 -0500 Subject: [PATCH 14/33] MAGETWO-58691: Refactor Module_Quote, Module_Sales - simplified regexp for the static test --- .../Magento/Test/Legacy/UnsecureFunctionsUsageTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php index 5220eb43644fd..e5263713d71d7 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php @@ -159,10 +159,6 @@ private function prepareRegexp(array $functions) if (empty($functions)) { return ''; } - $regexArray = []; - foreach ($functions as $function) { - $regexArray[] = '(? Date: Thu, 20 Oct 2016 16:16:25 -0500 Subject: [PATCH 15/33] MAGETWO-58693: Refactor Module_Integration, Module_MarketPlace - replaced serialization with SerializeInterface --- app/code/Magento/Integration/Model/Config.php | 24 ++++- .../Integration/Model/ConsolidatedConfig.php | 20 +++- .../Integration/Model/IntegrationConfig.php | 20 +++- .../Unit/Model/ConsolidatedConfigTest.php | 37 +++++-- .../Test/Unit/Model/IntegrationConfigTest.php | 38 +++++-- app/code/Magento/Marketplace/Helper/Cache.php | 17 +++- .../Test/Unit/Helper/CacheTest.php | 98 ++++++++++--------- 7 files changed, 176 insertions(+), 78 deletions(-) diff --git a/app/code/Magento/Integration/Model/Config.php b/app/code/Magento/Integration/Model/Config.php index 70795cae00350..3cea4d3374319 100644 --- a/app/code/Magento/Integration/Model/Config.php +++ b/app/code/Magento/Integration/Model/Config.php @@ -5,6 +5,8 @@ */ namespace Magento\Integration\Model; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Integration\Model\Cache\Type; /** @@ -34,14 +36,24 @@ class Config */ protected $_integrations; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param Cache\Type $configCacheType * @param Config\Reader $configReader + * @param SerializerInterface $serializer */ - public function __construct(Cache\Type $configCacheType, Config\Reader $configReader) - { + public function __construct( + Cache\Type $configCacheType, + Config\Reader $configReader, + SerializerInterface $serializer = null + ) { $this->_configCacheType = $configCacheType; $this->_configReader = $configReader; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -55,10 +67,14 @@ public function getIntegrations() if (null === $this->_integrations) { $integrations = $this->_configCacheType->load(self::CACHE_ID); if ($integrations && is_string($integrations)) { - $this->_integrations = unserialize($integrations); + $this->_integrations = $this->serializer->unserialize($integrations); } else { $this->_integrations = $this->_configReader->read(); - $this->_configCacheType->save(serialize($this->_integrations), self::CACHE_ID, [Type::CACHE_TAG]); + $this->_configCacheType->save( + $this->serializer->serialize($this->_integrations), + self::CACHE_ID, + [Type::CACHE_TAG] + ); } } return $this->_integrations; diff --git a/app/code/Magento/Integration/Model/ConsolidatedConfig.php b/app/code/Magento/Integration/Model/ConsolidatedConfig.php index 9027bf774bc30..9208d19e7028f 100644 --- a/app/code/Magento/Integration/Model/ConsolidatedConfig.php +++ b/app/code/Magento/Integration/Model/ConsolidatedConfig.php @@ -5,6 +5,8 @@ */ namespace Magento\Integration\Model; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Integration\Model\Cache\TypeConsolidated; /** @@ -31,14 +33,24 @@ class ConsolidatedConfig */ protected $integrations; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param Cache\TypeConsolidated $configCacheType * @param Config\Consolidated\Reader $configReader + * @param SerializerInterface $serializer */ - public function __construct(Cache\TypeConsolidated $configCacheType, Config\Consolidated\Reader $configReader) - { + public function __construct( + Cache\TypeConsolidated $configCacheType, + Config\Consolidated\Reader $configReader, + SerializerInterface $serializer = null + ) { $this->configCacheType = $configCacheType; $this->configReader = $configReader; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -51,11 +63,11 @@ public function getIntegrations() if (null === $this->integrations) { $integrations = $this->configCacheType->load(self::CACHE_ID); if ($integrations && is_string($integrations)) { - $this->integrations = unserialize($integrations); + $this->integrations = $this->serializer->unserialize($integrations); } else { $this->integrations = $this->configReader->read(); $this->configCacheType->save( - serialize($this->integrations), + $this->serializer->serialize($this->integrations), self::CACHE_ID, [TypeConsolidated::CACHE_TAG] ); diff --git a/app/code/Magento/Integration/Model/IntegrationConfig.php b/app/code/Magento/Integration/Model/IntegrationConfig.php index 647bff70efe4a..cde4fc20d2235 100644 --- a/app/code/Magento/Integration/Model/IntegrationConfig.php +++ b/app/code/Magento/Integration/Model/IntegrationConfig.php @@ -6,6 +6,8 @@ namespace Magento\Integration\Model; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Integration\Model\Cache\TypeIntegration; use Magento\Integration\Model\Config\Integration\Reader; @@ -36,14 +38,24 @@ class IntegrationConfig */ protected $_integrations; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param TypeIntegration $configCacheType * @param Reader $configReader + * @param SerializerInterface $serializer */ - public function __construct(TypeIntegration $configCacheType, Reader $configReader) - { + public function __construct( + TypeIntegration $configCacheType, + Reader $configReader, + SerializerInterface $serializer = null + ) { $this->_configCacheType = $configCacheType; $this->_configReader = $configReader; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -57,11 +69,11 @@ public function getIntegrations() if (null === $this->_integrations) { $integrations = $this->_configCacheType->load(self::CACHE_ID); if ($integrations && is_string($integrations)) { - $this->_integrations = unserialize($integrations); + $this->_integrations = $this->serializer->unserialize($integrations); } else { $this->_integrations = $this->_configReader->read(); $this->_configCacheType->save( - serialize($this->_integrations), + $this->serializer->serialize($this->_integrations), self::CACHE_ID, [TypeIntegration::CACHE_TAG] ); diff --git a/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php b/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php index 0b75592782773..22b50a8aef8d0 100644 --- a/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php +++ b/app/code/Magento/Integration/Test/Unit/Model/ConsolidatedConfigTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Integration\Test\Unit\Model; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Integration\Model\ConsolidatedConfig as Config; use Magento\Integration\Model\Cache\TypeConsolidated as Type; @@ -18,17 +19,22 @@ class ConsolidatedConfigTest extends \PHPUnit_Framework_TestCase * * @var Config */ - protected $configModel; + private $configModel; /** * @var Type|\PHPUnit_Framework_MockObject_MockObject */ - protected $configCacheTypeMock; + private $configCacheTypeMock; /** * @var \Magento\Integration\Model\Config\Consolidated\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $configReaderMock; + private $configReaderMock; + + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializer; protected function setUp() { @@ -38,12 +44,16 @@ protected function setUp() $this->configReaderMock = $this->getMockBuilder(\Magento\Integration\Model\Config\Consolidated\Reader::class) ->disableOriginalConstructor() ->getMock(); + $this->serializer = $this->getMockBuilder(SerializerInterface::class) + ->disableOriginalConstructor() + ->getMock(); $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->configModel = $objectManagerHelper->getObject( \Magento\Integration\Model\ConsolidatedConfig::class, [ 'configCacheType' => $this->configCacheTypeMock, - 'configReader' => $this->configReaderMock + 'configReader' => $this->configReaderMock, + 'serializer' => $this->serializer, ] ); } @@ -51,10 +61,15 @@ protected function setUp() public function testGetIntegrationsFromConfigCacheType() { $integrations = ['foo', 'bar', 'baz']; + $serializedIntegrations = '["foo","bar","baz"]'; $this->configCacheTypeMock->expects($this->once()) ->method('load') ->with(Config::CACHE_ID) - ->will($this->returnValue(serialize($integrations))); + ->will($this->returnValue($serializedIntegrations)); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedIntegrations) + ->willReturn($integrations); $this->assertEquals($integrations, $this->configModel->getIntegrations()); } @@ -62,17 +77,21 @@ public function testGetIntegrationsFromConfigCacheType() public function testGetIntegrationsFromConfigReader() { $integrations = ['foo', 'bar', 'baz']; + $serializedIntegrations = '["foo","bar","baz"]'; $this->configCacheTypeMock->expects($this->once()) ->method('load') ->with(Config::CACHE_ID) ->will($this->returnValue(null)); - $this->configCacheTypeMock->expects($this->once()) - ->method('save') - ->with(serialize($integrations), Config::CACHE_ID, [Type::CACHE_TAG]) - ->will($this->returnValue(null)); $this->configReaderMock->expects($this->once()) ->method('read') ->will($this->returnValue($integrations)); + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($integrations) + ->willReturn($serializedIntegrations); + $this->configCacheTypeMock->expects($this->once()) + ->method('save') + ->with($serializedIntegrations, Config::CACHE_ID, [Type::CACHE_TAG]); $this->assertEquals($integrations, $this->configModel->getIntegrations()); } diff --git a/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php b/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php index aed4e02453dc4..14871420a621a 100644 --- a/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php +++ b/app/code/Magento/Integration/Test/Unit/Model/IntegrationConfigTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Integration\Test\Unit\Model; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Integration\Model\IntegrationConfig; use Magento\Integration\Model\Cache\TypeIntegration; @@ -16,17 +17,22 @@ class IntegrationConfigTest extends \PHPUnit_Framework_TestCase /** * @var IntegrationConfig */ - protected $integrationConfigModel; + private $integrationConfigModel; /** * @var TypeIntegration|\PHPUnit_Framework_MockObject_MockObject */ - protected $configCacheTypeMock; + private $configCacheTypeMock; /** * @var \Magento\Integration\Model\Config\Integration\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $configReaderMock; + private $configReaderMock; + + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializer; protected function setUp() { @@ -36,19 +42,28 @@ protected function setUp() $this->configReaderMock = $this->getMockBuilder(\Magento\Integration\Model\Config\Integration\Reader::class) ->disableOriginalConstructor() ->getMock(); + $this->serializer = $this->getMockBuilder(SerializerInterface::class) + ->disableOriginalConstructor() + ->getMock(); $this->integrationConfigModel = new IntegrationConfig( $this->configCacheTypeMock, - $this->configReaderMock + $this->configReaderMock, + $this->serializer ); } public function testGetIntegrationsFromConfigCacheType() { $integrations = ['foo', 'bar', 'baz']; + $serializedIntegrations = '["foo","bar","baz"]'; $this->configCacheTypeMock->expects($this->once()) ->method('load') ->with(IntegrationConfig::CACHE_ID) - ->will($this->returnValue(serialize($integrations))); + ->will($this->returnValue($serializedIntegrations)); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedIntegrations) + ->willReturn($integrations); $this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations()); } @@ -56,17 +71,22 @@ public function testGetIntegrationsFromConfigCacheType() public function testGetIntegrationsFromConfigReader() { $integrations = ['foo', 'bar', 'baz']; + $serializedIntegrations = '["foo","bar","baz"]'; $this->configCacheTypeMock->expects($this->once()) ->method('load') ->with(IntegrationConfig::CACHE_ID) ->will($this->returnValue(null)); - $this->configCacheTypeMock->expects($this->once()) - ->method('save') - ->with(serialize($integrations), IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG]) - ->will($this->returnValue(null)); $this->configReaderMock->expects($this->once()) ->method('read') ->will($this->returnValue($integrations)); + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($integrations) + ->willReturn($serializedIntegrations); + $this->configCacheTypeMock->expects($this->once()) + ->method('save') + ->with($serializedIntegrations, IntegrationConfig::CACHE_ID, [TypeIntegration::CACHE_TAG]) + ->will($this->returnValue(null)); $this->assertEquals($integrations, $this->integrationConfigModel->getIntegrations()); } diff --git a/app/code/Magento/Marketplace/Helper/Cache.php b/app/code/Magento/Marketplace/Helper/Cache.php index 1cb5fb9c710e6..a0a4ce73e0373 100644 --- a/app/code/Magento/Marketplace/Helper/Cache.php +++ b/app/code/Magento/Marketplace/Helper/Cache.php @@ -6,7 +6,8 @@ namespace Magento\Marketplace\Helper; -use Magento\Framework\Filesystem; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; /** * Cache helper @@ -25,15 +26,23 @@ class Cache extends \Magento\Framework\App\Helper\AbstractHelper */ protected $cache; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\Config\CacheInterface $cache + * @param SerializerInterface $serializer */ public function __construct( \Magento\Framework\App\Helper\Context $context, - \Magento\Framework\Config\CacheInterface $cache + \Magento\Framework\Config\CacheInterface $cache, + SerializerInterface $serializer = null ) { $this->cache = $cache; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); parent::__construct($context); } @@ -46,7 +55,7 @@ public function loadPartnersFromCache() { $data = $this->getCache()->load($this->pathToCacheFile); if (false !== $data) { - $data = unserialize($data); + $data = $this->serializer->unserialize($data); } return $data; } @@ -59,7 +68,7 @@ public function loadPartnersFromCache() */ public function savePartnersToCache($partners) { - return $this->getCache()->save(serialize($partners), $this->pathToCacheFile); + return $this->getCache()->save($this->serializer->serialize($partners), $this->pathToCacheFile); } /** diff --git a/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php b/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php index 75c6e6110389c..411c81d75479c 100644 --- a/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php +++ b/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php @@ -6,70 +6,80 @@ namespace Magento\Marketplace\Test\Unit\Helper; +use Magento\Framework\Serialize\SerializerInterface; + class CacheTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Marketplace\Helper\Cache + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $cache; + + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializer; + + /** + * @var \Magento\Marketplace\Helper\Cache */ - private $cacheHelperMock; + private $cacheHelper; protected function setUp() { - $this->cacheHelperMock = $this->getCacheHelperMock(['getCache']); + $this->cache = $this->getMockForAbstractClass(\Magento\Framework\Config\CacheInterface::class); + $this->serializer = $this->getMockForAbstractClass(SerializerInterface::class); + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->cacheHelper = $objectManagerHelper->getObject( + \Magento\Marketplace\Helper\Cache::class, + [ + 'cache' => $this->cache, + 'serializer' => $this->serializer, + ] + ); } - /** - * @covers \Magento\Marketplace\Helper\Cache::loadPartnersFromCache - */ public function testLoadPartnersFromCache() { - $cache = $this->getCacheMock(); - $this->cacheHelperMock - ->expects($this->once()) - ->method('getCache') - ->will($this->returnValue($cache)); - $cache->expects($this->once()) + $partners = ['partner1', 'partner2']; + $serializedPartners = '["partner1", "partner2"]'; + $this->cache->expects($this->once()) ->method('load') - ->will($this->returnValue('')); + ->with('partners') + ->willReturn($serializedPartners); + $this->serializer->expects($this->once()) + ->method('unserialize') + ->with($serializedPartners) + ->willReturn($partners); - $this->cacheHelperMock->loadPartnersFromCache(); + + $this->assertSame($partners, $this->cacheHelper->loadPartnersFromCache()); } - /** - * @covers \Magento\Marketplace\Helper\Cache::savePartnersToCache - */ - public function testSavePartnersToCache() + public function testLoadPartnersFromCacheNoCachedData() { - $cache = $this->getCacheMock(); - $this->cacheHelperMock - ->expects($this->once()) - ->method('getCache') - ->will($this->returnValue($cache)); - $cache->expects($this->once()) - ->method('save') - ->will($this->returnValue(true)); + $this->cache->expects($this->once()) + ->method('load') + ->with('partners') + ->willReturn(false); + $this->serializer->expects($this->never()) + ->method('unserialize'); - $this->cacheHelperMock->savePartnersToCache([]); + $this->assertSame(false, $this->cacheHelper->loadPartnersFromCache()); } - /** - * Gets cache helper mock - * - * @param null $methods - * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Marketplace\Helper\Cache - */ - public function getCacheHelperMock($methods = null) + public function testSavePartnersToCache() { - return $this->getMock(\Magento\Marketplace\Helper\Cache::class, $methods, [], '', false); - } + $partners = ['partner1', 'partner2']; + $serializedPartners = '["partner1", "partner2"]'; + $this->serializer->expects($this->once()) + ->method('serialize') + ->with($partners) + ->willReturn($serializedPartners); + $this->cache->expects($this->once()) + ->method('save') + ->with($serializedPartners); - /** - * Gets Filesystem mock - * - * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Config\CacheInterface - */ - public function getCacheMock() - { - return $this->getMockForAbstractClass(\Magento\Framework\Config\CacheInterface::class); + $this->cacheHelper->savePartnersToCache($partners); } } From c84101b3287497e696e48a15b885328d5d61af73 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Thu, 20 Oct 2016 20:09:57 -0500 Subject: [PATCH 16/33] MAGETWO-58693: Refactor Module_Integration, Module_MarketPlace - fixed code style --- app/code/Magento/Integration/Model/Config.php | 2 +- app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/Magento/Integration/Model/Config.php b/app/code/Magento/Integration/Model/Config.php index 3cea4d3374319..37ed0e7b2fb61 100644 --- a/app/code/Magento/Integration/Model/Config.php +++ b/app/code/Magento/Integration/Model/Config.php @@ -15,7 +15,7 @@ * This is a parent class for storing information about Integrations. * @deprecated */ -class Config +class Config extends ConsolidatedConfig { const CACHE_ID = 'integration'; diff --git a/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php b/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php index 411c81d75479c..00b78a47eb4b0 100644 --- a/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php +++ b/app/code/Magento/Marketplace/Test/Unit/Helper/CacheTest.php @@ -52,7 +52,6 @@ public function testLoadPartnersFromCache() ->with($serializedPartners) ->willReturn($partners); - $this->assertSame($partners, $this->cacheHelper->loadPartnersFromCache()); } From 8272e97e0c907d007c9737234b660ea1694e3438 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Thu, 20 Oct 2016 20:21:31 -0500 Subject: [PATCH 17/33] MAGETWO-58693: Refactor Module_Integration, Module_MarketPlace - blacklisted deprecated classes --- .../Magento/Test/Php/_files/phpcpd/blacklist/common.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt index 56e0d51516c26..19b152df88fdf 100644 --- a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt +++ b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt @@ -196,4 +196,6 @@ Magento/Framework/Mview/Config/Data Magento/Framework/View/File/Collector/Override Magento/Framework/MessageQueue/Consumer/Config/ConsumerConfigItem Magento/Framework/MessageQueue/Publisher/Config/PublisherConfigItem -Magento/Framework/MessageQueue/Topology/Config/ExchangeConfigItem \ No newline at end of file +Magento/Framework/MessageQueue/Topology/Config/ExchangeConfigItem +Magento/Integration/Model/Config +Magento/Integration/Model/IntegrationConfig From 09f3c9d2629b0b573b6fb03c4e9871d1ba998c5b Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Fri, 21 Oct 2016 10:50:03 -0500 Subject: [PATCH 18/33] MAGETWO-58693: Refactor Module_Integration, Module_MarketPlace - improved Copy-Paste static test to allow file names in the blacklist --- .../CodingStandard/Tool/CopyPasteDetector.php | 32 +++++++++++++------ .../Php/_files/phpcpd/blacklist/common.txt | 4 +-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CopyPasteDetector.php b/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CopyPasteDetector.php index b2d9c164fbd1b..b2766ebd90d92 100644 --- a/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CopyPasteDetector.php +++ b/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CopyPasteDetector.php @@ -56,8 +56,7 @@ public function setBlackList(array $blackList) */ public function canRun() { - $vendorDir = require BP . '/app/etc/vendor_path.php'; - exec('php ' . BP . '/' . $vendorDir . '/bin/phpcpd --version', $output, $exitCode); + exec($this->getCommand() . ' --version', $output, $exitCode); return $exitCode === 0; } @@ -71,22 +70,37 @@ public function canRun() */ public function run(array $whiteList) { - $blackListStr = ' '; + $blacklistedDirs = []; + $blacklistedFileNames = []; foreach ($this->blacklist as $file) { $file = escapeshellarg(trim($file)); if (!$file) { continue; } - $blackListStr .= '--exclude ' . $file . ' '; + $ext = pathinfo($file, PATHINFO_EXTENSION); + if ($ext != '') { + $blacklistedFileNames[] = $file; + } else { + $blacklistedDirs[] = '--exclude ' . $file . ' '; + } } - $vendorDir = require BP . '/app/etc/vendor_path.php'; - $command = 'php ' . BP . '/' . $vendorDir . '/bin/phpcpd' . ' --log-pmd ' . escapeshellarg( - $this->reportFile - ) . ' --names-exclude "*Test.php" --min-lines 13' . $blackListStr . ' ' . implode(' ', $whiteList); - + $command = $this->getCommand() . ' --log-pmd ' . escapeshellarg($this->reportFile) + . ' --names-exclude ' . join(',', $blacklistedFileNames) . ' --min-lines 13 ' . join(' ', $blacklistedDirs) + . ' ' . implode(' ', $whiteList); exec($command, $output, $exitCode); return !(bool)$exitCode; } + + /** + * Get PHPCPD command + * + * @return string + */ + private function getCommand() + { + $vendorDir = require BP . '/app/etc/vendor_path.php'; + return 'php ' . BP . '/' . $vendorDir . '/bin/phpcpd'; + } } diff --git a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt index 19b152df88fdf..3afe3af79b14f 100644 --- a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt +++ b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt @@ -197,5 +197,5 @@ Magento/Framework/View/File/Collector/Override Magento/Framework/MessageQueue/Consumer/Config/ConsumerConfigItem Magento/Framework/MessageQueue/Publisher/Config/PublisherConfigItem Magento/Framework/MessageQueue/Topology/Config/ExchangeConfigItem -Magento/Integration/Model/Config -Magento/Integration/Model/IntegrationConfig +IntegrationConfig.php +*Test.php From 9ba6fa8f6fa5c5580b56a4a82ba23930fc5de710 Mon Sep 17 00:00:00 2001 From: Olga Kopylova Date: Fri, 21 Oct 2016 15:54:22 -0500 Subject: [PATCH 19/33] MAGETWO-58693: Refactor Module_Integration, Module_MarketPlace - removed unnecessary inheritance --- app/code/Magento/Integration/Model/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Integration/Model/Config.php b/app/code/Magento/Integration/Model/Config.php index 37ed0e7b2fb61..3cea4d3374319 100644 --- a/app/code/Magento/Integration/Model/Config.php +++ b/app/code/Magento/Integration/Model/Config.php @@ -15,7 +15,7 @@ * This is a parent class for storing information about Integrations. * @deprecated */ -class Config extends ConsolidatedConfig +class Config { const CACHE_ID = 'integration'; From fa6501cf667a30d200d2e6b7c4b7a76ba81a1e51 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 24 Oct 2016 17:52:43 -0500 Subject: [PATCH 20/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Introducing SerializerInterface --- app/code/Magento/Webapi/Model/Config.php | 19 +- .../Magento/Webapi/Model/ServiceMetadata.php | 39 +- .../Webapi/Test/Unit/Model/ConfigTest.php | 96 +++ .../Test/Unit/Model/ServiceMetadataTest.php | 591 +++++++++++------- .../Webapi/Model/ServiceMetadataTest.php | 250 ++++++++ 5 files changed, 769 insertions(+), 226 deletions(-) create mode 100644 app/code/Magento/Webapi/Test/Unit/Model/ConfigTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php diff --git a/app/code/Magento/Webapi/Model/Config.php b/app/code/Magento/Webapi/Model/Config.php index 45d29bc59cf0d..53bfda46eb495 100644 --- a/app/code/Magento/Webapi/Model/Config.php +++ b/app/code/Magento/Webapi/Model/Config.php @@ -8,6 +8,8 @@ use Magento\Webapi\Model\Cache\Type\Webapi as WebapiCache; use Magento\Webapi\Model\Config\Reader; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; /** * Web API Config Model. @@ -40,16 +42,25 @@ class Config */ protected $services; + /** + * @var SerializerInterface + */ + private $serializer; + /** * Initialize dependencies. * * @param WebapiCache $cache * @param Reader $configReader */ - public function __construct(WebapiCache $cache, Reader $configReader) - { + public function __construct( + WebapiCache $cache, + Reader $configReader, + SerializerInterface $serializer = null + ) { $this->cache = $cache; $this->configReader = $configReader; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -62,10 +73,10 @@ public function getServices() if (null === $this->services) { $services = $this->cache->load(self::CACHE_ID); if ($services && is_string($services)) { - $this->services = unserialize($services); + $this->services = $this->serializer->unserialize($services); } else { $this->services = $this->configReader->read(); - $this->cache->save(serialize($this->services), self::CACHE_ID); + $this->cache->save($this->serializer->serialize($this->services), self::CACHE_ID); } } return $this->services; diff --git a/app/code/Magento/Webapi/Model/ServiceMetadata.php b/app/code/Magento/Webapi/Model/ServiceMetadata.php index b75d10f9a271f..1b1eea3442a00 100644 --- a/app/code/Magento/Webapi/Model/ServiceMetadata.php +++ b/app/code/Magento/Webapi/Model/ServiceMetadata.php @@ -7,6 +7,8 @@ use Magento\Webapi\Model\Config\Converter; use Magento\Webapi\Model\Cache\Type\Webapi as WebApiCache; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; /** * Service Metadata Model @@ -74,6 +76,11 @@ class ServiceMetadata */ protected $typeProcessor; + /** + * @var SerializerInterface + */ + private $serializer; + /** * Initialize dependencies. * @@ -86,12 +93,14 @@ public function __construct( \Magento\Webapi\Model\Config $config, WebApiCache $cache, \Magento\Webapi\Model\Config\ClassReflector $classReflector, - \Magento\Framework\Reflection\TypeProcessor $typeProcessor + \Magento\Framework\Reflection\TypeProcessor $typeProcessor, + SerializerInterface $serializer = null ) { $this->config = $config; $this->cache = $cache; $this->classReflector = $classReflector; $this->typeProcessor = $typeProcessor; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -142,12 +151,18 @@ public function getServicesConfig() $servicesConfig = $this->cache->load(self::SERVICES_CONFIG_CACHE_ID); $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID); if ($servicesConfig && is_string($servicesConfig) && $typesData && is_string($typesData)) { - $this->services = unserialize($servicesConfig); - $this->typeProcessor->setTypesData(unserialize($typesData)); + $this->services = $this->serializer->unserialize($servicesConfig); + $this->typeProcessor->setTypesData($this->serializer->unserialize($typesData)); } else { $this->services = $this->initServicesMetadata(); - $this->cache->save(serialize($this->services), self::SERVICES_CONFIG_CACHE_ID); - $this->cache->save(serialize($this->typeProcessor->getTypesData()), self::REFLECTED_TYPES_CACHE_ID); + $this->cache->save( + $this->serializer->serialize($this->services), + self::SERVICES_CONFIG_CACHE_ID + ); + $this->cache->save( + $this->serializer->serialize($this->typeProcessor->getTypesData()), + self::REFLECTED_TYPES_CACHE_ID + ); } } return $this->services; @@ -256,12 +271,18 @@ public function getRoutesConfig() $routesConfig = $this->cache->load(self::ROUTES_CONFIG_CACHE_ID); $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID); if ($routesConfig && is_string($routesConfig) && $typesData && is_string($typesData)) { - $this->routes = unserialize($routesConfig); - $this->typeProcessor->setTypesData(unserialize($typesData)); + $this->routes = $this->serializer->unserialize($routesConfig); + $this->typeProcessor->setTypesData($this->serializer->unserialize($typesData)); } else { $this->routes = $this->initRoutesMetadata(); - $this->cache->save(serialize($this->routes), self::ROUTES_CONFIG_CACHE_ID); - $this->cache->save(serialize($this->typeProcessor->getTypesData()), self::REFLECTED_TYPES_CACHE_ID); + $this->cache->save( + $this->serializer->serialize($this->routes), + self::ROUTES_CONFIG_CACHE_ID + ); + $this->cache->save( + $this->serializer->serialize($this->typeProcessor->getTypesData()), + self::REFLECTED_TYPES_CACHE_ID + ); } } return $this->routes; diff --git a/app/code/Magento/Webapi/Test/Unit/Model/ConfigTest.php b/app/code/Magento/Webapi/Test/Unit/Model/ConfigTest.php new file mode 100644 index 0000000000000..74280f61916d7 --- /dev/null +++ b/app/code/Magento/Webapi/Test/Unit/Model/ConfigTest.php @@ -0,0 +1,96 @@ +webapiCacheMock = $this->getMock(\Magento\Webapi\Model\Cache\Type\Webapi::class, [], [], '', false); + $this->configReaderMock = $this->getMock(\Magento\Webapi\Model\Config\Reader::class, [], [], '', false); + $this->serializerMock = $this->getMock(SerializerInterface::class); + + $this->config = $objectManager->getObject( + Config::class, + [ + 'cache' => $this->webapiCacheMock, + 'configReader' => $this->configReaderMock, + 'serializer' => $this->serializerMock + ] + ); + } + + public function testGetServices() + { + $data = ['foo' => 'bar']; + $serializedData = 'serialized data'; + $this->webapiCacheMock->expects($this->once()) + ->method('load') + ->with(Config::CACHE_ID) + ->willReturn($serializedData); + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with($serializedData) + ->willReturn($data); + $this->config->getServices(); + $this->assertEquals($data, $this->config->getServices()); + } + + public function testGetServicesNoCache() + { + $data = ['foo' => 'bar']; + $serializedData = 'serialized data'; + $this->webapiCacheMock->expects($this->once()) + ->method('load') + ->with(Config::CACHE_ID) + ->willReturn(false); + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->configReaderMock->expects($this->once()) + ->method('read') + ->willReturn($data); + $this->serializerMock->expects($this->once()) + ->method('serialize') + ->with($data) + ->willReturn($serializedData); + $this->webapiCacheMock->expects($this->once()) + ->method('save') + ->with( + $serializedData, + Config::CACHE_ID + ); + + $this->config->getServices(); + $this->assertEquals($data, $this->config->getServices()); + } +} diff --git a/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php b/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php index 29c1bf90402eb..7aa99a69c109a 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php @@ -1,187 +1,444 @@ [ + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->configMock = $this->getMock(Config::class, [], [], '', false); + $this->cacheMock = $this->getMock(Webapi::class, [], [], '', false); + $this->classReflectorMock = $this->getMock(ClassReflector::class, [], [], '', false); + $this->typeProcessorMock = $this->getMock(TypeProcessor::class, [], [], '', false); + $this->serializerMock = $this->getMock(SerializerInterface::class); + + $this->serviceMetadata = $objectManager->getObject( + ServiceMetadata::class, + [ + 'config' => $this->configMock, + 'cache' => $this->cacheMock, + 'classReflector' => $this->classReflectorMock, + 'typeProcessor' => $this->typeProcessorMock, + 'serializer' => $this->serializerMock + ] + ); + } + + public function testGetServicesConfig() + { + $servicesConfig = ['foo' => 'bar']; + $typeData = ['bar' => 'foo']; + $serializedServicesConfig = 'serialized services config'; + $serializedTypeData = 'serialized type data'; + $this->cacheMock->expects($this->at(0)) + ->method('load') + ->with(ServiceMetadata::SERVICES_CONFIG_CACHE_ID) + ->willReturn($serializedServicesConfig); + $this->cacheMock->expects($this->at(1)) + ->method('load') + ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID) + ->willReturn($serializedTypeData); + $this->serializerMock->expects($this->at(0)) + ->method('unserialize') + ->with($serializedServicesConfig) + ->willReturn($servicesConfig); + $this->serializerMock->expects($this->at(1)) + ->method('unserialize') + ->with($serializedTypeData) + ->willReturn($typeData); + $this->typeProcessorMock->expects($this->once()) + ->method('setTypesData') + ->with($typeData); + $this->serviceMetadata->getServicesConfig(); + $this->assertEquals($servicesConfig, $this->serviceMetadata->getServicesConfig()); + } + + public function testGetServicesConfigNoCache() + { + $servicesConfig = [ + 'services' => [ + CustomerRepositoryInterface::class => [ + 'V1' => [ + 'methods' => [ + 'getById' => [ + 'resources' => [ + [ + 'Magento_Customer::customer', + ] + ], + 'secure' => false + ] + ] + ] + ] + ] + ]; + $methodsReflectionData = [ + 'getById' => [ + 'documentation' => 'Get customer by customer ID.', 'interface' => [ 'in' => [ 'parameters' => [ 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - 'requiredInputParameter' => [ + 'type' => 'int', 'required' => true, - ], - ], + 'documentation' => null + ] + ] ], 'out' => [ 'parameters' => [ - 'outputParameter' => [ - 'type' => 'string', + 'result' => [ + 'type' => 'CustomerDataCustomerInterface', + 'required' => true, + 'documentation' => null + ] + ] + ] + ] + ] + ]; + $servicesMetadata = [ + 'customerCustomerRepositoryV1' => [ + 'methods' => array_merge_recursive( + [ + 'getById' => [ + 'resources' => [ + [ + 'Magento_Customer::customer', + ], ], - ], + 'method' => 'getById', + 'inputRequired' => false, + 'isSecure' => false, + ] ], - ], - ], + $methodsReflectionData + ), + 'class' => CustomerRepositoryInterface::class, + 'description' => 'Customer CRUD interface.' + ] ]; - $classReflection = $this->getMock( - \Magento\Webapi\Model\Config\ClassReflector::class, - ['reflectClassMethods', 'extractClassDescription'], - [], - '', - false - ); - $classReflection->expects($this->any()) + $typeData = [ + 'CustomerDataCustomerInterface' => [ + 'documentation' => 'Customer interface.', + 'parameters' => [ + 'id' => [ + 'type' => 'int', + 'required' => false, + 'documentation' => 'Customer id' + ] + ] + ] + ]; + $serializedServicesConfig = 'serialized services config'; + $serializedTypeData = 'serialized type data'; + $this->cacheMock->expects($this->at(0)) + ->method('load') + ->with(ServiceMetadata::SERVICES_CONFIG_CACHE_ID) + ->willReturn(false); + $this->cacheMock->expects($this->at(1)) + ->method('load') + ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID) + ->willReturn(false); + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->configMock->expects($this->once()) + ->method('getServices') + ->willReturn($servicesConfig); + $this->classReflectorMock->expects($this->once()) ->method('reflectClassMethods') - ->will($this->returnValue($interfaceParameters)); - $classReflection->expects($this->any()) + ->willReturn($methodsReflectionData); + $this->classReflectorMock->expects($this->once()) ->method('extractClassDescription') - ->will($this->returnValue('classDescription')); + ->with(CustomerRepositoryInterface::class) + ->willReturn('Customer CRUD interface.'); + $this->typeProcessorMock->expects($this->once()) + ->method('getTypesData') + ->willReturn($typeData); + $this->serializerMock->expects($this->at(0)) + ->method('serialize') + ->with($servicesMetadata) + ->willReturn($serializedServicesConfig); + $this->serializerMock->expects($this->at(1)) + ->method('serialize') + ->with($typeData) + ->willReturn($serializedTypeData); + $this->cacheMock->expects($this->at(2)) + ->method('save') + ->with( + $serializedServicesConfig, + ServiceMetadata::SERVICES_CONFIG_CACHE_ID + ); + $this->cacheMock->expects($this->at(3)) + ->method('save') + ->with( + $serializedTypeData, + ServiceMetadata::REFLECTED_TYPES_CACHE_ID + ); + $this->serviceMetadata->getServicesConfig(); + $this->assertEquals($servicesMetadata, $this->serviceMetadata->getServicesConfig()); + } + + public function testGetRoutesConfig() + { + $routesConfig = ['foo' => 'bar']; + $typeData = ['bar' => 'foo']; + $serializedRoutesConfig = 'serialized routes config'; + $serializedTypeData = 'serialized type data'; + $this->cacheMock->expects($this->at(0)) + ->method('load') + ->with(ServiceMetadata::ROUTES_CONFIG_CACHE_ID) + ->willReturn($serializedRoutesConfig); + $this->cacheMock->expects($this->at(1)) + ->method('load') + ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID) + ->willReturn($serializedTypeData); + $this->serializerMock->expects($this->at(0)) + ->method('unserialize') + ->with($serializedRoutesConfig) + ->willReturn($routesConfig); + $this->serializerMock->expects($this->at(1)) + ->method('unserialize') + ->with($serializedTypeData) + ->willReturn($typeData); + $this->typeProcessorMock->expects($this->once()) + ->method('setTypesData') + ->with($typeData); + $this->serviceMetadata->getRoutesConfig(); + $this->assertEquals($routesConfig, $this->serviceMetadata->getRoutesConfig()); + } + public function testGetRoutesConfigNoCache() + { $servicesConfig = [ - 'services' => [\Magento\Customer\Api\AccountManagementInterface::class => [ - 'V1' => [ - 'methods' => [ - 'activateById' => [ - 'resources' => [ - [ - 'Magento_Customer::manage', - ], - ], - 'secure' => false, - ], - ], - ], - ], \Magento\Customer\Api\CustomerRepositoryInterface::class => [ + 'services' => [ + CustomerRepositoryInterface::class => [ 'V1' => [ 'methods' => [ 'getById' => [ 'resources' => [ [ 'Magento_Customer::customer', - ], + ] ], - 'secure' => false, - ], - ], - ], - ], + 'secure' => false + ] + ] + ] + ] ], 'routes' => [ - '/V1/customers/me/activate' => [ - 'PUT' => [ + '/V1/customers/:customerId' => [ + 'GET' => [ 'secure' => false, 'service' => [ - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'method' => 'activateById', + 'class' => CustomerRepositoryInterface::class, + 'method' => 'getById' ], 'resources' => [ - 'self' => true, + 'Magento_Customer::customer' => true ], + 'parameters' => [] + ] + ] + ], + 'class' => CustomerRepositoryInterface::class, + 'description' => 'Customer CRUD interface.', + ]; + $methodsReflectionData = [ + 'getById' => [ + 'documentation' => 'Get customer by customer ID.', + 'interface' => [ + 'in' => [ 'parameters' => [ 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - ], + 'type' => 'int', + 'required' => true, + 'documentation' => null + ] + ] ], - ], - '/V1/customers/:customerId' => [ - 'GET' => [ - 'secure' => false, - 'service' => [ - 'class' => \Magento\Customer\Api\CustomerRepositoryInterface::class, - 'method' => 'getById', - ], - 'resources' => [ - 'Magento_Customer::customer' => true, - ], + 'out' => [ 'parameters' => [ - ], + 'result' => [ + 'type' => 'CustomerDataCustomerInterface', + 'required' => true, + 'documentation' => null + ] + ] + ] + ] + ] + ]; + $routesMetadata = [ + 'customerCustomerRepositoryV1' => [ + 'methods' => array_merge_recursive( + [ + 'getById' => [ + 'resources' => [ + [ + 'Magento_Customer::customer', + ] + ], + 'method' => 'getById', + 'inputRequired' => false, + 'isSecure' => false, + ] ], + $methodsReflectionData + ), + 'routes' => [ + '/V1/customers/:customerId' => [ + 'GET' => [ + 'method' => 'getById', + 'parameters' => [] + ] + ] ], + 'class' => CustomerRepositoryInterface::class, + 'description' => 'Customer CRUD interface.' ] ]; - - /** - * @var $cacheMock \Magento\Webapi\Model\Cache\Type\Webapi - */ - $cacheMock = $this->getMockBuilder(\Magento\Webapi\Model\Cache\Type\Webapi::class) - ->disableOriginalConstructor() - ->getMock(); - - /** @var $readerMock \Magento\Webapi\Model\Config\Reader */ - $readerMock = $this->getMockBuilder(\Magento\Webapi\Model\Config\Reader::class) - ->disableOriginalConstructor() - ->getMock(); - $readerMock->expects($this->any())->method('read')->will($this->returnValue($servicesConfig)); - - /** @var $config \Magento\Webapi\Model\Config */ - $config = new \Magento\Webapi\Model\Config($cacheMock, $readerMock); - - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $typeProcessor = $objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class); - - /** @var $config \Magento\Webapi\Model\ServiceMetadata */ - $this->serviceMetadata = new \Magento\Webapi\Model\ServiceMetadata( - $config, - $cacheMock, - $classReflection, - $typeProcessor - ); - - parent::setUp(); + $typeData = [ + 'CustomerDataCustomerInterface' => [ + 'documentation' => 'Customer interface.', + 'parameters' => [ + 'id' => [ + 'type' => 'int', + 'required' => false, + 'documentation' => 'Customer id' + ] + ] + ] + ]; + $serializedRoutesConfig = 'serialized routes config'; + $serializedTypeData = 'serialized type data'; + $this->cacheMock->expects($this->at(0)) + ->method('load') + ->with(ServiceMetadata::ROUTES_CONFIG_CACHE_ID) + ->willReturn(false); + $this->cacheMock->expects($this->at(1)) + ->method('load') + ->with(ServiceMetadata::REFLECTED_TYPES_CACHE_ID) + ->willReturn(false); + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->configMock->expects($this->exactly(2)) + ->method('getServices') + ->willReturn($servicesConfig); + $this->classReflectorMock->expects($this->once()) + ->method('reflectClassMethods') + ->willReturn($methodsReflectionData); + $this->classReflectorMock->expects($this->once()) + ->method('extractClassDescription') + ->with(CustomerRepositoryInterface::class) + ->willReturn('Customer CRUD interface.'); + $this->typeProcessorMock->expects($this->exactly(2)) + ->method('getTypesData') + ->willReturn($typeData); + $this->serializerMock->expects($this->at(2)) + ->method('serialize') + ->with($routesMetadata) + ->willReturn($serializedRoutesConfig); + $this->serializerMock->expects($this->at(3)) + ->method('serialize') + ->with($typeData) + ->willReturn($serializedTypeData); + $this->cacheMock->expects($this->at(6)) + ->method('save') + ->with( + $serializedRoutesConfig, + ServiceMetadata::ROUTES_CONFIG_CACHE_ID + ); + $this->cacheMock->expects($this->at(7)) + ->method('save') + ->with( + $serializedTypeData, + ServiceMetadata::REFLECTED_TYPES_CACHE_ID + ); + $this->serviceMetadata->getRoutesConfig(); + $this->assertEquals($routesMetadata, $this->serviceMetadata->getRoutesConfig()); } /** - * Test identifying service name including subservices using class name. - * - * @dataProvider serviceNameDataProvider + * @dataProvider getServiceNameDataProvider */ public function testGetServiceName($className, $version, $preserveVersion, $expected) { - $actual = $this->serviceMetadata->getServiceName($className, $version, $preserveVersion); - $this->assertEquals($expected, $actual); + $this->assertEquals( + $expected, + $this->serviceMetadata->getServiceName($className, $version, $preserveVersion) + ); } /** - * Dataprovider for testGetServiceName - * * @return string */ - public function serviceNameDataProvider() + public function getServiceNameDataProvider() { return [ - [\Magento\Customer\Api\AccountManagementInterface::class, 'V1', false, 'customerAccountManagement'], - [\Magento\Customer\Api\AddressRepositoryInterface::class, 'V1', true, 'customerAddressRepositoryV1'], + [ + \Magento\Customer\Api\AccountManagementInterface::class, + 'V1', + false, + 'customerAccountManagement' + ], + [ + \Magento\Customer\Api\AddressRepositoryInterface::class, + 'V1', + true, + 'customerAddressRepositoryV1' + ], ]; } /** * @expectedException \InvalidArgumentException - * @dataProvider dataProviderForTestGetServiceNameInvalidName + * @dataProvider getServiceNameInvalidNameDataProvider */ public function testGetServiceNameInvalidName($interfaceClassName, $version) { @@ -189,111 +446,19 @@ public function testGetServiceNameInvalidName($interfaceClassName, $version) } /** - * Dataprovider for testGetServiceNameInvalidName - * * @return string */ - public function dataProviderForTestGetServiceNameInvalidName() + public function getServiceNameInvalidNameDataProvider() { return [ - ['BarV1Interface', 'V1'], // Missed vendor, module, 'Service' + ['BarV1Interface', 'V1'], // Missed vendor, module and Service ['Service\\V1Interface', 'V1'], // Missed vendor and module ['Magento\\Foo\\Service\\BarVxInterface', 'V1'], // Version number should be a number - ['Magento\\Foo\\Service\\BarInterface', 'V1'], // Version missed - ['Magento\\Foo\\Service\\BarV1', 'V1'], // 'Interface' missed - ['Foo\\Service\\BarV1Interface', 'V1'], // Module missed - ['Foo\\BarV1Interface', 'V1'] // Module and 'Service' missed - ]; - } - - public function testGetServiceMetadata() - { - $expectedResult = [ - 'methods' => [ - 'activateById' => [ - 'method' => 'activateById', - 'inputRequired' => '', - 'isSecure' => '', - 'resources' => [['Magento_Customer::manage']], - 'interface' => [ - 'in' => [ - 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - 'requiredInputParameter' => [ - 'required' => true, - ], - ], - ], - 'out' => [ - 'parameters' => [ - 'outputParameter' => [ - 'type' => 'string', - ], - ], - ], - ], - ], - ], - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'description' => 'classDescription', + ['Magento\\Foo\\Service\\BarInterface', 'V1'], // Missed version + ['Magento\\Foo\\Service\\BarV1', 'V1'], // Missed Interface + ['Foo\\Service\\BarV1Interface', 'V1'], // Missed module + ['Foo\\BarV1Interface', 'V1'] // Missed module and Service ]; - $result = $this->serviceMetadata->getServiceMetadata('customerAccountManagementV1'); - $this->assertEquals($expectedResult, $result); } - public function testGetRouteMetadata() - { - $expectedResult = [ - 'methods' => [ - 'activateById' => [ - 'method' => 'activateById', - 'inputRequired' => '', - 'isSecure' => '', - 'resources' => [['Magento_Customer::manage']], - 'interface' => [ - 'in' => [ - 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - 'requiredInputParameter' => [ - 'required' => true, - ], - ], - ], - 'out' => [ - 'parameters' => [ - 'outputParameter' => [ - 'type' => 'string', - ], - ], - ], - ], - ], - ], - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'description' => 'classDescription', - 'routes' => [ - '/V1/customers/me/activate' => [ - 'PUT' => [ - 'method' => 'activateById', - 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%' - ] - ] - ] - ] - ] - ]; - $result = $this->serviceMetadata->getRouteMetadata('customerAccountManagementV1'); - $this->assertEquals($expectedResult, $result); - } } - -require_once realpath(__DIR__ . '/../_files/test_interfaces.php'); diff --git a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php new file mode 100644 index 0000000000000..c74b0888d24d5 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php @@ -0,0 +1,250 @@ + [ + 'interface' => [ + 'in' => [ + 'parameters' => [ + 'customerId' => [ + 'force' => true, + 'value' => '%customer_id%', + ], + 'requiredInputParameter' => [ + 'required' => true, + ], + ], + ], + 'out' => [ + 'parameters' => [ + 'outputParameter' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ]; + $classReflection = $this->getMock( + \Magento\Webapi\Model\Config\ClassReflector::class, + ['reflectClassMethods', 'extractClassDescription'], + [], + '', + false + ); + $classReflection->expects($this->any()) + ->method('reflectClassMethods') + ->will($this->returnValue($interfaceParameters)); + $classReflection->expects($this->any()) + ->method('extractClassDescription') + ->will($this->returnValue('classDescription')); + + $servicesConfig = [ + 'services' => [\Magento\Customer\Api\AccountManagementInterface::class => [ + 'V1' => [ + 'methods' => [ + 'activateById' => [ + 'resources' => [ + [ + 'Magento_Customer::manage', + ], + ], + 'secure' => false, + ], + ], + ], + ], \Magento\Customer\Api\CustomerRepositoryInterface::class => [ + 'V1' => [ + 'methods' => [ + 'getById' => [ + 'resources' => [ + [ + 'Magento_Customer::customer', + ], + ], + 'secure' => false, + ], + ], + ], + ], + ], + 'routes' => [ + '/V1/customers/me/activate' => [ + 'PUT' => [ + 'secure' => false, + 'service' => [ + 'class' => \Magento\Customer\Api\AccountManagementInterface::class, + 'method' => 'activateById', + ], + 'resources' => [ + 'self' => true, + ], + 'parameters' => [ + 'customerId' => [ + 'force' => true, + 'value' => '%customer_id%', + ], + ], + ], + ], + '/V1/customers/:customerId' => [ + 'GET' => [ + 'secure' => false, + 'service' => [ + 'class' => \Magento\Customer\Api\CustomerRepositoryInterface::class, + 'method' => 'getById', + ], + 'resources' => [ + 'Magento_Customer::customer' => true, + ], + 'parameters' => [ + ], + ], + ], + ] + ]; + + /** + * @var $cacheMock \Magento\Webapi\Model\Cache\Type\Webapi + */ + $cacheMock = $this->getMockBuilder(\Magento\Webapi\Model\Cache\Type\Webapi::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var $readerMock \Magento\Webapi\Model\Config\Reader */ + $readerMock = $this->getMockBuilder(\Magento\Webapi\Model\Config\Reader::class) + ->disableOriginalConstructor() + ->getMock(); + $readerMock->expects($this->any())->method('read')->will($this->returnValue($servicesConfig)); + + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + /** @var $config \Magento\Webapi\Model\Config */ + $config = $objectManager->create( + \Magento\Webapi\Model\Config::class, + [ + 'cache' => $cacheMock, + 'configReader' => $readerMock, + ] + ); + + $typeProcessor = $objectManager->create(\Magento\Framework\Reflection\TypeProcessor::class); + + /** @var $config \Magento\Webapi\Model\ServiceMetadata */ + $this->serviceMetadata = $objectManager->create( + \Magento\Webapi\Model\ServiceMetadata::class, + [ + 'config' => $config, + 'cache' => $cacheMock, + 'classReflector' => $classReflection, + 'typeProcessor' => $typeProcessor, + ] + ); + } + + public function testGetServiceMetadata() + { + $expectedResult = [ + 'methods' => [ + 'activateById' => [ + 'method' => 'activateById', + 'inputRequired' => '', + 'isSecure' => '', + 'resources' => [['Magento_Customer::manage']], + 'interface' => [ + 'in' => [ + 'parameters' => [ + 'customerId' => [ + 'force' => true, + 'value' => '%customer_id%', + ], + 'requiredInputParameter' => [ + 'required' => true, + ], + ], + ], + 'out' => [ + 'parameters' => [ + 'outputParameter' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + 'class' => \Magento\Customer\Api\AccountManagementInterface::class, + 'description' => 'classDescription', + ]; + $result = $this->serviceMetadata->getServiceMetadata('customerAccountManagementV1'); + $this->assertEquals($expectedResult, $result); + } + + public function testGetRouteMetadata() + { + $expectedResult = [ + 'methods' => [ + 'activateById' => [ + 'method' => 'activateById', + 'inputRequired' => '', + 'isSecure' => '', + 'resources' => [['Magento_Customer::manage']], + 'interface' => [ + 'in' => [ + 'parameters' => [ + 'customerId' => [ + 'force' => true, + 'value' => '%customer_id%', + ], + 'requiredInputParameter' => [ + 'required' => true, + ], + ], + ], + 'out' => [ + 'parameters' => [ + 'outputParameter' => [ + 'type' => 'string', + ], + ], + ], + ], + ], + ], + 'class' => \Magento\Customer\Api\AccountManagementInterface::class, + 'description' => 'classDescription', + 'routes' => [ + '/V1/customers/me/activate' => [ + 'PUT' => [ + 'method' => 'activateById', + 'parameters' => [ + 'customerId' => [ + 'force' => true, + 'value' => '%customer_id%' + ] + ] + ] + ] + ] + ]; + $result = $this->serviceMetadata->getRouteMetadata('customerAccountManagementV1'); + $this->assertEquals($expectedResult, $result); + } +} From 1eb7a9c8224b14be4950e0963fec3ebb05091dd7 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 24 Oct 2016 17:55:32 -0500 Subject: [PATCH 21/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Introducing SerializerInterface --- app/code/Magento/Webapi/Model/Config.php | 1 + app/code/Magento/Webapi/Model/ServiceMetadata.php | 1 + 2 files changed, 2 insertions(+) diff --git a/app/code/Magento/Webapi/Model/Config.php b/app/code/Magento/Webapi/Model/Config.php index 53bfda46eb495..fb6dc894e3c0a 100644 --- a/app/code/Magento/Webapi/Model/Config.php +++ b/app/code/Magento/Webapi/Model/Config.php @@ -52,6 +52,7 @@ class Config * * @param WebapiCache $cache * @param Reader $configReader + * @param SerializerInterface|null $serializer */ public function __construct( WebapiCache $cache, diff --git a/app/code/Magento/Webapi/Model/ServiceMetadata.php b/app/code/Magento/Webapi/Model/ServiceMetadata.php index 1b1eea3442a00..14e45ccb409db 100644 --- a/app/code/Magento/Webapi/Model/ServiceMetadata.php +++ b/app/code/Magento/Webapi/Model/ServiceMetadata.php @@ -88,6 +88,7 @@ class ServiceMetadata * @param WebApiCache $cache * @param \Magento\Webapi\Model\Config\ClassReflector $classReflector * @param \Magento\Framework\Reflection\TypeProcessor $typeProcessor + * @param SerializerInterface|null $serializer */ public function __construct( \Magento\Webapi\Model\Config $config, From 8cfebabafc499366128f8a626cf42eae52c36e29 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 24 Oct 2016 19:26:02 -0500 Subject: [PATCH 22/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Moving unit test to integration tests --- .../Test/Unit/_files/test_interfaces.php | 31 ------------- .../Magento/Webapi}/Model/Soap/ConfigTest.php | 43 +++++++++---------- 2 files changed, 21 insertions(+), 53 deletions(-) delete mode 100644 app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php rename {app/code/Magento/Webapi/Test/Unit => dev/tests/integration/testsuite/Magento/Webapi}/Model/Soap/ConfigTest.php (84%) diff --git a/app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php b/app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php deleted file mode 100644 index 288a747cefd89..0000000000000 --- a/app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php +++ /dev/null @@ -1,31 +0,0 @@ -objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $typeProcessor = $this->objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class); + $typeProcessor = $this->objectManager->create(\Magento\Framework\Reflection\TypeProcessor::class); $objectManagerMock = $this->getMockBuilder( \Magento\Framework\App\ObjectManager::class @@ -97,13 +95,17 @@ protected function setUp() $config = new \Magento\Webapi\Model\Config($cacheMock, $readerMock); /** @var $config \Magento\Webapi\Model\ServiceMetadata */ - $serviceMetadata = new \Magento\Webapi\Model\ServiceMetadata( - $config, - $cacheMock, - $classReflection, - $typeProcessor); + $serviceMetadata = $this->objectManager->create( + \Magento\Webapi\Model\ServiceMetadata::class, + [ + 'config' => $config, + 'cache' => $cacheMock, + 'classReflector' => $classReflection, + 'typeProcessor' => $typeProcessor + ] + ); - $this->_soapConfig = $this->objectManager->getObject( + $this->_soapConfig = $this->objectManager->create( \Magento\Webapi\Model\Soap\Config::class, [ 'objectManager' => $objectManagerMock, @@ -111,7 +113,6 @@ protected function setUp() 'serviceMetadata' => $serviceMetadata, ] ); - parent::setUp(); } public function testGetRequestedSoapServices() @@ -162,5 +163,3 @@ public function testGetSoapOperation() $this->assertEquals($expectedResult, $soapOperation); } } - -require_once realpath(__DIR__ . '/../../_files/test_interfaces.php'); From 83de1a1982d465b8af4d9b27e0a3df9e0d42f0c4 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 24 Oct 2016 19:23:39 -0500 Subject: [PATCH 23/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Introducing SerializerInterface --- .../Test/Unit/_files/test_interfaces.php | 31 ------------- .../Magento/Webapi}/Model/Soap/ConfigTest.php | 43 +++++++++---------- 2 files changed, 21 insertions(+), 53 deletions(-) delete mode 100644 app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php rename {app/code/Magento/Webapi/Test/Unit => dev/tests/integration/testsuite/Magento/Webapi}/Model/Soap/ConfigTest.php (84%) diff --git a/app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php b/app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php deleted file mode 100644 index 288a747cefd89..0000000000000 --- a/app/code/Magento/Webapi/Test/Unit/_files/test_interfaces.php +++ /dev/null @@ -1,31 +0,0 @@ -objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $typeProcessor = $this->objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class); + $typeProcessor = $this->objectManager->create(\Magento\Framework\Reflection\TypeProcessor::class); $objectManagerMock = $this->getMockBuilder( \Magento\Framework\App\ObjectManager::class @@ -97,13 +95,17 @@ protected function setUp() $config = new \Magento\Webapi\Model\Config($cacheMock, $readerMock); /** @var $config \Magento\Webapi\Model\ServiceMetadata */ - $serviceMetadata = new \Magento\Webapi\Model\ServiceMetadata( - $config, - $cacheMock, - $classReflection, - $typeProcessor); + $serviceMetadata = $this->objectManager->create( + \Magento\Webapi\Model\ServiceMetadata::class, + [ + 'config' => $config, + 'cache' => $cacheMock, + 'classReflector' => $classReflection, + 'typeProcessor' => $typeProcessor + ] + ); - $this->_soapConfig = $this->objectManager->getObject( + $this->_soapConfig = $this->objectManager->create( \Magento\Webapi\Model\Soap\Config::class, [ 'objectManager' => $objectManagerMock, @@ -111,7 +113,6 @@ protected function setUp() 'serviceMetadata' => $serviceMetadata, ] ); - parent::setUp(); } public function testGetRequestedSoapServices() @@ -162,5 +163,3 @@ public function testGetSoapOperation() $this->assertEquals($expectedResult, $soapOperation); } } - -require_once realpath(__DIR__ . '/../../_files/test_interfaces.php'); From ed1b9c56bdca9aecd3f922ec7c1a6316ef1e6873 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Mon, 24 Oct 2016 19:37:17 -0500 Subject: [PATCH 24/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Introducing SerializerInterface --- .../Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php b/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php index 7aa99a69c109a..4125f82b7923f 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/ServiceMetadataTest.php @@ -96,6 +96,9 @@ public function testGetServicesConfig() $this->assertEquals($servicesConfig, $this->serviceMetadata->getServicesConfig()); } + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ public function testGetServicesConfigNoCache() { $servicesConfig = [ @@ -252,6 +255,9 @@ public function testGetRoutesConfig() $this->assertEquals($routesConfig, $this->serviceMetadata->getRoutesConfig()); } + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ public function testGetRoutesConfigNoCache() { $servicesConfig = [ @@ -460,5 +466,4 @@ public function getServiceNameInvalidNameDataProvider() ['Foo\\BarV1Interface', 'V1'] // Missed module and Service ]; } - } From 0e2beb8ad87785604c4676df791b85c41c5f49b2 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 25 Oct 2016 15:49:10 -0500 Subject: [PATCH 25/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Refactoring integration tests --- .../Webapi/Model/ServiceMetadataTest.php | 261 +++++------------- .../Magento/Webapi/Model/Soap/ConfigTest.php | 203 ++++++-------- 2 files changed, 150 insertions(+), 314 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php index c74b0888d24d5..521e0b6fc7c3a 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php @@ -5,231 +5,116 @@ */ namespace Magento\Webapi\Model; +use Magento\TestFramework\Helper\Bootstrap; +use Magento\Webapi\Model\ServiceMetadata; +use Magento\Customer\Api\AccountManagementInterface; + class ServiceMetadataTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Webapi\Model\ServiceMetadata + * @var ServiceMetadata */ private $serviceMetadata; - /** - * Set up helper. - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - */ protected function setUp() { - $interfaceParameters = [ - 'activateById' => [ - 'interface' => [ - 'in' => [ - 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - 'requiredInputParameter' => [ - 'required' => true, - ], - ], - ], - 'out' => [ - 'parameters' => [ - 'outputParameter' => [ - 'type' => 'string', - ], - ], - ], - ], - ], - ]; - $classReflection = $this->getMock( - \Magento\Webapi\Model\Config\ClassReflector::class, - ['reflectClassMethods', 'extractClassDescription'], - [], - '', - false - ); - $classReflection->expects($this->any()) - ->method('reflectClassMethods') - ->will($this->returnValue($interfaceParameters)); - $classReflection->expects($this->any()) - ->method('extractClassDescription') - ->will($this->returnValue('classDescription')); - - $servicesConfig = [ - 'services' => [\Magento\Customer\Api\AccountManagementInterface::class => [ - 'V1' => [ - 'methods' => [ - 'activateById' => [ - 'resources' => [ - [ - 'Magento_Customer::manage', - ], - ], - 'secure' => false, - ], - ], - ], - ], \Magento\Customer\Api\CustomerRepositoryInterface::class => [ - 'V1' => [ - 'methods' => [ - 'getById' => [ - 'resources' => [ - [ - 'Magento_Customer::customer', - ], - ], - 'secure' => false, - ], - ], - ], - ], - ], - 'routes' => [ - '/V1/customers/me/activate' => [ - 'PUT' => [ - 'secure' => false, - 'service' => [ - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'method' => 'activateById', - ], - 'resources' => [ - 'self' => true, - ], - 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - ], - ], - ], - '/V1/customers/:customerId' => [ - 'GET' => [ - 'secure' => false, - 'service' => [ - 'class' => \Magento\Customer\Api\CustomerRepositoryInterface::class, - 'method' => 'getById', - ], - 'resources' => [ - 'Magento_Customer::customer' => true, - ], - 'parameters' => [ - ], - ], - ], - ] - ]; - - /** - * @var $cacheMock \Magento\Webapi\Model\Cache\Type\Webapi - */ - $cacheMock = $this->getMockBuilder(\Magento\Webapi\Model\Cache\Type\Webapi::class) - ->disableOriginalConstructor() - ->getMock(); - - /** @var $readerMock \Magento\Webapi\Model\Config\Reader */ - $readerMock = $this->getMockBuilder(\Magento\Webapi\Model\Config\Reader::class) - ->disableOriginalConstructor() - ->getMock(); - $readerMock->expects($this->any())->method('read')->will($this->returnValue($servicesConfig)); - - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - - /** @var $config \Magento\Webapi\Model\Config */ - $config = $objectManager->create( - \Magento\Webapi\Model\Config::class, - [ - 'cache' => $cacheMock, - 'configReader' => $readerMock, - ] - ); - - $typeProcessor = $objectManager->create(\Magento\Framework\Reflection\TypeProcessor::class); - - /** @var $config \Magento\Webapi\Model\ServiceMetadata */ - $this->serviceMetadata = $objectManager->create( - \Magento\Webapi\Model\ServiceMetadata::class, - [ - 'config' => $config, - 'cache' => $cacheMock, - 'classReflector' => $classReflection, - 'typeProcessor' => $typeProcessor, - ] - ); + $objectManager = Bootstrap::getObjectManager(); + $this->serviceMetadata = $objectManager->create(ServiceMetadata::class); } public function testGetServiceMetadata() { - $expectedResult = [ + $expected = [ 'methods' => [ - 'activateById' => [ - 'method' => 'activateById', - 'inputRequired' => '', - 'isSecure' => '', - 'resources' => [['Magento_Customer::manage']], + 'activate' => [ + 'method' => 'activate', + 'inputRequired' => false, + 'isSecure' => false, + 'resources' => [ + 'Magento_Customer::manage' + ], + 'documentation' => 'Activate a customer account using a key that was sent in a confirmation email.', 'interface' => [ 'in' => [ 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - 'requiredInputParameter' => [ + 'email' => [ + 'type' => 'string', 'required' => true, + 'documentation' => null ], - ], + 'confirmationKey' => [ + 'type' => 'string', + 'required' => true, + 'documentation' => null + ] + ] ], 'out' => [ 'parameters' => [ - 'outputParameter' => [ - 'type' => 'string', - ], + 'result' => [ + 'type' => 'CustomerDataCustomerInterface', + 'required' => true, + 'documentation' => '' + ] ], - ], - ], - ], + 'throws' => [ + '\Magento\Framework\Exception\LocalizedException' + ] + ] + ] + ] ], - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'description' => 'classDescription', + 'class' => AccountManagementInterface::class, + 'description' => 'Interface for managing customers accounts.', ]; - $result = $this->serviceMetadata->getServiceMetadata('customerAccountManagementV1'); - $this->assertEquals($expectedResult, $result); + $actual = $this->serviceMetadata->getServiceMetadata('customerAccountManagementV1'); + $this->assertEquals(array_replace_recursive($expected, $actual), $actual); } public function testGetRouteMetadata() { - $expectedResult = [ + $expected = [ 'methods' => [ - 'activateById' => [ - 'method' => 'activateById', - 'inputRequired' => '', - 'isSecure' => '', - 'resources' => [['Magento_Customer::manage']], + 'activate' => [ + 'method' => 'activate', + 'inputRequired' => false, + 'isSecure' => false, + 'resources' => [ + 'Magento_Customer::manage' + ], + 'documentation' => 'Activate a customer account using a key that was sent in a confirmation email.', 'interface' => [ 'in' => [ 'parameters' => [ - 'customerId' => [ - 'force' => true, - 'value' => '%customer_id%', - ], - 'requiredInputParameter' => [ + 'email' => [ + 'type' => 'string', 'required' => true, + 'documentation' => null ], - ], + 'confirmationKey' => [ + 'type' => 'string', + 'required' => true, + 'documentation' => null + ] + ] ], 'out' => [ 'parameters' => [ - 'outputParameter' => [ - 'type' => 'string', - ], + 'result' => [ + 'type' => 'CustomerDataCustomerInterface', + 'required' => true, + 'documentation' => '' + ] ], - ], - ], - ], + 'throws' => [ + '\Magento\Framework\Exception\LocalizedException' + ] + ] + ] + ] ], - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'description' => 'classDescription', + 'class' => AccountManagementInterface::class, + 'description' => 'Interface for managing customers accounts.', 'routes' => [ '/V1/customers/me/activate' => [ 'PUT' => [ @@ -244,7 +129,7 @@ public function testGetRouteMetadata() ] ] ]; - $result = $this->serviceMetadata->getRouteMetadata('customerAccountManagementV1'); - $this->assertEquals($expectedResult, $result); + $actual = $this->serviceMetadata->getRouteMetadata('customerAccountManagementV1'); + $this->assertEquals(array_replace_recursive($expected, $actual), $actual); } } 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 74e6728fcee26..4ab3bccd42d95 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php @@ -5,161 +5,112 @@ */ namespace Magento\Webapi\Model\Soap; -// @codingStandardsIgnoreFile +use Magento\TestFramework\Helper\Bootstrap; +use Magento\Webapi\Model\Soap\Config; +use Magento\Customer\Api\AccountManagementInterface; +use Magento\Customer\Api\CustomerRepositoryInterface; +use Magento\Framework\Exception\LocalizedException; class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Webapi\Model\Soap\Config + * @var Config */ - private $_soapConfig; + private $soapConfig; - /** - * @var \Magento\TestFramework\Helper\Bootstrap - */ - private $objectManager; - - /** - * Set up helper. - */ protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - - $typeProcessor = $this->objectManager->create(\Magento\Framework\Reflection\TypeProcessor::class); - - $objectManagerMock = $this->getMockBuilder( - \Magento\Framework\App\ObjectManager::class - )->disableOriginalConstructor()->getMock(); - - $classReflection = $this->getMock( - \Magento\Webapi\Model\Config\ClassReflector::class, - ['reflectClassMethods'], - ['_typeProcessor' => $typeProcessor], - '' - ); - $classReflection->expects($this->any())->method('reflectClassMethods')->will($this->returnValue([])); + $objectManager = Bootstrap::getObjectManager(); + $this->soapConfig = $objectManager->create(Config::class); + } - $servicesConfig = [ - 'services' => [\Magento\Customer\Api\AccountManagementInterface::class => [ - 'V1' => [ - 'methods' => [ - 'activate' => [ - 'resources' => [ - [ - 'Magento_Customer::manage', - ], - ], - 'secure' => false, - ], + public function testGetRequestedSoapServices() + { + $expected = [ + 'customerAccountManagementV1' => [ + 'methods' => [ + 'activate' => [ + 'method' => 'activate', + 'inputRequired' => false, + 'isSecure' => false, + 'resources' => [ + 'Magento_Customer::manage' ], - ], - ], \Magento\Customer\Api\CustomerRepositoryInterface::class => [ - 'V1' => [ - 'methods' => [ - 'getById' => [ - 'resources' => [ - [ - 'Magento_Customer::customer', + 'documentation' + => 'Activate a customer account using a key that was sent in a confirmation email.', + 'interface' => [ + 'in' => [ + 'parameters' => [ + 'email' => [ + 'type' => 'string', + 'required' => true, + 'documentation' => null ], - ], - 'secure' => false, + 'confirmationKey' => [ + 'type' => 'string', + 'required' => true, + 'documentation' => null + ] + ] ], - ], - ], + 'out' => [ + 'parameters' => [ + 'result' => [ + 'type' => 'CustomerDataCustomerInterface', + 'required' => true, + 'documentation' => null + ] + ], + 'throws' => [ + '\Magento\Framework\Exception\LocalizedException' + ] + ] + ] + ] ], - ], - ]; - - /** - * @var $registryMock \Magento\Framework\Registry - */ - $registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class) - ->disableOriginalConstructor() - ->getMock(); - - /** - * @var $cacheMock \Magento\Webapi\Model\Cache\Type\Webapi - */ - $cacheMock = $this->getMockBuilder(\Magento\Webapi\Model\Cache\Type\Webapi::class) - ->disableOriginalConstructor() - ->getMock(); - - /** @var $readerMock \Magento\Webapi\Model\Config\Reader */ - $readerMock = $this->getMockBuilder(\Magento\Webapi\Model\Config\Reader::class) - ->disableOriginalConstructor() - ->getMock(); - $readerMock->expects($this->any())->method('read')->will($this->returnValue($servicesConfig)); - - /** @var $config \Magento\Webapi\Model\Config */ - $config = new \Magento\Webapi\Model\Config($cacheMock, $readerMock); - - /** @var $config \Magento\Webapi\Model\ServiceMetadata */ - $serviceMetadata = $this->objectManager->create( - \Magento\Webapi\Model\ServiceMetadata::class, - [ - 'config' => $config, - 'cache' => $cacheMock, - 'classReflector' => $classReflection, - 'typeProcessor' => $typeProcessor + 'class' => AccountManagementInterface::class, + 'description' => 'Interface for managing customers accounts.', ] - ); - - $this->_soapConfig = $this->objectManager->create( - \Magento\Webapi\Model\Soap\Config::class, + ]; + $actual = $this->soapConfig->getRequestedSoapServices( [ - 'objectManager' => $objectManagerMock, - 'registry' => $registryMock, - 'serviceMetadata' => $serviceMetadata, + 'customerAccountManagementV1', + 'NonExistentService' ] ); - } - - public function testGetRequestedSoapServices() - { - $expectedResult = [ - 'customerAccountManagementV1' => - [ - 'methods' => [ - 'activate' => [ - 'method' => 'activate', - 'inputRequired' => '', - 'isSecure' => '', - 'resources' => [['Magento_Customer::manage']], - ], - ], - 'class' => \Magento\Customer\Api\AccountManagementInterface::class, - 'description' => 'Interface for managing customers accounts.', - ], - ]; - - $result = $this->_soapConfig->getRequestedSoapServices( - ['customerAccountManagementV1', 'moduleBarV2', 'moduleBazV1'] - ); - - $this->assertEquals($expectedResult, $result); + $this->assertEquals(array_replace_recursive($expected, $actual), $actual); } public function testGetServiceMethodInfo() { - $expectedResult = [ - 'class' => \Magento\Customer\Api\CustomerRepositoryInterface::class, + $expected = [ + 'class' => CustomerRepositoryInterface::class, 'method' => 'getById', 'isSecure' => false, - 'resources' => [['Magento_Customer::customer']], + 'resources' => [ + 'Magento_Customer::customer', + 'self' + ], ]; - $methodInfo = $this->_soapConfig->getServiceMethodInfo( + $actual = $this->soapConfig->getServiceMethodInfo( 'customerCustomerRepositoryV1GetById', - ['customerCustomerRepositoryV1', 'moduleBazV1'] + [ + 'customerCustomerRepositoryV1', + 'NonExistentService' + ] ); - $this->assertEquals($expectedResult, $methodInfo); + $this->assertEquals($expected, $actual); } public function testGetSoapOperation() { - $expectedResult = 'customerAccountManagementV1Activate'; - $soapOperation = $this->_soapConfig - ->getSoapOperation(\Magento\Customer\Api\AccountManagementInterface::class, 'activate', 'V1'); - $this->assertEquals($expectedResult, $soapOperation); + $expected = 'customerAccountManagementV1Activate'; + $actual = $this->soapConfig + ->getSoapOperation( + AccountManagementInterface::class, + 'activate', + 'V1' + ); + $this->assertEquals($expected, $actual); } } From 2e9233b3ea266516a5b7a7817bfcc6e36006ed91 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Tue, 25 Oct 2016 15:54:09 -0500 Subject: [PATCH 26/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Refactoring integration tests --- .../testsuite/Magento/Webapi/Model/ServiceMetadataTest.php | 6 +++--- .../testsuite/Magento/Webapi/Model/Soap/ConfigTest.php | 2 +- 2 files changed, 4 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 521e0b6fc7c3a..78748e84c1131 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php @@ -11,7 +11,7 @@ class ServiceMetadataTest extends \PHPUnit_Framework_TestCase { - /** + /**bootstrap.sh * @var ServiceMetadata */ private $serviceMetadata; @@ -68,7 +68,7 @@ public function testGetServiceMetadata() 'description' => 'Interface for managing customers accounts.', ]; $actual = $this->serviceMetadata->getServiceMetadata('customerAccountManagementV1'); - $this->assertEquals(array_replace_recursive($expected, $actual), $actual); + $this->assertEquals(array_replace_recursive($actual, $expected), $actual); } public function testGetRouteMetadata() @@ -130,6 +130,6 @@ public function testGetRouteMetadata() ] ]; $actual = $this->serviceMetadata->getRouteMetadata('customerAccountManagementV1'); - $this->assertEquals(array_replace_recursive($expected, $actual), $actual); + $this->assertEquals(array_replace_recursive($actual, $expected), $actual); } } 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 4ab3bccd42d95..9625803339fa6 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php @@ -78,7 +78,7 @@ public function testGetRequestedSoapServices() 'NonExistentService' ] ); - $this->assertEquals(array_replace_recursive($expected, $actual), $actual); + $this->assertEquals(array_replace_recursive($actual, $expected), $actual); } public function testGetServiceMethodInfo() From 2bb4c834d5d9fe1a9f8bef3c06b9cf3d27e65214 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 27 Oct 2016 14:44:32 -0500 Subject: [PATCH 27/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Refactoring integration tests --- .../testsuite/Magento/Webapi/Model/ServiceMetadataTest.php | 4 ++-- .../testsuite/Magento/Webapi/Model/Soap/ConfigTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php index 78748e84c1131..37bbd10363ef5 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php @@ -58,7 +58,7 @@ public function testGetServiceMetadata() ] ], 'throws' => [ - '\Magento\Framework\Exception\LocalizedException' + \Magento\Framework\Exception\LocalizedException::class ] ] ] @@ -107,7 +107,7 @@ public function testGetRouteMetadata() ] ], 'throws' => [ - '\Magento\Framework\Exception\LocalizedException' + \Magento\Framework\Exception\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 9625803339fa6..8b016fe35fc95 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php @@ -62,7 +62,7 @@ public function testGetRequestedSoapServices() ] ], 'throws' => [ - '\Magento\Framework\Exception\LocalizedException' + \Magento\Framework\Exception\LocalizedException::class ] ] ] From 4c31f32033238aca4434c7f018188d55abeb598e Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 27 Oct 2016 17:02:18 -0500 Subject: [PATCH 28/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Refactoring integration tests --- .../testsuite/Magento/Webapi/Model/ServiceMetadataTest.php | 5 ++--- .../testsuite/Magento/Webapi/Model/Soap/ConfigTest.php | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php index 37bbd10363ef5..7bfbe08a34a09 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/ServiceMetadataTest.php @@ -6,7 +6,6 @@ namespace Magento\Webapi\Model; use Magento\TestFramework\Helper\Bootstrap; -use Magento\Webapi\Model\ServiceMetadata; use Magento\Customer\Api\AccountManagementInterface; class ServiceMetadataTest extends \PHPUnit_Framework_TestCase @@ -58,7 +57,7 @@ public function testGetServiceMetadata() ] ], 'throws' => [ - \Magento\Framework\Exception\LocalizedException::class + "\\Magento\\Framework\\Exception\\LocalizedException" ] ] ] @@ -107,7 +106,7 @@ public function testGetRouteMetadata() ] ], 'throws' => [ - \Magento\Framework\Exception\LocalizedException::class + "\\Magento\\Framework\\Exception\\LocalizedException" ] ] ] 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 8b016fe35fc95..95743e9e10537 100644 --- a/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php +++ b/dev/tests/integration/testsuite/Magento/Webapi/Model/Soap/ConfigTest.php @@ -6,10 +6,8 @@ namespace Magento\Webapi\Model\Soap; use Magento\TestFramework\Helper\Bootstrap; -use Magento\Webapi\Model\Soap\Config; use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Api\CustomerRepositoryInterface; -use Magento\Framework\Exception\LocalizedException; class ConfigTest extends \PHPUnit_Framework_TestCase { @@ -62,7 +60,7 @@ public function testGetRequestedSoapServices() ] ], 'throws' => [ - \Magento\Framework\Exception\LocalizedException::class + "\\Magento\\Framework\\Exception\\LocalizedException" ] ] ] From 61eda85b7da8f658b0c334f3ca744af180dfc784 Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 4 Nov 2016 15:46:16 -0500 Subject: [PATCH 29/33] MAGETWO-59444: Create serializer interface and json class in framework --- .../Framework/Serialize/Serializer/Json.php | 4 ++ .../Serialize/SerializerInterface.php | 3 + .../Test/Unit/Serializer/JsonTest.php | 55 +++++++++++++++---- 3 files changed, 51 insertions(+), 11 deletions(-) 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 { /** 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..88e06d89e3763 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 @@ -21,25 +22,57 @@ protected function setUp() } /** - * @param null|bool|array $value - * @dataProvider serializeUnserializeDataProvider + * @param string|int|float|bool|array|null $value + * @param string $expected + * @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 string $value + * @param string|int|float|bool|array|null $expected + * @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 7439d1d4183ec4e37d7240108fb2550fd2b79f6b Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 4 Nov 2016 15:46:30 -0500 Subject: [PATCH 30/33] MAGETWO-59764: Create serialize class in framework --- .../Magento/Framework/Serialize/README.md | 7 +- .../Serialize/Serializer/Serialize.php | 45 ++++++++++++ .../Test/Unit/Serializer/SerializeTest.php | 71 +++++++++++++++++++ 3 files changed, 121 insertions(+), 2 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/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..3d2dc66e502ef --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php @@ -0,0 +1,45 @@ +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..874647b5d705f --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/SerializeTest.php @@ -0,0 +1,71 @@ +serialize = $objectManager->getObject(Serialize::class); + } + + /** + * @param string|int|float|bool|array|null $value + * @param string $serializedValue + * @dataProvider serializeDataProvider + */ + public function testSerialize($value, $serializedValue) + { + $this->assertEquals($serializedValue, $this->serialize->serialize($value)); + } + + public function serializeDataProvider() + { + return [ + ['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 2db8223269b7e72c8706ed93cb3c6f640aecc1d1 Mon Sep 17 00:00:00 2001 From: Igor Melnikov Date: Thu, 27 Oct 2016 17:50:41 -0500 Subject: [PATCH 31/33] MAGETWO-58692: Refactor Module_Webapi, Module_Elasticsearch Fixing tests (cherry picked from commit 5b636d0) --- .../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 71335bdcd5a20cfea63cb7ca5a1ebe5bd8bcb1ad Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 4 Nov 2016 16:51:34 -0500 Subject: [PATCH 32/33] MAGETWO-59444: Create serializer interface and json class in framework --- lib/internal/Magento/Framework/Serialize/Serializer/Json.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Serialize/Serializer/Json.php b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php index bf7a34c21fbdd..9c5e55b194165 100644 --- a/lib/internal/Magento/Framework/Serialize/Serializer/Json.php +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php @@ -9,7 +9,6 @@ /** * Class for serializing data to json string and unserializing json string to data - * */ class Json implements SerializerInterface { From 287c7733ae368f040d6afe2d99279154911ede9f Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 4 Nov 2016 17:16:25 -0500 Subject: [PATCH 33/33] MAGETWO-59764: Create serialize class in framework --- .../testsuite/Magento/Test/Legacy/_files/security/blacklist.php | 1 + 1 file changed, 1 insertion(+) diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php index 3c93d37deba52..42b8e68e78411 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php @@ -6,4 +6,5 @@ return [ 'Test/Unit', 'lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php', + 'lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php', ];