|
| 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\Quote\Address\Total; |
| 9 | + |
| 10 | +use Magento\SalesRule\Model\Quote\Address\Total\ShippingDiscount; |
| 11 | +use Magento\SalesRule\Model\Validator; |
| 12 | +use Magento\Quote\Model\Quote; |
| 13 | +use Magento\Quote\Model\Quote\Address; |
| 14 | +use Magento\Quote\Api\Data\ShippingAssignmentInterface; |
| 15 | +use Magento\Quote\Api\Data\ShippingInterface; |
| 16 | +use Magento\Quote\Model\Quote\Address\Total; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class \Magento\SalesRule\Test\Unit\Model\Quote\Address\Total\ShippingDiscountTest |
| 20 | + */ |
| 21 | +class ShippingDiscountTest extends \PHPUnit\Framework\TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var \PHPUnit_Framework_MockObject_MockObject | Validator |
| 25 | + */ |
| 26 | + protected $validatorMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \PHPUnit_Framework_MockObject_MockObject | Quote |
| 30 | + */ |
| 31 | + private $quoteMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \PHPUnit_Framework_MockObject_MockObject | Total |
| 35 | + */ |
| 36 | + private $totalMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \PHPUnit_Framework_MockObject_MockObject | Address |
| 40 | + */ |
| 41 | + private $addressMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \PHPUnit_Framework_MockObject_MockObject | ShippingAssignmentInterface |
| 45 | + */ |
| 46 | + private $shippingAssignmentMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ShippingDiscount |
| 50 | + */ |
| 51 | + private $discount; |
| 52 | + |
| 53 | + protected function setUp() |
| 54 | + { |
| 55 | + $this->validatorMock = $this->getMockBuilder(\Magento\SalesRule\Model\Validator::class) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->setMethods( |
| 58 | + [ |
| 59 | + 'reset', |
| 60 | + 'processShippingAmount', |
| 61 | + '__wakeup', |
| 62 | + ] |
| 63 | + ) |
| 64 | + ->getMock(); |
| 65 | + $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class); |
| 66 | + $this->totalMock = $this->createPartialMock( |
| 67 | + \Magento\Quote\Model\Quote\Address\Total::class, |
| 68 | + [ |
| 69 | + 'getDiscountAmount', |
| 70 | + 'getDiscountDescription', |
| 71 | + 'addTotalAmount', |
| 72 | + 'addBaseTotalAmount', |
| 73 | + 'setShippingDiscountAmount', |
| 74 | + 'setBaseShippingDiscountAmount', |
| 75 | + 'getSubtotal', |
| 76 | + 'setSubtotalWithDiscount', |
| 77 | + 'setBaseSubtotalWithDiscount', |
| 78 | + 'getBaseSubtotal', |
| 79 | + 'getBaseDiscountAmount', |
| 80 | + 'setDiscountDescription' |
| 81 | + ] |
| 82 | + ); |
| 83 | + |
| 84 | + $this->addressMock = $this->createPartialMock( |
| 85 | + Address::class, |
| 86 | + [ |
| 87 | + 'getQuote', |
| 88 | + 'getShippingAmount', |
| 89 | + 'getShippingDiscountAmount', |
| 90 | + 'getBaseShippingDiscountAmount', |
| 91 | + 'setShippingDiscountAmount', |
| 92 | + 'setBaseShippingDiscountAmount', |
| 93 | + 'getDiscountDescription', |
| 94 | + 'setDiscountAmount', |
| 95 | + 'setBaseDiscountAmount', |
| 96 | + '__wakeup' |
| 97 | + ] |
| 98 | + ); |
| 99 | + |
| 100 | + $shipping = $this->createMock(ShippingInterface::class); |
| 101 | + $shipping->expects($this->any())->method('getAddress')->willReturn($this->addressMock); |
| 102 | + $this->shippingAssignmentMock = $this->createMock(ShippingAssignmentInterface::class); |
| 103 | + $this->shippingAssignmentMock->expects($this->any())->method('getShipping')->willReturn($shipping); |
| 104 | + |
| 105 | + $this->discount = new ShippingDiscount( |
| 106 | + $this->validatorMock |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test collect with the quote has no shipping amount discount |
| 112 | + */ |
| 113 | + public function testCollectNoShippingAmount() |
| 114 | + { |
| 115 | + $itemNoDiscount = $this->createMock(\Magento\Quote\Model\Quote\Item::class); |
| 116 | + |
| 117 | + $this->addressMock->expects($this->any())->method('getQuote')->willReturn($this->quoteMock); |
| 118 | + |
| 119 | + $this->addressMock->expects($this->any())->method('getShippingAmount')->willReturn(0); |
| 120 | + |
| 121 | + $this->shippingAssignmentMock->expects($this->any())->method('getItems') |
| 122 | + ->willReturn([$itemNoDiscount]); |
| 123 | + |
| 124 | + $this->addressMock->expects($this->once())->method('setShippingDiscountAmount') |
| 125 | + ->with(0) |
| 126 | + ->willReturnSelf(); |
| 127 | + $this->addressMock->expects($this->once())->method('setBaseShippingDiscountAmount') |
| 128 | + ->with(0) |
| 129 | + ->willReturnSelf(); |
| 130 | + |
| 131 | + /* Assert Collect function */ |
| 132 | + $this->assertInstanceOf( |
| 133 | + ShippingDiscount::class, |
| 134 | + $this->discount->collect($this->quoteMock, $this->shippingAssignmentMock, $this->totalMock) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Test collect with the quote has shipping amount discount |
| 140 | + */ |
| 141 | + public function testCollectWithShippingAmountDiscount() |
| 142 | + { |
| 143 | + $shippingAmount = 100; |
| 144 | + $shippingDiscountAmount = 50; |
| 145 | + $baseShippingDiscountAmount = 50; |
| 146 | + $discountDescription = 'Discount $50'; |
| 147 | + $subTotal = 200; |
| 148 | + $discountAmount = -100; |
| 149 | + $baseSubTotal = 200; |
| 150 | + $baseDiscountAmount = -100; |
| 151 | + |
| 152 | + $itemNoDiscount = $this->createMock(\Magento\Quote\Model\Quote\Item::class); |
| 153 | + |
| 154 | + $this->addressMock->expects($this->any())->method('getQuote')->willReturn($this->quoteMock); |
| 155 | + |
| 156 | + $this->addressMock->expects($this->any())->method('getShippingAmount')->willReturn($shippingAmount); |
| 157 | + |
| 158 | + $this->addressMock->expects($this->any())->method('getShippingDiscountAmount') |
| 159 | + ->willReturn($shippingDiscountAmount); |
| 160 | + $this->addressMock->expects($this->any())->method('getBaseShippingDiscountAmount') |
| 161 | + ->willReturn($baseShippingDiscountAmount); |
| 162 | + |
| 163 | + $this->addressMock->expects($this->any())->method('getDiscountDescription') |
| 164 | + ->willReturn($discountDescription); |
| 165 | + |
| 166 | + $this->shippingAssignmentMock->expects($this->any())->method('getItems') |
| 167 | + ->willReturn([$itemNoDiscount]); |
| 168 | + |
| 169 | + $this->totalMock->expects($this->once())->method('addTotalAmount') |
| 170 | + ->with('discount', -$shippingDiscountAmount)->willReturnSelf(); |
| 171 | + $this->totalMock->expects($this->once())->method('addBaseTotalAmount') |
| 172 | + ->with('discount', -$baseShippingDiscountAmount)->willReturnSelf(); |
| 173 | + |
| 174 | + $this->totalMock->expects($this->once())->method('setShippingDiscountAmount') |
| 175 | + ->with($shippingDiscountAmount)->willReturnSelf(); |
| 176 | + $this->totalMock->expects($this->once())->method('setBaseShippingDiscountAmount') |
| 177 | + ->with($baseShippingDiscountAmount)->willReturnSelf(); |
| 178 | + |
| 179 | + $this->totalMock->expects($this->any())->method('getSubtotal') |
| 180 | + ->willReturn($subTotal); |
| 181 | + $this->totalMock->expects($this->any())->method('getDiscountAmount') |
| 182 | + ->willReturn($discountAmount); |
| 183 | + |
| 184 | + $this->totalMock->expects($this->any())->method('getBaseSubtotal') |
| 185 | + ->willReturn($baseSubTotal); |
| 186 | + $this->totalMock->expects($this->any())->method('getBaseDiscountAmount') |
| 187 | + ->willReturn($baseDiscountAmount); |
| 188 | + |
| 189 | + $this->totalMock->expects($this->once())->method('setDiscountDescription') |
| 190 | + ->with($discountDescription)->willReturnSelf(); |
| 191 | + |
| 192 | + $this->totalMock->expects($this->once())->method('setSubtotalWithDiscount') |
| 193 | + ->with(100)->willReturnSelf(); |
| 194 | + $this->totalMock->expects($this->once())->method('setBaseSubtotalWithDiscount') |
| 195 | + ->with(100)->willReturnSelf(); |
| 196 | + |
| 197 | + $this->addressMock->expects($this->once())->method('setDiscountAmount') |
| 198 | + ->with($discountAmount)->willReturnSelf(); |
| 199 | + |
| 200 | + $this->addressMock->expects($this->once())->method('setBaseDiscountAmount') |
| 201 | + ->with($baseDiscountAmount)->willReturnSelf(); |
| 202 | + |
| 203 | + /* Assert Collect function */ |
| 204 | + $this->assertInstanceOf( |
| 205 | + ShippingDiscount::class, |
| 206 | + $this->discount->collect($this->quoteMock, $this->shippingAssignmentMock, $this->totalMock) |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Test fetch function with discount = 100 |
| 212 | + */ |
| 213 | + public function testFetch() |
| 214 | + { |
| 215 | + $discountAmount = 100; |
| 216 | + $discountDescription = 100; |
| 217 | + $expectedResult = [ |
| 218 | + 'code' => 'discount', |
| 219 | + 'value' => 100, |
| 220 | + 'title' => __('Discount (%1)', $discountDescription) |
| 221 | + ]; |
| 222 | + $this->totalMock->expects($this->once())->method('getDiscountAmount') |
| 223 | + ->willReturn($discountAmount); |
| 224 | + $this->totalMock->expects($this->once())->method('getDiscountDescription') |
| 225 | + ->willReturn($discountDescription); |
| 226 | + $this->assertEquals($expectedResult, $this->discount->fetch($this->quoteMock, $this->totalMock)); |
| 227 | + } |
| 228 | +} |
0 commit comments