Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayerTest.php
Go to the documentation of this file.
1 <?php
7 
10 
15 class LayerTest extends \PHPUnit\Framework\TestCase
16 {
20  private $model;
21 
25  private $category;
26 
30  private $registry;
31 
35  private $storeManager;
36 
40  private $store;
41 
45  private $context;
46 
50  private $stateKeyGenerator;
51 
55  private $stateFactory;
56 
60  private $state;
61 
65  private $collectionFilter;
66 
70  private $collection;
71 
75  private $collectionProvider;
76 
80  private $filter;
81 
85  private $abstractFilter;
86 
90  private $categoryRepository;
91 
95  private $currentCategory;
96 
97  protected function setUp()
98  {
99  $helper = new ObjectManager($this);
100 
101  $this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
102  ->setMethods(['getId', '__wakeup'])
103  ->disableOriginalConstructor()
104  ->getMock();
105 
106  $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
107  ->setMethods(['registry'])
108  ->disableOriginalConstructor()
109  ->getMock();
110 
111  $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
112  ->setMethods(['getRootCategoryId', 'getFilters', '__wakeup'])
113  ->disableOriginalConstructor()
114  ->getMockForAbstractClass();
115 
116  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
117  ->setMethods(['getStore'])
118  ->disableOriginalConstructor()
119  ->getMockForAbstractClass();
120  $this->storeManager->expects($this->any())->method('getStore')
121  ->will($this->returnValue($this->store));
122 
123  $this->stateKeyGenerator = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Category\StateKey::class)
124  ->setMethods(['toString'])
125  ->disableOriginalConstructor()
126  ->getMock();
127 
128  $this->collectionFilter = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Category\CollectionFilter::class)
129  ->setMethods(['filter'])
130  ->disableOriginalConstructor()
131  ->getMock();
132 
133  $this->collectionProvider = $this->getMockBuilder(
134  \Magento\Catalog\Model\Layer\ItemCollectionProviderInterface::class
135  )->disableOriginalConstructor()->getMockForAbstractClass();
136 
137  $this->filter = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\Item::class)
138  ->setMethods(['getFilter', 'getValueString'])
139  ->disableOriginalConstructor()
140  ->getMock();
141 
142  $this->abstractFilter = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\AbstractFilter::class)
143  ->setMethods(['getRequestVar'])
144  ->disableOriginalConstructor()
145  ->getMock();
146 
147  $this->context = $this->getMockBuilder(\Magento\Catalog\Model\Layer\ContextInterface::class)
148  ->setMethods(['getStateKey', 'getCollectionFilter'])
149  ->disableOriginalConstructor()
150  ->getMockForAbstractClass();
151  $this->context->expects($this->any())->method('getStateKey')
152  ->will($this->returnValue($this->stateKeyGenerator));
153  $this->context->expects($this->any())->method('getCollectionFilter')
154  ->will($this->returnValue($this->collectionFilter));
155  $this->context->expects($this->any())->method('getCollectionProvider')
156  ->will($this->returnValue($this->collectionProvider));
157 
158  $this->state = $this->getMockBuilder(\Magento\Catalog\Model\Layer\State::class)
159  ->disableOriginalConstructor()
160  ->getMock();
161 
162  $this->stateFactory = $this->getMockBuilder(\Magento\Catalog\Model\Layer\StateFactory::class)
163  ->setMethods(['create'])
164  ->disableOriginalConstructor()
165  ->getMock();
166  $this->stateFactory->expects($this->any())->method('create')->will($this->returnValue($this->state));
167 
168  $this->collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
169  ->disableOriginalConstructor()
170  ->getMock();
171 
172  $this->categoryRepository = $this->createMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
173  $this->currentCategory = $this->createPartialMock(
174  \Magento\Catalog\Model\Category::class,
175  ['getId', '__wakeup']
176  );
177 
178  $this->model = $helper->getObject(
179  \Magento\Catalog\Model\Layer::class,
180  [
181  'registry' => $this->registry,
182  'storeManager' => $this->storeManager,
183  'context' => $this->context,
184  'layerStateFactory' => $this->stateFactory,
185  'categoryRepository' => $this->categoryRepository,
186  ]
187  );
188  }
189 
190  public function testGetState()
191  {
192  $this->assertInstanceOf(\Magento\Catalog\Model\Layer\State::class, $this->model->getState());
193  }
194 
195  public function testGetStateKey()
196  {
197  $stateKey = 'sk';
198  $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
199  ->will($this->returnValue($this->category));
200 
201  $this->stateKeyGenerator->expects($this->once())->method('toString')
202  ->with($this->equalTo($this->category))
203  ->will($this->returnValue($stateKey));
204 
205  $this->assertEquals($stateKey, $this->model->getStateKey());
206  }
207 
208  public function testGetProductCollection()
209  {
210  $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
211  ->will($this->returnValue($this->category));
212 
213  $this->category->expects($this->any())->method('getId')->will($this->returnValue(333));
214 
215  $this->collectionFilter->expects($this->once())->method('filter')
216  ->with($this->equalTo($this->collection), $this->equalTo($this->category));
217 
218  $this->collectionProvider->expects($this->once())->method('getCollection')
219  ->with($this->equalTo($this->category))
220  ->will($this->returnValue($this->collection));
221 
222  $result = $this->model->getProductCollection();
223  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $result);
224  $result = $this->model->getProductCollection();
225  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $result);
226  }
227 
228  public function testApply()
229  {
230  $stateKey = 'sk';
231  $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
232  ->will($this->returnValue($this->category));
233 
234  $this->stateKeyGenerator->expects($this->once())->method('toString')
235  ->with($this->equalTo($this->category))
236  ->will($this->returnValue($stateKey));
237 
238  $this->state->expects($this->any())->method('getFilters')->will($this->returnValue([$this->filter]));
239 
240  $this->filter->expects($this->once())->method('getFilter')->will($this->returnValue($this->abstractFilter));
241  $this->filter->expects($this->once())->method('getValueString')->will($this->returnValue('t'));
242 
243  $this->abstractFilter->expects($this->once())->method('getRequestVar')->will($this->returnValue('t'));
244 
245  $result = $this->model->apply();
246  $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $result);
247  }
248 
250  {
251  $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
252  ->will($this->returnValue($this->category));
253 
254  $this->collectionFilter->expects($this->once())->method('filter')
255  ->with($this->equalTo($this->collection), $this->equalTo($this->category));
256 
257  $result = $this->model->prepareProductCollection($this->collection);
258  $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $result);
259  }
260 
261  public function testGetCurrentStore()
262  {
263  $this->assertInstanceOf(\Magento\Store\Model\Store::class, $this->model->getCurrentStore());
264  }
265 
267  {
268  $categoryId = 333;
269  $currentCategoryId = 334;
270 
271  $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId));
272  $this->categoryRepository->expects($this->once())->method('get')->with($categoryId)
273  ->willReturn($this->currentCategory);
274 
275  $this->currentCategory->expects($this->any())->method('getId')->willReturn($currentCategoryId);
276  $this->registry->expects($this->once())->method('registry')->with('current_category')
277  ->willReturn($this->currentCategory);
278 
279  $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $this->model->setCurrentCategory($categoryId));
280  $this->assertEquals($this->currentCategory, $this->model->getData('current_category'));
281  }
282 
284  {
285  $categoryId = 333;
286 
287  $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId));
288 
289  $this->categoryRepository->expects($this->once())->method('get')->with($categoryId)
290  ->willReturn($this->category);
291  $this->registry->expects($this->once())->method('registry')->with('current_category')
292  ->willReturn($this->category);
293 
294  $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $this->model->setCurrentCategory($categoryId));
295  $this->assertEquals($this->category, $this->model->getData('current_category'));
296  }
297 
303  {
304  $this->categoryRepository->expects($this->once())->method('get')
305  ->will($this->throwException(new NoSuchEntityException()));
306 
307  $this->model->setCurrentCategory(1);
308  }
309 
315  {
316  $this->model->setCurrentCategory(null);
317  }
318 
324  {
325  $this->category->expects($this->once())->method('getId')->will($this->returnValue(null));
326 
327  $this->model->setCurrentCategory($this->category);
328  }
329 
330  public function testGetCurrentCategory()
331  {
332  $this->currentCategory->getData('current_category', null);
333 
334  $this->registry->expects($this->once())->method('registry')->with('current_category')
335  ->willReturn($this->currentCategory);
336 
337  $this->assertEquals($this->currentCategory, $this->model->getCurrentCategory());
338  $this->assertEquals($this->currentCategory, $this->model->getData('current_category'));
339  }
340 
342  {
343  $rootCategoryId = 333;
344  $this->currentCategory->getData('current_category', null);
345 
346  $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
347  ->willReturn(null);
348  $this->categoryRepository->expects($this->once())->method('get')->with($rootCategoryId)
349  ->willReturn($this->currentCategory);
350  $this->store->expects($this->any())->method('getRootCategoryId')
351  ->will($this->returnValue($rootCategoryId));
352 
353  $this->assertEquals($this->currentCategory, $this->model->getCurrentCategory());
354  $this->assertEquals($this->currentCategory, $this->model->getData('current_category'));
355  }
356 }
$helper
Definition: iframe.phtml:13