|
| 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\AdvancedSearch\Test\Unit\Model\Recommendations; |
| 9 | + |
| 10 | +use Magento\AdvancedSearch\Model\Recommendations\DataProvider; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 13 | +use Magento\Catalog\Model\Layer\Resolver; |
| 14 | +use Magento\AdvancedSearch\Model\ResourceModel\Recommendations; |
| 15 | +use Magento\AdvancedSearch\Model\ResourceModel\RecommendationsFactory; |
| 16 | +use Magento\Search\Model\QueryResult; |
| 17 | +use Magento\Search\Model\QueryResultFactory; |
| 18 | +use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; |
| 19 | +use Magento\Catalog\Model\Layer as SearchLayer; |
| 20 | +use Magento\Store\Model\ScopeInterface; |
| 21 | +use Magento\Search\Model\QueryInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 25 | + * |
| 26 | + * Class \Magento\AdvancedSearch\Test\Unit\Model\Recommendations\DataProviderTest |
| 27 | + */ |
| 28 | +class DataProviderTest extends \PHPUnit\Framework\TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var DataProvider; |
| 32 | + */ |
| 33 | + private $model; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ObjectManagerHelper |
| 37 | + */ |
| 38 | + private $objectManagerHelper; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \PHPUnit_Framework_MockObject_MockObject|ScopeConfigInterface |
| 42 | + */ |
| 43 | + private $scopeConfigMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \PHPUnit_Framework_MockObject_MockObject|Resolver |
| 47 | + */ |
| 48 | + private $layerResolverMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var \PHPUnit_Framework_MockObject_MockObject|SearchLayer |
| 52 | + */ |
| 53 | + private $searchLayerMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var \PHPUnit_Framework_MockObject_MockObject|RecommendationsFactory |
| 57 | + */ |
| 58 | + private $recommendationsFactoryMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var \PHPUnit_Framework_MockObject_MockObject|Recommendations |
| 62 | + */ |
| 63 | + private $recommendationsMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var \PHPUnit_Framework_MockObject_MockObject|Resolver |
| 67 | + */ |
| 68 | + private $queryResultFactory; |
| 69 | + |
| 70 | + /** |
| 71 | + * Set up test environment. |
| 72 | + * |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + protected function setUp() |
| 76 | + { |
| 77 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 78 | + $this->layerResolverMock = $this->getMockBuilder(Resolver::class) |
| 79 | + ->disableOriginalConstructor() |
| 80 | + ->setMethods(['get']) |
| 81 | + ->getMock(); |
| 82 | + |
| 83 | + $this->searchLayerMock = $this->createMock(SearchLayer::class); |
| 84 | + |
| 85 | + $this->layerResolverMock->expects($this->any()) |
| 86 | + ->method('get') |
| 87 | + ->will($this->returnValue($this->searchLayerMock)); |
| 88 | + |
| 89 | + $this->recommendationsFactoryMock = $this->getMockBuilder(RecommendationsFactory::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->setMethods(['create']) |
| 92 | + ->getMock(); |
| 93 | + |
| 94 | + $this->recommendationsMock = $this->createMock(Recommendations::class); |
| 95 | + |
| 96 | + $this->queryResultFactory = $this->getMockBuilder(QueryResultFactory::class) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->setMethods(['create']) |
| 99 | + ->getMock(); |
| 100 | + |
| 101 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 102 | + $this->model = $this->objectManagerHelper->getObject( |
| 103 | + DataProvider::class, |
| 104 | + [ |
| 105 | + 'scopeConfig' => $this->scopeConfigMock, |
| 106 | + 'layerResolver' => $this->layerResolverMock, |
| 107 | + 'recommendationsFactory' => $this->recommendationsFactoryMock, |
| 108 | + 'queryResultFactory' => $this->queryResultFactory |
| 109 | + ] |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Test testGetItems() when Search Recommendations disabled. |
| 115 | + * |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function testGetItemsWhenDisabledSearchRecommendations() |
| 119 | + { |
| 120 | + $isEnabledSearchRecommendations = false; |
| 121 | + |
| 122 | + /** @var $queryInterfaceMock QueryInterface */ |
| 123 | + $queryInterfaceMock = $this->createMock(QueryInterface::class); |
| 124 | + |
| 125 | + $this->scopeConfigMock->expects($this->any()) |
| 126 | + ->method('isSetFlag') |
| 127 | + ->with('catalog/search/search_recommendations_enabled', ScopeInterface::SCOPE_STORE) |
| 128 | + ->willReturn($isEnabledSearchRecommendations); |
| 129 | + |
| 130 | + $result = $this->model->getItems($queryInterfaceMock); |
| 131 | + $this->assertEquals([], $result); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Test testGetItems() when Search Recommendations enabled. |
| 136 | + * |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + public function testGetItemsWhenEnabledSearchRecommendations() |
| 140 | + { |
| 141 | + $storeId = 1; |
| 142 | + $searchRecommendationsCountConfig = 2; |
| 143 | + $isEnabledSearchRecommendations = true; |
| 144 | + $queryText = 'test'; |
| 145 | + |
| 146 | + /** @var $queryInterfaceMock QueryInterface */ |
| 147 | + $queryInterfaceMock = $this->createMock(QueryInterface::class); |
| 148 | + $queryInterfaceMock->expects($this->any())->method('getQueryText')->willReturn($queryText); |
| 149 | + |
| 150 | + $this->scopeConfigMock->expects($this->any()) |
| 151 | + ->method('isSetFlag') |
| 152 | + ->with('catalog/search/search_recommendations_enabled', ScopeInterface::SCOPE_STORE) |
| 153 | + ->willReturn($isEnabledSearchRecommendations); |
| 154 | + |
| 155 | + $this->scopeConfigMock->expects($this->any()) |
| 156 | + ->method('getValue') |
| 157 | + ->with('catalog/search/search_recommendations_count', ScopeInterface::SCOPE_STORE) |
| 158 | + ->willReturn($searchRecommendationsCountConfig); |
| 159 | + |
| 160 | + $productCollectionMock = $this->createMock(ProductCollection::class); |
| 161 | + $productCollectionMock->expects($this->any())->method('getStoreId')->willReturn($storeId); |
| 162 | + |
| 163 | + $this->searchLayerMock->expects($this->any())->method('getProductCollection') |
| 164 | + ->willReturn($productCollectionMock); |
| 165 | + |
| 166 | + $this->recommendationsFactoryMock->expects($this->any())->method('create') |
| 167 | + ->willReturn($this->recommendationsMock); |
| 168 | + |
| 169 | + $this->recommendationsMock->expects($this->any())->method('getRecommendationsByQuery') |
| 170 | + ->with($queryText, ['store_id' => $storeId], $searchRecommendationsCountConfig) |
| 171 | + ->willReturn( |
| 172 | + [ |
| 173 | + [ |
| 174 | + 'query_text' => 'a', |
| 175 | + 'num_results' => 3 |
| 176 | + ], |
| 177 | + [ |
| 178 | + 'query_text' => 'b', |
| 179 | + 'num_results' => 2 |
| 180 | + ] |
| 181 | + ] |
| 182 | + ); |
| 183 | + $queryResultMock = $this->createMock(QueryResult::class); |
| 184 | + $this->queryResultFactory->expects($this->any())->method('create')->willReturn($queryResultMock); |
| 185 | + |
| 186 | + $result = $this->model->getItems($queryInterfaceMock); |
| 187 | + $this->assertEquals(2, count($result)); |
| 188 | + } |
| 189 | +} |
0 commit comments