|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogSearch\Test\Unit\Model\ResourceModel\Advanced; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; |
| 14 | +use Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection; |
| 15 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolverFactory; |
| 16 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolverInterface; |
| 17 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierFactory; |
| 18 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierInterface; |
| 19 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\TotalRecordsResolverFactory; |
| 20 | +use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\TotalRecordsResolverInterface; |
| 21 | +use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollection; |
| 22 | +use Magento\Eav\Model\Config; |
| 23 | +use Magento\Framework\Api\Filter; |
| 24 | +use Magento\Framework\Api\FilterBuilder; |
| 25 | +use Magento\Framework\Api\Search\SearchCriteriaBuilder; |
| 26 | +use Magento\Framework\Api\Search\SearchResultInterface; |
| 27 | +use Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory; |
| 28 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 29 | +use Magento\Search\Api\SearchInterface; |
| 30 | +use PHPUnit\Framework\MockObject\MockObject; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection |
| 34 | + * |
| 35 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 36 | + * @deprecated Implementation class was replaced |
| 37 | + * @see \Magento\ElasticSearch |
| 38 | + */ |
| 39 | +class CollectionTest extends BaseCollection |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @var ObjectManager |
| 43 | + */ |
| 44 | + private $objectManager; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Collection |
| 48 | + */ |
| 49 | + private $advancedCollection; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var FilterBuilder|MockObject |
| 53 | + */ |
| 54 | + private $filterBuilder; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var SearchCriteriaBuilder|MockObject |
| 58 | + */ |
| 59 | + private $criteriaBuilder; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var TemporaryStorageFactory|MockObject |
| 63 | + */ |
| 64 | + private $temporaryStorageFactory; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var SearchInterface|MockObject |
| 68 | + */ |
| 69 | + private $search; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var Config|MockObject |
| 73 | + */ |
| 74 | + private $eavConfig; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var SearchResultApplierFactory|MockObject |
| 78 | + */ |
| 79 | + private $searchResultApplierFactory; |
| 80 | + |
| 81 | + /** |
| 82 | + * @inheritdoc |
| 83 | + */ |
| 84 | + protected function setUp(): void |
| 85 | + { |
| 86 | + $this->objectManager = new ObjectManager($this); |
| 87 | + $this->eavConfig = $this->createMock(Config::class); |
| 88 | + $storeManager = $this->getStoreManager(); |
| 89 | + $universalFactory = $this->getUniversalFactory(); |
| 90 | + $this->criteriaBuilder = $this->getCriteriaBuilder(); |
| 91 | + $this->filterBuilder = $this->createMock(FilterBuilder::class); |
| 92 | + $this->temporaryStorageFactory = $this->createMock( |
| 93 | + TemporaryStorageFactory::class |
| 94 | + ); |
| 95 | + $this->search = $this->getMockForAbstractClass(SearchInterface::class); |
| 96 | + |
| 97 | + $productLimitationMock = $this->createMock( |
| 98 | + ProductLimitation::class |
| 99 | + ); |
| 100 | + $productLimitationFactoryMock = $this->getMockBuilder(ProductLimitationFactory::class) |
| 101 | + ->disableOriginalConstructor() |
| 102 | + ->setMethods(['create']) |
| 103 | + ->getMock(); |
| 104 | + $productLimitationFactoryMock->method('create') |
| 105 | + ->willReturn($productLimitationMock); |
| 106 | + |
| 107 | + $searchCriteriaResolver = $this->getMockBuilder(SearchCriteriaResolverInterface::class) |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->setMethods(['resolve']) |
| 110 | + ->getMockForAbstractClass(); |
| 111 | + $searchCriteriaResolverFactory = $this->getMockBuilder(SearchCriteriaResolverFactory::class) |
| 112 | + ->disableOriginalConstructor() |
| 113 | + ->setMethods(['create']) |
| 114 | + ->getMock(); |
| 115 | + $searchCriteriaResolverFactory->expects($this->any()) |
| 116 | + ->method('create') |
| 117 | + ->willReturn($searchCriteriaResolver); |
| 118 | + |
| 119 | + $this->searchResultApplierFactory = $this->getMockBuilder(SearchResultApplierFactory::class) |
| 120 | + ->disableOriginalConstructor() |
| 121 | + ->setMethods(['create']) |
| 122 | + ->getMock(); |
| 123 | + |
| 124 | + $totalRecordsResolver = $this->getMockBuilder(TotalRecordsResolverInterface::class) |
| 125 | + ->disableOriginalConstructor() |
| 126 | + ->setMethods(['resolve']) |
| 127 | + ->getMockForAbstractClass(); |
| 128 | + $totalRecordsResolverFactory = $this->getMockBuilder(TotalRecordsResolverFactory::class) |
| 129 | + ->disableOriginalConstructor() |
| 130 | + ->setMethods(['create']) |
| 131 | + ->getMock(); |
| 132 | + $totalRecordsResolverFactory->expects($this->any()) |
| 133 | + ->method('create') |
| 134 | + ->willReturn($totalRecordsResolver); |
| 135 | + |
| 136 | + $this->advancedCollection = $this->objectManager->getObject( |
| 137 | + Collection::class, |
| 138 | + [ |
| 139 | + 'eavConfig' => $this->eavConfig, |
| 140 | + 'storeManager' => $storeManager, |
| 141 | + 'universalFactory' => $universalFactory, |
| 142 | + 'searchCriteriaBuilder' => $this->criteriaBuilder, |
| 143 | + 'filterBuilder' => $this->filterBuilder, |
| 144 | + 'temporaryStorageFactory' => $this->temporaryStorageFactory, |
| 145 | + 'search' => $this->search, |
| 146 | + 'productLimitationFactory' => $productLimitationFactoryMock, |
| 147 | + 'collectionProvider' => null, |
| 148 | + 'searchCriteriaResolverFactory' => $searchCriteriaResolverFactory, |
| 149 | + 'searchResultApplierFactory' => $this->searchResultApplierFactory, |
| 150 | + 'totalRecordsResolverFactory' => $totalRecordsResolverFactory |
| 151 | + ] |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Test to Load data with filter in place |
| 157 | + */ |
| 158 | + public function testLoadWithFilterNoFilters() |
| 159 | + { |
| 160 | + $this->advancedCollection->loadWithFilter(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Test a search using 'like' condition |
| 165 | + */ |
| 166 | + public function testLike() |
| 167 | + { |
| 168 | + $pageSize = 10; |
| 169 | + $attributeCode = 'description'; |
| 170 | + $attributeCodeId = 42; |
| 171 | + $attribute = $this->createMock(Attribute::class); |
| 172 | + $attribute->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode); |
| 173 | + $this->eavConfig->expects($this->once())->method('getAttribute')->with(Product::ENTITY, $attributeCodeId) |
| 174 | + ->willReturn($attribute); |
| 175 | + $filtersData = ['catalog_product_entity_text' => [$attributeCodeId => ['like' => 'search text']]]; |
| 176 | + $this->filterBuilder->expects($this->once())->method('setField')->with($attributeCode) |
| 177 | + ->willReturn($this->filterBuilder); |
| 178 | + $this->filterBuilder->expects($this->once())->method('setValue')->with('search text') |
| 179 | + ->willReturn($this->filterBuilder); |
| 180 | + |
| 181 | + $filter = $this->createMock(Filter::class); |
| 182 | + $this->filterBuilder->expects($this->any())->method('create')->willReturn($filter); |
| 183 | + |
| 184 | + $searchResult = $this->getMockForAbstractClass(SearchResultInterface::class); |
| 185 | + $this->search->expects($this->once())->method('search')->willReturn($searchResult); |
| 186 | + |
| 187 | + $this->advancedCollection->setPageSize($pageSize); |
| 188 | + $this->advancedCollection->setCurPage(0); |
| 189 | + |
| 190 | + $searchResultApplier = $this->getMockForAbstractClass(SearchResultApplierInterface::class); |
| 191 | + $this->searchResultApplierFactory->expects($this->once()) |
| 192 | + ->method('create') |
| 193 | + ->with( |
| 194 | + [ |
| 195 | + 'collection' => $this->advancedCollection, |
| 196 | + 'searchResult' => $searchResult, |
| 197 | + 'orders' => [], |
| 198 | + 'size' => $pageSize, |
| 199 | + 'currentPage' => 0, |
| 200 | + ] |
| 201 | + ) |
| 202 | + ->willReturn($searchResultApplier); |
| 203 | + |
| 204 | + // addFieldsToFilter will load filters, |
| 205 | + // then loadWithFilter will trigger _renderFiltersBefore code in Advanced/Collection |
| 206 | + $this->assertSame( |
| 207 | + $this->advancedCollection, |
| 208 | + $this->advancedCollection->addFieldsToFilter($filtersData)->loadWithFilter() |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @return MockObject |
| 214 | + */ |
| 215 | + protected function getCriteriaBuilder() |
| 216 | + { |
| 217 | + $criteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class) |
| 218 | + ->setMethods(['addFilter', 'create', 'setRequestName']) |
| 219 | + ->disableOriginalConstructor() |
| 220 | + ->getMock(); |
| 221 | + |
| 222 | + return $criteriaBuilder; |
| 223 | + } |
| 224 | +} |
0 commit comments