LayerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. * @SuppressWarnings(PHPMD.TooManyFields)
  12. */
  13. class LayerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Catalog\Model\Layer
  17. */
  18. private $model;
  19. /**
  20. * @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $category;
  23. /**
  24. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $registry;
  27. /**
  28. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $storeManager;
  31. /**
  32. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $store;
  35. /**
  36. * @var \Magento\Catalog\Model\Layer\ContextInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $context;
  39. /**
  40. * @var \Magento\Catalog\Model\Layer\Category\StateKey|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $stateKeyGenerator;
  43. /**
  44. * @var \Magento\Catalog\Model\Layer\StateFactory|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $stateFactory;
  47. /**
  48. * @var \Magento\Catalog\Model\Layer\State|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $state;
  51. /**
  52. * @var \Magento\Catalog\Model\Layer\Category\CollectionFilter|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. private $collectionFilter;
  55. /**
  56. * @var \Magento\Catalog\Model\ResourceModel\Product\Collection|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. private $collection;
  59. /**
  60. * @var \Magento\Catalog\Model\Layer\ItemCollectionProviderInterface|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. private $collectionProvider;
  63. /**
  64. * @var \Magento\Catalog\Model\Layer\Filter\Item|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. private $filter;
  67. /**
  68. * @var \Magento\Catalog\Model\Layer\Filter\AbstractFilter|\PHPUnit_Framework_MockObject_MockObject
  69. */
  70. private $abstractFilter;
  71. /**
  72. * @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  73. */
  74. private $categoryRepository;
  75. /**
  76. * @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject
  77. */
  78. private $currentCategory;
  79. protected function setUp()
  80. {
  81. $helper = new ObjectManager($this);
  82. $this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
  83. ->setMethods(['getId', '__wakeup'])
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
  87. ->setMethods(['registry'])
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  91. ->setMethods(['getRootCategoryId', 'getFilters', '__wakeup'])
  92. ->disableOriginalConstructor()
  93. ->getMockForAbstractClass();
  94. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  95. ->setMethods(['getStore'])
  96. ->disableOriginalConstructor()
  97. ->getMockForAbstractClass();
  98. $this->storeManager->expects($this->any())->method('getStore')
  99. ->will($this->returnValue($this->store));
  100. $this->stateKeyGenerator = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Category\StateKey::class)
  101. ->setMethods(['toString'])
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $this->collectionFilter = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Category\CollectionFilter::class)
  105. ->setMethods(['filter'])
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->collectionProvider = $this->getMockBuilder(
  109. \Magento\Catalog\Model\Layer\ItemCollectionProviderInterface::class
  110. )->disableOriginalConstructor()->getMockForAbstractClass();
  111. $this->filter = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\Item::class)
  112. ->setMethods(['getFilter', 'getValueString'])
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->abstractFilter = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\AbstractFilter::class)
  116. ->setMethods(['getRequestVar'])
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $this->context = $this->getMockBuilder(\Magento\Catalog\Model\Layer\ContextInterface::class)
  120. ->setMethods(['getStateKey', 'getCollectionFilter'])
  121. ->disableOriginalConstructor()
  122. ->getMockForAbstractClass();
  123. $this->context->expects($this->any())->method('getStateKey')
  124. ->will($this->returnValue($this->stateKeyGenerator));
  125. $this->context->expects($this->any())->method('getCollectionFilter')
  126. ->will($this->returnValue($this->collectionFilter));
  127. $this->context->expects($this->any())->method('getCollectionProvider')
  128. ->will($this->returnValue($this->collectionProvider));
  129. $this->state = $this->getMockBuilder(\Magento\Catalog\Model\Layer\State::class)
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $this->stateFactory = $this->getMockBuilder(\Magento\Catalog\Model\Layer\StateFactory::class)
  133. ->setMethods(['create'])
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $this->stateFactory->expects($this->any())->method('create')->will($this->returnValue($this->state));
  137. $this->collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $this->categoryRepository = $this->createMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
  141. $this->currentCategory = $this->createPartialMock(
  142. \Magento\Catalog\Model\Category::class,
  143. ['getId', '__wakeup']
  144. );
  145. $this->model = $helper->getObject(
  146. \Magento\Catalog\Model\Layer::class,
  147. [
  148. 'registry' => $this->registry,
  149. 'storeManager' => $this->storeManager,
  150. 'context' => $this->context,
  151. 'layerStateFactory' => $this->stateFactory,
  152. 'categoryRepository' => $this->categoryRepository,
  153. ]
  154. );
  155. }
  156. public function testGetState()
  157. {
  158. $this->assertInstanceOf(\Magento\Catalog\Model\Layer\State::class, $this->model->getState());
  159. }
  160. public function testGetStateKey()
  161. {
  162. $stateKey = 'sk';
  163. $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
  164. ->will($this->returnValue($this->category));
  165. $this->stateKeyGenerator->expects($this->once())->method('toString')
  166. ->with($this->equalTo($this->category))
  167. ->will($this->returnValue($stateKey));
  168. $this->assertEquals($stateKey, $this->model->getStateKey());
  169. }
  170. public function testGetProductCollection()
  171. {
  172. $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
  173. ->will($this->returnValue($this->category));
  174. $this->category->expects($this->any())->method('getId')->will($this->returnValue(333));
  175. $this->collectionFilter->expects($this->once())->method('filter')
  176. ->with($this->equalTo($this->collection), $this->equalTo($this->category));
  177. $this->collectionProvider->expects($this->once())->method('getCollection')
  178. ->with($this->equalTo($this->category))
  179. ->will($this->returnValue($this->collection));
  180. $result = $this->model->getProductCollection();
  181. $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $result);
  182. $result = $this->model->getProductCollection();
  183. $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $result);
  184. }
  185. public function testApply()
  186. {
  187. $stateKey = 'sk';
  188. $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
  189. ->will($this->returnValue($this->category));
  190. $this->stateKeyGenerator->expects($this->once())->method('toString')
  191. ->with($this->equalTo($this->category))
  192. ->will($this->returnValue($stateKey));
  193. $this->state->expects($this->any())->method('getFilters')->will($this->returnValue([$this->filter]));
  194. $this->filter->expects($this->once())->method('getFilter')->will($this->returnValue($this->abstractFilter));
  195. $this->filter->expects($this->once())->method('getValueString')->will($this->returnValue('t'));
  196. $this->abstractFilter->expects($this->once())->method('getRequestVar')->will($this->returnValue('t'));
  197. $result = $this->model->apply();
  198. $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $result);
  199. }
  200. public function testPrepareProductCollection()
  201. {
  202. $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
  203. ->will($this->returnValue($this->category));
  204. $this->collectionFilter->expects($this->once())->method('filter')
  205. ->with($this->equalTo($this->collection), $this->equalTo($this->category));
  206. $result = $this->model->prepareProductCollection($this->collection);
  207. $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $result);
  208. }
  209. public function testGetCurrentStore()
  210. {
  211. $this->assertInstanceOf(\Magento\Store\Model\Store::class, $this->model->getCurrentStore());
  212. }
  213. public function testSetNewCurrentCategoryIfCurrentCategoryIsAnother()
  214. {
  215. $categoryId = 333;
  216. $currentCategoryId = 334;
  217. $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId));
  218. $this->categoryRepository->expects($this->once())->method('get')->with($categoryId)
  219. ->willReturn($this->currentCategory);
  220. $this->currentCategory->expects($this->any())->method('getId')->willReturn($currentCategoryId);
  221. $this->registry->expects($this->once())->method('registry')->with('current_category')
  222. ->willReturn($this->currentCategory);
  223. $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $this->model->setCurrentCategory($categoryId));
  224. $this->assertEquals($this->currentCategory, $this->model->getData('current_category'));
  225. }
  226. public function testSetNewCurrentCategoryIfCurrentCategoryIsSame()
  227. {
  228. $categoryId = 333;
  229. $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId));
  230. $this->categoryRepository->expects($this->once())->method('get')->with($categoryId)
  231. ->willReturn($this->category);
  232. $this->registry->expects($this->once())->method('registry')->with('current_category')
  233. ->willReturn($this->category);
  234. $this->assertInstanceOf(\Magento\Catalog\Model\Layer::class, $this->model->setCurrentCategory($categoryId));
  235. $this->assertEquals($this->category, $this->model->getData('current_category'));
  236. }
  237. /**
  238. * @expectedException \Magento\Framework\Exception\LocalizedException
  239. * @expectedExceptionMessage Please correct the category.
  240. */
  241. public function testSetNewCurrentCategoryIfCategoryIsNotFound()
  242. {
  243. $this->categoryRepository->expects($this->once())->method('get')
  244. ->will($this->throwException(new NoSuchEntityException()));
  245. $this->model->setCurrentCategory(1);
  246. }
  247. /**
  248. * @expectedException \Magento\Framework\Exception\LocalizedException
  249. * @expectedExceptionMessage Must be category model instance or its id.
  250. */
  251. public function testSetCurrentCategoryInstanceOfException()
  252. {
  253. $this->model->setCurrentCategory(null);
  254. }
  255. /**
  256. * @expectedException \Magento\Framework\Exception\LocalizedException
  257. * @expectedExceptionMessage Please correct the category.
  258. */
  259. public function testSetCurrentCategoryNotFoundException()
  260. {
  261. $this->category->expects($this->once())->method('getId')->will($this->returnValue(null));
  262. $this->model->setCurrentCategory($this->category);
  263. }
  264. public function testGetCurrentCategory()
  265. {
  266. $this->currentCategory->getData('current_category', null);
  267. $this->registry->expects($this->once())->method('registry')->with('current_category')
  268. ->willReturn($this->currentCategory);
  269. $this->assertEquals($this->currentCategory, $this->model->getCurrentCategory());
  270. $this->assertEquals($this->currentCategory, $this->model->getData('current_category'));
  271. }
  272. public function testGetCurrentCategoryIfCurrentCategoryIsNotSet()
  273. {
  274. $rootCategoryId = 333;
  275. $this->currentCategory->getData('current_category', null);
  276. $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))
  277. ->willReturn(null);
  278. $this->categoryRepository->expects($this->once())->method('get')->with($rootCategoryId)
  279. ->willReturn($this->currentCategory);
  280. $this->store->expects($this->any())->method('getRootCategoryId')
  281. ->will($this->returnValue($rootCategoryId));
  282. $this->assertEquals($this->currentCategory, $this->model->getCurrentCategory());
  283. $this->assertEquals($this->currentCategory, $this->model->getData('current_category'));
  284. }
  285. }