|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2015 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Sales\Model; |
| 10 | + |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture; |
| 13 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 14 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 15 | +use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture; |
| 16 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 17 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 18 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 19 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 20 | +use Magento\Sales\Api\Data\OrderInterface; |
| 21 | +use Magento\TestFramework\Fixture\Config; |
| 22 | +use Magento\TestFramework\Fixture\DataFixture; |
| 23 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 24 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 25 | +use Magento\TestFramework\Helper\Bootstrap; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +/** |
| 29 | + * Integration test for asynchronous grid processing with auto-invoiced orders |
| 30 | + * |
| 31 | + * @magentoDbIsolation enabled |
| 32 | + * @magentoAppIsolation enabled |
| 33 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 34 | + */ |
| 35 | +class AsyncGridWithAutoInvoiceTest extends TestCase |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @var GridAsyncInsert |
| 39 | + */ |
| 40 | + private GridAsyncInsert $gridAsyncInsert; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var DataFixtureStorage |
| 44 | + */ |
| 45 | + private DataFixtureStorage $fixtures; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $objectManager = Bootstrap::getObjectManager(); |
| 53 | + |
| 54 | + $this->fixtures = DataFixtureStorageManager::getStorage(); |
| 55 | + |
| 56 | + /** @var GridAsyncInsert $gridAsyncInsert */ |
| 57 | + $this->gridAsyncInsert = $objectManager->get('SalesOrderIndexGridAsyncInsert'); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test async grid processing with auto-invoiced zero subtotal order |
| 62 | + * Expected: Order placed successfully with no errors in logs |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + #[ |
| 67 | + Config('dev/grid/async_indexing', '1', 'default'), |
| 68 | + Config('carriers/freeshipping/active', '1', 'store', 'default'), |
| 69 | + Config('payment/free/active', '1', 'store', 'default'), |
| 70 | + Config('payment/free/order_status', 'processing', 'store', 'default'), |
| 71 | + Config('payment/free/payment_action', 'authorize_capture', 'store', 'default'), |
| 72 | + DataFixture( |
| 73 | + ProductFixture::class, |
| 74 | + [ |
| 75 | + 'sku' => 'free-product-test', |
| 76 | + 'price' => 0.00, |
| 77 | + 'special_price' => null, |
| 78 | + ], |
| 79 | + 'product' |
| 80 | + ), |
| 81 | + DataFixture(GuestCartFixture::class, as: 'cart'), |
| 82 | + DataFixture( |
| 83 | + AddProductToCartFixture::class, |
| 84 | + [ |
| 85 | + 'cart_id' => '$cart.id$', |
| 86 | + 'product_id' => '$product.id$', |
| 87 | + 'qty' => 1 |
| 88 | + ] |
| 89 | + ), |
| 90 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 91 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 92 | + DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']), |
| 93 | + DataFixture( |
| 94 | + SetDeliveryMethodFixture::class, |
| 95 | + [ |
| 96 | + 'cart_id' => '$cart.id$', |
| 97 | + 'carrier_code' => 'freeshipping', |
| 98 | + 'method_code' => 'freeshipping' |
| 99 | + ] |
| 100 | + ), |
| 101 | + DataFixture( |
| 102 | + SetPaymentMethodFixture::class, |
| 103 | + [ |
| 104 | + 'cart_id' => '$cart.id$', |
| 105 | + 'method' => 'free' |
| 106 | + ] |
| 107 | + ), |
| 108 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$cart.id$'], 'order') |
| 109 | + ] |
| 110 | + public function testAsyncGridProcessingWithAutoInvoicedZeroSubtotalOrder(): void |
| 111 | + { |
| 112 | + /** @var OrderInterface $order */ |
| 113 | + $order = $this->fixtures->get('order'); |
| 114 | + |
| 115 | + $this->assertNotNull($order->getEntityId(), 'Order should be created successfully'); |
| 116 | + $this->assertEquals(0.00, (float) $order->getGrandTotal(), 'Order grand total should be 0.00'); |
| 117 | + $this->assertEquals('free', $order->getPayment()->getMethod(), 'Payment method should be "free"'); |
| 118 | + |
| 119 | + $this->assertEquals( |
| 120 | + Order::STATE_PROCESSING, |
| 121 | + $order->getState(), |
| 122 | + 'Order state should be Processing (triggers auto-invoice)' |
| 123 | + ); |
| 124 | + |
| 125 | + $invoiceCollection = $order->getInvoiceCollection(); |
| 126 | + $this->assertGreaterThan( |
| 127 | + 0, |
| 128 | + $invoiceCollection->count(), |
| 129 | + 'Invoice should be automatically created when order status = Processing' |
| 130 | + ); |
| 131 | + |
| 132 | + $exceptionThrown = false; |
| 133 | + $exceptionMessage = ''; |
| 134 | + |
| 135 | + try { |
| 136 | + $this->gridAsyncInsert->asyncInsert(); |
| 137 | + } catch (\Exception $e) { |
| 138 | + $exceptionThrown = true; |
| 139 | + $exceptionMessage = $e->getMessage(); |
| 140 | + } |
| 141 | + |
| 142 | + $this->assertFalse( |
| 143 | + $exceptionThrown, |
| 144 | + 'There should be no error in the logs. Exception thrown: ' . $exceptionMessage |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments