@@ -30,9 +30,14 @@ class TreeTest extends \PHPUnit_Framework_TestCase
30
30
*/
31
31
protected $ _collectionFactory ;
32
32
33
+ /**
34
+ * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
35
+ */
36
+ protected $ _objectHelper ;
37
+
33
38
protected function setUp ()
34
39
{
35
- $ objectHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
40
+ $ this -> _objectHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
36
41
$ select = $ this ->getMock ('Zend_Db_Select ' , [], [], '' , false );
37
42
$ select ->expects ($ this ->once ())->method ('from ' )->with ('catalog_category_entity ' );
38
43
$ connection = $ this ->getMock ('Magento\Framework\DB\Adapter\AdapterInterface ' );
@@ -71,7 +76,7 @@ protected function setUp()
71
76
'' ,
72
77
false
73
78
);
74
- $ this ->_model = $ objectHelper ->getObject (
79
+ $ this ->_model = $ this -> _objectHelper ->getObject (
75
80
'Magento\Catalog\Model\Resource\Category\Tree ' ,
76
81
[
77
82
'resource ' => $ this ->_resource ,
@@ -128,9 +133,8 @@ public function testCallCleaningDuringSetCollection()
128
133
$ this ->assertEquals ($ model , $ model ->setCollection ($ this ->getCollectionMock ()));
129
134
}
130
135
131
- public function testAddCollectionData ()
136
+ protected function getConfiguredResource ()
132
137
{
133
- $ objectHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
134
138
$ select = $ this ->getMock ('Zend_Db_Select ' , [], [], '' , false );
135
139
$ select ->expects ($ this ->any ())->method ('from ' )->will ($ this ->returnSelf ());
136
140
$ select ->expects ($ this ->any ())->method ('join ' )->will ($ this ->returnSelf ());
@@ -145,26 +149,11 @@ public function testAddCollectionData()
145
149
$ resource ->expects ($ this ->any ())->method ('getConnection ' )->will ($ this ->returnValue ($ connection ));
146
150
$ resource ->expects ($ this ->any ())->method ('getTableName ' )->will ($ this ->returnArgument (0 ));
147
151
148
- $ eventManager = $ this ->getMock ('Magento\Framework\Event\ManagerInterface ' , [], [], '' , false );
149
- $ attributeConfig = $ this ->getMock (
150
- 'Magento\Catalog\Model\Attribute\Config ' ,
151
- [],
152
- [],
153
- '' ,
154
- false
155
- );
156
-
157
- $ attributes = ['attribute_one ' , 'attribute_two ' ];
158
- $ attributeConfig ->expects (
159
- $ this ->once ()
160
- )->method (
161
- 'getAttributeNames '
162
- )->with (
163
- 'catalog_category '
164
- )->will (
165
- $ this ->returnValue ($ attributes )
166
- );
152
+ return $ resource ;
153
+ }
167
154
155
+ protected function getConfiguredCollectionFactory ()
156
+ {
168
157
$ collection = $ this ->getMock ('Magento\Catalog\Model\Resource\Category\Collection ' , [], [], '' , false );
169
158
$ collection ->expects ($ this ->never ())->method ('getAllIds ' )->will ($ this ->returnValue ([]));
170
159
$ collectionFactory = $ this ->getMock (
@@ -176,44 +165,101 @@ public function testAddCollectionData()
176
165
);
177
166
$ collectionFactory ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ collection ));
178
167
168
+ return $ collectionFactory ;
169
+ }
170
+
171
+ protected function getConfiguredStoreManager ()
172
+ {
179
173
$ store = $ this ->getMock ('Magento\Store\Model\Store ' , [], [], '' , false );
180
174
$ store ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (1 ));
181
175
182
176
$ storeManager = $ this ->getMockForAbstractClass ('Magento\Store\Model\StoreManagerInterface ' );
183
177
$ storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ store ));
184
178
179
+ return $ storeManager ;
180
+ }
181
+
182
+ protected function getConfiguredAttributeConfig ()
183
+ {
184
+ $ attributeConfig = $ this ->getMock (
185
+ 'Magento\Catalog\Model\Attribute\Config ' ,
186
+ [],
187
+ [],
188
+ '' ,
189
+ false
190
+ );
191
+
192
+ $ attributes = ['attribute_one ' , 'attribute_two ' ];
193
+ $ attributeConfig
194
+ ->expects ($ this ->once ())
195
+ ->method ('getAttributeNames ' )
196
+ ->with ('catalog_category ' )
197
+ ->willReturn ($ attributes );
198
+
199
+ return $ attributeConfig ;
200
+ }
201
+
202
+ public function testAddCollectionData ()
203
+ {
204
+ $ eventManager = $ this ->getMock ('Magento\Framework\Event\ManagerInterface ' , [], [], '' , false );
205
+
206
+ $ model = $ this ->_objectHelper ->getObject (
207
+ 'Magento\Catalog\Model\Resource\Category\Tree ' ,
208
+ [
209
+ 'storeManager ' => $ this ->getConfiguredStoreManager (),
210
+ 'resource ' => $ this ->getConfiguredResource (),
211
+ 'eventManager ' => $ eventManager ,
212
+ 'attributeConfig ' => $ this ->getConfiguredAttributeConfig (),
213
+ 'collectionFactory ' => $ this ->getConfiguredCollectionFactory ()
214
+ ]
215
+ );
216
+
217
+ $ nodeMock = $ this ->getMock ('\Magento\Framework\Data\Tree\Node ' , ['getId ' , 'getPath ' ], [], '' , false );
218
+ $ nodeMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (1 ));
219
+ $ nodeMock ->expects ($ this ->once ())->method ('getPath ' )->will ($ this ->returnValue ([]));
220
+
221
+ $ model ->addNode ($ nodeMock );
222
+
223
+ $ this ->assertSame ($ model , $ model ->addCollectionData (null , false , [], false , true ));
224
+ }
225
+
226
+ public function testAddCollectionDataWhenThereIsCurrentCategoryInRegistry ()
227
+ {
228
+ $ eventManager = $ this ->getMock ('Magento\Framework\Event\ManagerInterface ' , [], [], '' , false );
229
+
185
230
$ category = $ this ->getMock ('Magento\Catalog\Model\Category ' , [], [], '' , false );
186
231
$ category
187
- ->expects ($ this ->exactly ( 2 ))
232
+ ->expects ($ this ->atLeastOnce ( ))
188
233
->method ('getId ' )
189
234
->willReturn (12 );
190
- $ category
191
- ->expects ($ this ->once ())
192
- ->method ('__call ' )
193
- ->with ('setIsCurrentCategory ' , [true ]);
235
+
194
236
$ registry = $ this ->getMock ('Magento\Framework\Registry ' , [], [], '' , false );
195
237
$ registry
196
238
->expects ($ this ->once ())
197
239
->method ('registry ' )
198
240
->with ('current_category ' )
199
241
->willReturn ($ category );
200
242
201
- $ model = $ objectHelper ->getObject (
243
+ $ model = $ this -> _objectHelper ->getObject (
202
244
'Magento\Catalog\Model\Resource\Category\Tree ' ,
203
245
[
204
- 'storeManager ' => $ storeManager ,
205
- 'resource ' => $ resource ,
246
+ 'storeManager ' => $ this -> getConfiguredStoreManager () ,
247
+ 'resource ' => $ this -> getConfiguredResource () ,
206
248
'eventManager ' => $ eventManager ,
207
- 'attributeConfig ' => $ attributeConfig ,
208
- 'collectionFactory ' => $ collectionFactory ,
249
+ 'attributeConfig ' => $ this -> getConfiguredAttributeConfig () ,
250
+ 'collectionFactory ' => $ this -> getConfiguredCollectionFactory () ,
209
251
'registry ' => $ registry
210
252
]
211
253
);
212
254
213
- $ nodeMock = $ this ->getMock ('\Magento\Framework\Data\Tree\Node ' , ['getId ' , 'getPath ' ], [], '' , false );
214
- $ nodeMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (1 ));
255
+ $ nodeMock = $ this ->getMock ('\Magento\Framework\Data\Tree\Node ' , ['getId ' , 'getPath ' , ' __call ' ], [], '' , false );
256
+ $ nodeMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue (12 ));
215
257
$ nodeMock ->expects ($ this ->once ())->method ('getPath ' )->will ($ this ->returnValue ([]));
216
- $ nodeMock ->setData ('id ' , 12 );
258
+ $ nodeMock
259
+ ->expects ($ this ->once ())
260
+ ->method ('__call ' )
261
+ ->with ('setIsCurrentItem ' , [true ])
262
+ ->willReturn (true );
217
263
218
264
$ model ->addNode ($ nodeMock );
219
265
0 commit comments