|
| 1 | +<?php |
| 2 | +/************************* |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * *********************** |
| 8 | + */ |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Magento\InventoryConfigurableProduct\Test\Unit\Plugin\Model\ResourceModel\Attribute; |
| 12 | + |
| 13 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\DB\Select; |
| 16 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 17 | +use Magento\Framework\App\ScopeInterface; |
| 18 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 19 | +use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface; |
| 20 | +use Magento\Framework\EntityManager\EntityMetadataInterface; |
| 21 | +use Magento\Framework\EntityManager\MetadataPool; |
| 22 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 23 | +use Magento\InventoryConfigurableProduct\Plugin\Model\ResourceModel\Attribute\IsEnabledOptionSelectBuilder; |
| 24 | +use Magento\Store\Model\Store; |
| 25 | +use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus; |
| 26 | +use PHPUnit\Framework\MockObject\Exception; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | + |
| 30 | +/** |
| 31 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 32 | + */ |
| 33 | +class IsEnabledOptionSelectBuilderTest extends TestCase |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var ProductAttributeRepositoryInterface|MockObject |
| 37 | + */ |
| 38 | + private ProductAttributeRepositoryInterface $attributeRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var MetadataPool|MockObject |
| 42 | + */ |
| 43 | + private MetadataPool $metadataPool; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function setUp(): void |
| 49 | + { |
| 50 | + $this->attributeRepository = $this->createMock(ProductAttributeRepositoryInterface::class); |
| 51 | + $this->metadataPool = $this->createMock(MetadataPool::class); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return void |
| 56 | + * @throws NoSuchEntityException |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + public function testAfterGetSelect(): void |
| 60 | + { |
| 61 | + $subject = $this->createMock(OptionSelectBuilderInterface::class); |
| 62 | + $superAttribute = $this->createMock(AbstractAttribute::class); |
| 63 | + $productId = 1; |
| 64 | + |
| 65 | + $scope = $this->createMock(ScopeInterface::class); |
| 66 | + $scope->expects($this->once())->method('getId')->willReturn(1); |
| 67 | + |
| 68 | + $status = $this->createMock(AbstractAttribute::class); |
| 69 | + $status->expects($this->exactly(2)) |
| 70 | + ->method('getBackendTable') |
| 71 | + ->willReturn('catalog_product_entity_int'); |
| 72 | + $status->expects($this->exactly(2))->method('getAttributeId')->willReturn(95); |
| 73 | + |
| 74 | + $metadata = $this->createMock(EntityMetadataInterface::class); |
| 75 | + $metadata->expects($this->once())->method('getLinkField')->willReturn('row_id'); |
| 76 | + $this->metadataPool->expects($this->once())->method('getMetadata') |
| 77 | + ->with(ProductInterface::class) |
| 78 | + ->willReturn($metadata); |
| 79 | + $this->attributeRepository->expects($this->once()) |
| 80 | + ->method('get') |
| 81 | + ->with(ProductInterface::STATUS) |
| 82 | + ->willReturn($status); |
| 83 | + |
| 84 | + $adapter = $this->createMock(AdapterInterface::class); |
| 85 | + $adapter->expects($this->once()) |
| 86 | + ->method('getIfNullSql') |
| 87 | + ->with('entity_status_store.value', 'entity_status_global.value') |
| 88 | + ->willReturn('IFNULL(entity_status_store.value, entity_status_global.value) = 1'); |
| 89 | + |
| 90 | + $select = $this->createMock(Select::class); |
| 91 | + $select->expects($this->once())->method('getConnection')->willReturn($adapter); |
| 92 | + $select->expects($this->once())->method('joinInner')->with( |
| 93 | + ['entity_status_global' => 'catalog_product_entity_int'], |
| 94 | + "entity_status_global.row_id = entity.row_id" |
| 95 | + . " AND entity_status_global.attribute_id = 95" |
| 96 | + . " AND entity_status_global.store_id = " . Store::DEFAULT_STORE_ID, |
| 97 | + [] |
| 98 | + )->willReturnSelf(); |
| 99 | + $select->expects($this->once())->method('joinLeft')->with( |
| 100 | + ['entity_status_store' => 'catalog_product_entity_int'], |
| 101 | + "entity_status_store.row_id = entity.row_id" |
| 102 | + . " AND entity_status_store.attribute_id = 95" |
| 103 | + . " AND entity_status_store.store_id = 1", |
| 104 | + [] |
| 105 | + )->willReturnSelf(); |
| 106 | + $select->expects($this->once())->method('where')->with( |
| 107 | + 'IFNULL(entity_status_store.value, entity_status_global.value) = 1 = ?', |
| 108 | + ProductStatus::STATUS_ENABLED |
| 109 | + )->willReturnSelf(); |
| 110 | + |
| 111 | + $plugin = new IsEnabledOptionSelectBuilder($this->attributeRepository, $this->metadataPool); |
| 112 | + $plugin->afterGetSelect($subject, $select, $superAttribute, $productId, $scope); |
| 113 | + } |
| 114 | +} |
0 commit comments