DataProviderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Test\Unit\SearchAdapter\Dynamic;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class DataProviderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Elasticsearch\SearchAdapter\QueryContainer|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $queryContainer;
  16. /**
  17. * @var \Magento\Elasticsearch\SearchAdapter\Dynamic\DataProvider
  18. */
  19. protected $model;
  20. /**
  21. * @var \Magento\Elasticsearch\SearchAdapter\ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $connectionManager;
  24. /**
  25. * @var \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $fieldMapper;
  28. /**
  29. * @var \Magento\Catalog\Model\Layer\Filter\Price\Range|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $range;
  32. /**
  33. * @var \Magento\Framework\Search\Dynamic\IntervalFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $intervalFactory;
  36. /**
  37. * @var \Magento\Elasticsearch\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $clientConfig;
  40. /**
  41. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $storeManager;
  44. /**
  45. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $customerSession;
  48. /**
  49. * @var \Magento\Framework\Search\Dynamic\EntityStorage|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $entityStorage;
  52. /**
  53. * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $storeMock;
  56. /**
  57. * @var \Magento\Elasticsearch\Model\Client\Elasticsearch|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. protected $clientMock;
  60. /**
  61. * @var \Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. protected $searchIndexNameResolver;
  64. /**
  65. * @var \Magento\Framework\App\ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  66. */
  67. protected $scopeResolver;
  68. /**
  69. * @var \Magento\Framework\App\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  70. */
  71. protected $scopeInterface;
  72. /**
  73. * A private helper for setUp method.
  74. * @return void
  75. */
  76. private function setUpMockObjects()
  77. {
  78. $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
  79. ->setMethods(['getConnection'])
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->range = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\Price\Range::class)
  83. ->setMethods(['getPriceRange'])
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->intervalFactory = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\IntervalFactory::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->clientConfig = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
  90. ->setMethods([
  91. 'getIndexName',
  92. 'getEntityType',
  93. ])
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  100. ->setMethods(['getCustomerGroupId'])
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $this->entityStorage = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\EntityStorage::class)
  104. ->setMethods(['getSource'])
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $this->entityStorage->expects($this->any())
  108. ->method('getSource')
  109. ->willReturn([1]);
  110. $this->customerSession->expects($this->any())
  111. ->method('getCustomerGroupId')
  112. ->willReturn(1);
  113. $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $this->storeManager->expects($this->any())
  117. ->method('getStore')
  118. ->willReturn($this->storeMock);
  119. $this->storeMock->expects($this->any())
  120. ->method('getWebsiteId')
  121. ->willReturn(1);
  122. $this->storeMock->expects($this->any())
  123. ->method('getId')
  124. ->willReturn(1);
  125. $this->clientConfig->expects($this->any())
  126. ->method('getIndexName')
  127. ->willReturn('indexName');
  128. $this->clientConfig->expects($this->any())
  129. ->method('getEntityType')
  130. ->willReturn('product');
  131. $this->clientMock = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
  132. ->setMethods(['query'])
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $this->connectionManager->expects($this->any())
  136. ->method('getConnection')
  137. ->willReturn($this->clientMock);
  138. $this->fieldMapper = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\FieldMapperInterface::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $this->searchIndexNameResolver = $this
  142. ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver::class)
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $this->scopeResolver = $this->getMockForAbstractClass(
  146. \Magento\Framework\App\ScopeResolverInterface::class,
  147. [],
  148. '',
  149. false
  150. );
  151. $this->scopeInterface = $this->getMockForAbstractClass(
  152. \Magento\Framework\App\ScopeInterface::class,
  153. [],
  154. '',
  155. false
  156. );
  157. $this->queryContainer = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\QueryContainer::class)
  158. ->disableOriginalConstructor()
  159. ->setMethods(['getQuery'])
  160. ->getMock();
  161. }
  162. /**
  163. * Setup method
  164. * @return void
  165. */
  166. protected function setUp()
  167. {
  168. $this->setUpMockObjects();
  169. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  170. $this->model = $objectManagerHelper->getObject(
  171. \Magento\Elasticsearch\SearchAdapter\Dynamic\DataProvider::class,
  172. [
  173. 'connectionManager' => $this->connectionManager,
  174. 'fieldMapper' => $this->fieldMapper,
  175. 'range' => $this->range,
  176. 'intervalFactory' => $this->intervalFactory,
  177. 'clientConfig' => $this->clientConfig,
  178. 'storeManager' => $this->storeManager,
  179. 'customerSession' => $this->customerSession,
  180. 'searchIndexNameResolver' => $this->searchIndexNameResolver,
  181. 'indexerId' => 'catalogsearch_fulltext',
  182. 'scopeResolver' => $this->scopeResolver,
  183. 'queryContainer' => $this->queryContainer,
  184. ]
  185. );
  186. }
  187. /**
  188. * Test getRange() method
  189. */
  190. public function testGetRange()
  191. {
  192. $this->range->expects($this->once())
  193. ->method('getPriceRange')
  194. ->willReturn([]);
  195. $this->assertEquals(
  196. [],
  197. $this->model->getRange()
  198. );
  199. }
  200. /**
  201. * Test getAggregations() method
  202. */
  203. public function testGetAggregations()
  204. {
  205. $expectedResult = [
  206. 'count' => 1,
  207. 'max' => 1,
  208. 'min' => 1,
  209. 'std' => 1,
  210. ];
  211. $this->clientMock->expects($this->once())
  212. ->method('query')
  213. ->willReturn([
  214. 'aggregations' => [
  215. 'prices' => [
  216. 'count' => 1,
  217. 'max' => 1,
  218. 'min' => 1,
  219. 'std_deviation' => 1,
  220. ],
  221. ],
  222. ]);
  223. $this->queryContainer->expects($this->once())
  224. ->method('getQuery')
  225. ->willReturn([]);
  226. $this->assertEquals(
  227. $expectedResult,
  228. $this->model->getAggregations($this->entityStorage)
  229. );
  230. }
  231. /**
  232. * Test getInterval() method
  233. */
  234. public function testGetInterval()
  235. {
  236. $dimensionValue = 1;
  237. $bucket = $this->getMockBuilder(\Magento\Framework\Search\Request\BucketInterface::class)
  238. ->disableOriginalConstructor()
  239. ->getMock();
  240. $interval = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\IntervalInterface::class)
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
  244. ->setMethods(['getValue'])
  245. ->disableOriginalConstructor()
  246. ->getMock();
  247. $dimension->expects($this->once())
  248. ->method('getValue')
  249. ->willReturn($dimensionValue);
  250. $this->scopeResolver->expects($this->once())
  251. ->method('getScope')
  252. ->willReturn($this->scopeInterface);
  253. $this->scopeInterface->expects($this->once())
  254. ->method('getId')
  255. ->willReturn($dimensionValue);
  256. $this->intervalFactory->expects($this->once())
  257. ->method('create')
  258. ->willReturn($interval);
  259. $this->assertEquals(
  260. $interval,
  261. $this->model->getInterval(
  262. $bucket,
  263. [$dimension],
  264. $this->entityStorage
  265. )
  266. );
  267. }
  268. /**
  269. * Test getAggregation() method
  270. */
  271. public function testGetAggregation()
  272. {
  273. $expectedResult = [
  274. 1 => 1,
  275. ];
  276. $bucket = $this->getMockBuilder(\Magento\Framework\Search\Request\BucketInterface::class)
  277. ->disableOriginalConstructor()
  278. ->getMock();
  279. $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
  280. ->setMethods(['getValue'])
  281. ->disableOriginalConstructor()
  282. ->getMock();
  283. $dimension->expects($this->never())
  284. ->method('getValue');
  285. $this->scopeResolver->expects($this->never())
  286. ->method('getScope');
  287. $this->scopeInterface->expects($this->never())
  288. ->method('getId');
  289. $this->clientMock->expects($this->once())
  290. ->method('query')
  291. ->with($this->callback(function ($query) {
  292. $histogramParams = $query['body']['aggregations']['prices']['histogram'];
  293. // Assert the interval is queried as a float. See MAGETWO-95471
  294. if ($histogramParams['interval'] !== 10.0) {
  295. return false;
  296. }
  297. if (!isset($histogramParams['min_doc_count']) || $histogramParams['min_doc_count'] !== 1) {
  298. return false;
  299. }
  300. return true;
  301. }))
  302. ->willReturn([
  303. 'aggregations' => [
  304. 'prices' => [
  305. 'buckets' => [
  306. [
  307. 'key' => 1,
  308. 'doc_count' => 1,
  309. ],
  310. ],
  311. ],
  312. ],
  313. ]);
  314. $this->queryContainer->expects($this->once())
  315. ->method('getQuery')
  316. ->willReturn([]);
  317. $this->assertEquals(
  318. $expectedResult,
  319. $this->model->getAggregation(
  320. $bucket,
  321. [$dimension],
  322. 10,
  323. $this->entityStorage
  324. )
  325. );
  326. }
  327. /**
  328. * Test prepareData() method
  329. */
  330. public function testPrepareData()
  331. {
  332. $expectedResult = [
  333. [
  334. 'from' => '',
  335. 'to' => 10,
  336. 'count' => 1,
  337. ],
  338. [
  339. 'from' => 10,
  340. 'to' => '',
  341. 'count' => 1,
  342. ],
  343. ];
  344. $this->assertEquals(
  345. $expectedResult,
  346. $this->model->prepareData(
  347. 10,
  348. [
  349. 1 => 1,
  350. 2 => 1,
  351. ]
  352. )
  353. );
  354. }
  355. }