IntervalTest.php 11 KB

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