Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
CategoryTest Class Reference
Inheritance diagram for CategoryTest:

Public Member Functions

 testApplyWithEmptyRequest ($requestValue, $idValue)
 
 applyWithEmptyRequestDataProvider ()
 
 testApply ()
 
 testGetItems ()
 

Protected Member Functions

 setUp ()
 

Detailed Description

Test for \Magento\CatalogSearch\Model\Layer\Filter\Category

Definition at line 15 of file CategoryTest.php.

Member Function Documentation

◆ applyWithEmptyRequestDataProvider()

applyWithEmptyRequestDataProvider ( )
Returns
array

Definition at line 177 of file CategoryTest.php.

178  {
179  return [
180  [
181  'requestValue' => null,
182  'id' => 0,
183  ],
184  [
185  'requestValue' => 0,
186  'id' => false,
187  ],
188  [
189  'requestValue' => 0,
190  'id' => null,
191  ]
192  ];
193  }

◆ setUp()

setUp ( )
protected

Definition at line 50 of file CategoryTest.php.

51  {
52  $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
53  ->disableOriginalConstructor()
54  ->setMethods(['getParam'])
55  ->getMockForAbstractClass();
56 
57  $dataProviderFactory = $this->getMockBuilder(
58  \Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory::class
59  )->disableOriginalConstructor()->setMethods(['create'])->getMock();
60 
61  $this->dataProvider = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\DataProvider\Category::class)
62  ->disableOriginalConstructor()
63  ->setMethods(['setCategoryId', 'getCategory'])
64  ->getMock();
65 
66  $dataProviderFactory->expects($this->once())
67  ->method('create')
68  ->will($this->returnValue($this->dataProvider));
69 
70  $this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
71  ->disableOriginalConstructor()
72  ->setMethods(['getId', 'getChildrenCategories', 'getIsActive'])
73  ->getMock();
74 
75  $this->dataProvider->expects($this->any())
76  ->method('getCategory', 'isValid')
77  ->will($this->returnValue($this->category));
78 
79  $this->layer = $this->getMockBuilder(\Magento\Catalog\Model\Layer::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['getState', 'getProductCollection'])
82  ->getMock();
83 
84  $this->fulltextCollection = $this->fulltextCollection = $this->getMockBuilder(
85  \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::class
86  )
87  ->disableOriginalConstructor()
88  ->setMethods(['addCategoryFilter', 'getFacetedData', 'getSize'])
89  ->getMock();
90 
91  $this->layer->expects($this->any())
92  ->method('getProductCollection')
93  ->will($this->returnValue($this->fulltextCollection));
94 
95  $this->itemDataBuilder = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder::class)
96  ->disableOriginalConstructor()
97  ->setMethods(['addItemData', 'build'])
98  ->getMock();
99 
100  $this->filterItemFactory = $this->getMockBuilder(
101  \Magento\Catalog\Model\Layer\Filter\ItemFactory::class
102  )
103  ->disableOriginalConstructor()
104  ->setMethods(['create'])
105  ->getMock();
106 
107  $filterItem = $this->getMockBuilder(
108  \Magento\Catalog\Model\Layer\Filter\Item::class
109  )
110  ->disableOriginalConstructor()
111  ->setMethods(['setFilter', 'setLabel', 'setValue', 'setCount'])
112  ->getMock();
113  $filterItem->expects($this->any())
114  ->method($this->anything())
115  ->will($this->returnSelf());
116  $this->filterItemFactory->expects($this->any())
117  ->method('create')
118  ->will($this->returnValue($filterItem));
119 
120  $escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
121  ->disableOriginalConstructor()
122  ->setMethods(['escapeHtml'])
123  ->getMock();
124  $escaper->expects($this->any())
125  ->method('escapeHtml')
126  ->will($this->returnArgument(0));
127 
128  $objectManagerHelper = new ObjectManagerHelper($this);
129  $this->target = $objectManagerHelper->getObject(
130  \Magento\CatalogSearch\Model\Layer\Filter\Category::class,
131  [
132  'categoryDataProviderFactory' => $dataProviderFactory,
133  'layer' => $this->layer,
134  'itemDataBuilder' => $this->itemDataBuilder,
135  'filterItemFactory' => $this->filterItemFactory,
136  'escaper' => $escaper,
137  ]
138  );
139  }

◆ testApply()

testApply ( )

Definition at line 195 of file CategoryTest.php.

196  {
197  $categoryId = 123;
198  $requestVar = 'test_request_var';
199 
200  $this->target->setRequestVar($requestVar);
201  $this->request->expects($this->exactly(2))
202  ->method('getParam')
203  ->will(
204  $this->returnCallback(
205  function ($field) use ($requestVar, $categoryId) {
206  $this->assertTrue(in_array($field, [$requestVar, 'id']));
207  return $categoryId;
208  }
209  )
210  );
211 
212  $this->dataProvider->expects($this->once())
213  ->method('setCategoryId')
214  ->with($categoryId)
215  ->will($this->returnSelf());
216 
217  $this->category->expects($this->once())
218  ->method('getId')
219  ->will($this->returnValue($categoryId));
220 
221  $this->fulltextCollection->expects($this->once())
222  ->method('addCategoryFilter')
223  ->with($this->category)
224  ->will($this->returnSelf());
225 
226  $this->target->apply($this->request);
227  }

◆ testApplyWithEmptyRequest()

testApplyWithEmptyRequest (   $requestValue,
  $idValue 
)
Parameters
$requestValue
$idValue
$isIdUsed@dataProvider applyWithEmptyRequestDataProvider

Definition at line 147 of file CategoryTest.php.

148  {
149  $requestField = 'test_request_var';
150  $idField = 'id';
151 
152  $this->target->setRequestVar($requestField);
153 
154  $this->request->expects($this->at(0))
155  ->method('getParam')
156  ->with($requestField)
157  ->will(
158  $this->returnCallback(
159  function ($field) use ($requestField, $idField, $requestValue, $idValue) {
160  switch ($field) {
161  case $requestField:
162  return $requestValue;
163  case $idField:
164  return $idValue;
165  }
166  }
167  )
168  );
169 
170  $result = $this->target->apply($this->request);
171  $this->assertSame($this->target, $result);
172  }

◆ testGetItems()

testGetItems ( )

@SuppressWarnings(PHPMD.ExcessiveMethodLength)

@magentoDataFixture Magento/Catalog/_files/categories.php @magentoAppIsolation enabled @magentoDbIsolation disabled

@magentoDbIsolation disabled

Definition at line 232 of file CategoryTest.php.

233  {
234  $this->category->expects($this->any())
235  ->method('getIsActive')
236  ->will($this->returnValue(true));
237 
238  $category1 = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
239  ->disableOriginalConstructor()
240  ->setMethods(['getId', 'getName', 'getIsActive'])
241  ->getMock();
242  $category1->expects($this->atLeastOnce())
243  ->method('getId')
244  ->will($this->returnValue(120));
245  $category1->expects($this->once())
246  ->method('getName')
247  ->will($this->returnValue('Category 1'));
248  $category1->expects($this->once())
249  ->method('getIsActive')
250  ->will($this->returnValue(true));
251 
252  $category2 = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
253  ->disableOriginalConstructor()
254  ->setMethods(['getId', 'getName', 'getIsActive'])
255  ->getMock();
256  $category2->expects($this->atLeastOnce())
257  ->method('getId')
258  ->will($this->returnValue(5641));
259  $category2->expects($this->once())
260  ->method('getName')
261  ->will($this->returnValue('Category 2'));
262  $category2->expects($this->once())
263  ->method('getIsActive')
264  ->will($this->returnValue(true));
265 
266  $category3 = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
267  ->disableOriginalConstructor()
268  ->setMethods(['getId', 'getName', 'getIsActive'])
269  ->getMock();
270  $category3->expects($this->atLeastOnce())
271  ->method('getId')
272  ->will($this->returnValue(777));
273  $category3->expects($this->never())
274  ->method('getName');
275  $category3->expects($this->once())
276  ->method('getIsActive')
277  ->will($this->returnValue(true));
278 
279  $categories = [
280  $category1,
281  $category2,
282  $category3,
283  ];
284  $this->category->expects($this->once())
285  ->method('getChildrenCategories')
286  ->will($this->returnValue($categories));
287 
288  $facetedData = [
289  120 => ['count' => 10],
290  5641 => ['count' => 45],
291  777 => ['count' => 80],
292  ];
293 
294  $this->fulltextCollection->expects($this->once())
295  ->method('getSize')
296  ->will($this->returnValue(50));
297 
298  $this->fulltextCollection->expects($this->once())
299  ->method('getFacetedData')
300  ->with('category')
301  ->will($this->returnValue($facetedData));
302 
303  $builtData = [
304  [
305  'label' => 'Category 1',
306  'value' => 120,
307  'count' => 10,
308  ],
309  [
310  'label' => 'Category 2',
311  'value' => 5641,
312  'count' => 45,
313  ],
314  ];
315 
316  $this->itemDataBuilder->expects($this->at(0))
317  ->method('addItemData')
318  ->with(
319  'Category 1',
320  120,
321  10
322  )
323  ->will($this->returnSelf());
324  $this->itemDataBuilder->expects($this->at(1))
325  ->method('addItemData')
326  ->with(
327  'Category 2',
328  5641,
329  45
330  )
331  ->will($this->returnSelf());
332  $this->itemDataBuilder->expects($this->once())
333  ->method('build')
334  ->will($this->returnValue($builtData));
335 
336  $this->target->getItems();
337  }
$category2
$category3
$category1
$categories

The documentation for this class was generated from the following file: