IndexTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. * @SuppressWarnings(PHPMD.TooManyFields)
  11. */
  12. class IndexTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Elasticsearch\Model\ResourceModel\Index
  16. */
  17. private $model;
  18. /**
  19. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $storeManager;
  22. /**
  23. * @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $productRepository;
  26. /**
  27. * @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $categoryRepository;
  30. /**
  31. * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $eavConfig;
  34. /**
  35. * @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $fullText;
  38. /**
  39. * @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $context;
  42. /**
  43. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $eventManager;
  46. /**
  47. * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $metadataPool;
  50. /**
  51. * @var \Magento\Catalog\Api\Data\ProductInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $product;
  54. /**
  55. * @var \Magento\Catalog\Api\Data\CategoryInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $category;
  58. /**
  59. * @var \Magento\Catalog\Api\Data\ProductAttributeInterface|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $productAttributeInterface;
  62. /**
  63. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $connection;
  66. /**
  67. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $select;
  70. /**
  71. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $resources;
  74. /**
  75. * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected $storeInterface;
  78. /**
  79. * @var \PHPUnit_Framework_MockObject_MockObject
  80. */
  81. protected $tableResolver;
  82. /**
  83. * Setup
  84. *
  85. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  86. * @return void
  87. */
  88. protected function setUp()
  89. {
  90. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  91. ->disableOriginalConstructor()
  92. ->setMethods([
  93. 'getStore',
  94. ])
  95. ->getMockForAbstractClass();
  96. $this->storeInterface = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  97. ->disableOriginalConstructor()
  98. ->setMethods([
  99. 'getWebsiteId',
  100. ])
  101. ->getMockForAbstractClass();
  102. $this->productRepository = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
  103. ->getMockForAbstractClass();
  104. $this->categoryRepository = $this->getMockBuilder(\Magento\Catalog\Api\CategoryRepositoryInterface::class)
  105. ->getMockForAbstractClass();
  106. $this->eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
  107. ->disableOriginalConstructor()
  108. ->setMethods([
  109. 'getEntityAttributeCodes',
  110. 'getAttribute',
  111. ])
  112. ->getMock();
  113. $this->fullText = $this->getMockBuilder(\Magento\CatalogSearch\Model\ResourceModel\Fulltext::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $this->context = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\Context::class)
  117. ->disableOriginalConstructor()
  118. ->setMethods([
  119. 'getTransactionManager',
  120. 'getResources',
  121. 'getObjectRelationProcessor',
  122. ])
  123. ->getMock();
  124. $this->eventManager = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  125. ->setMethods(['dispatch'])
  126. ->getMock();
  127. $this->product = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
  128. ->disableOriginalConstructor()
  129. ->setMethods([
  130. 'getData',
  131. ])
  132. ->getMockForAbstractClass();
  133. $this->category = $this->getMockBuilder(\Magento\Catalog\Api\Data\CategoryInterface::class)
  134. ->disableOriginalConstructor()
  135. ->setMethods([
  136. 'getName',
  137. ])
  138. ->getMockForAbstractClass();
  139. $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  143. ->disableOriginalConstructor()
  144. ->setMethods([
  145. 'distinct',
  146. 'from',
  147. 'join',
  148. 'where',
  149. 'orWhere',
  150. ])
  151. ->getMock();
  152. $this->resources = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  153. ->disableOriginalConstructor()
  154. ->setMethods([
  155. 'getConnection',
  156. 'getTableName',
  157. 'getTablePrefix',
  158. ])
  159. ->getMock();
  160. $this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  161. ->disableOriginalConstructor()
  162. ->setMethods([
  163. 'getMetadata',
  164. 'getIdentifierField'
  165. ])
  166. ->getMock();
  167. $this->context->expects($this->any())
  168. ->method('getResources')
  169. ->willReturn($this->resources);
  170. $this->resources->expects($this->any())
  171. ->method('getConnection')
  172. ->willReturn($this->connection);
  173. $this->resources->expects($this->any())
  174. ->method('getTablePrefix')
  175. ->willReturn('');
  176. $this->metadataPool->method('getMetadata')
  177. ->willReturnSelf();
  178. $this->metadataPool->method('getIdentifierField')
  179. ->willReturn('entity_id');
  180. $objectManager = new ObjectManagerHelper($this);
  181. $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  185. ->setMethods([
  186. 'getConnection',
  187. 'getTableName'
  188. ])
  189. ->disableOriginalConstructor()
  190. ->getMock();
  191. $resource->expects($this->any())
  192. ->method('getConnection')
  193. ->willReturn($connection);
  194. $resource->expects($this->any())->method('getTableName')->willReturnArgument(0);
  195. $this->tableResolver = $objectManager->getObject(
  196. \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver::class,
  197. [
  198. 'resource' => $resource
  199. ]
  200. );
  201. $traversableMock = $this->createMock(\Traversable::class);
  202. $dimensionsMock = $this->createMock(\Magento\Framework\Indexer\MultiDimensionProvider::class);
  203. $dimensionsMock->method('getIterator')->willReturn($traversableMock);
  204. $indexScopeResolverMock = $this->createMock(
  205. \Magento\Framework\Search\Request\IndexScopeResolverInterface::class
  206. );
  207. $dimensionFactoryMock = $this->createMock(
  208. \Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory::class
  209. );
  210. $dimensionFactoryMock->method('create')->willReturn($dimensionsMock);
  211. $indexScopeResolverMock->method('resolve')->willReturn('catalog_product_index_price');
  212. $this->model = $objectManager->getObject(
  213. \Magento\Elasticsearch\Model\ResourceModel\Index::class,
  214. [
  215. 'context' => $this->context,
  216. 'storeManager' => $this->storeManager,
  217. 'metadataPool' => $this->metadataPool,
  218. 'productRepository' => $this->productRepository,
  219. 'categoryRepository' => $this->categoryRepository,
  220. 'eavConfig' => $this->eavConfig,
  221. 'connectionName' => 'default',
  222. 'tableResolver' => $this->tableResolver,
  223. 'dimensionCollectionFactory' => $dimensionFactoryMock,
  224. ]
  225. );
  226. }
  227. /**
  228. * Test getPriceIndexDataEmpty method which return empty array
  229. */
  230. public function testGetPriceIndexData()
  231. {
  232. $connection = $this->connection;
  233. $select = $this->select;
  234. $connection->expects($this->any())
  235. ->method('select')
  236. ->willReturn($select);
  237. $select->expects($this->any())
  238. ->method('from')
  239. ->willReturnSelf();
  240. $connection->expects($this->once())
  241. ->method('fetchAll')
  242. ->with($select)
  243. ->willReturn([[
  244. 'website_id' => 1,
  245. 'entity_id' => 1,
  246. 'customer_group_id' => 1,
  247. 'min_price' => 1,
  248. ]]);
  249. $this->storeManager->expects($this->once())
  250. ->method('getStore')
  251. ->willReturn($this->storeInterface);
  252. $this->storeInterface->expects($this->once())
  253. ->method('getWebsiteId')
  254. ->willReturn(1);
  255. $this->assertEquals(
  256. [
  257. 1 => [
  258. 1 => 1,
  259. ],
  260. ],
  261. $this->model->getPriceIndexData([1 ], 1)
  262. );
  263. }
  264. /**
  265. * Test getPriceIndexDataEmpty method which return empty array
  266. */
  267. public function testGetPriceIndexDataEmpty()
  268. {
  269. $connection = $this->connection;
  270. $select = $this->select;
  271. $connection->expects($this->any())
  272. ->method('select')
  273. ->willReturn($select);
  274. $select->expects($this->any())
  275. ->method('from')
  276. ->willReturnSelf();
  277. $connection->expects($this->once())
  278. ->method('fetchAll')
  279. ->with($select)
  280. ->willReturn([]);
  281. $this->storeManager->expects($this->once())
  282. ->method('getStore')
  283. ->willReturn($this->storeInterface);
  284. $this->storeInterface->expects($this->once())
  285. ->method('getWebsiteId')
  286. ->willReturn(1);
  287. $this->assertEquals(
  288. [],
  289. $this->model->getPriceIndexData([1 ], 1)
  290. );
  291. }
  292. /**
  293. * Test getCategoryProductIndexData method
  294. */
  295. public function testGetCategoryProductIndexData()
  296. {
  297. $connection = $this->connection;
  298. $select = $this->select;
  299. $connection->expects($this->any())
  300. ->method('select')
  301. ->willReturn($select);
  302. $select->expects($this->any())
  303. ->method('from')
  304. ->with(
  305. ['catalog_category_product_index_store1'],
  306. ['category_id', 'product_id', 'position', 'store_id']
  307. )->willReturnSelf();
  308. $select->expects($this->any())
  309. ->method('where')
  310. ->willReturnSelf();
  311. $connection->expects($this->once())
  312. ->method('fetchAll')
  313. ->with($select)
  314. ->willReturn([[
  315. 'product_id' => 1,
  316. 'category_id' => 1,
  317. 'position' => 1,
  318. ]]);
  319. $this->assertEquals(
  320. [
  321. 1 => [
  322. 1 => 1,
  323. ],
  324. ],
  325. $this->model->getCategoryProductIndexData(1, [1, ])
  326. );
  327. }
  328. /**
  329. * Test getMovedCategoryProductIds method
  330. */
  331. public function testGetMovedCategoryProductIds()
  332. {
  333. $connection = $this->connection;
  334. $select = $this->select;
  335. $connection->expects($this->any())
  336. ->method('select')
  337. ->willReturn($select);
  338. $select->expects($this->any())
  339. ->method('distinct')
  340. ->willReturnSelf();
  341. $this->resources->expects($this->exactly(2))
  342. ->method('getTableName');
  343. $select->expects($this->any())
  344. ->method('from')
  345. ->willReturnSelf();
  346. $select->expects($this->any())
  347. ->method('join')
  348. ->willReturnSelf();
  349. $select->expects($this->any())
  350. ->method('where')
  351. ->willReturnSelf();
  352. $select->expects($this->any())
  353. ->method('orWhere')
  354. ->willReturnSelf();
  355. $connection->expects($this->once())
  356. ->method('fetchCol')
  357. ->with($select)
  358. ->willReturn([1, ]);
  359. $this->assertEquals([1, ], $this->model->getMovedCategoryProductIds(1));
  360. }
  361. /**
  362. * Test getFullProductIndexData method
  363. *
  364. * @param string $frontendInput
  365. * @param mixed $indexData
  366. * @return void
  367. * @dataProvider attributeCodeProvider
  368. */
  369. public function testGetFullProductIndexData($frontendInput, $indexData)
  370. {
  371. $this->productRepository->expects($this->once())
  372. ->method('getById')
  373. ->willReturn($this->product);
  374. $this->product->expects($this->once())
  375. ->method('getData')
  376. ->willReturn([
  377. 'name' => 'Product Name'
  378. ]);
  379. $this->eavConfig->expects($this->once())
  380. ->method('getEntityAttributeCodes')
  381. ->with('catalog_product')
  382. ->willReturn([
  383. 'name',
  384. ]);
  385. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  386. ->disableOriginalConstructor()
  387. ->setMethods([
  388. 'getFrontendInput',
  389. 'getOptions',
  390. 'getData',
  391. 'getAttributeId',
  392. ])
  393. ->getMock();
  394. $this->eavConfig->expects($this->once())
  395. ->method('getAttribute')
  396. ->with('catalog_product', 'name')
  397. ->willReturn($attributeMock);
  398. $attributeMock->expects($this->any())
  399. ->method('getAttributeId')
  400. ->willReturn(1);
  401. $attributeMock->expects($this->any())
  402. ->method('getFrontendInput')
  403. ->willReturn($frontendInput);
  404. $attributeOption = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
  405. $attributeOption->expects($this->any())->method('getValue')->willReturn('240-LV04');
  406. $attributeOption->expects($this->any())->method('getLabel')->willReturn('label');
  407. $attributeMock->expects($this->any())
  408. ->method('getOptions')
  409. ->willReturn([$attributeOption]);
  410. $this->assertInternalType(
  411. 'array',
  412. $this->model->getFullProductIndexData(
  413. 1,
  414. [
  415. 1 => $indexData
  416. ]
  417. )
  418. );
  419. }
  420. /**
  421. * Test getFullCategoryProductIndexData method
  422. */
  423. public function testGetFullCategoryProductIndexData()
  424. {
  425. $this->categoryRepository->expects($this->once())
  426. ->method('get')
  427. ->willReturn($this->category);
  428. $this->category->expects($this->once())
  429. ->method('getName')
  430. ->willReturn([
  431. 'name' => 'Category Name',
  432. ]);
  433. $connection = $this->connection;
  434. $select = $this->select;
  435. $connection->expects($this->any())
  436. ->method('select')
  437. ->willReturn($select);
  438. $select->expects($this->any())
  439. ->method('from')
  440. ->willReturnSelf();
  441. $select->expects($this->any())
  442. ->method('where')
  443. ->willReturnSelf();
  444. $connection->expects($this->once())
  445. ->method('fetchAll')
  446. ->with($select)
  447. ->willReturn([[
  448. 'product_id' => 1,
  449. 'category_id' => 1,
  450. 'position' => 1,
  451. ]]);
  452. $this->assertInternalType(
  453. 'array',
  454. $this->model->getFullCategoryProductIndexData(1, [1, ])
  455. );
  456. }
  457. /**
  458. * Provides data for testGetFullProductIndexData method.
  459. *
  460. * @return array
  461. */
  462. public static function attributeCodeProvider()
  463. {
  464. return [
  465. [
  466. 'string',
  467. '240-LV04',
  468. ],
  469. [
  470. 'select',
  471. '240-LV04',
  472. ],
  473. [
  474. 'select',
  475. [1, ],
  476. ],
  477. [
  478. 'select',
  479. [
  480. 1 => 1,
  481. ],
  482. ]
  483. ];
  484. }
  485. }