|
| 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\ConfigurableProductGraphQl\Test\Unit\Model\Cart\BuyRequest; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\CatalogInventory\Api\StockStateInterface; |
| 13 | +use Magento\ConfigurableProductGraphQl\Model\Cart\BuyRequest\SuperAttributeDataProvider; |
| 14 | +use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection; |
| 15 | +use Magento\Framework\EntityManager\MetadataPool; |
| 16 | +use Magento\Framework\Stdlib\ArrayManager; |
| 17 | +use Magento\Quote\Model\Quote; |
| 18 | +use Magento\Store\Api\Data\StoreInterface; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test for SuperAttributeDataProvider |
| 24 | + */ |
| 25 | +class SuperAttributeDataProviderTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var ArrayManager|MockObject |
| 29 | + */ |
| 30 | + private $arrayManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ProductRepositoryInterface|MockObject |
| 34 | + */ |
| 35 | + private $productRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var OptionCollection|MockObject |
| 39 | + */ |
| 40 | + private $optionCollection; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var MetadataPool|MockObject |
| 44 | + */ |
| 45 | + private $metadataPool; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var StockStateInterface|MockObject |
| 49 | + */ |
| 50 | + private $stockState; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var SuperAttributeDataProvider|MockObject |
| 54 | + */ |
| 55 | + private $superAttributeDataProvider; |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritDoc |
| 59 | + */ |
| 60 | + protected function setUp(): void |
| 61 | + { |
| 62 | + $this->arrayManager = $this->getMockBuilder(ArrayManager::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + $this->productRepository = $this->getMockBuilder(ProductRepositoryInterface::class) |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->getMockForAbstractClass(); |
| 68 | + $this->optionCollection = $this->createMock(OptionCollection::class); |
| 69 | + $this->metadataPool = $this->getMockBuilder(MetadataPool::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->onlyMethods(['getMetadata']) |
| 72 | + ->addMethods(['getLinkField']) |
| 73 | + ->getMock(); |
| 74 | + $this->stockState = $this->getMockBuilder(StockStateInterface::class) |
| 75 | + ->disableOriginalConstructor() |
| 76 | + ->addMethods(['getHasError']) |
| 77 | + ->getMockForAbstractClass(); |
| 78 | + |
| 79 | + $this->superAttributeDataProvider = new SuperAttributeDataProvider( |
| 80 | + $this->arrayManager, |
| 81 | + $this->productRepository, |
| 82 | + $this->optionCollection, |
| 83 | + $this->metadataPool, |
| 84 | + $this->stockState |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Check that website id is correctly retrieved |
| 90 | + */ |
| 91 | + public function testExecute(): void |
| 92 | + { |
| 93 | + $quoteMock = $this->getMockBuilder(Quote::class) |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + $cartItemData = [ |
| 97 | + 'data' => [ |
| 98 | + 'quantity' => 2.0, |
| 99 | + 'sku' => 'simple1', |
| 100 | + ], |
| 101 | + 'parent_sku' => 'configurable', |
| 102 | + 'model' => $quoteMock, |
| 103 | + ]; |
| 104 | + |
| 105 | + $this->arrayManager->method('get') |
| 106 | + ->withConsecutive( |
| 107 | + ['parent_sku', $cartItemData], |
| 108 | + ['data/sku', $cartItemData], |
| 109 | + ['data/quantity', $cartItemData], |
| 110 | + ['model', $cartItemData], |
| 111 | + ) |
| 112 | + ->willReturnOnConsecutiveCalls( |
| 113 | + 'configurable', |
| 114 | + 'simple1', |
| 115 | + 2.0, |
| 116 | + $quoteMock, |
| 117 | + ); |
| 118 | + |
| 119 | + $storeMock = $this->getMockBuilder(StoreInterface::class) |
| 120 | + ->disableOriginalConstructor() |
| 121 | + ->addMethods(['getWebsite']) |
| 122 | + ->getMockForAbstractClass(); |
| 123 | + $storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); |
| 124 | + $storeMock->expects($this->never())->method('getWebsite'); |
| 125 | + $quoteMock->expects($this->atLeastOnce()) |
| 126 | + ->method('getStore') |
| 127 | + ->willReturn($storeMock); |
| 128 | + |
| 129 | + $productMock = $this->getMockBuilder(Product::class) |
| 130 | + ->disableOriginalConstructor() |
| 131 | + ->onlyMethods(['getId', 'getExtensionAttributes', 'getData']) |
| 132 | + ->addMethods(['getConfigurableProductLinks']) |
| 133 | + ->getMock(); |
| 134 | + $productMock->method('getId') |
| 135 | + ->willReturn(1); |
| 136 | + $productMock->method('getExtensionAttributes') |
| 137 | + ->willReturnSelf(); |
| 138 | + $productMock->method('getConfigurableProductLinks') |
| 139 | + ->willReturn([1]); |
| 140 | + $productMock->method('getData') |
| 141 | + ->willReturn(1); |
| 142 | + $this->productRepository->method('get') |
| 143 | + ->willReturn($productMock); |
| 144 | + $this->stockState->method('checkQuoteItemQty') |
| 145 | + ->willReturnSelf(); |
| 146 | + $this->stockState->method('getHasError') |
| 147 | + ->willReturn(false); |
| 148 | + $this->metadataPool->method('getMetadata') |
| 149 | + ->willReturnSelf(); |
| 150 | + $this->metadataPool->method('getLinkField') |
| 151 | + ->willReturnSelf(); |
| 152 | + $this->optionCollection->method('getAttributesByProductId') |
| 153 | + ->willReturn([ |
| 154 | + [ |
| 155 | + 'attribute_code' => 'code', |
| 156 | + 'attribute_id' => 1, |
| 157 | + 'values' => [['value_index' => 1]], |
| 158 | + ] |
| 159 | + ]); |
| 160 | + |
| 161 | + $this->assertEquals(['super_attribute' => [1 => 1]], $this->superAttributeDataProvider->execute($cartItemData)); |
| 162 | + } |
| 163 | +} |
0 commit comments