|
| 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\SalesRule\Test\Unit\Model\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 11 | +use Magento\Quote\Model\Quote; |
| 12 | +use Magento\Quote\Model\QuoteRepository; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Magento\SalesRule\Model\Coupon\Quote\UpdateCouponUsages; |
| 16 | +use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderDefault; |
| 17 | +use Magento\Sales\Model\Order; |
| 18 | +use Magento\SalesRule\Plugin\CouponUsagesIncrementMultishipping; |
| 19 | + |
| 20 | +class CouponUsageIncreamentForMultishippingTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var PlaceOrderDefault|MockObject |
| 24 | + */ |
| 25 | + private $subjectMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var UpdateCouponUsages|MockObject |
| 29 | + */ |
| 30 | + private $updateCouponUsagesMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var QuoteRepository|MockObject |
| 34 | + */ |
| 35 | + private $quoteRepositoryMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Order[]|MockObject |
| 39 | + */ |
| 40 | + private $orderMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var CouponUsagesIncrementMultishipping |
| 44 | + */ |
| 45 | + private $plugin; |
| 46 | + |
| 47 | + /** |
| 48 | + * Set Up |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 53 | + $this->subjectMock = $this->getMockBuilder(PlaceOrderDefault::class) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->getMock(); |
| 56 | + $this->updateCouponUsagesMock = $this->getMockBuilder(UpdateCouponUsages::class) |
| 57 | + ->disableOriginalConstructor() |
| 58 | + ->onlyMethods(['execute']) |
| 59 | + ->getMock(); |
| 60 | + $this->quoteRepositoryMock = $this->getMockBuilder(QuoteRepository::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $this->orderMock = $this->getMockBuilder(Order::class) |
| 64 | + ->onlyMethods(['getQuoteId']) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->getMock(); |
| 67 | + $this->plugin = $this->objectManagerHelper->getObject( |
| 68 | + CouponUsagesIncrementMultishipping::class, |
| 69 | + [ |
| 70 | + 'updateCouponUsages' => $this->updateCouponUsagesMock, |
| 71 | + 'quoteRepository' => $this->quoteRepositoryMock |
| 72 | + ] |
| 73 | + ); |
| 74 | + } |
| 75 | + /** |
| 76 | + * Testing Increments number of coupon usages before placing order |
| 77 | + */ |
| 78 | + public function testAroundPlace() |
| 79 | + { |
| 80 | + $couponCode = 'coupon code'; |
| 81 | + $proceed = function ($orderMock) { |
| 82 | + return $orderMock; |
| 83 | + }; |
| 84 | + /** @var Quote|MockObject $quote */ |
| 85 | + $quoteMock = $this->getMockBuilder(Quote::class) |
| 86 | + ->disableOriginalConstructor() |
| 87 | + ->addMethods(['getCouponCode']) |
| 88 | + ->onlyMethods(['dataHasChangedFor']) |
| 89 | + ->getMock(); |
| 90 | + $this->orderMock->expects($this->once())->method('getQuoteId') |
| 91 | + ->willReturn(1); |
| 92 | + |
| 93 | + $this->quoteRepositoryMock->expects($this->once())->method('get')->with(1)->willReturn($quoteMock); |
| 94 | + $quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); |
| 95 | + $quoteMock->expects($this->any())->method('dataHasChangedFor')->with($couponCode)->willReturn(true); |
| 96 | + $this->updateCouponUsagesMock |
| 97 | + ->expects($this->once()) |
| 98 | + ->method('execute'); |
| 99 | + $this->assertSame( |
| 100 | + [$this->orderMock], |
| 101 | + $this->plugin->aroundPlace($this->subjectMock, $proceed, [$this->orderMock]) |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments