IntervalTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Elasticsearch\Test\Unit\Elasticsearch5\SearchAdapter\Aggregation;
  8. use Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  11. use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\Customer\Model\Session as CustomerSession;
  14. use Magento\Elasticsearch\Model\Config;
  15. use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch as ElasticsearchClient;
  16. use Magento\Store\Api\Data\StoreInterface;
  17. use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
  18. /**
  19. * Test for Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval class.
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class IntervalTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * @var Interval
  27. */
  28. private $model;
  29. /**
  30. * @var ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $connectionManager;
  33. /**
  34. * @var FieldMapperInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $fieldMapper;
  37. /**
  38. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $clientConfig;
  41. /**
  42. * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $storeManager;
  45. /**
  46. * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $customerSession;
  49. /**
  50. * @var ElasticsearchClient|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $clientMock;
  53. /**
  54. * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $storeMock;
  57. /**
  58. * @var SearchIndexNameResolver|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $searchIndexNameResolver;
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function setUp()
  65. {
  66. $this->connectionManager = $this->getMockBuilder(ConnectionManager::class)
  67. ->setMethods(['getConnection'])
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->fieldMapper = $this->getMockBuilder(FieldMapperInterface::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->clientConfig = $this->getMockBuilder(Config::class)
  74. ->setMethods([
  75. 'getIndexName',
  76. 'getEntityType',
  77. ])
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->customerSession = $this->getMockBuilder(CustomerSession::class)
  84. ->setMethods(['getCustomerGroupId'])
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->customerSession->expects($this->any())
  88. ->method('getCustomerGroupId')
  89. ->willReturn(1);
  90. $this->storeMock = $this->getMockBuilder(StoreInterface::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->searchIndexNameResolver = $this
  94. ->getMockBuilder(SearchIndexNameResolver::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->storeMock->expects($this->any())
  98. ->method('getWebsiteId')
  99. ->willReturn(1);
  100. $this->storeMock->expects($this->any())
  101. ->method('getId')
  102. ->willReturn(1);
  103. $this->clientConfig->expects($this->any())
  104. ->method('getIndexName')
  105. ->willReturn('indexName');
  106. $this->clientConfig->expects($this->any())
  107. ->method('getEntityType')
  108. ->willReturn('product');
  109. $this->clientMock = $this->getMockBuilder(ElasticsearchClient::class)
  110. ->setMethods(['query'])
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $this->connectionManager->expects($this->any())
  114. ->method('getConnection')
  115. ->willReturn($this->clientMock);
  116. $objectManagerHelper = new ObjectManagerHelper($this);
  117. $this->model = $objectManagerHelper->getObject(
  118. Interval::class,
  119. [
  120. 'connectionManager' => $this->connectionManager,
  121. 'fieldMapper' => $this->fieldMapper,
  122. 'clientConfig' => $this->clientConfig,
  123. 'searchIndexNameResolver' => $this->searchIndexNameResolver,
  124. 'fieldName' => 'price_0_1',
  125. 'storeId' => 1,
  126. 'entityIds' => [265, 313, 281],
  127. ]
  128. );
  129. }
  130. /**
  131. * @dataProvider loadParamsProvider
  132. * @param string $limit
  133. * @param string $offset
  134. * @param string $lower
  135. * @param string $upper
  136. * @param array $queryResult
  137. * @param array $expected
  138. * @return void
  139. */
  140. public function testLoad(
  141. string $limit,
  142. string $offset,
  143. string $lower,
  144. string $upper,
  145. array $queryResult,
  146. array $expected
  147. ): void {
  148. $this->processQuery($queryResult);
  149. $this->assertEquals(
  150. $expected,
  151. $this->model->load($limit, $offset, $lower, $upper)
  152. );
  153. }
  154. /**
  155. * @dataProvider loadPrevParamsProvider
  156. * @param string $data
  157. * @param string $index
  158. * @param string $lower
  159. * @param array $queryResult
  160. * @param array|bool $expected
  161. * @return void
  162. */
  163. public function testLoadPrev(string $data, string $index, string $lower, array $queryResult, $expected): void
  164. {
  165. $this->processQuery($queryResult);
  166. $this->assertEquals(
  167. $expected,
  168. $this->model->loadPrevious($data, $index, $lower)
  169. );
  170. }
  171. /**
  172. * @dataProvider loadNextParamsProvider
  173. * @param string $data
  174. * @param string $rightIndex
  175. * @param string $upper
  176. * @param array $queryResult
  177. * @param array|bool $expected
  178. * @return void
  179. */
  180. public function testLoadNext(string $data, string $rightIndex, string $upper, array $queryResult, $expected): void
  181. {
  182. $this->processQuery($queryResult);
  183. $this->assertEquals(
  184. $expected,
  185. $this->model->loadNext($data, $rightIndex, $upper)
  186. );
  187. }
  188. /**
  189. * @param array $queryResult
  190. * @return void
  191. */
  192. private function processQuery(array $queryResult): void
  193. {
  194. $this->searchIndexNameResolver->expects($this->any())
  195. ->method('getIndexName')
  196. ->willReturn('magento2_product_1');
  197. $this->clientConfig->expects($this->any())
  198. ->method('getEntityType')
  199. ->willReturn('document');
  200. $this->clientMock->expects($this->any())
  201. ->method('query')
  202. ->willReturn($queryResult);
  203. }
  204. /**
  205. * @return array
  206. */
  207. public function loadParamsProvider(): array
  208. {
  209. return [
  210. [
  211. 'limit' => '6',
  212. 'offset' => '2',
  213. 'lower' => '24',
  214. 'upper' => '42',
  215. 'queryResult' => [
  216. 'hits' => [
  217. 'hits' => [
  218. [
  219. 'fields' => [
  220. 'price_0_1' => [25],
  221. ],
  222. ],
  223. ],
  224. ],
  225. ],
  226. 'expected' => [25],
  227. ],
  228. ];
  229. }
  230. /**
  231. * @return array
  232. */
  233. public function loadPrevParamsProvider(): array
  234. {
  235. return [
  236. [
  237. 'data' => '24',
  238. 'rightIndex' => '1',
  239. 'upper' => '24',
  240. 'queryResult' => [
  241. 'hits' => [
  242. 'total'=> '1',
  243. 'hits' => [
  244. [
  245. 'fields' => [
  246. 'price_0_1' => ['25'],
  247. ],
  248. ],
  249. ],
  250. ],
  251. ],
  252. 'expected' => ['25.0'],
  253. ],
  254. [
  255. 'data' => '24',
  256. 'rightIndex' => '1',
  257. 'upper' => '24',
  258. 'queryResult' => [
  259. 'hits' => ['total'=> '0'],
  260. ],
  261. 'expected' => false,
  262. ],
  263. ];
  264. }
  265. /**
  266. * @return array
  267. */
  268. public function loadNextParamsProvider(): array
  269. {
  270. return [
  271. [
  272. 'data' => '24',
  273. 'rightIndex' => '2',
  274. 'upper' => '42',
  275. 'queryResult' => [
  276. 'hits' => [
  277. 'total'=> '1',
  278. 'hits' => [
  279. [
  280. 'fields' => [
  281. 'price_0_1' => ['25'],
  282. ],
  283. ],
  284. ],
  285. ],
  286. ],
  287. 'expected' => ['25.0'],
  288. ],
  289. [
  290. 'data' => '24',
  291. 'rightIndex' => '2',
  292. 'upper' => '42',
  293. 'queryResult' => [
  294. 'hits' => ['total'=> '0'],
  295. ],
  296. 'expected' => false,
  297. ],
  298. ];
  299. }
  300. }