|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Test\Unit\Setup\Patch\Data; |
| 9 | + |
| 10 | +use Magento\Catalog\Setup\Patch\Data\UpdateMultiselectAttributesBackendTypes; |
| 11 | +use Magento\Eav\Setup\EavSetup; |
| 12 | +use Magento\Eav\Setup\EavSetupFactory; |
| 13 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 14 | +use Magento\Framework\DB\Select; |
| 15 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class UpdateMultiselectAttributesBackendTypesTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var ModuleDataSetupInterface|\PHPUnit\Framework\MockObject\MockObject |
| 22 | + */ |
| 23 | + private $dataSetup; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var EavSetupFactory|\PHPUnit\Framework\MockObject\MockObject |
| 27 | + */ |
| 28 | + private $eavSetupFactory; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var UpdateMultiselectAttributesBackendTypes |
| 32 | + */ |
| 33 | + private $model; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + $this->dataSetup = $this->createMock(ModuleDataSetupInterface::class); |
| 42 | + $this->eavSetupFactory = $this->createMock(EavSetupFactory::class); |
| 43 | + $this->model = new UpdateMultiselectAttributesBackendTypes($this->dataSetup, $this->eavSetupFactory); |
| 44 | + } |
| 45 | + |
| 46 | + public function testApply(): void |
| 47 | + { |
| 48 | + $attributeIds = [3, 7]; |
| 49 | + $entityTypeId = 4; |
| 50 | + $eavSetup = $this->createMock(EavSetup::class); |
| 51 | + $connection = $this->createMock(AdapterInterface::class); |
| 52 | + $select1 = $this->createMock(Select::class); |
| 53 | + $select2 = $this->createMock(Select::class); |
| 54 | + $select3 = $this->createMock(Select::class); |
| 55 | + $this->eavSetupFactory->method('create') |
| 56 | + ->willReturn($eavSetup); |
| 57 | + $this->dataSetup->method('getConnection') |
| 58 | + ->willReturn($connection); |
| 59 | + $this->dataSetup->method('getTable') |
| 60 | + ->willReturnArgument(0); |
| 61 | + $eavSetup->method('getEntityTypeId') |
| 62 | + ->willReturn(4); |
| 63 | + $eavSetup->method('updateAttribute') |
| 64 | + ->withConsecutive( |
| 65 | + [$entityTypeId, 3, 'backend_type', 'text'], |
| 66 | + [$entityTypeId, 7, 'backend_type', 'text'] |
| 67 | + ); |
| 68 | + $connection->expects($this->exactly(3)) |
| 69 | + ->method('select') |
| 70 | + ->willReturnOnConsecutiveCalls($select1, $select2, $select3); |
| 71 | + $connection->method('describeTable') |
| 72 | + ->willReturn( |
| 73 | + [ |
| 74 | + 'value_id' => [], |
| 75 | + 'attribute_id' => [], |
| 76 | + 'store_id' => [], |
| 77 | + 'value' => [], |
| 78 | + 'row_id' => [], |
| 79 | + ] |
| 80 | + ); |
| 81 | + $connection->method('fetchCol') |
| 82 | + ->with($select1) |
| 83 | + ->willReturn($attributeIds); |
| 84 | + $connection->method('insertFromSelect') |
| 85 | + ->with($select3, 'catalog_product_entity_text', ['attribute_id', 'store_id', 'value', 'row_id']) |
| 86 | + ->willReturn(''); |
| 87 | + $connection->method('deleteFromSelect') |
| 88 | + ->with($select2, 'catalog_product_entity_varchar') |
| 89 | + ->willReturn(''); |
| 90 | + $select1->method('from') |
| 91 | + ->with('eav_attribute', ['attribute_id']) |
| 92 | + ->willReturnSelf(); |
| 93 | + $select1->method('where') |
| 94 | + ->withConsecutive( |
| 95 | + ['entity_type_id = ?', $entityTypeId], |
| 96 | + ['backend_type = ?', 'varchar'], |
| 97 | + ['frontend_input = ?', 'multiselect'] |
| 98 | + ) |
| 99 | + ->willReturnSelf(); |
| 100 | + $select2->method('from') |
| 101 | + ->with('catalog_product_entity_varchar') |
| 102 | + ->willReturnSelf(); |
| 103 | + $select2->method('where') |
| 104 | + ->with('attribute_id in (?)', $attributeIds) |
| 105 | + ->willReturnSelf(); |
| 106 | + $select3->method('from') |
| 107 | + ->with('catalog_product_entity_varchar', ['attribute_id', 'store_id', 'value', 'row_id']) |
| 108 | + ->willReturnSelf(); |
| 109 | + $select3->method('where') |
| 110 | + ->with('attribute_id in (?)', $attributeIds) |
| 111 | + ->willReturnSelf(); |
| 112 | + $this->model->apply(); |
| 113 | + } |
| 114 | +} |
0 commit comments