4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- // @codingStandardsIgnoreFile
8
-
9
7
namespace Magento \Catalog \Test \Unit \Plugin \Block ;
10
8
11
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
12
10
11
+ /**
12
+ * Class TopmenuTest
13
+ *
14
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
+ */
13
16
class TopmenuTest extends \PHPUnit_Framework_TestCase
14
17
{
15
18
/**
@@ -18,96 +21,109 @@ class TopmenuTest extends \PHPUnit_Framework_TestCase
18
21
protected $ block ;
19
22
20
23
/**
21
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Helper\Category
24
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
22
25
*/
23
- protected $ _catalogCategory ;
26
+ protected $ storeManagerMock ;
24
27
25
28
/**
26
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog \Model\Category
29
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store \Model\Store
27
30
*/
28
- protected $ _childrenCategory ;
31
+ protected $ storeMock ;
29
32
30
33
/**
31
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Category
34
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Layer\Resolver
35
+ */
36
+ protected $ layerResolverMock ;
37
+
38
+ /**
39
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Layer
40
+ */
41
+ protected $ catalogLayerMock ;
42
+
43
+ /**
44
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
32
45
*/
33
- protected $ _category ;
46
+ protected $ categoryCollectionFactoryMock ;
34
47
35
48
/**
36
- * @var \PHPUnit_Framework_MockObject_MockObject
49
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\ResourceModel\Category\Collection
37
50
*/
38
- protected $ menuCategoryData ;
51
+ protected $ categoryCollectionMock ;
39
52
40
53
/**
41
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Indexer\Category\Flat\State
54
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Helper\Category
55
+ */
56
+ protected $ categoryHelperMock ;
57
+
58
+ /**
59
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Category
60
+ */
61
+ protected $ childrenCategoryMock ;
62
+
63
+ /**
64
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Category
42
65
*/
43
- protected $ _categoryFlatState ;
66
+ protected $ categoryMock ;
44
67
68
+ /**
69
+ * Set up
70
+ *
71
+ * @return void
72
+ */
45
73
protected function setUp ()
46
74
{
47
- $ this ->_catalogCategory = $ this ->getMock (
48
- \Magento \Catalog \Helper \Category::class,
49
- ['getStoreCategories ' , 'getCategoryUrl ' ],
50
- [],
51
- '' ,
52
- false
75
+ $ rootCategoryId = 2 ;
76
+ $ categoryParentId = 3 ;
77
+ $ categoryParentIds = [1 , 2 , 3 ];
78
+
79
+ $ this ->childrenCategoryMock = $ this ->_getCleanMock (\Magento \Catalog \Model \Category::class);
80
+ $ this ->categoryHelperMock = $ this ->_getCleanMock (\Magento \Catalog \Helper \Category::class);
81
+ $ this ->catalogLayerMock = $ this ->_getCleanMock (\Magento \Catalog \Model \Layer::class);
82
+ $ this ->categoryMock = $ this ->_getCleanMock (\Magento \Catalog \Model \Category::class);
83
+ $ this ->layerResolverMock = $ this ->_getCleanMock (\Magento \Catalog \Model \Layer \Resolver::class);
84
+ $ this ->storeMock = $ this ->_getCleanMock (\Magento \Store \Model \Store::class);
85
+ $ this ->storeManagerMock = $ this ->_getCleanMock (\Magento \Store \Model \StoreManagerInterface::class);
86
+ $ this ->categoryCollectionMock = $ this ->_getCleanMock (
87
+ \Magento \Catalog \Model \ResourceModel \Category \Collection::class
53
88
);
54
-
55
- $ this ->menuCategoryData = $ this ->getMock (
56
- \Magento \Catalog \Observer \MenuCategoryData::class,
57
- ['getMenuCategoryData ' ],
89
+ $ this ->categoryCollectionFactoryMock = $ this ->getMock (
90
+ \Magento \Catalog \Model \ResourceModel \Category \CollectionFactory::class,
91
+ ['create ' ],
58
92
[],
59
93
'' ,
60
94
false
61
95
);
62
96
63
- $ this ->store = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
64
- ->setMethods (['getRootCategoryId ' , 'getFilters ' , '__wakeup ' ])
65
- ->disableOriginalConstructor ()
66
- ->getMockForAbstractClass ();
67
-
68
- $ this ->storeManager = $ this ->getMockBuilder (\Magento \Store \Model \StoreManagerInterface::class)
69
- ->setMethods (['getStore ' ])
70
- ->disableOriginalConstructor ()
71
- ->getMockForAbstractClass ();
72
- $ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )
73
- ->will ($ this ->returnValue ($ this ->store ));
74
-
75
- $ this ->store ->expects ($ this ->any ())->method ('getRootCategoryId ' )
76
- ->will ($ this ->returnValue (1 ));
77
-
78
- $ collectionFactory = $ this ->getMockBuilder (
79
- \Magento \Catalog \Model \ResourceModel \Category \CollectionFactory::class)
80
- ->setMethods (['create ' ])
81
- ->disableOriginalConstructor ()
82
- ->getMock ();
83
-
84
- $ collection = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Category \Collection::class)
85
- ->setMethods (
86
- [
87
- 'addIsActiveFilter ' ,
88
- 'addAttributeToSelect ' ,
89
- 'addFieldToFilter ' ,
90
- 'addAttributeToFilter ' ,
91
- 'addUrlRewriteToResult ' ,
92
- 'getIterator ' ,
93
- 'setStoreId '
94
- ]
95
- )->disableOriginalConstructor ()
96
- ->getMock ();
97
- $ collection ->expects ($ this ->once ())->method ('addIsActiveFilter ' );
98
- $ collectionFactory ->expects ($ this ->once ())->method ('create ' )
99
- ->willReturn ($ collection );
100
-
101
- $ collection ->expects ($ this ->once ())->method ('getIterator ' )
102
- ->willReturn (new \ArrayIterator ([]));
97
+ $ this ->catalogLayerMock ->expects ($ this ->once ())->method ('getCurrentCategory ' )
98
+ ->will ($ this ->returnValue ($ this ->childrenCategoryMock ));
99
+
100
+ $ this ->storeManagerMock ->expects ($ this ->atLeastOnce ())->method ('getStore ' )
101
+ ->will ($ this ->returnValue ($ this ->storeMock ));
102
+
103
+ $ this ->categoryMock ->expects ($ this ->atLeastOnce ())->method ('getParentId ' )
104
+ ->will ($ this ->returnValue ($ categoryParentId ));
105
+ $ this ->categoryMock ->expects ($ this ->once ())->method ('getParentIds ' )
106
+ ->will ($ this ->returnValue ($ categoryParentIds ));
107
+
108
+ $ this ->layerResolverMock ->expects ($ this ->once ())->method ('get ' )
109
+ ->will ($ this ->returnValue ($ this ->catalogLayerMock ));
110
+
111
+ $ this ->storeMock ->expects ($ this ->once ())->method ('getRootCategoryId ' )
112
+ ->will ($ this ->returnValue ($ rootCategoryId ));
113
+
114
+ $ this ->categoryCollectionMock ->expects ($ this ->once ())->method ('getIterator ' )
115
+ ->willReturn (new \ArrayIterator ([$ this ->categoryMock ]));
116
+
117
+ $ this ->categoryCollectionFactoryMock ->expects ($ this ->once ())->method ('create ' )
118
+ ->willReturn ($ this ->categoryCollectionMock );
103
119
104
120
$ this ->block = (new ObjectManager ($ this ))->getObject (
105
121
\Magento \Catalog \Plugin \Block \Topmenu::class,
106
122
[
107
- 'catalogCategory ' => $ this ->_catalogCategory ,
108
- 'menuCategoryData ' => $ this ->menuCategoryData ,
109
- 'storeManager ' => $ this ->storeManager ,
110
- 'categoryCollectionFactory ' => $ collectionFactory ,
123
+ 'catalogCategory ' => $ this ->categoryHelperMock ,
124
+ 'categoryCollectionFactory ' => $ this ->categoryCollectionFactoryMock ,
125
+ 'storeManager ' => $ this ->storeManagerMock ,
126
+ 'layerResolver ' => $ this -> layerResolverMock ,
111
127
]
112
128
);
113
129
}
@@ -123,31 +139,21 @@ protected function _getCleanMock($className)
123
139
return $ this ->getMock ($ className , [], [], '' , false );
124
140
}
125
141
126
- protected function _preparationData ()
142
+ /**
143
+ * Test beforeGetHtml
144
+ *
145
+ */
146
+ public function testBeforeGetHtml ()
127
147
{
128
- $ this ->_childrenCategory = $ this ->getMock (
129
- \Magento \Catalog \Model \Category::class,
130
- ['getIsActive ' , '__wakeup ' ],
131
- [],
132
- '' ,
133
- false
134
- );
135
-
148
+ $ treeMock = $ this ->getMock (\Magento \Framework \Data \Tree::class);
136
149
137
- $ this ->_category = $ this ->getMock (
138
- \Magento \Catalog \Model \Category::class,
139
- ['getIsActive ' , '__wakeup ' , 'getName ' , 'getChildren ' , 'getUseFlatResource ' , 'getChildrenNodes ' ],
140
- [],
141
- '' ,
142
- false
143
- );
150
+ $ parentCategoryNodeMock = $ this ->_getCleanMock (\Magento \Framework \Data \Tree \Node::class);
151
+ $ parentCategoryNodeMock ->expects ($ this ->once ())->method ('getTree ' )->will ($ this ->returnValue ($ treeMock ));
152
+ $ parentCategoryNodeMock ->expects ($ this ->once ())->method ('addChild ' );
144
153
145
154
$ blockMock = $ this ->_getCleanMock (\Magento \Theme \Block \Html \Topmenu::class);
146
- return $ blockMock ;
147
- }
155
+ $ blockMock ->expects ($ this ->once ())->method ('getMenu ' )->will ($ this ->returnValue ($ parentCategoryNodeMock ));
148
156
149
- public function testAddCatalogToTopMenuItems ()
150
- {
151
- $ this ->block ->beforeGetHtml ($ this ->_preparationData ());
157
+ $ this ->block ->beforeGetHtml ($ blockMock );
152
158
}
153
159
}
0 commit comments