|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product; |
| 8 | + |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 10 | + |
| 11 | +class CategoryProcessorTest extends \PHPUnit\Framework\TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 15 | + */ |
| 16 | + protected $objectManager; |
| 17 | + |
| 18 | + /** @var ObjectManagerHelper */ |
| 19 | + protected $objectManagerHelper; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\CatalogImportExport\Model\Import\Product\LinkProcessor|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $linkProcessor; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType |
| 28 | + */ |
| 29 | + protected $product; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\ImportExport\Model\ResourceModel\Helper |
| 33 | + */ |
| 34 | + protected $resourceHelper; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Catalog\Model\ResourceModel\Product\Link |
| 38 | + */ |
| 39 | + protected $resource; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Magento\Catalog\Model\ResourceModel\Product\LinkFactory |
| 43 | + */ |
| 44 | + protected $linkFactory; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\CatalogImportExport\Model\Import\Product\SkuProcessor::class |
| 48 | + */ |
| 49 | + protected $skuProcessor; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Psr\Log\LoggerInterface |
| 53 | + */ |
| 54 | + protected $logger; |
| 55 | + |
| 56 | + protected function setUp() |
| 57 | + { |
| 58 | + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 59 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 60 | + |
| 61 | + $this->resourceHelper = $this->createMock(\Magento\ImportExport\Model\ResourceModel\Helper::class); |
| 62 | + |
| 63 | + $this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Link::class); |
| 64 | + $this->resource->method('getMainTable')->willReturn('main_link_table'); |
| 65 | + |
| 66 | + $this->linkFactory = $this->createPartialMock(\Magento\Catalog\Model\ResourceModel\Product\LinkFactory::class, ['create']); |
| 67 | + $this->linkFactory->method('create')->willReturn($this->resource); |
| 68 | + |
| 69 | + $this->skuProcessor = $this->createMock(\Magento\CatalogImportExport\Model\Import\Product\SkuProcessor::class, []); |
| 70 | + $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @dataProvider diConfigDataProvider |
| 75 | + * @param $expectedCallCount |
| 76 | + * @param $linkToNameId |
| 77 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 78 | + */ |
| 79 | + public function testSaveLinks($expectedCallCount, $linkToNameId) |
| 80 | + { |
| 81 | + $this->linkProcessor = |
| 82 | + new \Magento\CatalogImportExport\Model\Import\Product\LinkProcessor( |
| 83 | + $this->linkFactory, |
| 84 | + $this->resourceHelper, |
| 85 | + $this->skuProcessor, |
| 86 | + $this->logger, |
| 87 | + $linkToNameId |
| 88 | + ); |
| 89 | + |
| 90 | + $importEntity = $this->createMock(\Magento\CatalogImportExport\Model\Import\Product::class); |
| 91 | + $connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class); |
| 92 | + $importEntity->method('getConnection')->willReturn($connection); |
| 93 | + $select = $this->createMock(\Magento\Framework\DB\Select::class); |
| 94 | + |
| 95 | + // expect one call per linkToNameId |
| 96 | + $connection->expects($this->exactly($expectedCallCount))->method('select')->willReturn($select); |
| 97 | + |
| 98 | + $select->method('from')->willReturn($select); |
| 99 | + |
| 100 | + $dataSourceModel = $this->createMock(\Magento\ImportExport\Model\ResourceModel\Import\Data::class); |
| 101 | + |
| 102 | + $this->linkProcessor->saveLinks($importEntity, $dataSourceModel, '_related_'); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array |
| 107 | + */ |
| 108 | + public function diConfigDataProvider() |
| 109 | + { |
| 110 | + return [ |
| 111 | + [3, [ |
| 112 | + '_related_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_RELATED, |
| 113 | + '_crosssell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_CROSSSELL, |
| 114 | + '_upsell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL, |
| 115 | + ]], |
| 116 | + [4, [ |
| 117 | + '_related_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_RELATED, |
| 118 | + '_crosssell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_CROSSSELL, |
| 119 | + '_upsell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL, |
| 120 | + '_custom_link_' => 9, |
| 121 | + ]], |
| 122 | + ]; |
| 123 | + } |
| 124 | +} |
0 commit comments