DataTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Test\Unit\Helper;
  7. use Magento\Catalog\Model\Product\Image\UrlBuilder;
  8. use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\Swatches\Model\ResourceModel\Swatch\Collection;
  11. use Magento\Swatches\Model\SwatchAttributesProvider;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class DataTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Helper\Image */
  18. protected $imageHelperMock;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory */
  20. protected $productCollectionFactoryMock;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\ResourceModel\Product\Collection */
  22. protected $productCollectionMock;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\ConfigurableProduct\Model\Product\Type\Configurable */
  24. protected $configurableMock;
  25. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\ProductFactory */
  26. protected $productModelFactoryMock;
  27. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product */
  28. protected $productMock;
  29. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManager */
  30. protected $storeManagerMock;
  31. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory */
  32. protected $swatchCollectionFactoryMock;
  33. /** @var \PHPUnit_Framework_MockObject_MockObject|Attribute */
  34. protected $attributeMock;
  35. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  36. protected $objectManager;
  37. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager|\Magento\Swatches\Helper\Data */
  38. protected $swatchHelperObject;
  39. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Api\ProductRepositoryInterface */
  40. protected $productRepoMock;
  41. /** @var \PHPUnit_Framework_MockObject_MockObject|MetadataPool */
  42. private $metaDataPoolMock;
  43. /**
  44. * @var SwatchAttributesProvider|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $swatchAttributesProvider;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product\Image\UrlBuilder
  49. */
  50. private $imageUrlBuilderMock;
  51. protected function setUp()
  52. {
  53. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  54. $this->imageHelperMock = $this->createMock(\Magento\Catalog\Helper\Image::class);
  55. $this->productCollectionFactoryMock = $this->createPartialMock(
  56. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class,
  57. ['create']
  58. );
  59. $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  60. $this->productCollectionMock = $this->objectManager->getCollectionMock(
  61. \Magento\Catalog\Model\ResourceModel\Product\Collection::class,
  62. [
  63. $this->productMock,
  64. $this->productMock,
  65. ]
  66. );
  67. $this->configurableMock = $this->createMock(
  68. \Magento\ConfigurableProduct\Model\Product\Type\Configurable::class
  69. );
  70. $this->productModelFactoryMock = $this->createPartialMock(
  71. \Magento\Catalog\Model\ProductFactory::class,
  72. ['create']
  73. );
  74. $this->productRepoMock = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  75. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
  76. $this->swatchCollectionFactoryMock = $this->createPartialMock(
  77. \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory::class,
  78. ['create']
  79. );
  80. $this->attributeMock = $this->getMockBuilder(Attribute::class)
  81. ->disableOriginalConstructor()
  82. ->setMethods(['setStoreId', 'getData', 'setData', 'getSource', 'hasData'])
  83. ->getMock();
  84. $this->metaDataPoolMock = $this->getMockBuilder(MetadataPool::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $serializer = $this->createPartialMock(
  88. \Magento\Framework\Serialize\Serializer\Json::class,
  89. ['serialize', 'unserialize']
  90. );
  91. $serializer->expects($this->any())
  92. ->method('serialize')->willReturnCallback(function ($parameter) {
  93. return json_encode($parameter);
  94. });
  95. $serializer->expects($this->any())
  96. ->method('unserialize')->willReturnCallback(function ($parameter) {
  97. return json_decode($parameter, true);
  98. });
  99. $this->swatchAttributesProvider = $this->getMockBuilder(SwatchAttributesProvider::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->imageUrlBuilderMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Image\UrlBuilder::class)
  103. ->disableOriginalConstructor()
  104. ->setMethods(['getUrl'])
  105. ->getMock();
  106. $this->swatchHelperObject = $this->objectManager->getObject(
  107. \Magento\Swatches\Helper\Data::class,
  108. [
  109. 'productCollectionFactory' => $this->productCollectionFactoryMock,
  110. 'configurable' => $this->configurableMock,
  111. 'productRepository' => $this->productRepoMock,
  112. 'storeManager' => $this->storeManagerMock,
  113. 'swatchCollectionFactory' => $this->swatchCollectionFactoryMock,
  114. 'imageUrlBuilder' => $this->imageUrlBuilderMock,
  115. 'serializer' => $serializer,
  116. 'swatchAttributesProvider' => $this->swatchAttributesProvider,
  117. ]
  118. );
  119. $this->objectManager->setBackwardCompatibleProperty(
  120. $this->swatchHelperObject,
  121. 'metadataPool',
  122. $this->metaDataPoolMock
  123. );
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function dataForAdditionalData()
  129. {
  130. $additionalData = [
  131. 'swatch_input_type' => 'visual',
  132. 'update_product_preview_image' => 1,
  133. 'use_product_image_for_swatch' => 0
  134. ];
  135. return [
  136. [
  137. json_encode($additionalData),
  138. [
  139. 'getData' => 1,
  140. 'setData' => 3,
  141. ]
  142. ],
  143. [
  144. null,
  145. [
  146. 'getData' => 1,
  147. 'setData' => 0,
  148. ]
  149. ],
  150. ];
  151. }
  152. /**
  153. * @dataProvider dataForAssembleEavAttribute
  154. */
  155. public function testAssembleAdditionalDataEavAttribute($dataFromDb, $attributeData)
  156. {
  157. $this->attributeMock
  158. ->expects($this->at(0))
  159. ->method('getData')
  160. ->with('additional_data')
  161. ->will($this->returnValue($dataFromDb));
  162. $i = 1;
  163. foreach ($attributeData as $key => $value) {
  164. $this->attributeMock
  165. ->expects($this->at($i))
  166. ->method('getData')
  167. ->with($key)
  168. ->willReturn($value);
  169. $i++;
  170. }
  171. $this->attributeMock->expects($this->once())->method('setData');
  172. $this->swatchHelperObject->assembleAdditionalDataEavAttribute($this->attributeMock);
  173. }
  174. /**
  175. * @return array
  176. */
  177. public function dataForAssembleEavAttribute()
  178. {
  179. $additionalData = [
  180. 'swatch_input_type' => 'visual',
  181. 'update_product_preview_image' => 1,
  182. 'use_product_image_for_swatch' => 0
  183. ];
  184. return [
  185. [
  186. json_encode($additionalData),
  187. [
  188. 'swatch_input_type' => 'visual',
  189. 'update_product_preview_image' => 1,
  190. 'use_product_image_for_swatch' => 1,
  191. ],
  192. ],
  193. [
  194. null,
  195. [
  196. 'swatch_input_type' => null,
  197. 'update_product_preview_image' => 0,
  198. 'use_product_image_for_swatch' => 0,
  199. ],
  200. ],
  201. ];
  202. }
  203. /**
  204. * @dataProvider dataForVariationWithSwatchImage
  205. */
  206. public function testLoadFirstVariationWithSwatchImage($imageTypes, $expected, $requiredAttributes)
  207. {
  208. $this->getSwatchAttributes($this->productMock);
  209. $this->getUsedProducts($imageTypes + $requiredAttributes, $imageTypes);
  210. $result = $this->swatchHelperObject->loadFirstVariationWithSwatchImage($this->productMock, $requiredAttributes);
  211. if ($expected === false) {
  212. $this->assertFalse($result);
  213. } else {
  214. $this->assertInstanceOf(\Magento\Catalog\Model\Product::class, $result);
  215. }
  216. }
  217. /**
  218. * @return array
  219. */
  220. public function dataForVariationWithSwatchImage()
  221. {
  222. return [
  223. [
  224. [
  225. 'image' => '/m/a/magento.png',
  226. 'small_image' => '/m/a/magento.png',
  227. 'thumbnail' => '/m/a/magento.png',
  228. 'swatch_image' => '/m/a/magento.png', //important
  229. ],
  230. \Magento\Catalog\Model\Product::class,
  231. ['color' => 31],
  232. ],
  233. [
  234. [
  235. 'image' => '/m/a/magento.png',
  236. 'small_image' => '/m/a/magento.png',
  237. 'thumbnail' => '/m/a/magento.png',
  238. ],
  239. false,
  240. ['size' => 31],
  241. ],
  242. ];
  243. }
  244. /**
  245. * @dataProvider dataForCreateSwatchProductByFallback
  246. */
  247. public function testLoadVariationByFallback($product)
  248. {
  249. $metadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadataInterface::class);
  250. $this->metaDataPoolMock->expects($this->once())->method('getMetadata')->willReturn($metadataMock);
  251. $metadataMock->expects($this->once())->method('getLinkField')->willReturn('id');
  252. $this->getSwatchAttributes($product);
  253. $this->prepareVariationCollection();
  254. $this->productCollectionMock->method('getFirstItem')->willReturn($this->productMock);
  255. $this->productMock->method('getData')->with('id')->willReturn(95);
  256. $this->productModelFactoryMock->method('create')->willReturn($this->productMock);
  257. $this->productMock->method('load')->with(95)->will($this->returnSelf());
  258. $this->swatchHelperObject->loadVariationByFallback($this->productMock, ['color' => 31]);
  259. }
  260. /**
  261. * @dataProvider dataForVariationWithImage
  262. */
  263. public function testLoadFirstVariationWithImage($imageTypes, $expected, $requiredAttributes)
  264. {
  265. $this->getSwatchAttributes($this->productMock);
  266. $this->getUsedProducts($imageTypes + $requiredAttributes, $imageTypes);
  267. $result = $this->swatchHelperObject->loadFirstVariationWithImage($this->productMock, $requiredAttributes);
  268. if ($expected === false) {
  269. $this->assertFalse($result);
  270. } else {
  271. $this->assertInstanceOf(\Magento\Catalog\Model\Product::class, $result);
  272. }
  273. }
  274. /**
  275. * @return array
  276. */
  277. public function dataForVariationWithImage()
  278. {
  279. return [
  280. [
  281. [
  282. 'image' => '/m/a/magento.png', //important
  283. 'small_image' => '/m/a/magento.png',
  284. 'thumbnail' => '/m/a/magento.png',
  285. 'swatch_image' => '/m/a/magento.png',
  286. ],
  287. \Magento\Catalog\Model\Product::class,
  288. ['color' => 31],
  289. ],
  290. [
  291. [
  292. 'small_image' => '/m/a/magento.png',
  293. 'thumbnail' => '/m/a/magento.png',
  294. 'swatch_image' => '/m/a/magento.png',
  295. ],
  296. false,
  297. ['size' => 31],
  298. ],
  299. ];
  300. }
  301. public function testLoadFirstVariationWithImageNoProduct()
  302. {
  303. $result = $this->swatchHelperObject->loadVariationByFallback($this->productMock, ['color' => 31]);
  304. $this->assertFalse($result);
  305. }
  306. public function testLoadVariationByFallbackWithoutProduct()
  307. {
  308. $result = $this->swatchHelperObject->loadFirstVariationWithImage($this->productMock, ['color' => 31]);
  309. $this->assertFalse($result);
  310. }
  311. /**
  312. * @dataProvider dataForMediaGallery
  313. */
  314. public function testGetProductMediaGallery($mediaGallery, $image)
  315. {
  316. $mediaGalleryEntries = [];
  317. $id = 0;
  318. $mediaUrls = [];
  319. foreach ($mediaGallery as $mediaType => $mediaFile) {
  320. $mediaGalleryEntryMock = $this->getMockBuilder(
  321. \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
  322. )->getMock();
  323. $mediaGalleryEntryMock->expects($this->atLeastOnce())
  324. ->method('isDisabled')
  325. ->willReturn(false);
  326. $mediaGalleryEntryMock->expects($this->atLeastOnce())
  327. ->method('getTypes')
  328. ->willReturn([$mediaType]);
  329. $mediaGalleryEntryMock->expects($this->atLeastOnce())
  330. ->method('getFile')
  331. ->willReturn($mediaFile);
  332. $mediaGalleryEntryMock->expects($this->atLeastOnce())
  333. ->method('getId')
  334. ->willReturn(++$id);
  335. $mediaGalleryEntries[] = $mediaGalleryEntryMock;
  336. $mediaUrls[] = [$mediaFile, 'product_swatch_image_large', 'http://full_path_to_image' . $mediaFile];
  337. $mediaUrls[] = [$mediaFile, 'product_swatch_image_medium' ,'http://full_path_to_image' . $mediaFile];
  338. $mediaUrls[] = [$mediaFile, 'product_swatch_image_small','http://full_path_to_image' . $mediaFile];
  339. }
  340. $this->productMock->expects($this->once())
  341. ->method('getMediaGalleryEntries')
  342. ->willReturn($mediaGalleryEntries);
  343. if ($mediaGallery) {
  344. $this->imageUrlBuilderMock->expects($this->any())
  345. ->method('getUrl')
  346. ->willReturnMap($mediaUrls);
  347. }
  348. $productMediaGallery = $this->swatchHelperObject->getProductMediaGallery($this->productMock);
  349. if ($mediaGallery) {
  350. $this->assertContains($image, $productMediaGallery['large']);
  351. $this->assertContains($image, $productMediaGallery['medium']);
  352. $this->assertContains($image, $productMediaGallery['small']);
  353. } else {
  354. $this->assertEmpty($productMediaGallery);
  355. }
  356. }
  357. /**
  358. * @return array
  359. */
  360. public function dataForMediaGallery()
  361. {
  362. return [
  363. [
  364. [
  365. 'image' => '/m/a/magento1.png',
  366. 'small_image' => '/m/a/magento2.png',
  367. 'thumbnail' => '/m/a/magento3.png',
  368. 'swatch_image' => '/m/a/magento4.png',
  369. ],
  370. '/m/a/magento1.png',
  371. ],
  372. [
  373. [
  374. 'small_image' => '/m/a/magento4.png',
  375. 'thumbnail' => '/m/a/magento5.png',
  376. 'swatch_image' => '/m/a/magento6.png',
  377. ],
  378. '/m/a/magento4.png',
  379. ],
  380. [
  381. [],
  382. ''
  383. ],
  384. ];
  385. }
  386. protected function getSwatchAttributes()
  387. {
  388. $this->getAttributesFromConfigurable();
  389. $returnFromProvideMethod = [$this->attributeMock];
  390. $this->swatchAttributesProvider
  391. ->method('provide')
  392. ->with($this->productMock)
  393. ->willReturn($returnFromProvideMethod);
  394. }
  395. /**
  396. * @param array $attributes
  397. * @param array $imageTypes
  398. */
  399. protected function getUsedProducts(array $attributes, array $imageTypes)
  400. {
  401. $this->productMock
  402. ->expects($this->atLeastOnce())
  403. ->method('getTypeInstance')
  404. ->willReturn($this->configurableMock);
  405. $simpleProducts = [];
  406. for ($i = 0; $i < 2; $i++) {
  407. $simpleProduct = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  408. ->disableOriginalConstructor()
  409. ->setMethods(['hasData', 'getMediaGalleryEntries'])
  410. ->getMock();
  411. $simpleProduct->setData($attributes);
  412. $mediaGalleryEntries = [];
  413. foreach (array_keys($imageTypes) as $mediaType) {
  414. $mediaGalleryEntryMock = $this->getMockBuilder(
  415. \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
  416. )->getMock();
  417. $mediaGalleryEntryMock->expects($this->any())
  418. ->method('isDisabled')
  419. ->willReturn(false);
  420. $mediaGalleryEntryMock->expects($this->any())
  421. ->method('getTypes')
  422. ->willReturn([$mediaType]);
  423. $mediaGalleryEntries[] = $mediaGalleryEntryMock;
  424. }
  425. $simpleProduct->expects($this->any())
  426. ->method('getMediaGalleryEntries')
  427. ->willReturn($mediaGalleryEntries);
  428. $simpleProducts[] = $simpleProduct;
  429. }
  430. $this->configurableMock->expects($this->once())
  431. ->method('getUsedProducts')
  432. ->with($this->productMock)
  433. ->willReturn($simpleProducts);
  434. }
  435. protected function getAttributesFromConfigurable()
  436. {
  437. $confAttribute = $this->createMock(
  438. \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute::class
  439. );
  440. $this->configurableMock
  441. ->expects($this->any())
  442. ->method('getConfigurableAttributes')
  443. ->with($this->productMock)
  444. ->willReturn([$confAttribute, $confAttribute]);
  445. $confAttribute
  446. ->expects($this->any())
  447. ->method('__call')
  448. ->with('getProductAttribute')
  449. ->willReturn($this->attributeMock);
  450. }
  451. protected function prepareVariationCollection()
  452. {
  453. $this->productCollectionFactoryMock
  454. ->expects($this->any())
  455. ->method('create')
  456. ->willReturn($this->productCollectionMock);
  457. $this->addfilterByParent();
  458. }
  459. protected function addfilterByParent()
  460. {
  461. $this->productCollectionMock
  462. ->method('getTable')
  463. ->with('catalog_product_relation')
  464. ->willReturn('catalog_product_relation');
  465. $zendDbSelectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  466. $this->productCollectionMock->method('getSelect')->willReturn($zendDbSelectMock);
  467. $zendDbSelectMock->method('join')->willReturn($zendDbSelectMock);
  468. $zendDbSelectMock->method('where')->willReturn($zendDbSelectMock);
  469. }
  470. /**
  471. * @return array
  472. */
  473. public function dataForCreateSwatchProduct()
  474. {
  475. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  476. return [
  477. [
  478. $productMock,
  479. [
  480. 'image' => '',
  481. 'small_image' => '',
  482. 'thumbnail' => '',
  483. 'swatch_image' => '',
  484. ]
  485. ],
  486. [
  487. $productMock,
  488. [
  489. 'small_image' => 'img1.png',
  490. 'thumbnail' => 'img1.png',
  491. ]
  492. ],
  493. [
  494. $productMock,
  495. []
  496. ],
  497. [
  498. $productMock,
  499. [
  500. 'image' => 'img1.png',
  501. 'small_image' => 'img1.png',
  502. 'thumbnail' => 'img1.png',
  503. ]
  504. ],
  505. ];
  506. }
  507. /**
  508. * @return array
  509. */
  510. public function dataForCreateSwatchProductByFallback()
  511. {
  512. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  513. return [
  514. [
  515. 95,
  516. ],
  517. [
  518. $productMock,
  519. ],
  520. ];
  521. }
  522. /**
  523. * @dataProvider dataForGettingSwatchAsArray
  524. */
  525. public function testGetSwatchAttributesAsArray($optionsArray, $attributeData, $expected)
  526. {
  527. $this->swatchAttributesProvider
  528. ->method('provide')
  529. ->with($this->productMock)
  530. ->willReturn([$this->attributeMock]);
  531. $storeId = 1;
  532. $this->attributeMock->method('setStoreId')->with($storeId)->will($this->returnSelf());
  533. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  534. $storeMock->method('getId')->willReturn($storeId);
  535. $this->storeManagerMock->method('getStore')->willReturn($storeMock);
  536. $this->attributeMock->method('getData')->with('')->willReturn($attributeData);
  537. $sourceMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class);
  538. $sourceMock->expects($this->any())->method('getAllOptions')->with(false)->willReturn($optionsArray);
  539. $this->attributeMock->method('getSource')->willReturn($sourceMock);
  540. $result = $this->swatchHelperObject->getSwatchAttributesAsArray($this->productMock);
  541. $this->assertEquals($result, $expected);
  542. }
  543. /**
  544. * @return array
  545. */
  546. public function dataForGettingSwatchAsArray()
  547. {
  548. return [
  549. [
  550. [
  551. ['value' => 45, 'label' => 'green'],
  552. ['value' => 46, 'label' => 'yellow'],
  553. ['value' => 47, 'label' => 'red'],
  554. ['value' => 48, 'label' => 'blue'],
  555. ],
  556. [
  557. 'attribute_id' => 52
  558. ],
  559. [
  560. 52 => [
  561. 'attribute_id' => 52,
  562. 'options' => [
  563. 45 => 'green',
  564. 46 => 'yellow',
  565. 47 => 'red',
  566. 48 => 'blue',
  567. ],
  568. ]
  569. ],
  570. ],
  571. [
  572. [
  573. ['value' => 45, 'label' => 'green'],
  574. ['value' => 46, 'label' => 'yellow'],
  575. ],
  576. [
  577. 'attribute_id' => 324
  578. ],
  579. [
  580. 324 => [
  581. 'attribute_id' => 324,
  582. 'options' => [
  583. 45 => 'green',
  584. 46 => 'yellow',
  585. ],
  586. ]
  587. ],
  588. ],
  589. ];
  590. }
  591. public function testGetSwatchesByOptionsIdIf1()
  592. {
  593. $swatchMock = $this->createMock(\Magento\Swatches\Model\Swatch::class);
  594. $optionsData = [
  595. [
  596. 'type' => 1,
  597. 'store_id' => 1,
  598. 'value' => '#324234',
  599. 'option_id' => 35,
  600. 'id' => 423,
  601. ],
  602. [
  603. 'type' => 0,
  604. 'store_id' => 0,
  605. 'value' => 'test2',
  606. 'option_id' => 35,
  607. 'id' => 424,
  608. ]
  609. ];
  610. $swatchMock->expects($this->at(0))->method('offsetGet')->with('type')
  611. ->willReturn($optionsData[0]['type']);
  612. $swatchMock->expects($this->at(1))->method('offsetGet')->with('option_id')
  613. ->willReturn($optionsData[0]['option_id']);
  614. $swatchMock->expects($this->at(2))->method('getData')->with('')
  615. ->willReturn($optionsData[0]);
  616. $swatchMock->expects($this->at(3))->method('offsetGet')->with('type')
  617. ->willReturn($optionsData[1]['type']);
  618. $swatchMock->expects($this->at(4))->method('offsetGet')->with('store_id')
  619. ->willReturn($optionsData[1]['store_id']);
  620. $swatchMock->expects($this->at(5))->method('offsetGet')->with('store_id')
  621. ->willReturn($optionsData[1]['store_id']);
  622. $swatchMock->expects($this->at(6))->method('offsetGet')->with('option_id')
  623. ->willReturn($optionsData[1]['option_id']);
  624. $swatchMock->expects($this->at(7))->method('getData')->with('')
  625. ->willReturn($optionsData[1]);
  626. $swatchCollectionMock = $this->objectManager
  627. ->getCollectionMock(Collection::class, [$swatchMock, $swatchMock]);
  628. $swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf());
  629. $this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock);
  630. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  631. $this->storeManagerMock->method('getStore')->willReturn($storeMock);
  632. $storeMock->method('getId')->willReturn(1);
  633. $this->swatchHelperObject->getSwatchesByOptionsId([35]);
  634. }
  635. public function testGetSwatchesByOptionsIdIf2()
  636. {
  637. $swatchMock = $this->createMock(\Magento\Swatches\Model\Swatch::class);
  638. $optionsData = [
  639. [
  640. 'type' => 0,
  641. 'store_id' => 1,
  642. 'value' => 'test',
  643. 'option_id' => 35,
  644. 'id' => 487,
  645. ],
  646. [
  647. 'type' => 0,
  648. 'store_id' => 1,
  649. 'value' => 'test2',
  650. 'option_id' => 36,
  651. 'id' => 488,
  652. ]
  653. ];
  654. $swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn(0);
  655. $swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn(1);
  656. $swatchMock->expects($this->at(2))->method('offsetGet')->with('value')->willReturn('test');
  657. $swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn(35);
  658. $swatchMock->expects($this->at(4))->method('getData')->with('')->willReturn($optionsData[0]);
  659. $swatchMock->expects($this->at(5))->method('offsetGet')->with('type')->willReturn(0);
  660. $swatchMock->expects($this->at(6))->method('offsetGet')->with('store_id')->willReturn(1);
  661. $swatchMock->expects($this->at(7))->method('offsetGet')->with('value')->willReturn('test2');
  662. $swatchMock->expects($this->at(8))->method('offsetGet')->with('option_id')->willReturn(36);
  663. $swatchMock->expects($this->at(9))->method('getData')->with('')->willReturn($optionsData[1]);
  664. $swatchCollectionMock = $this->objectManager->getCollectionMock(
  665. Collection::class,
  666. [
  667. $swatchMock,
  668. $swatchMock,
  669. ]
  670. );
  671. $this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock);
  672. $swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf());
  673. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  674. $this->storeManagerMock->method('getStore')->willReturn($storeMock);
  675. $storeMock->method('getId')->willReturn(1);
  676. $this->swatchHelperObject->getSwatchesByOptionsId([35]);
  677. }
  678. public function testGetSwatchesByOptionsIdIf3()
  679. {
  680. $swatchMock = $this->createMock(\Magento\Swatches\Model\Swatch::class);
  681. $optionsData = [
  682. 'type' => 0,
  683. 'store_id' => 0,
  684. 'value' => 'test_test',
  685. 'option_id' => 35,
  686. 'id' => 423,
  687. ];
  688. $swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn(0);
  689. $swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn(0);
  690. $swatchMock->expects($this->at(2))->method('offsetGet')->with('store_id')->willReturn(0);
  691. $swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn(35);
  692. $swatchMock->expects($this->at(4))->method('getData')->with('')->willReturn($optionsData);
  693. $swatchCollectionMock = $this->objectManager->getCollectionMock(
  694. Collection::class,
  695. [
  696. $swatchMock,
  697. ]
  698. );
  699. $this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock);
  700. $swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf());
  701. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  702. $this->storeManagerMock->method('getStore')->willReturn($storeMock);
  703. $storeMock->method('getId')->willReturn(1);
  704. $this->swatchHelperObject->getSwatchesByOptionsId([35]);
  705. }
  706. public function testIsProductHasSwatch()
  707. {
  708. $this->getSwatchAttributes();
  709. $result = $this->swatchHelperObject->isProductHasSwatch($this->productMock);
  710. $this->assertEquals(true, $result);
  711. }
  712. }