|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CatalogInventory\Api; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\ProductCustomOptionInterface; |
| 9 | +use Magento\Catalog\Api\Data\ProductExtensionInterface; |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 14 | +use Magento\Customer\Model\Customer; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Magento\Framework\ObjectManagerInterface; |
| 17 | +use Magento\InstantPurchase\Model\InstantPurchaseOption; |
| 18 | +use Magento\InstantPurchase\Model\InstantPurchaseOptionLoadingFactory; |
| 19 | +use Magento\InstantPurchase\Model\PlaceOrder; |
| 20 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 21 | +use Magento\Store\Model\Store; |
| 22 | +use Magento\TestFramework\Helper\Bootstrap; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +class OutOfStockItemSaveTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var ObjectManagerInterface |
| 29 | + */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** @var ProductRepositoryInterface $productRepository */ |
| 33 | + private $productRepository; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 38 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 43 | + * @magentoConfigFixture cataloginventory/options/enable_inventory_check 0 |
| 44 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 45 | + * @magentoDataFixture Magento/Customer/_files/customer_address.php |
| 46 | + * @magentoDbIsolation disabled |
| 47 | + */ |
| 48 | + public function testSaveWithZeroQuantityAndInventoryCheckDisabled() |
| 49 | + { |
| 50 | + |
| 51 | + /** @var ProductInterface $product */ |
| 52 | + $product = $this->productRepository->get('simple', false, null, true); |
| 53 | + |
| 54 | + /** @var ProductExtensionInterface $ea */ |
| 55 | + $ea = $product->getExtensionAttributes(); |
| 56 | + $this->productRepository->save($product); |
| 57 | + |
| 58 | + $stockItem = $product->getExtensionAttributes()->getStockItem(); |
| 59 | + $stockItem->setQty(0); |
| 60 | + $stockItem->setIsInStock(0); |
| 61 | + /** @var StockItemRepositoryInterface $stockItemRepository */ |
| 62 | + $stockItemRepository = $this->objectManager->get(StockItemRepositoryInterface::class); |
| 63 | + $stockItemRepository->save($stockItem); |
| 64 | + |
| 65 | + $this->expectExceptionObject( |
| 66 | + new LocalizedException(__('This product is out of stock.')) |
| 67 | + ); |
| 68 | + $this->invokeTestProductPlacement($product->getSku(),[]); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 74 | + * @magentoConfigFixture cataloginventory/options/enable_inventory_check 0 |
| 75 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 76 | + * @magentoDataFixture Magento/Customer/_files/customer_address.php |
| 77 | + * @magentoDbIsolation disabled |
| 78 | + */ |
| 79 | + public function testSaveWithPositiveQuantityAndInventoryCheckDisabled() |
| 80 | + { |
| 81 | + /** @var ProductInterface $product */ |
| 82 | + $product = $this->productRepository->get('simple', false, null, true); |
| 83 | + |
| 84 | + /** @var ProductExtensionInterface $ea */ |
| 85 | + $ea = $product->getExtensionAttributes(); |
| 86 | + $this->productRepository->save($product); |
| 87 | + |
| 88 | + $stockItem = $product->getExtensionAttributes()->getStockItem(); |
| 89 | + $stockItem->setQty(100); |
| 90 | + $stockItem->setIsInStock(0); |
| 91 | + /** @var StockItemRepositoryInterface $stockItemRepository */ |
| 92 | + $stockItemRepository = $this->objectManager->get(StockItemRepositoryInterface::class); |
| 93 | + $stockItemRepository->save($stockItem); |
| 94 | + |
| 95 | + $this->expectExceptionObject( |
| 96 | + new LocalizedException(__('This product is out of stock.')) |
| 97 | + ); |
| 98 | + $this->invokeTestProductPlacement($product->getSku(),[]); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + /** |
| 103 | + * Run system under test. |
| 104 | + * |
| 105 | + * @param $productSku |
| 106 | + * @param array $productRequest |
| 107 | + * @param string $expectedResult |
| 108 | + * @return int order identifier |
| 109 | + */ |
| 110 | + private function invokeTestProductPlacement($productSku, array $productRequest) |
| 111 | + { |
| 112 | + /** @var PlaceOrder $model */ |
| 113 | + $model = $this->objectManager->create(PlaceOrder::class); |
| 114 | + |
| 115 | + $store = $this->getFixtureStore(); |
| 116 | + $customer = $this->getFixtureCustomer(); |
| 117 | + $instantPurchaseOption = $this->createInstantPurchaseOptionFromFixture(); |
| 118 | + $product = $this->getFixtureProduct($productSku); |
| 119 | + |
| 120 | + $model->placeOrder( |
| 121 | + $store, |
| 122 | + $customer, |
| 123 | + $instantPurchaseOption, |
| 124 | + $product, |
| 125 | + array_merge( |
| 126 | + [ |
| 127 | + 'qty' => '1', |
| 128 | + 'options' => $this->createProductOptionsRequest($product) |
| 129 | + ], |
| 130 | + $productRequest |
| 131 | + ) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns Store created by fixture. |
| 137 | + * |
| 138 | + * @return Store |
| 139 | + */ |
| 140 | + private function getFixtureStore(): Store |
| 141 | + { |
| 142 | + $repository = $this->objectManager->create(StoreRepositoryInterface::class); |
| 143 | + $store = $repository->get('default'); |
| 144 | + return $store; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns Product created by fixture. |
| 149 | + * |
| 150 | + * @param string $sku |
| 151 | + * @return Product |
| 152 | + */ |
| 153 | + private function getFixtureProduct(string $sku): Product |
| 154 | + { |
| 155 | + $repository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 156 | + $product = $repository->get($sku, false, $this->getFixtureStore()->getId()); |
| 157 | + $product->setData('salable', true); |
| 158 | + return $product; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns Customer created by fixture. |
| 163 | + * |
| 164 | + * @return Customer |
| 165 | + */ |
| 166 | + private function getFixtureCustomer(): Customer |
| 167 | + { |
| 168 | + $repository = $this->objectManager->create(CustomerRepositoryInterface::class); |
| 169 | + $customerData = $repository->getById(1); |
| 170 | + $customer = $this->objectManager->create(Customer::class); |
| 171 | + $customer->updateData($customerData); |
| 172 | + return $customer; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Creates instant purchase option based on data from fixture. |
| 177 | + * |
| 178 | + * @return InstantPurchaseOption |
| 179 | + */ |
| 180 | + private function createInstantPurchaseOptionFromFixture(): InstantPurchaseOption |
| 181 | + { |
| 182 | + $factory = $this->objectManager->get(InstantPurchaseOptionLoadingFactory::class); |
| 183 | + $fixtureCustomer = $this->getFixtureCustomer(); |
| 184 | + $option = $factory->create( |
| 185 | + $fixtureCustomer->getId(), |
| 186 | + 'fakePublicHash', // @see Magento/InstantPurchase/_files/fake_payment_token.php |
| 187 | + $fixtureCustomer->getDefaultShippingAddress()->getId(), |
| 188 | + $fixtureCustomer->getDefaultBillingAddress()->getId(), |
| 189 | + 'instant-purchase', |
| 190 | + 'cheapest' |
| 191 | + ); |
| 192 | + return $option; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Creates custom options selection product request data. |
| 197 | + * |
| 198 | + * @param Product $product |
| 199 | + * @return array |
| 200 | + */ |
| 201 | + private function createProductOptionsRequest(Product $product): array |
| 202 | + { |
| 203 | + $options = []; |
| 204 | + /** @var Product\Option $option */ |
| 205 | + foreach ($product->getOptions() as $option) { |
| 206 | + switch ($option->getGroupByType()) { |
| 207 | + case ProductCustomOptionInterface::OPTION_GROUP_DATE: |
| 208 | + $value = [ |
| 209 | + 'year' => date('Y'), |
| 210 | + 'month' => date('n'), |
| 211 | + 'day' => date('j'), |
| 212 | + 'hour' => date('G'), |
| 213 | + 'minute' => date('i') |
| 214 | + ]; |
| 215 | + break; |
| 216 | + case ProductCustomOptionInterface::OPTION_GROUP_SELECT: |
| 217 | + $value = key($option->getValues()); |
| 218 | + break; |
| 219 | + default: |
| 220 | + $value = 'test'; |
| 221 | + break; |
| 222 | + } |
| 223 | + $options[$option->getId()] = $value; |
| 224 | + } |
| 225 | + return $options; |
| 226 | + } |
| 227 | +} |
0 commit comments