@@ -35,24 +35,33 @@ public function testGetByFullPath()
35
35
false
36
36
);
37
37
$ collectionMock = $ this ->getMock (\Magento \Theme \Model \ResourceModel \Theme \Collection::class, [], [], '' , false );
38
- $ theme = $ this ->getMock (\Magento \Framework \View \Design \ThemeInterface::class, [], [], '' , false );
39
- $ collectionMock ->expects (
40
- $ this ->once ()
41
- )->method (
42
- 'getThemeByFullPath '
43
- )->with (
44
- $ path
45
- )->will (
46
- $ this ->returnValue ($ theme )
47
- );
38
+
39
+ $ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
40
+ $ theme ->expects ($ this ->exactly (2 ))
41
+ ->method ('getId ' )
42
+ ->willReturn (1 );
43
+ $ theme ->expects ($ this ->exactly (2 ))
44
+ ->method ('toArray ' )
45
+ ->willReturn (['theme_data ' => 'theme_data ' ]);
46
+
47
+ $ collectionMock ->expects ($ this ->once ())
48
+ ->method ('getThemeByFullPath ' )
49
+ ->with ($ path )
50
+ ->willReturn ($ theme );
48
51
$ collectionFactory ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ collectionMock ));
49
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 ))
55
+ ->method ('serialize ' )
56
+ ->with (['theme_data ' => 'theme_data ' ])
57
+ ->willReturn ('{"theme_data":"theme_data"} ' );
50
58
51
59
$ themeProvider = $ this ->objectManager ->getObject (
52
60
\Magento \Theme \Model \Theme \ThemeProvider::class,
53
61
[
54
62
'collectionFactory ' => $ collectionFactory ,
55
- 'themeFactory ' => $ themeFactory
63
+ 'themeFactory ' => $ themeFactory ,
64
+ 'serializer ' => $ serializerMock
56
65
]
57
66
);
58
67
@@ -71,23 +80,77 @@ public function testGetByFullPath()
71
80
]);
72
81
\Magento \Framework \App \ObjectManager::setInstance ($ objectManagerMock );
73
82
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 ));
87
+ }
88
+
89
+ public function testGetByFullPathWithCache ()
90
+ {
91
+ $ path = 'frontend/Magento/luma ' ;
92
+ $ deploymentConfig = $ this ->getMockBuilder (\Magento \Framework \App \DeploymentConfig::class)
93
+ ->disableOriginalConstructor ()
94
+ ->getMock ();
95
+ $ deploymentConfig ->expects ($ this ->once ())
96
+ ->method ('isDbAvailable ' )
97
+ ->willReturn (true );
98
+
99
+ $ objectManagerMock = $ this ->getMock (\Magento \Framework \ObjectManagerInterface::class);
100
+ $ objectManagerMock ->expects ($ this ->any ())
101
+ ->method ('get ' )
102
+ ->willReturnMap ([
103
+ [\Magento \Framework \App \DeploymentConfig::class, $ deploymentConfig ],
104
+ ]);
105
+ \Magento \Framework \App \ObjectManager::setInstance ($ objectManagerMock );
106
+
107
+ $ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
108
+ $ theme ->expects ($ this ->once ())
109
+ ->method ('populateFromArray ' )
110
+ ->willReturnSelf ();
111
+ $ themeFactory = $ this ->getMock (\Magento \Theme \Model \ThemeFactory::class, [], [], '' , false );
112
+ $ themeFactory ->expects ($ this ->once ())
113
+ ->method ('create ' )
114
+ ->willReturn ($ theme );
115
+
116
+ $ serializerMock = $ this ->getMock (\Magento \Framework \Serialize \Serializer \Json::class);
117
+ $ serializerMock ->expects ($ this ->once ())
118
+ ->method ('unserialize ' )
119
+ ->with ('{"theme_data":"theme_data"} ' )
120
+ ->willReturn (['theme_data ' => 'theme_data ' ]);
121
+
122
+ $ cacheMock = $ this ->getMockBuilder (\Magento \Framework \App \CacheInterface::class)
123
+ ->disableOriginalConstructor ()
124
+ ->getMock ();
125
+ $ cacheMock ->expects ($ this ->once ())
126
+ ->method ('load ' )
127
+ ->with ('theme ' . $ path )
128
+ ->willReturn ('{"theme_data":"theme_data"} ' );
129
+
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
74
142
$ this ->assertSame ($ theme , $ themeProvider ->getThemeByFullPath ($ path ));
75
143
}
76
144
77
145
public function testGetById ()
78
146
{
79
147
$ themeId = 755 ;
80
- $ collectionFactory = $ this ->getMock (
81
- \Magento \Theme \Model \ResourceModel \Theme \CollectionFactory::class,
82
- [],
83
- [],
84
- '' ,
85
- false
86
- );
87
148
$ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
88
149
$ theme ->expects ($ this ->once ())->method ('load ' )->with ($ themeId )->will ($ this ->returnSelf ());
89
150
$ theme ->expects ($ this ->once ())->method ('getId ' )->will ($ this ->returnValue (1 ));
90
- $ theme ->expects ($ this ->never ())->method ('__sleep ' );
151
+ $ theme ->expects ($ this ->once ())
152
+ ->method ('toArray ' )
153
+ ->willReturn (['theme_data ' => 'theme_data ' ]);
91
154
92
155
$ themeFactory = $ this ->getMock (\Magento \Theme \Model \ThemeFactory::class, ['create ' ], [], '' , false );
93
156
$ themeFactory ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ theme ));
@@ -100,15 +163,65 @@ public function testGetById()
100
163
->with ('theme-by-id- ' . $ themeId )
101
164
->willReturn (false );
102
165
166
+ $ serializerMock = $ this ->getMock (\Magento \Framework \Serialize \Serializer \Json::class);
167
+ $ serializerMock ->expects ($ this ->once ())
168
+ ->method ('serialize ' )
169
+ ->with (['theme_data ' => 'theme_data ' ])
170
+ ->willReturn ('{"theme_data":"theme_data"} ' );
171
+
103
172
$ themeProvider = $ this ->objectManager ->getObject (
104
173
\Magento \Theme \Model \Theme \ThemeProvider::class,
105
174
[
106
- 'collectionFactory ' => $ collectionFactory ,
107
175
'themeFactory ' => $ themeFactory ,
108
- 'cache ' => $ cacheMock
176
+ 'cache ' => $ cacheMock ,
177
+ 'serializer ' => $ serializerMock
109
178
]
110
179
);
111
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 ));
185
+ }
186
+
187
+ public function testGetByIdWithCache ()
188
+ {
189
+ $ themeId = 755 ;
190
+ $ theme = $ this ->getMock (\Magento \Theme \Model \Theme::class, [], [], '' , false );
191
+ $ theme ->expects ($ this ->once ())
192
+ ->method ('populateFromArray ' )
193
+ ->with (['theme_data ' => 'theme_data ' ])
194
+ ->willReturnSelf ();
195
+ $ cacheMock = $ this ->getMockBuilder (\Magento \Framework \App \CacheInterface::class)
196
+ ->disableOriginalConstructor ()
197
+ ->getMock ();
198
+ $ cacheMock ->expects ($ this ->once ())
199
+ ->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 ())
204
+ ->method ('unserialize ' )
205
+ ->with ('{"theme_data":"theme_data"} ' )
206
+ ->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 ())
210
+ ->method ('create ' )
211
+ ->willReturn ($ theme );
212
+
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
112
225
$ this ->assertSame ($ theme , $ themeProvider ->getThemeById ($ themeId ));
113
226
}
114
227
0 commit comments