16
16
*/
17
17
class ThemeProviderTest extends \PHPUnit_Framework_TestCase
18
18
{
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
+
19
25
/** @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 ;
21
42
22
43
protected function setUp ()
23
44
{
24
45
$ 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 (
31
47
\Magento \Theme \Model \ResourceModel \Theme \CollectionFactory::class,
32
- [' create ' ],
48
+ [],
33
49
[],
34
50
'' ,
35
51
false
36
52
);
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
+ }
38
68
69
+ public function testGetByFullPath ()
70
+ {
71
+ $ themeArray = ['theme_data ' => 'theme_data ' ];
39
72
$ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
40
73
$ theme ->expects ($ this ->exactly (2 ))
41
74
->method ('getId ' )
42
- ->willReturn (1 );
75
+ ->willReturn (self :: THEME_ID );
43
76
$ theme ->expects ($ this ->exactly (2 ))
44
77
->method ('toArray ' )
45
- ->willReturn ([ ' theme_data ' => ' theme_data ' ] );
78
+ ->willReturn ($ themeArray );
46
79
80
+ $ collectionMock = $ this ->getMock (\Magento \Theme \Model \ResourceModel \Theme \Collection::class, [], [], '' , false );
47
81
$ collectionMock ->expects ($ this ->once ())
48
82
->method ('getThemeByFullPath ' )
49
- ->with ($ path )
83
+ ->with (self :: THEME_PATH )
50
84
->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 ))
55
89
->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 ' );
67
92
68
93
$ deploymentConfig = $ this ->getMockBuilder (\Magento \Framework \App \DeploymentConfig::class)
69
94
->disableOriginalConstructor ()
@@ -80,15 +105,14 @@ public function testGetByFullPath()
80
105
]);
81
106
\Magento \Framework \App \ObjectManager::setInstance ($ objectManagerMock );
82
107
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 ));
87
112
}
88
113
89
114
public function testGetByFullPathWithCache ()
90
115
{
91
- $ path = 'frontend/Magento/luma ' ;
92
116
$ deploymentConfig = $ this ->getMockBuilder (\Magento \Framework \App \DeploymentConfig::class)
93
117
->disableOriginalConstructor ()
94
118
->getMock ();
@@ -104,144 +128,88 @@ public function testGetByFullPathWithCache()
104
128
]);
105
129
\Magento \Framework \App \ObjectManager::setInstance ($ objectManagerMock );
106
130
131
+ $ serializedTheme = '{"theme_data":"theme_data"} ' ;
107
132
$ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
108
133
$ theme ->expects ($ this ->once ())
109
134
->method ('populateFromArray ' )
110
135
->willReturnSelf ();
111
- $ themeFactory = $ this ->getMock (\Magento \Theme \Model \ThemeFactory::class, [], [], '' , false );
112
- $ themeFactory ->expects ($ this ->once ())
136
+ $ this ->themeFactory ->expects ($ this ->once ())
113
137
->method ('create ' )
114
138
->willReturn ($ theme );
115
139
116
- $ serializerMock = $ this ->getMock (\Magento \Framework \Serialize \Serializer \Json::class);
117
- $ serializerMock ->expects ($ this ->once ())
140
+ $ this ->serializer ->expects ($ this ->once ())
118
141
->method ('unserialize ' )
119
- ->with (' {"theme_data":"theme_data"} ' )
142
+ ->with ($ serializedTheme )
120
143
->willReturn (['theme_data ' => 'theme_data ' ]);
121
144
122
- $ cacheMock = $ this ->getMockBuilder (\Magento \Framework \App \CacheInterface::class)
123
- ->disableOriginalConstructor ()
124
- ->getMock ();
125
- $ cacheMock ->expects ($ this ->once ())
145
+ $ this ->cache ->expects ($ this ->once ())
126
146
->method ('load ' )
127
- ->with ('theme ' . $ path )
128
- ->willReturn (' {"theme_data":"theme_data"} ' );
147
+ ->with ('theme ' . self :: THEME_PATH )
148
+ ->willReturn ($ serializedTheme );
129
149
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 ));
143
154
}
144
155
145
156
public function testGetById ()
146
157
{
147
- $ themeId = 755 ;
158
+ $ themeArray = [ ' theme_data ' => ' theme_data ' ] ;
148
159
$ 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 ));
151
162
$ theme ->expects ($ this ->once ())
152
163
->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 );
157
165
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 ())
162
168
->method ('load ' )
163
- ->with ('theme-by-id- ' . $ themeId )
169
+ ->with ('theme-by-id- ' . self :: THEME_ID )
164
170
->willReturn (false );
165
-
166
- $ serializerMock = $ this ->getMock (\Magento \Framework \Serialize \Serializer \Json::class);
167
- $ serializerMock ->expects ($ this ->once ())
171
+ $ this ->serializer ->expects ($ this ->once ())
168
172
->method ('serialize ' )
169
- ->with ([ ' theme_data ' => ' theme_data ' ] )
173
+ ->with ($ themeArray )
170
174
->willReturn ('{"theme_data":"theme_data"} ' );
171
175
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 ));
185
180
}
186
181
187
182
public function testGetByIdWithCache ()
188
183
{
189
- $ themeId = 755 ;
184
+ $ serializedTheme = ' {"theme_data":"theme_data"} ' ;
190
185
$ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
191
186
$ theme ->expects ($ this ->once ())
192
187
->method ('populateFromArray ' )
193
188
->with (['theme_data ' => 'theme_data ' ])
194
189
->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 ())
199
191
->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 ())
204
195
->method ('unserialize ' )
205
- ->with (' {"theme_data":"theme_data"} ' )
196
+ ->with ($ serializedTheme )
206
197
->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 ())
210
199
->method ('create ' )
211
200
->willReturn ($ theme );
212
201
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 ));
226
206
}
227
207
228
208
public function testGetThemeCustomizations ()
229
209
{
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 ();
238
210
$ collection = $ this ->getMockBuilder (\Magento \Theme \Model \ResourceModel \Theme \Collection::class)
239
211
->disableOriginalConstructor ()
240
212
->getMock ();
241
-
242
- $ collectionFactory ->expects ($ this ->once ())
243
- ->method ('create ' )
244
- ->willReturn ($ collection );
245
213
$ collection ->expects ($ this ->once ())
246
214
->method ('addAreaFilter ' )
247
215
->with (Area::AREA_FRONTEND )
@@ -250,15 +218,10 @@ public function testGetThemeCustomizations()
250
218
->method ('addTypeFilter ' )
251
219
->with (ThemeInterface::TYPE_VIRTUAL )
252
220
->willReturnSelf ();
221
+ $ this ->collectionFactory ->expects ($ this ->once ())
222
+ ->method ('create ' )
223
+ ->willReturn ($ collection );
253
224
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 ());
263
226
}
264
227
}
0 commit comments