Skip to content

Commit 5b7238f

Browse files
committed
MAGETWO-63444: Remove object from cache in \Magento\Theme\Model\Theme\ThemeProvider
- Theme: Change method name and fix short description - ThemeProviderTest: Mock theme in setUp. Fix weird issue with generated files, and add stubs for methods used. Extract themeArray to variable.
1 parent 965f169 commit 5b7238f

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

app/code/Magento/Theme/Model/Theme.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,24 +413,25 @@ public function populateFromArray(array $data)
413413
{
414414
$this->_data = $data;
415415
if (isset($data['parent_theme'])) {
416-
$this->_data['parent_theme'] = $this->getThemeInstance()->populateFromArray($data['parent_theme']);
416+
$this->_data['parent_theme'] = $this->createThemeInstance()->populateFromArray($data['parent_theme']);
417417
}
418418

419419
if (isset($data['inherited_themes'])) {
420420
foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
421-
$this->_data['inherited_themes'][$key] = $this->getThemeInstance()->populateFromArray($inheritedTheme);
421+
$themeInstance = $this->createThemeInstance()->populateFromArray($inheritedTheme);
422+
$this->_data['inherited_themes'][$key] = $themeInstance;
422423
}
423424
}
424425

425426
return $this;
426427
}
427428

428429
/**
429-
* Create a new Theme model from factory
430+
* Create Theme instance
430431
*
431432
* @return \Magento\Theme\Model\Theme
432433
*/
433-
private function getThemeInstance()
434+
private function createThemeInstance()
434435
{
435436
return $this->themeModelFactory->create();
436437
}

app/code/Magento/Theme/Test/Unit/Model/Theme/ThemeProviderTest.php

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,26 @@ class ThemeProviderTest extends \PHPUnit_Framework_TestCase
4040
/** @var \Magento\Theme\Model\Theme\ThemeProvider|\PHPUnit_Framework_MockObject_MockObject */
4141
private $themeProvider;
4242

43+
/** @var \Magento\Theme\Model\Theme|\PHPUnit_Framework_MockObject_MockObject */
44+
private $theme;
45+
4346
protected function setUp()
4447
{
4548
$this->objectManager = new ObjectManagerHelper($this);
4649
$this->collectionFactory = $this->getMock(
4750
\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory::class,
51+
['create'],
4852
[],
53+
'',
54+
false
55+
);
56+
$this->themeFactory = $this->getMock(
57+
\Magento\Theme\Model\ThemeFactory::class,
58+
['create'],
4959
[],
5060
'',
5161
false
5262
);
53-
$this->themeFactory = $this->getMock(\Magento\Theme\Model\ThemeFactory::class, [], [], '', false);
5463
$this->cache = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
5564
->disableOriginalConstructor()
5665
->getMock();
@@ -64,24 +73,24 @@ protected function setUp()
6473
'serializer' => $this->serializer
6574
]
6675
);
76+
$this->theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
6777
}
6878

6979
public function testGetByFullPath()
7080
{
7181
$themeArray = ['theme_data' => 'theme_data'];
72-
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
73-
$theme->expects($this->exactly(2))
82+
$this->theme->expects($this->exactly(2))
7483
->method('getId')
7584
->willReturn(self::THEME_ID);
76-
$theme->expects($this->exactly(2))
85+
$this->theme->expects($this->exactly(2))
7786
->method('toArray')
7887
->willReturn($themeArray);
7988

8089
$collectionMock = $this->getMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class, [], [], '', false);
8190
$collectionMock->expects($this->once())
8291
->method('getThemeByFullPath')
8392
->with(self::THEME_PATH)
84-
->willReturn($theme);
93+
->willReturn($this->theme);
8594
$this->collectionFactory->expects($this->once())
8695
->method('create')
8796
->willReturn($collectionMock);
@@ -106,9 +115,9 @@ public function testGetByFullPath()
106115
\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
107116

108117
// Assertion for first time load
109-
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
118+
$this->assertSame($this->theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
110119
// Assertion for loading from local cache
111-
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
120+
$this->assertSame($this->theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
112121
}
113122

114123
public function testGetByFullPathWithCache()
@@ -129,41 +138,46 @@ public function testGetByFullPathWithCache()
129138
\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
130139

131140
$serializedTheme = '{"theme_data":"theme_data"}';
132-
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
133-
$theme->expects($this->once())
141+
$themeArray = ['theme_data' => 'theme_data'];
142+
$this->theme->expects($this->once())
134143
->method('populateFromArray')
144+
->with($themeArray)
135145
->willReturnSelf();
136146
$this->themeFactory->expects($this->once())
137147
->method('create')
138-
->willReturn($theme);
148+
->willReturn($this->theme);
139149

140150
$this->serializer->expects($this->once())
141151
->method('unserialize')
142152
->with($serializedTheme)
143-
->willReturn(['theme_data' => 'theme_data']);
153+
->willReturn($themeArray);
144154

145155
$this->cache->expects($this->once())
146156
->method('load')
147157
->with('theme' . self::THEME_PATH)
148158
->willReturn($serializedTheme);
149159

150160
// Assertion for load from cache
151-
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
161+
$this->assertSame($this->theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
152162
// Assertion for load from object cache
153-
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
163+
$this->assertSame($this->theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
154164
}
155165

156166
public function testGetById()
157167
{
158168
$themeArray = ['theme_data' => 'theme_data'];
159-
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
160-
$theme->expects($this->once())->method('load')->with(self::THEME_ID)->will($this->returnSelf());
161-
$theme->expects($this->once())->method('getId')->will($this->returnValue(self::THEME_ID));
162-
$theme->expects($this->once())
169+
$this->theme->expects($this->once())
170+
->method('load')
171+
->with(self::THEME_ID)
172+
->willReturnSelf();
173+
$this->theme->expects($this->once())
174+
->method('getId')
175+
->willReturn(self::THEME_ID);
176+
$this->theme->expects($this->once())
163177
->method('toArray')
164178
->willReturn($themeArray);
165179

166-
$this->themeFactory->expects($this->once())->method('create')->will($this->returnValue($theme));
180+
$this->themeFactory->expects($this->once())->method('create')->will($this->returnValue($this->theme));
167181
$this->cache->expects($this->once())
168182
->method('load')
169183
->with('theme-by-id-' . self::THEME_ID)
@@ -174,18 +188,18 @@ public function testGetById()
174188
->willReturn('{"theme_data":"theme_data"}');
175189

176190
// Assertion for initial load
177-
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
191+
$this->assertSame($this->theme, $this->themeProvider->getThemeById(self::THEME_ID));
178192
// Assertion for load from object cache
179-
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
193+
$this->assertSame($this->theme, $this->themeProvider->getThemeById(self::THEME_ID));
180194
}
181195

182196
public function testGetByIdWithCache()
183197
{
184198
$serializedTheme = '{"theme_data":"theme_data"}';
185-
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
186-
$theme->expects($this->once())
199+
$themeArray = ['theme_data' => 'theme_data'];
200+
$this->theme->expects($this->once())
187201
->method('populateFromArray')
188-
->with(['theme_data' => 'theme_data'])
202+
->with($themeArray)
189203
->willReturnSelf();
190204
$this->cache->expects($this->once())
191205
->method('load')
@@ -194,15 +208,15 @@ public function testGetByIdWithCache()
194208
$this->serializer->expects($this->once())
195209
->method('unserialize')
196210
->with($serializedTheme)
197-
->willReturn(['theme_data' => 'theme_data']);
211+
->willReturn($themeArray);
198212
$this->themeFactory->expects($this->once())
199213
->method('create')
200-
->willReturn($theme);
214+
->willReturn($this->theme);
201215

202216
// Assertion for initial load from cache
203-
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
217+
$this->assertSame($this->theme, $this->themeProvider->getThemeById(self::THEME_ID));
204218
// Assertion for load from object cache
205-
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
219+
$this->assertSame($this->theme, $this->themeProvider->getThemeById(self::THEME_ID));
206220
}
207221

208222
public function testGetThemeCustomizations()

0 commit comments

Comments
 (0)