From 2e72fb04702f08c30661c32cbffb5d877f27f0e1 Mon Sep 17 00:00:00 2001 From: dmanners Date: Mon, 17 Jul 2017 10:03:03 +0000 Subject: [PATCH 1/5] Remove Zend_Json call from js/customer-data/invalidation-rules.phtml - remove zend call and use the Magento Framework, - move work from template into app/code/Magento/Customer/Block/CustomerScopeData.php --- .../Customer/Block/CustomerScopeData.php | 26 +++++++++++++++++++ .../js/customer-data/invalidation-rules.phtml | 16 ++---------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php index 244437e870b98..50df1054e79dd 100644 --- a/app/code/Magento/Customer/Block/CustomerScopeData.php +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -50,4 +50,30 @@ public function getWebsiteId() { return (int)$this->_storeManager->getStore()->getWebsiteId(); } + + /** + * Get the invalidation rules json encoded + * + * @return string + */ + public function getInvalidationRulesJson() + { + return $this->jsonEncoder->encode( + [ + '*' => [ + 'Magento_Customer/js/invalidation-processor' => [ + 'invalidationRules' => [ + 'website-rule' => [ + 'Magento_Customer/js/invalidation-rules/website-rule' => [ + 'scopeConfig' => [ + 'websiteId' => $this->getWebsiteId(), + ] + ] + ] + ] + ] + ], + ] + ); + } } diff --git a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml index 7905b1d9925e3..7ded67344c011 100644 --- a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml @@ -12,18 +12,6 @@ From 0a7ee5921c68edfe4ed0cdcfef399ee6e164f4ea Mon Sep 17 00:00:00 2001 From: dmanners Date: Mon, 17 Jul 2017 10:12:56 +0000 Subject: [PATCH 2/5] Replace the usage of the depricated \Magento\Framework\Json\EncoderInterface in the CustomerScopeData block, - Use \Magento\Framework\Serialize\Serializer\Json instead when dealing with serilization - Update test to mirror the change in the main class --- .../Customer/Block/CustomerScopeData.php | 17 +++++++++-------- .../Test/Unit/Block/CustomerScopeDataTest.php | 8 ++++---- .../js/customer-data/invalidation-rules.phtml | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php index 50df1054e79dd..7b48b4eb2576e 100644 --- a/app/code/Magento/Customer/Block/CustomerScopeData.php +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -20,23 +20,23 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template private $storeManager; /** - * @var \Magento\Framework\Json\EncoderInterface + * @var \Magento\Framework\Serialize\Serializer\Json */ - private $jsonEncoder; + private $serializer; /** * @param \Magento\Framework\View\Element\Template\Context $context - * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder + * @param \Magento\Framework\Serialize\Serializer\Json $serializer * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, - \Magento\Framework\Json\EncoderInterface $jsonEncoder, + \Magento\Framework\Serialize\Serializer\Json $serializer, array $data = [] ) { parent::__construct($context, $data); $this->storeManager = $context->getStoreManager(); - $this->jsonEncoder = $jsonEncoder; + $this->serializer = $serializer; } /** @@ -54,11 +54,12 @@ public function getWebsiteId() /** * Get the invalidation rules json encoded * - * @return string + * @return bool|string + * @throws \InvalidArgumentException */ - public function getInvalidationRulesJson() + public function getSerializedInvalidationRules() { - return $this->jsonEncoder->encode( + return $this->serializer->serialize( [ '*' => [ 'Magento_Customer/js/invalidation-processor' => [ diff --git a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php index dcbe4882231ca..af0f86a423b0b 100644 --- a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php +++ b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php @@ -26,8 +26,8 @@ class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase /** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */ private $scopeConfigMock; - /** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $encoderMock; + /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; protected function setUp() { @@ -41,7 +41,7 @@ protected function setUp() $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) ->getMock(); - $this->encoderMock = $this->getMockBuilder(EncoderInterface::class) + $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class) ->getMock(); $this->contextMock->expects($this->exactly(2)) @@ -54,7 +54,7 @@ protected function setUp() $this->model = new CustomerScopeData( $this->contextMock, - $this->encoderMock, + $this->serializerMock, [] ); } diff --git a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml index 7ded67344c011..ad8aab76e483f 100644 --- a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml @@ -12,6 +12,6 @@ From eaad78c90f79c850c45d4fb67971d53139130092 Mon Sep 17 00:00:00 2001 From: dmanners Date: Tue, 18 Jul 2017 16:03:57 +0000 Subject: [PATCH 3/5] Update the CustomerScopeData changes to use serializer to fit with BiC rules - keep the $jsonEncoder injection but remove the type, - mark the constructor as PHPMD.UnusedFormalParameter, - split out the config and the serilization to happen in two methods, --- .../Customer/Block/CustomerScopeData.php | 52 +++++++----- .../Test/Unit/Block/CustomerScopeDataTest.php | 85 ++++++++++++++++++- 2 files changed, 114 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php index 7b48b4eb2576e..f3a17aedc055a 100644 --- a/app/code/Magento/Customer/Block/CustomerScopeData.php +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -26,17 +26,21 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template /** * @param \Magento\Framework\View\Element\Template\Context $context - * @param \Magento\Framework\Serialize\Serializer\Json $serializer + * @param $jsonEncoder * @param array $data + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, - \Magento\Framework\Serialize\Serializer\Json $serializer, - array $data = [] + $jsonEncoder, + array $data = [], + \Magento\Framework\Serialize\Serializer\Json $serializer = null ) { parent::__construct($context, $data); $this->storeManager = $context->getStoreManager(); - $this->serializer = $serializer; + $this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); } /** @@ -51,6 +55,28 @@ public function getWebsiteId() return (int)$this->_storeManager->getStore()->getWebsiteId(); } + /** + * @return array + */ + public function getInvalidationRules() + { + return [ + '*' => [ + 'Magento_Customer/js/invalidation-processor' => [ + 'invalidationRules' => [ + 'website-rule' => [ + 'Magento_Customer/js/invalidation-rules/website-rule' => [ + 'scopeConfig' => [ + 'websiteId' => $this->getWebsiteId(), + ] + ] + ] + ] + ] + ], + ]; + } + /** * Get the invalidation rules json encoded * @@ -59,22 +85,6 @@ public function getWebsiteId() */ public function getSerializedInvalidationRules() { - return $this->serializer->serialize( - [ - '*' => [ - 'Magento_Customer/js/invalidation-processor' => [ - 'invalidationRules' => [ - 'website-rule' => [ - 'Magento_Customer/js/invalidation-rules/website-rule' => [ - 'scopeConfig' => [ - 'websiteId' => $this->getWebsiteId(), - ] - ] - ] - ] - ] - ], - ] - ); + return $this->serializer->serialize($this->getInvalidationRules()); } } diff --git a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php index af0f86a423b0b..529ee1a88abe1 100644 --- a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php +++ b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php @@ -54,8 +54,9 @@ protected function setUp() $this->model = new CustomerScopeData( $this->contextMock, - $this->serializerMock, - [] + null, + [], + $this->serializerMock ); } @@ -78,4 +79,84 @@ public function testGetWebsiteId() $this->assertEquals($storeId, $this->model->getWebsiteId()); } + + public function testGetInvalidationRules() + { + $storeId = 1; + + $storeMock = $this->getMockBuilder(StoreInterface::class) + ->setMethods(['getWebsiteId']) + ->getMockForAbstractClass(); + + $storeMock->expects($this->any()) + ->method('getWebsiteId') + ->willReturn($storeId); + + $this->storeManagerMock->expects($this->any()) + ->method('getStore') + ->with(null) + ->willReturn($storeMock); + + $this->assertEquals( + [ + '*' => [ + 'Magento_Customer/js/invalidation-processor' => [ + 'invalidationRules' => [ + 'website-rule' => [ + 'Magento_Customer/js/invalidation-rules/website-rule' => [ + 'scopeConfig' => [ + 'websiteId' => 1, + ] + ] + ] + ] + ] + ], + ], + $this->model->getInvalidationRules() + ); + } + + public function testGetSerializedInvalidationRules() + { + $storeId = 1; + $rules = [ + '*' => [ + 'Magento_Customer/js/invalidation-processor' => [ + 'invalidationRules' => [ + 'website-rule' => [ + 'Magento_Customer/js/invalidation-rules/website-rule' => [ + 'scopeConfig' => [ + 'websiteId' => 1, + ] + ] + ] + ] + ] + ], + ]; + + $storeMock = $this->getMockBuilder(StoreInterface::class) + ->setMethods(['getWebsiteId']) + ->getMockForAbstractClass(); + + $storeMock->expects($this->any()) + ->method('getWebsiteId') + ->willReturn($storeId); + + $this->storeManagerMock->expects($this->any()) + ->method('getStore') + ->with(null) + ->willReturn($storeMock); + + $this->serializerMock->expects($this->any()) + ->method('serialize') + ->with($rules) + ->willReturn(json_encode($rules)); + + $this->assertEquals( + json_encode($rules), + $this->model->getSerializedInvalidationRules() + ); + } } From 5568b8dc25ba989f64fca41d073a3dfe831dd3d0 Mon Sep 17 00:00:00 2001 From: dmanners Date: Tue, 18 Jul 2017 19:11:40 +0000 Subject: [PATCH 4/5] Add back in the type for the $jsonEncoder object injected into CustomerScopeData --- app/code/Magento/Customer/Block/CustomerScopeData.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php index f3a17aedc055a..397e830619895 100644 --- a/app/code/Magento/Customer/Block/CustomerScopeData.php +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -25,15 +25,16 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template private $serializer; /** + * CustomerScopeData constructor. * @param \Magento\Framework\View\Element\Template\Context $context - * @param $jsonEncoder + * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder * @param array $data * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer - * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @throws \RuntimeException */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, - $jsonEncoder, + \Magento\Framework\Json\EncoderInterface $jsonEncoder, array $data = [], \Magento\Framework\Serialize\Serializer\Json $serializer = null ) { From 31764a5c64bf9c81fcb32d98d5ac5ef8cab6a8d6 Mon Sep 17 00:00:00 2001 From: dmanners Date: Wed, 19 Jul 2017 07:05:58 +0000 Subject: [PATCH 5/5] Add back in the mock of the jsonEncoder to the test case --- app/code/Magento/Customer/Block/CustomerScopeData.php | 2 +- .../Customer/Test/Unit/Block/CustomerScopeDataTest.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php index 397e830619895..6ba08c6f59b75 100644 --- a/app/code/Magento/Customer/Block/CustomerScopeData.php +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -25,12 +25,12 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template private $serializer; /** - * CustomerScopeData constructor. * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder * @param array $data * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer * @throws \RuntimeException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, diff --git a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php index 529ee1a88abe1..1f6976a7f3338 100644 --- a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php +++ b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php @@ -10,7 +10,6 @@ use Magento\Store\Api\Data\StoreInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\Customer\Block\CustomerScopeData; -use Magento\Framework\Json\EncoderInterface; class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase { @@ -26,6 +25,9 @@ class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase /** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */ private $scopeConfigMock; + /** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $encoderMock; + /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */ private $serializerMock; @@ -41,6 +43,9 @@ protected function setUp() $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) ->getMock(); + $this->encoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class) + ->getMock(); + $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class) ->getMock(); @@ -54,7 +59,7 @@ protected function setUp() $this->model = new CustomerScopeData( $this->contextMock, - null, + $this->encoderMock, [], $this->serializerMock );