7
7
namespace Magento \Framework \View \Test \Unit \Layout \ScheduledStructure ;
8
8
9
9
use Magento \Framework \View \Layout ;
10
+ use Magento \Framework \App \State ;
10
11
11
12
/**
12
13
* Class HelperTest
@@ -22,7 +23,17 @@ class HelperTest extends \PHPUnit_Framework_TestCase
22
23
/**
23
24
* @var \Magento\Framework\View\Layout\Data\Structure|\PHPUnit_Framework_MockObject_MockObject
24
25
*/
25
- protected $ dataStructure ;
26
+ protected $ dataStructureMock ;
27
+
28
+ /**
29
+ * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
30
+ */
31
+ protected $ loggerMock ;
32
+
33
+ /**
34
+ * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
35
+ */
36
+ protected $ stateMock ;
26
37
27
38
/**
28
39
* @var \Magento\Framework\View\Layout\ScheduledStructure\Helper
@@ -37,13 +48,20 @@ public function setUp()
37
48
$ this ->scheduledStructureMock = $ this ->getMockBuilder ('Magento\Framework\View\Layout\ScheduledStructure ' )
38
49
->disableOriginalConstructor ()
39
50
->getMock ();
40
-
41
- $ this ->dataStructure = $ this ->getMockBuilder ('Magento\Framework\View\Layout\Data\Structure ' )
51
+ $ this ->dataStructureMock = $ this ->getMockBuilder ('Magento\Framework\View\Layout\Data\Structure ' )
42
52
->disableOriginalConstructor ()
43
53
->getMock ();
54
+ $ this ->loggerMock = $ this ->getMock ('Psr\Log\LoggerInterface ' );
55
+ $ this ->stateMock = $ this ->getMock ('Magento\Framework\App\State ' , [], [], '' , false );
44
56
45
57
$ helperObjectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
46
- $ this ->helper = $ helperObjectManager ->getObject ('Magento\Framework\View\Layout\ScheduledStructure\Helper ' );
58
+ $ this ->helper = $ helperObjectManager ->getObject (
59
+ 'Magento\Framework\View\Layout\ScheduledStructure\Helper ' ,
60
+ [
61
+ 'logger ' => $ this ->loggerMock ,
62
+ 'state ' => $ this ->stateMock
63
+ ]
64
+ );
47
65
}
48
66
49
67
/**
@@ -122,7 +140,76 @@ public function testScheduleNonExistentElement()
122
140
$ this ->scheduledStructureMock ->expects ($ this ->once ())->method ('unsetPathElement ' )->with ($ key );
123
141
$ this ->scheduledStructureMock ->expects ($ this ->once ())->method ('unsetStructureElement ' )->with ($ key );
124
142
125
- $ this ->helper ->scheduleElement ($ this ->scheduledStructureMock , $ this ->dataStructure , $ key );
143
+ $ this ->helper ->scheduleElement ($ this ->scheduledStructureMock , $ this ->dataStructureMock , $ key );
144
+ }
145
+
146
+ /**
147
+ * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $loggerExpects
148
+ * @param string $stateMode
149
+ * @return void
150
+ * @dataProvider scheduleElementLogDataProvider
151
+ */
152
+ public function testScheduleElementLog ($ loggerExpects , $ stateMode )
153
+ {
154
+ $ key = 'key ' ;
155
+ $ parentName = 'parent ' ;
156
+ $ alias = 'alias ' ;
157
+ $ block = 'block ' ;
158
+ $ siblingName = null ;
159
+ $ isAfter = false ;
160
+
161
+ $ this ->scheduledStructureMock ->expects ($ this ->once ())
162
+ ->method ('getStructureElement ' )
163
+ ->willReturn (
164
+ [
165
+ Layout \ScheduledStructure \Helper::SCHEDULED_STRUCTURE_INDEX_TYPE => $ block ,
166
+ Layout \ScheduledStructure \Helper::SCHEDULED_STRUCTURE_INDEX_ALIAS => $ alias ,
167
+ Layout \ScheduledStructure \Helper::SCHEDULED_STRUCTURE_INDEX_PARENT_NAME => $ parentName ,
168
+ Layout \ScheduledStructure \Helper::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME => $ siblingName ,
169
+ Layout \ScheduledStructure \Helper::SCHEDULED_STRUCTURE_INDEX_IS_AFTER => $ isAfter
170
+ ]
171
+ );
172
+ $ this ->scheduledStructureMock ->expects ($ this ->once ())
173
+ ->method ('hasStructureElement ' )
174
+ ->with ($ parentName )
175
+ ->willReturn (false );
176
+
177
+ $ this ->dataStructureMock ->expects ($ this ->once ())
178
+ ->method ('hasElement ' )
179
+ ->with ($ parentName )
180
+ ->willReturn (false );
181
+
182
+ $ this ->stateMock ->expects ($ this ->once ())
183
+ ->method ('getMode ' )
184
+ ->willReturn ($ stateMode );
185
+
186
+ $ this ->loggerMock ->expects ($ loggerExpects )
187
+ ->method ('critical ' )
188
+ ->with ("Broken reference: the ' {$ key }' element cannot be added as child to ' {$ parentName }', " .
189
+ 'because the latter doesn \'t exist ' );
190
+
191
+ $ this ->helper ->scheduleElement ($ this ->scheduledStructureMock , $ this ->dataStructureMock , $ key );
192
+ }
193
+
194
+ /**
195
+ * @return array
196
+ */
197
+ public function scheduleElementLogDataProvider ()
198
+ {
199
+ return [
200
+ [
201
+ 'loggerExpects ' => $ this ->once (),
202
+ 'stateMode ' => State::MODE_DEVELOPER
203
+ ],
204
+ [
205
+ 'loggerExpects ' => $ this ->never (),
206
+ 'stateMode ' => State::MODE_DEFAULT
207
+ ],
208
+ [
209
+ 'loggerExpects ' => $ this ->never (),
210
+ 'stateMode ' => State::MODE_PRODUCTION
211
+ ]
212
+ ];
126
213
}
127
214
128
215
/**
@@ -171,9 +258,9 @@ public function testScheduleElement($hasParent, $setAsChild, $toRemoveList, $sib
171
258
$ this ->scheduledStructureMock ->expects ($ this ->any ())->method ('hasStructureElement ' )->willReturn (true );
172
259
$ this ->scheduledStructureMock ->expects ($ this ->once ())->method ('setElement ' )->with ($ key , [$ block , $ data ]);
173
260
174
- $ this ->dataStructure ->expects ($ this ->once ())->method ('createElement ' )->with ($ key , ['type ' => $ block ]);
175
- $ this ->dataStructure ->expects ($ this ->once ())->method ('hasElement ' )->with ($ parentName )->willReturn ($ hasParent );
176
- $ this ->dataStructure ->expects ($ this ->exactly ($ setAsChild ))
261
+ $ this ->dataStructureMock ->expects ($ this ->once ())->method ('createElement ' )->with ($ key , ['type ' => $ block ]);
262
+ $ this ->dataStructureMock ->expects ($ this ->once ())->method ('hasElement ' )->with ($ parentName )->willReturn ($ hasParent );
263
+ $ this ->dataStructureMock ->expects ($ this ->exactly ($ setAsChild ))
177
264
->method ('setAsChild ' )
178
265
->with ($ key , $ parentName , $ alias )
179
266
->willReturn (true );
@@ -185,7 +272,7 @@ public function testScheduleElement($hasParent, $setAsChild, $toRemoveList, $sib
185
272
->method ('setElementToSortList ' )
186
273
->with ($ parentName , $ key , $ siblingName , $ isAfter );
187
274
188
- $ this ->helper ->scheduleElement ($ this ->scheduledStructureMock , $ this ->dataStructure , $ key );
275
+ $ this ->helper ->scheduleElement ($ this ->scheduledStructureMock , $ this ->dataStructureMock , $ key );
189
276
}
190
277
191
278
/**
0 commit comments