Skip to content

Commit 276cb1c

Browse files
committed
failing tests fixed
1 parent 2ac6e0c commit 276cb1c

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

app/code/Magento/Theme/Model/PageLayout/Config/Builder.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,38 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
declare(strict_types=1);
9+
810
namespace Magento\Theme\Model\PageLayout\Config;
911

12+
use Magento\Framework\App\Cache\Type\Layout;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\View\Model\PageLayout\Config\BuilderInterface;
15+
use Magento\Framework\View\PageLayout\ConfigFactory;
16+
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
17+
use Magento\Theme\Model\ResourceModel\Theme\Collection;
18+
use Magento\Theme\Model\Theme\Data;
19+
use Magento\Framework\Serialize\SerializerInterface;
20+
1021
/**
1122
* Page layout config builder
1223
*/
13-
class Builder implements \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface
24+
class Builder implements BuilderInterface
1425
{
1526
const CACHE_KEY_LAYOUTS = 'THEME_LAYOUTS_FILES_MERGED';
1627

1728
/**
18-
* @var \Magento\Framework\View\PageLayout\ConfigFactory
29+
* @var ConfigFactory
1930
*/
2031
protected $configFactory;
2132

2233
/**
23-
* @var \Magento\Framework\View\PageLayout\File\Collector\Aggregated
34+
* @var Aggregated
2435
*/
2536
protected $fileCollector;
2637

2738
/**
28-
* @var \Magento\Theme\Model\ResourceModel\Theme\Collection
39+
* @var Collection
2940
*/
3041
protected $themeCollection;
3142

@@ -35,27 +46,36 @@ class Builder implements \Magento\Framework\View\Model\PageLayout\Config\Builder
3546
private $configFiles = [];
3647

3748
/**
38-
* @var \Magento\Framework\App\Cache\Type\Layout
49+
* @var Layout|null
50+
*/
51+
private $cacheModel;
52+
/**
53+
* @var SerializerInterface|null
3954
*/
40-
protected $cacheModel;
55+
private $serializer;
4156

4257
/**
43-
* @param \Magento\Framework\View\PageLayout\ConfigFactory $configFactory
44-
* @param \Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector
45-
* @param \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection
46-
* @param \Magento\Framework\App\Cache\Type\Layout $cacheModel
58+
* @param ConfigFactory $configFactory
59+
* @param Aggregated $fileCollector
60+
* @param Collection $themeCollection
61+
* @param Layout|null $cacheModel
62+
* @param SerializerInterface|null $serializer
4763
*/
4864
public function __construct(
49-
\Magento\Framework\View\PageLayout\ConfigFactory $configFactory,
50-
\Magento\Framework\View\PageLayout\File\Collector\Aggregated $fileCollector,
51-
\Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection,
52-
\Magento\Framework\App\Cache\Type\Layout $cacheModel
65+
ConfigFactory $configFactory,
66+
Aggregated $fileCollector,
67+
Collection $themeCollection,
68+
?Layout $cacheModel = null,
69+
?SerializerInterface $serializer = null
5370
) {
5471
$this->configFactory = $configFactory;
5572
$this->fileCollector = $fileCollector;
5673
$this->themeCollection = $themeCollection;
57-
$this->cacheModel = $cacheModel;
58-
$this->themeCollection->setItemObjectClass(\Magento\Theme\Model\Theme\Data::class);
74+
$this->themeCollection->setItemObjectClass(Data::class);
75+
$this->cacheModel = $cacheModel
76+
?? ObjectManager::getInstance()->get(Layout::class);
77+
$this->serializer = $serializer
78+
?? ObjectManager::getInstance()->get(SerializerInterface::class);
5979
}
6080

6181
/**
@@ -77,14 +97,15 @@ protected function getConfigFiles()
7797
$configFiles = [];
7898
$this->configFiles = $this->cacheModel->load(self::CACHE_KEY_LAYOUTS);
7999
if (!empty($this->configFiles)) {
80-
$this->configFiles = @unserialize($this->configFiles);//if value in cache is corrupted.
100+
//if value in cache is corrupted.
101+
$this->configFiles = $this->serializer->unserialize($this->configFiles);
81102
}
82103
if (empty($this->configFiles)) {
83104
foreach ($this->themeCollection->loadRegisteredThemes() as $theme) {
84105
$configFiles[] = $this->fileCollector->getFilesContent($theme, 'layouts.xml');
85106
}
86107
$this->configFiles = array_merge(...$configFiles);
87-
$this->cacheModel->save(serialize($this->configFiles), self::CACHE_KEY_LAYOUTS);
108+
$this->cacheModel->save($this->serializer->serialize($this->configFiles), self::CACHE_KEY_LAYOUTS);
88109
}
89110
}
90111

app/code/Magento/Theme/Test/Unit/Model/PageLayout/Config/BuilderTest.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
*/
1111
namespace Magento\Theme\Test\Unit\Model\PageLayout\Config;
1212

13-
use Magento\Framework\App\Cache\Type\FrontendPool;
14-
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
15-
use Magento\Framework\Cache\FrontendInterface;
13+
use Magento\Framework\App\Cache\Type\Layout;
14+
use Magento\Framework\Serialize\SerializerInterface;
1615
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1716
use Magento\Framework\View\PageLayout\Config;
17+
use Magento\Framework\View\PageLayout\ConfigFactory;
1818
use Magento\Framework\View\PageLayout\File\Collector\Aggregated;
19-
use Magento\TestFramework\Helper\Bootstrap;
2019
use Magento\Theme\Model\PageLayout\Config\Builder;
2120
use Magento\Theme\Model\ResourceModel\Theme\Collection;
2221
use Magento\Theme\Model\Theme\Data;
2322
use PHPUnit\Framework\MockObject\MockObject;
2423
use PHPUnit\Framework\TestCase;
25-
use Magento\Framework\App\Cache\Type\Layout;
2624

2725
class BuilderTest extends TestCase
2826
{
@@ -32,7 +30,7 @@ class BuilderTest extends TestCase
3230
protected $builder;
3331

3432
/**
35-
* @var \Magento\Framework\View\PageLayout\ConfigFactory|MockObject
33+
* @var ConfigFactory|MockObject
3634
*/
3735
protected $configFactory;
3836

@@ -50,6 +48,10 @@ class BuilderTest extends TestCase
5048
* @var Layout|MockObject
5149
*/
5250
protected $cacheModel;
51+
/**
52+
* @var SerializerInterface|MockObject
53+
*/
54+
protected $serializer;
5355

5456
/**
5557
* SetUp method
@@ -58,28 +60,29 @@ class BuilderTest extends TestCase
5860
*/
5961
protected function setUp(): void
6062
{
61-
$this->configFactory = $this->getMockBuilder(\Magento\Framework\View\PageLayout\ConfigFactory::class)
63+
$this->configFactory = $this->getMockBuilder(ConfigFactory::class)
6264
->disableOriginalConstructor()
6365
->setMethods(['create'])
6466
->getMock();
6567

66-
$this->fileCollector = $this->getMockBuilder(
67-
Aggregated::class
68-
)->disableOriginalConstructor()
68+
$this->fileCollector = $this->getMockBuilder(Aggregated::class)
69+
->disableOriginalConstructor()
6970
->getMock();
7071

71-
$helper = new ObjectManager($this);
7272
$this->themeCollection = $this->getMockBuilder(Collection::class)
7373
->disableOriginalConstructor()
7474
->getMock();
7575
$this->cacheModel = $this->getMockBuilder(Layout::class)
7676
->disableOriginalConstructor()
7777
->getMock();
7878

79+
$this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
80+
7981
$this->themeCollection->expects($this->once())
8082
->method('setItemObjectClass')
8183
->with(Data::class)
8284
->willReturnSelf();
85+
8386
$helper = new ObjectManager($this);
8487
$this->builder = $helper->getObject(
8588
Builder::class,
@@ -88,6 +91,7 @@ protected function setUp(): void
8891
'fileCollector' => $this->fileCollector,
8992
'themeCollection' => $this->themeCollection,
9093
'cacheModel' => $this->cacheModel,
94+
'serializer' => $this->serializer,
9195
]
9296
);
9397
}
@@ -102,6 +106,7 @@ public function testGetPageLayoutsConfig()
102106
$this->cacheModel->clean();
103107
$files1 = ['content layouts_1.xml', 'content layouts_2.xml'];
104108
$files2 = ['content layouts_3.xml', 'content layouts_4.xml'];
109+
$configFiles = array_merge($files1, $files2);
105110

106111
$theme1 = $this->getMockBuilder(Data::class)
107112
->disableOriginalConstructor()
@@ -129,9 +134,17 @@ public function testGetPageLayoutsConfig()
129134

130135
$this->configFactory->expects($this->once())
131136
->method('create')
132-
->with(['configFiles' => array_merge($files1, $files2)])
137+
->with(['configFiles' => $configFiles])
133138
->willReturn($config);
134139

140+
$this->serializer->expects($this->once())
141+
->method('serialize')
142+
->with($configFiles);
143+
144+
$this->cacheModel->expects($this->once())
145+
->method('save')
146+
->willReturnSelf();
147+
135148
$this->assertSame($config, $this->builder->getPageLayoutsConfig());
136149
}
137150
}

0 commit comments

Comments
 (0)