|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained |
| 13 | + * from Adobe. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Magento\SalesRule\Plugin; |
| 18 | + |
| 19 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 20 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddress; |
| 21 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod; |
| 22 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethod; |
| 23 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddress; |
| 24 | +use Magento\Customer\Test\Fixture\Customer; |
| 25 | +use Magento\Framework\MessageQueue\ConsumerFactory; |
| 26 | +use Magento\Multishipping\Model\Checkout\Type\Multishipping; |
| 27 | +use Magento\Quote\Api\CartManagementInterface; |
| 28 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 29 | +use Magento\Quote\Api\CouponManagementInterface; |
| 30 | +use Magento\Quote\Test\Fixture\AddProductToCart; |
| 31 | +use Magento\Quote\Test\Fixture\CustomerCart; |
| 32 | +use Magento\Quote\Test\Fixture\GuestCart; |
| 33 | +use Magento\Sales\Api\OrderManagementInterface; |
| 34 | +use Magento\SalesRule\Test\Fixture\Rule as SalesRuleFixture; |
| 35 | +use Magento\TestFramework\Fixture\DataFixture; |
| 36 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 37 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 38 | +use Magento\TestFramework\Helper\Bootstrap; |
| 39 | +use Magento\TestFramework\MessageQueue\ClearQueueProcessor; |
| 40 | +use PHPUnit\Framework\TestCase; |
| 41 | + |
| 42 | +class CouponUsagesIncrementMultishippingTest extends TestCase |
| 43 | +{ |
| 44 | + /** |
| 45 | + * @var DataFixtureStorage |
| 46 | + */ |
| 47 | + private $fixtures; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var CouponManagementInterface |
| 51 | + */ |
| 52 | + private $couponManagement; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var CartManagementInterface |
| 56 | + */ |
| 57 | + private $cartManagement; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var CartRepositoryInterface |
| 61 | + */ |
| 62 | + private $cartRepository; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var OrderManagementInterface |
| 66 | + */ |
| 67 | + private $orderManagement; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var ConsumerFactory |
| 71 | + */ |
| 72 | + private $consumerFactory; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var Multishipping |
| 76 | + */ |
| 77 | + protected $multishipping; |
| 78 | + |
| 79 | + protected function setUp(): void |
| 80 | + { |
| 81 | + $objectManager = Bootstrap::getObjectManager(); |
| 82 | + |
| 83 | + $clearQueueProcessor = $objectManager->get(ClearQueueProcessor::class); |
| 84 | + $clearQueueProcessor->execute('sales.rule.update.coupon.usage'); |
| 85 | + |
| 86 | + $this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 87 | + $this->couponManagement = $objectManager->get(CouponManagementInterface::class); |
| 88 | + $this->cartManagement = $objectManager->get(CartManagementInterface::class); |
| 89 | + $this->cartRepository = $objectManager->get(CartRepositoryInterface::class); |
| 90 | + $this->orderManagement = $objectManager->get(OrderManagementInterface::class); |
| 91 | + $this->consumerFactory = $objectManager->get(ConsumerFactory::class); |
| 92 | + $this->multishipping = $objectManager->get(Multishipping::class); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + #[ |
| 97 | + DataFixture(ProductFixture::class, as: 'p1'), |
| 98 | + DataFixture( |
| 99 | + SalesRuleFixture::class, |
| 100 | + ['coupon_code' => 'one_per_customer', 'uses_per_customer' => 1, 'discount_amount' => 10] |
| 101 | + ), |
| 102 | + DataFixture(Customer::class, as: 'customer'), |
| 103 | + |
| 104 | + DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], 'cart1'), |
| 105 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart1.id$', 'product_id' => '$p1.id$']), |
| 106 | + DataFixture(SetBillingAddress::class, ['cart_id' => '$cart1.id$']), |
| 107 | + DataFixture(SetShippingAddress::class, ['cart_id' => '$cart1.id$']), |
| 108 | + DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart1.id$']), |
| 109 | + DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart1.id$']), |
| 110 | + |
| 111 | + DataFixture(GuestCart::class, as: 'cart2'), |
| 112 | + DataFixture(AddProductToCart::class, ['cart_id' => '$cart2.id$', 'product_id' => '$p1.id$']), |
| 113 | + DataFixture(SetBillingAddress::class, ['cart_id' => '$cart2.id$']), |
| 114 | + DataFixture(SetShippingAddress::class, ['cart_id' => '$cart2.id$']), |
| 115 | + DataFixture(SetDeliveryMethod::class, ['cart_id' => '$cart2.id$']), |
| 116 | + DataFixture(SetPaymentMethod::class, ['cart_id' => '$cart2.id$']), |
| 117 | + ] |
| 118 | + public function testCancelOrderBeforeUsageConsumerExecution(): void |
| 119 | + { |
| 120 | + $cart = $this->fixtures->get('cart1'); |
| 121 | + $this->couponManagement->set($cart->getId(), 'one_per_customer'); |
| 122 | + $orderId = $this->multishipping->createOrders(); |
| 123 | + $this->orderManagement->cancel($orderId); |
| 124 | + $consumer = $this->consumerFactory->get('sales.rule.update.coupon.usage'); |
| 125 | + $consumer->process(1); |
| 126 | + |
| 127 | + $cart = $this->fixtures->get('cart2'); |
| 128 | + $customer = $this->fixtures->get('customer'); |
| 129 | + $this->cartManagement->assignCustomer($cart->getId(), $customer->getId(), 1); |
| 130 | + $cart = $this->cartRepository->get($cart->getId()); |
| 131 | + $this->couponManagement->set($cart->getId(), 'one_per_customer'); |
| 132 | + $this->cartManagement->placeOrder($cart->getId()); |
| 133 | + $consumer->process(1); |
| 134 | + } |
| 135 | +} |
0 commit comments