Skip to content

Commit 118dd7c

Browse files
committed
MAGETWO-62486: Refactor Layout to store data from object only and restore object
- unit tests
1 parent 085c507 commit 118dd7c

File tree

4 files changed

+102
-13
lines changed

4 files changed

+102
-13
lines changed

lib/internal/Magento/Framework/View/Layout.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,17 @@ public function generateElements()
318318
$cacheId = 'structure_' . $this->getUpdate()->getCacheId();
319319
$result = $this->cache->load($cacheId);
320320
if ($result) {
321-
$this->readerContext = $this->getReaderContext();
322321
$data = $this->serializer->unserialize($result);
323-
$this->readerContext->getPageConfigStructure()->populateWithArray($data['pageConfigStructure']);
324-
$this->readerContext->getScheduledStructure()->populateWithArray($data['scheduledStructure']);
322+
$this->getReaderContext()->getPageConfigStructure()->populateWithArray($data['pageConfigStructure']);
323+
$this->getReaderContext()->getScheduledStructure()->populateWithArray($data['scheduledStructure']);
325324
} else {
326325
\Magento\Framework\Profiler::start('build_structure');
327326
$this->readerPool->interpret($this->getReaderContext(), $this->getNode());
328327
\Magento\Framework\Profiler::stop('build_structure');
329328

330329
$data = [
331-
'pageConfigStructure' => $this->readerContext->getPageConfigStructure()->__toArray(),
332-
'scheduledStructure' => $this->readerContext->getScheduledStructure()->__toArray(),
330+
'pageConfigStructure' => $this->getReaderContext()->getPageConfigStructure()->__toArray(),
331+
'scheduledStructure' => $this->getReaderContext()->getScheduledStructure()->__toArray(),
333332
];
334333
$this->cache->save($this->serializer->serialize($data), $cacheId, $this->getUpdate()->getHandles());
335334
}

lib/internal/Magento/Framework/View/Layout/ScheduledStructure.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ public function __toArray()
559559
*
560560
* @param array $data
561561
* @return void
562+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
563+
* @SuppressWarnings(PHPMD.NPathComplexity)
562564
*/
563565
public function populateWithArray(array $data)
564566
{

lib/internal/Magento/Framework/View/Page/Config/Structure.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ public function __toArray()
221221
*
222222
* @param array $data
223223
* @return void
224+
* @SuppressWarnings(PHPMD.NPathComplexity)
224225
*/
225226
public function populateWithArray(array $data)
226227
{
@@ -229,7 +230,9 @@ public function populateWithArray(array $data)
229230
$this->title = isset($data['title']) ? $data['title'] : '';
230231
$this->metadata = isset($data['metadata']) ? $data['metadata'] : [];
231232
$this->elementAttributes = isset($data['elementAttributes']) ? $data['elementAttributes'] : [];
232-
$this->removeElementAttributes = isset($data['removeElementAttributes']) ? $data['removeElementAttributes'] : [];
233+
$this->removeElementAttributes = isset($data['removeElementAttributes'])
234+
? $data['removeElementAttributes']
235+
: [];
233236
$this->bodyClasses = isset($data['bodyClasses']) ? $data['bodyClasses'] : [];
234237
$this->isBodyClassesDeleted = isset($data['isBodyClassesDeleted']) ? $data['isBodyClassesDeleted'] : false;
235238
}

lib/internal/Magento/Framework/View/Test/Unit/LayoutTest.php

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Framework\View\Test\Unit;
7+
use Magento\Framework\Serialize\SerializerInterface;
78

89
/**
910
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -81,6 +82,16 @@ class LayoutTest extends \PHPUnit_Framework_TestCase
8182
*/
8283
protected $readerContextMock;
8384

85+
/**
86+
* @var \Magento\Framework\View\Page\Config\Structure|\PHPUnit_Framework_MockObject_MockObject
87+
*/
88+
private $pageConfigStructure;
89+
90+
/**
91+
* @var \Magento\Framework\View\Layout\ScheduledStructure|\PHPUnit_Framework_MockObject_MockObject
92+
*/
93+
private $layoutScheduledSructure;
94+
8495
/**
8596
* @var \Magento\Framework\View\Layout\Generator\ContextFactory|\PHPUnit_Framework_MockObject_MockObject
8697
*/
@@ -96,6 +107,11 @@ class LayoutTest extends \PHPUnit_Framework_TestCase
96107
*/
97108
protected $loggerMock;
98109

110+
/**
111+
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
112+
*/
113+
private $serializer;
114+
99115
protected function setUp()
100116
{
101117
$this->structureMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Data\Structure::class)
@@ -141,9 +157,22 @@ protected function setUp()
141157
$this->readerContextFactoryMock = $this->getMockBuilder(
142158
\Magento\Framework\View\Layout\Reader\ContextFactory::class
143159
)->disableOriginalConstructor()->getMock();
160+
161+
$this->pageConfigStructure = $this->getMockBuilder(\Magento\Framework\View\Page\Config\Structure::class)
162+
->setMethods(['__toArray', 'populateWithArray'])
163+
->getMock();
164+
$this->layoutScheduledSructure = $this->getMockBuilder(\Magento\Framework\View\Layout\ScheduledStructure::class)
165+
->setMethods(['__toArray', 'populateWithArray'])
166+
->getMock();
144167
$this->readerContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Reader\Context::class)
168+
->setMethods(['getPageConfigStructure', 'getScheduledStructure'])
145169
->disableOriginalConstructor()
146170
->getMock();
171+
$this->readerContextMock->expects($this->any())->method('getPageConfigStructure')
172+
->willReturn($this->pageConfigStructure);
173+
$this->readerContextMock->expects($this->any())->method('getScheduledStructure')
174+
->willReturn($this->layoutScheduledSructure);
175+
147176
$this->generatorContextFactoryMock = $this->getMockBuilder(
148177
\Magento\Framework\View\Layout\Generator\ContextFactory::class
149178
)
@@ -154,6 +183,16 @@ protected function setUp()
154183
->getMock();
155184
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
156185
->getMock();
186+
$this->serializer = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
187+
$this->serializer->expects($this->any())->method('serialize')
188+
->willReturnCallback(function ($value) {
189+
return json_encode($value);
190+
});
191+
$this->serializer->expects($this->any())->method('unserialize')
192+
->willReturnCallback(function ($value) {
193+
return json_decode($value, true);
194+
});
195+
157196

158197
$this->model = new \Magento\Framework\View\Layout(
159198
$this->processorFactoryMock,
@@ -168,7 +207,8 @@ protected function setUp()
168207
$this->generatorContextFactoryMock,
169208
$this->appStateMock,
170209
$this->loggerMock,
171-
true
210+
true,
211+
$this->serializer
172212
);
173213
}
174214

@@ -735,9 +775,32 @@ public function testGenerateElementsWithoutCache()
735775
->with($this->readerContextMock, $xml)
736776
->willReturnSelf();
737777

778+
$pageConfigStructureData = [
779+
'field_1' => 123,
780+
'field_2' => 'text',
781+
'field_3' => [
782+
'field_3_1' => '1244',
783+
'field_3_2' => null,
784+
'field_3_3' => false,
785+
]
786+
];
787+
$this->pageConfigStructure->expects($this->any())->method('__toArray')
788+
->willReturn($pageConfigStructureData);
789+
790+
$layoutScheduledStructureData = [
791+
'field_1' => 1283,
792+
'field_2' => 'text_qwertyuiop[]asdfghjkl;'
793+
];
794+
$this->layoutScheduledSructure->expects($this->any())->method('__toArray')
795+
->willReturn($layoutScheduledStructureData);
796+
$data = [
797+
'pageConfigStructure' => $pageConfigStructureData,
798+
'scheduledStructure' => $layoutScheduledStructureData
799+
];
800+
738801
$this->cacheMock->expects($this->once())
739802
->method('save')
740-
->with(serialize($this->readerContextMock), 'structure_' . $layoutCacheId, $handles)
803+
->with(json_encode($data), 'structure_' . $layoutCacheId, $handles)
741804
->willReturn(true);
742805

743806
$generatorContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Context::class)
@@ -774,6 +837,9 @@ public function testGenerateElementsWithCache()
774837
$xml = simplexml_load_string('<layout/>', \Magento\Framework\View\Layout\Element::class);
775838
$this->model->setXml($xml);
776839

840+
$this->readerContextFactoryMock->expects($this->once())
841+
->method('create')
842+
->willReturn($this->readerContextMock);
777843
$themeMock = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
778844
$this->themeResolverMock->expects($this->once())
779845
->method('get')
@@ -787,14 +853,33 @@ public function testGenerateElementsWithCache()
787853
->method('getCacheId')
788854
->willReturn($layoutCacheId);
789855

790-
$readerContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Reader\Context::class)
791-
->disableOriginalConstructor()
792-
->getMock();
856+
$pageConfigStructureData = [
857+
'field_1' => 123,
858+
'field_2' => 'text',
859+
'field_3' => [
860+
'field_3_1' => '1244',
861+
'field_3_2' => null,
862+
'field_3_3' => false,
863+
]
864+
];
865+
$this->pageConfigStructure->expects($this->once())->method('populateWithArray')
866+
->with($pageConfigStructureData);
867+
868+
$layoutScheduledStructureData = [
869+
'field_1' => 1283,
870+
'field_2' => 'text_qwertyuiop[]asdfghjkl;'
871+
];
872+
$this->layoutScheduledSructure->expects($this->once())->method('populateWithArray')
873+
->with($layoutScheduledStructureData);
874+
$data = [
875+
'pageConfigStructure' => $pageConfigStructureData,
876+
'scheduledStructure' => $layoutScheduledStructureData
877+
];
793878

794879
$this->cacheMock->expects($this->once())
795880
->method('load')
796881
->with('structure_' . $layoutCacheId)
797-
->willReturn(serialize($readerContextMock));
882+
->willReturn(json_encode($data));
798883

799884
$this->readerPoolMock->expects($this->never())
800885
->method('interpret');
@@ -811,7 +896,7 @@ public function testGenerateElementsWithCache()
811896

812897
$this->generatorPoolMock->expects($this->once())
813898
->method('process')
814-
->with($readerContextMock, $generatorContextMock)
899+
->with($this->readerContextMock, $generatorContextMock)
815900
->willReturn(true);
816901

817902
$elements = [

0 commit comments

Comments
 (0)