|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2023 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\SalesRule\Test\Unit\Plugin; |
| 20 | + |
| 21 | +use Magento\Quote\Model\Quote; |
| 22 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Magento\SalesRule\Model\Coupon\Quote\UpdateCouponUsages; |
| 26 | +use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault; |
| 27 | +use Magento\Sales\Model\Order; |
| 28 | +use Magento\SalesRule\Plugin\CouponUsagesIncrementMultishipping; |
| 29 | + |
| 30 | +class CouponUsageIncreamentForMultishippingTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var PlaceOrderDefault|MockObject |
| 34 | + */ |
| 35 | + private $subjectMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var UpdateCouponUsages|MockObject |
| 39 | + */ |
| 40 | + private $updateCouponUsagesMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var CartRepositoryInterface|MockObject |
| 44 | + */ |
| 45 | + private $cartRepositoryInterfaceMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Order[]|MockObject |
| 49 | + */ |
| 50 | + private $orderMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var CouponUsagesIncrementMultishipping |
| 54 | + */ |
| 55 | + private $plugin; |
| 56 | + |
| 57 | + /** |
| 58 | + * Set Up |
| 59 | + */ |
| 60 | + protected function setUp(): void |
| 61 | + { |
| 62 | + $this->subjectMock = $this->getMockBuilder(PlaceOrderDefault::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + $this->updateCouponUsagesMock = $this->getMockBuilder(UpdateCouponUsages::class) |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->onlyMethods(['execute']) |
| 68 | + ->getMock(); |
| 69 | + $this->cartRepositoryInterfaceMock = $this->getMockBuilder(CartRepositoryInterface::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + $this->orderMock = $this->getMockBuilder(Order::class) |
| 73 | + ->onlyMethods(['getQuoteId']) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->getMock(); |
| 76 | + $this->plugin = new CouponUsagesIncrementMultishipping( |
| 77 | + $this->updateCouponUsagesMock, |
| 78 | + $this->cartRepositoryInterfaceMock |
| 79 | + ); |
| 80 | + } |
| 81 | + /** |
| 82 | + * Testing Increments number of coupon usages before placing order |
| 83 | + */ |
| 84 | + public function testAroundPlace() |
| 85 | + { |
| 86 | + $couponCode = 'coupon code'; |
| 87 | + $proceed = function ($orderMock) { |
| 88 | + return $orderMock; |
| 89 | + }; |
| 90 | + /** @var Quote|MockObject $quote */ |
| 91 | + $quoteMock = $this->getMockBuilder(Quote::class) |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->addMethods(['getCouponCode']) |
| 94 | + ->onlyMethods(['dataHasChangedFor']) |
| 95 | + ->getMock(); |
| 96 | + $this->orderMock->expects($this->once())->method('getQuoteId') |
| 97 | + ->willReturn(1); |
| 98 | + |
| 99 | + $this->cartRepositoryInterfaceMock->expects($this->once())->method('get')->with(1)->willReturn($quoteMock); |
| 100 | + $quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); |
| 101 | + $quoteMock->expects($this->any())->method('dataHasChangedFor')->with($couponCode)->willReturn(true); |
| 102 | + $this->updateCouponUsagesMock |
| 103 | + ->expects($this->once()) |
| 104 | + ->method('execute'); |
| 105 | + $this->assertSame( |
| 106 | + [$this->orderMock], |
| 107 | + $this->plugin->aroundPlace($this->subjectMock, $proceed, [$this->orderMock]) |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments