|
| 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\InventoryConfigurableProduct\Test\Integration\Plugin\Model\Product\Type\Configurable; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\ConfigurableProduct\Block\Product\View\Type\Configurable as ConfigurableView; |
| 12 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Framework\Serialize\SerializerInterface; |
| 16 | +use Magento\Framework\View\LayoutInterface; |
| 17 | +use Magento\Inventory\Model\SourceItem\Command\SourceItemsSave; |
| 18 | +use Magento\InventoryApi\Api\Data\SourceItemInterface; |
| 19 | +use Magento\InventoryApi\Api\SourceItemRepositoryInterface; |
| 20 | +use Magento\TestFramework\Helper\Bootstrap; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * @magentoAppArea frontend |
| 25 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 26 | + */ |
| 27 | +class IsSalableOptionPluginTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var ObjectManagerInterface |
| 31 | + */ |
| 32 | + private $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ProductRepositoryInterface |
| 36 | + */ |
| 37 | + private $productRepository; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var SerializerInterface |
| 41 | + */ |
| 42 | + private $serializer; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ConfigurableView |
| 46 | + */ |
| 47 | + private $block; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var SourceItemRepositoryInterface |
| 51 | + */ |
| 52 | + private $sourceItemRepository; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var SearchCriteriaBuilder |
| 56 | + */ |
| 57 | + private $searchCriteriaBuilder; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var SourceItemsSave |
| 61 | + */ |
| 62 | + private $sourceItemSave; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var LayoutInterface |
| 66 | + */ |
| 67 | + private $layout; |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + protected function setUp(): void |
| 73 | + { |
| 74 | + parent::setUp(); |
| 75 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 76 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 77 | + $this->productRepository->cleanCache(); |
| 78 | + $this->serializer = $this->objectManager->get(SerializerInterface::class); |
| 79 | + $this->sourceItemRepository = $this->objectManager->get(SourceItemRepositoryInterface::class); |
| 80 | + $this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 81 | + $this->sourceItemSave = $this->objectManager->get(SourceItemsSave::class); |
| 82 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php |
| 87 | + * @return void |
| 88 | + * @throws NoSuchEntityException |
| 89 | + * @throws \Magento\Framework\Exception\CouldNotSaveException |
| 90 | + * @throws \Magento\Framework\Exception\InputException |
| 91 | + * @throws \Magento\Framework\Validation\ValidationException |
| 92 | + */ |
| 93 | + public function testGetSalableOptions() |
| 94 | + { |
| 95 | + $product = $this->productRepository->get('Configurable product'); |
| 96 | + $this->block = $this->layout->createBlock(ConfigurableView::class); |
| 97 | + $this->block->setProduct($product); |
| 98 | + $config = $this->serializer->unserialize($this->block->getJsonConfig()); |
| 99 | + |
| 100 | + $this->assertEquals(2, count($config['sku'])); |
| 101 | + $this->assertContains('Simple option 1', $config['sku']); |
| 102 | + $this->assertContains('Simple option 2', $config['sku']); |
| 103 | + |
| 104 | + $searchCriteria = $this->searchCriteriaBuilder |
| 105 | + ->addFilter(SourceItemInterface::SKU, 'Simple option 1') |
| 106 | + ->create(); |
| 107 | + $sourceItems = $this->sourceItemRepository->getList($searchCriteria)->getItems(); |
| 108 | + $sourceItem = reset($sourceItems); |
| 109 | + $sourceItem->setQuantity(0); |
| 110 | + $sourceItem->setStatus(0); |
| 111 | + $this->sourceItemSave->execute([$sourceItem]); |
| 112 | + |
| 113 | + $this->productRepository->cleanCache(); |
| 114 | + $product = $this->productRepository->get('Configurable product'); |
| 115 | + $block = $this->layout->createBlock(ConfigurableView::class); |
| 116 | + $block->setProduct($product); |
| 117 | + $config2 = $this->serializer->unserialize($block->getJsonConfig()); |
| 118 | + |
| 119 | + $this->assertEquals(1, count($config2['sku'])); |
| 120 | + $this->assertContains('Simple option 2', $config2['sku']); |
| 121 | + } |
| 122 | +} |
0 commit comments