Skip to content

Commit 0844fbd

Browse files
committed
MAGETWO-63444: Remove object from cache in \Magento\Theme\Model\Theme\ThemeProvider
- Update docblocks to remove unneeded "method" in short descriptions - ThemeProviderTest: Refactor to initialize all re-used mocks in the setUp method. Extract reused parameters to variables. Capitalize inline comments.
1 parent 5105e65 commit 0844fbd

File tree

3 files changed

+99
-136
lines changed

3 files changed

+99
-136
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public function toArray(array $keys = [])
404404
}
405405

406406
/**
407-
* Method to populate Theme object from an array
407+
* Populate Theme object from an array
408408
*
409409
* @param array $data
410410
* @return Theme
@@ -426,7 +426,7 @@ public function populateFromArray(array $data)
426426
}
427427

428428
/**
429-
* Method to get a new Theme model from factory
429+
* Create a new Theme model from factory
430430
*
431431
* @return \Magento\Theme\Model\Theme
432432
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function getThemeById($themeId)
135135
}
136136

137137
/**
138-
* Method to load Theme model from cache
138+
* Load Theme model from cache
139139
*
140140
* @param string $cacheId
141141
* @return \Magento\Theme\Model\Theme|null
@@ -153,7 +153,7 @@ private function loadThemeFromCache($cacheId)
153153
}
154154

155155
/**
156-
* Method to save Theme model to the cache
156+
* Save Theme model to the cache
157157
*
158158
* @param \Magento\Theme\Model\Theme $theme
159159
* @param string $cacheId

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

Lines changed: 95 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,79 @@
1616
*/
1717
class ThemeProviderTest extends \PHPUnit_Framework_TestCase
1818
{
19+
/** Theme path used by tests */
20+
const THEME_PATH = 'frontend/Magento/luma';
21+
22+
/** Theme ID used by tests */
23+
const THEME_ID = 755;
24+
1925
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
20-
protected $objectManager;
26+
private $objectManager;
27+
28+
/** @var \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject */
29+
private $collectionFactory;
30+
31+
/** @var \Magento\Theme\Model\ThemeFactory|\PHPUnit_Framework_MockObject_MockObject */
32+
private $themeFactory;
33+
34+
/** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
private $cache;
36+
37+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
38+
private $serializer;
39+
40+
/** @var \Magento\Theme\Model\Theme\ThemeProvider|\PHPUnit_Framework_MockObject_MockObject */
41+
private $themeProvider;
2142

2243
protected function setUp()
2344
{
2445
$this->objectManager = new ObjectManagerHelper($this);
25-
}
26-
27-
public function testGetByFullPath()
28-
{
29-
$path = 'frontend/Magento/luma';
30-
$collectionFactory = $this->getMock(
46+
$this->collectionFactory = $this->getMock(
3147
\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory::class,
32-
['create'],
48+
[],
3349
[],
3450
'',
3551
false
3652
);
37-
$collectionMock = $this->getMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class, [], [], '', false);
53+
$this->themeFactory = $this->getMock(\Magento\Theme\Model\ThemeFactory::class, [], [], '', false);
54+
$this->cache = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
55+
->disableOriginalConstructor()
56+
->getMock();
57+
$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class);
58+
$this->themeProvider = $this->objectManager->getObject(
59+
\Magento\Theme\Model\Theme\ThemeProvider::class,
60+
[
61+
'collectionFactory' => $this->collectionFactory,
62+
'themeFactory' => $this->themeFactory,
63+
'cache' => $this->cache,
64+
'serializer' => $this->serializer
65+
]
66+
);
67+
}
3868

69+
public function testGetByFullPath()
70+
{
71+
$themeArray = ['theme_data' => 'theme_data'];
3972
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
4073
$theme->expects($this->exactly(2))
4174
->method('getId')
42-
->willReturn(1);
75+
->willReturn(self::THEME_ID);
4376
$theme->expects($this->exactly(2))
4477
->method('toArray')
45-
->willReturn(['theme_data' => 'theme_data']);
78+
->willReturn($themeArray);
4679

80+
$collectionMock = $this->getMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class, [], [], '', false);
4781
$collectionMock->expects($this->once())
4882
->method('getThemeByFullPath')
49-
->with($path)
83+
->with(self::THEME_PATH)
5084
->willReturn($theme);
51-
$collectionFactory->expects($this->once())->method('create')->will($this->returnValue($collectionMock));
52-
$themeFactory = $this->getMock(\Magento\Theme\Model\ThemeFactory::class, [], [], '', false);
53-
$serializerMock = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class);
54-
$serializerMock->expects($this->exactly(2))
85+
$this->collectionFactory->expects($this->once())
86+
->method('create')
87+
->willReturn($collectionMock);
88+
$this->serializer->expects($this->exactly(2))
5589
->method('serialize')
56-
->with(['theme_data' => 'theme_data'])
57-
->willReturn('{"theme_data":"theme_data"}');
58-
59-
$themeProvider = $this->objectManager->getObject(
60-
\Magento\Theme\Model\Theme\ThemeProvider::class,
61-
[
62-
'collectionFactory' => $collectionFactory,
63-
'themeFactory' => $themeFactory,
64-
'serializer' => $serializerMock
65-
]
66-
);
90+
->with($themeArray)
91+
->willReturn('serialized theme');
6792

6893
$deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
6994
->disableOriginalConstructor()
@@ -80,15 +105,14 @@ public function testGetByFullPath()
80105
]);
81106
\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
82107

83-
// assertion for first time load
84-
$this->assertSame($theme, $themeProvider->getThemeByFullPath($path));
85-
// assertion for loading from local cache
86-
$this->assertSame($theme, $themeProvider->getThemeByFullPath($path));
108+
// Assertion for first time load
109+
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
110+
// Assertion for loading from local cache
111+
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
87112
}
88113

89114
public function testGetByFullPathWithCache()
90115
{
91-
$path = 'frontend/Magento/luma';
92116
$deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
93117
->disableOriginalConstructor()
94118
->getMock();
@@ -104,144 +128,88 @@ public function testGetByFullPathWithCache()
104128
]);
105129
\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
106130

131+
$serializedTheme = '{"theme_data":"theme_data"}';
107132
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
108133
$theme->expects($this->once())
109134
->method('populateFromArray')
110135
->willReturnSelf();
111-
$themeFactory = $this->getMock(\Magento\Theme\Model\ThemeFactory::class, [], [], '', false);
112-
$themeFactory->expects($this->once())
136+
$this->themeFactory->expects($this->once())
113137
->method('create')
114138
->willReturn($theme);
115139

116-
$serializerMock = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class);
117-
$serializerMock->expects($this->once())
140+
$this->serializer->expects($this->once())
118141
->method('unserialize')
119-
->with('{"theme_data":"theme_data"}')
142+
->with($serializedTheme)
120143
->willReturn(['theme_data' => 'theme_data']);
121144

122-
$cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
123-
->disableOriginalConstructor()
124-
->getMock();
125-
$cacheMock->expects($this->once())
145+
$this->cache->expects($this->once())
126146
->method('load')
127-
->with('theme' . $path)
128-
->willReturn('{"theme_data":"theme_data"}');
147+
->with('theme' . self::THEME_PATH)
148+
->willReturn($serializedTheme);
129149

130-
$themeProvider = $this->objectManager->getObject(
131-
\Magento\Theme\Model\Theme\ThemeProvider::class,
132-
[
133-
'themeFactory' => $themeFactory,
134-
'cache' => $cacheMock,
135-
'serializer' => $serializerMock
136-
]
137-
);
138-
139-
// assertion for load from cache
140-
$this->assertSame($theme, $themeProvider->getThemeByFullPath($path));
141-
// assertion for load from object cache
142-
$this->assertSame($theme, $themeProvider->getThemeByFullPath($path));
150+
// Assertion for load from cache
151+
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
152+
// Assertion for load from object cache
153+
$this->assertSame($theme, $this->themeProvider->getThemeByFullPath(self::THEME_PATH));
143154
}
144155

145156
public function testGetById()
146157
{
147-
$themeId = 755;
158+
$themeArray = ['theme_data' => 'theme_data'];
148159
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
149-
$theme->expects($this->once())->method('load')->with($themeId)->will($this->returnSelf());
150-
$theme->expects($this->once())->method('getId')->will($this->returnValue(1));
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));
151162
$theme->expects($this->once())
152163
->method('toArray')
153-
->willReturn(['theme_data' => 'theme_data']);
154-
155-
$themeFactory = $this->getMock(\Magento\Theme\Model\ThemeFactory::class, ['create'], [], '', false);
156-
$themeFactory->expects($this->once())->method('create')->will($this->returnValue($theme));
164+
->willReturn($themeArray);
157165

158-
$cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
159-
->disableOriginalConstructor()
160-
->getMock();
161-
$cacheMock->expects($this->once())
166+
$this->themeFactory->expects($this->once())->method('create')->will($this->returnValue($theme));
167+
$this->cache->expects($this->once())
162168
->method('load')
163-
->with('theme-by-id-' . $themeId)
169+
->with('theme-by-id-' . self::THEME_ID)
164170
->willReturn(false);
165-
166-
$serializerMock = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class);
167-
$serializerMock->expects($this->once())
171+
$this->serializer->expects($this->once())
168172
->method('serialize')
169-
->with(['theme_data' => 'theme_data'])
173+
->with($themeArray)
170174
->willReturn('{"theme_data":"theme_data"}');
171175

172-
$themeProvider = $this->objectManager->getObject(
173-
\Magento\Theme\Model\Theme\ThemeProvider::class,
174-
[
175-
'themeFactory' => $themeFactory,
176-
'cache' => $cacheMock,
177-
'serializer' => $serializerMock
178-
]
179-
);
180-
181-
// assertion for initial load
182-
$this->assertSame($theme, $themeProvider->getThemeById($themeId));
183-
// assertion for load from object cache
184-
$this->assertSame($theme, $themeProvider->getThemeById($themeId));
176+
// Assertion for initial load
177+
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
178+
// Assertion for load from object cache
179+
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
185180
}
186181

187182
public function testGetByIdWithCache()
188183
{
189-
$themeId = 755;
184+
$serializedTheme = '{"theme_data":"theme_data"}';
190185
$theme = $this->getMock(\Magento\Theme\Model\Theme::class, [], [], '', false);
191186
$theme->expects($this->once())
192187
->method('populateFromArray')
193188
->with(['theme_data' => 'theme_data'])
194189
->willReturnSelf();
195-
$cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
196-
->disableOriginalConstructor()
197-
->getMock();
198-
$cacheMock->expects($this->once())
190+
$this->cache->expects($this->once())
199191
->method('load')
200-
->with('theme-by-id-' . $themeId)
201-
->willReturn('{"theme_data":"theme_data"}');
202-
$serializerMock = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class);
203-
$serializerMock->expects($this->once())
192+
->with('theme-by-id-' . self::THEME_ID)
193+
->willReturn($serializedTheme);
194+
$this->serializer->expects($this->once())
204195
->method('unserialize')
205-
->with('{"theme_data":"theme_data"}')
196+
->with($serializedTheme)
206197
->willReturn(['theme_data' => 'theme_data']);
207-
$themeFactory = $this->getMock(\Magento\Theme\Model\ThemeFactory::class, ['create'], [], '', false);
208-
$themeFactory->expects($this->once())->method('create')->will($this->returnValue($theme));
209-
$themeFactory->expects($this->once())
198+
$this->themeFactory->expects($this->once())
210199
->method('create')
211200
->willReturn($theme);
212201

213-
$themeProvider = $this->objectManager->getObject(
214-
\Magento\Theme\Model\Theme\ThemeProvider::class,
215-
[
216-
'themeFactory' => $themeFactory,
217-
'cache' => $cacheMock,
218-
'serializer' => $serializerMock
219-
]
220-
);
221-
222-
// assertion for initial load from cache
223-
$this->assertSame($theme, $themeProvider->getThemeById($themeId));
224-
// assertion for load from object cache
225-
$this->assertSame($theme, $themeProvider->getThemeById($themeId));
202+
// Assertion for initial load from cache
203+
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
204+
// Assertion for load from object cache
205+
$this->assertSame($theme, $this->themeProvider->getThemeById(self::THEME_ID));
226206
}
227207

228208
public function testGetThemeCustomizations()
229209
{
230-
$collectionFactory = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory::class)
231-
->setMethods(['create'])
232-
->disableOriginalConstructor()
233-
->getMock();
234-
$themeFactory = $this->getMockBuilder(\Magento\Theme\Model\ThemeFactory::class)
235-
->disableOriginalConstructor()
236-
->setMethods(['create'])
237-
->getMock();
238210
$collection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\Collection::class)
239211
->disableOriginalConstructor()
240212
->getMock();
241-
242-
$collectionFactory->expects($this->once())
243-
->method('create')
244-
->willReturn($collection);
245213
$collection->expects($this->once())
246214
->method('addAreaFilter')
247215
->with(Area::AREA_FRONTEND)
@@ -250,15 +218,10 @@ public function testGetThemeCustomizations()
250218
->method('addTypeFilter')
251219
->with(ThemeInterface::TYPE_VIRTUAL)
252220
->willReturnSelf();
221+
$this->collectionFactory->expects($this->once())
222+
->method('create')
223+
->willReturn($collection);
253224

254-
$themeProvider = $this->objectManager->getObject(
255-
\Magento\Theme\Model\Theme\ThemeProvider::class,
256-
[
257-
'collectionFactory' => $collectionFactory,
258-
'themeFactory' => $themeFactory
259-
]
260-
);
261-
262-
$this->assertInstanceOf(get_class($collection), $themeProvider->getThemeCustomizations());
225+
$this->assertInstanceOf(get_class($collection), $this->themeProvider->getThemeCustomizations());
263226
}
264227
}

0 commit comments

Comments
 (0)