|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Model; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 9 | +use Magento\Catalog\Api\Data\CategorySearchResultsInterface; |
| 10 | +use Magento\Catalog\Model\Category; |
| 11 | +use Magento\Catalog\Model\CategoryList; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Category\Collection; |
| 13 | +use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface; |
| 14 | +use Magento\Framework\Api\Filter; |
| 15 | +use Magento\Framework\Api\Search\FilterGroup; |
| 16 | +use Magento\Framework\Api\SearchCriteriaInterface; |
| 17 | +use Magento\Framework\Api\SortOrder; |
| 18 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
| 19 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 20 | +use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory; |
| 21 | + |
| 22 | +/** |
| 23 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 24 | + */ |
| 25 | +class CategoryListTest extends \PHPUnit_Framework_TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var CategoryList |
| 29 | + */ |
| 30 | + protected $model; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $categoryCollectionFactory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + private $extensionAttributesJoinProcessor; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var CategorySearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + private $categorySearchResultsFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 49 | + */ |
| 50 | + private $categoryRepository; |
| 51 | + |
| 52 | + protected function setUp() |
| 53 | + { |
| 54 | + $this->categoryCollectionFactory = $this->getMockBuilder(CollectionFactory::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->setMethods(['create']) |
| 57 | + ->getMock(); |
| 58 | + $this->extensionAttributesJoinProcessor = $this->getMock(JoinProcessorInterface::class); |
| 59 | + $this->categorySearchResultsFactory = $this->getMockBuilder(CategorySearchResultsInterfaceFactory::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->setMethods(['create']) |
| 62 | + ->getMock(); |
| 63 | + $this->categoryRepository = $this->getMock(CategoryRepositoryInterface::class); |
| 64 | + |
| 65 | + $this->model = (new ObjectManager($this))->getObject( |
| 66 | + CategoryList::class, |
| 67 | + [ |
| 68 | + 'categoryCollectionFactory' => $this->categoryCollectionFactory, |
| 69 | + 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessor, |
| 70 | + 'categorySearchResultsFactory' => $this->categorySearchResultsFactory, |
| 71 | + 'categoryRepository' => $this->categoryRepository, |
| 72 | + ] |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public function testGetList() |
| 77 | + { |
| 78 | + $fieldName = 'field_1'; |
| 79 | + $value = 'value_1'; |
| 80 | + $conditionType = 'eq'; |
| 81 | + $currentPage = 2; |
| 82 | + $pageSize = 1; |
| 83 | + $totalCount = 2; |
| 84 | + $categoryIdFirst = 1; |
| 85 | + $categoryIdSecond = 2; |
| 86 | + |
| 87 | + $categoryFirst = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock(); |
| 88 | + $categorySecond = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock(); |
| 89 | + |
| 90 | + $filter = $this->getMockBuilder(Filter::class)->disableOriginalConstructor()->getMock(); |
| 91 | + $filter->expects($this->atLeastOnce())->method('getConditionType')->willReturn($conditionType); |
| 92 | + $filter->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName); |
| 93 | + $filter->expects($this->once())->method('getValue')->willReturn($value); |
| 94 | + |
| 95 | + $filterGroup = $this->getMockBuilder(FilterGroup::class)->disableOriginalConstructor()->getMock(); |
| 96 | + $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); |
| 97 | + |
| 98 | + $sortOrder = $this->getMockBuilder(SortOrder::class)->disableOriginalConstructor()->getMock(); |
| 99 | + $sortOrder->expects($this->once())->method('getField')->willReturn($fieldName); |
| 100 | + $sortOrder->expects($this->once())->method('getDirection')->willReturn(SortOrder::SORT_ASC); |
| 101 | + |
| 102 | + /** @var SearchCriteriaInterface|\PHPUnit_Framework_MockObject_MockObject $searchCriteria */ |
| 103 | + $searchCriteria = $this->getMock(SearchCriteriaInterface::class); |
| 104 | + $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); |
| 105 | + $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn($currentPage); |
| 106 | + $searchCriteria->expects($this->once())->method('getPageSize')->willReturn($pageSize); |
| 107 | + $searchCriteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]); |
| 108 | + |
| 109 | + $collection = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock(); |
| 110 | + $collection->expects($this->once()) |
| 111 | + ->method('addFieldToFilter') |
| 112 | + ->with([['attribute' => $fieldName, $conditionType => $value]]); |
| 113 | + $collection->expects($this->once())->method('addOrder')->with($fieldName, SortOrder::SORT_ASC); |
| 114 | + $collection->expects($this->once())->method('setCurPage')->with($currentPage); |
| 115 | + $collection->expects($this->once())->method('setPageSize')->with($pageSize); |
| 116 | + $collection->expects($this->once())->method('getSize')->willReturn($totalCount); |
| 117 | + $collection->expects($this->once())->method('getAllIds')->willReturn([$categoryIdFirst, $categoryIdSecond]); |
| 118 | + |
| 119 | + $searchResult = $this->getMock(CategorySearchResultsInterface::class); |
| 120 | + $searchResult->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); |
| 121 | + $searchResult->expects($this->once())->method('setItems')->with([$categoryFirst, $categorySecond]); |
| 122 | + $searchResult->expects($this->once())->method('setTotalCount')->with($totalCount); |
| 123 | + |
| 124 | + $this->categoryRepository->expects($this->exactly(2)) |
| 125 | + ->method('get') |
| 126 | + ->willReturnMap([ |
| 127 | + [$categoryIdFirst, $categoryFirst], |
| 128 | + [$categoryIdSecond, $categorySecond], |
| 129 | + ]) |
| 130 | + ->willReturn($categoryFirst); |
| 131 | + |
| 132 | + $this->categorySearchResultsFactory->expects($this->once())->method('create')->willReturn($searchResult); |
| 133 | + $this->categoryCollectionFactory->expects($this->once())->method('create')->willReturn($collection); |
| 134 | + $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection); |
| 135 | + |
| 136 | + $this->assertEquals($searchResult, $this->model->getList($searchCriteria)); |
| 137 | + } |
| 138 | +} |
0 commit comments