|
| 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\Multishipping\Test\Unit\Plugin; |
| 20 | + |
| 21 | +use Magento\Catalog\Model\Product; |
| 22 | +use Magento\Catalog\Model\Product\Type; |
| 23 | +use Magento\Catalog\Model\Product\Type\Simple; |
| 24 | +use Magento\Multishipping\Plugin\MultishippingQuoteRepository; |
| 25 | +use Magento\Payment\Model\Method\AbstractMethod; |
| 26 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 27 | +use Magento\Quote\Api\Data\CartExtensionInterface; |
| 28 | +use Magento\Quote\Api\Data\CartInterface; |
| 29 | +use Magento\Quote\Model\Quote\Address; |
| 30 | +use Magento\Quote\Model\Quote\Address\Item; |
| 31 | +use Magento\Quote\Model\Quote\Item as QuoteItem; |
| 32 | +use Magento\Quote\Model\Quote\Address\Rate; |
| 33 | +use Magento\Quote\Model\Quote\Payment; |
| 34 | +use Magento\Quote\Model\Quote\ShippingAssignment\ShippingProcessor; |
| 35 | +use Magento\Quote\Model\ShippingAssignmentFactory; |
| 36 | +use PHPUnit\Framework\MockObject\MockObject; |
| 37 | +use PHPUnit\Framework\TestCase; |
| 38 | + |
| 39 | +/** |
| 40 | + * Unit Test case for MultishippingQuoteRepository plugin |
| 41 | + * |
| 42 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 43 | + */ |
| 44 | +class MultishippingQuoteRepositoryTest extends TestCase |
| 45 | +{ |
| 46 | + /** |
| 47 | + * @var CartRepositoryInterface|MockObject |
| 48 | + */ |
| 49 | + private $cartMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var CartInterface|MockObject |
| 53 | + */ |
| 54 | + private $quoteMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var QuoteItem|MockObject |
| 58 | + */ |
| 59 | + private $quoteItemMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ShippingAssignmentFactory|MockObject |
| 63 | + */ |
| 64 | + private $shippingAssignmentFactoryMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var ShippingProcessor|MockObject |
| 68 | + */ |
| 69 | + private $shippingProcessorMock; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var MultishippingQuoteRepository |
| 73 | + */ |
| 74 | + private $multishippingQuoteRepository; |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritdoc |
| 78 | + */ |
| 79 | + protected function setUp(): void |
| 80 | + { |
| 81 | + $this->cartMock = $this->createMock(CartRepositoryInterface::class); |
| 82 | + $this->quoteMock = $this->getMockBuilder(CartInterface::class) |
| 83 | + ->addMethods( |
| 84 | + [ |
| 85 | + 'hasVirtualItems', |
| 86 | + 'getAllShippingAddresses', |
| 87 | + 'getPayment', |
| 88 | + 'getIsMultiShipping', |
| 89 | + 'reserveOrderId' |
| 90 | + ] |
| 91 | + ) |
| 92 | + ->onlyMethods(['setItems', 'getItems']) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMockForAbstractClass(); |
| 95 | + $this->quoteItemMock = $this->getMockBuilder(QuoteItem::class) |
| 96 | + ->disableOriginalConstructor() |
| 97 | + ->onlyMethods(['getProductType', 'getProduct', 'getQuote', 'getQty', 'getPrice', 'setQuote']) |
| 98 | + ->getMock(); |
| 99 | + $this->shippingAssignmentFactoryMock = $this->getMockBuilder(ShippingAssignmentFactory::class) |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMockForAbstractClass(); |
| 102 | + $this->shippingProcessorMock = $this->getMockBuilder(ShippingProcessor::class) |
| 103 | + ->disableOriginalConstructor() |
| 104 | + ->getMockForAbstractClass(); |
| 105 | + $this->multishippingQuoteRepository = new MultishippingQuoteRepository( |
| 106 | + $this->shippingAssignmentFactoryMock, |
| 107 | + $this->shippingProcessorMock |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test afterGet plugin and check the quote has items or null |
| 113 | + * |
| 114 | + * @param bool $isMultiShippingMode |
| 115 | + * @param array $productData |
| 116 | + * @return void |
| 117 | + * @dataProvider pluginForAfterGetMultiShippingModeDataProvider |
| 118 | + */ |
| 119 | + public function testPluginAfterGetWithMultiShippingMode(bool $isMultiShippingMode, array $productData): void |
| 120 | + { |
| 121 | + $simpleProductTypeMock = $this->getMockBuilder(Simple::class) |
| 122 | + ->disableOriginalConstructor() |
| 123 | + ->onlyMethods(['getOrderOptions']) |
| 124 | + ->getMock(); |
| 125 | + $productMock = $this->getProductMock($simpleProductTypeMock); |
| 126 | + $this->getQuoteItemMock($productData['productType'], $productMock); |
| 127 | + $quoteAddressItemMock = $this->getQuoteAddressItemMock( |
| 128 | + $productData['productType'], |
| 129 | + $productData['productOptions'] |
| 130 | + ); |
| 131 | + list($shippingAddressMock, $billingAddressMock) = |
| 132 | + $this->getQuoteAddressesMock($quoteAddressItemMock); |
| 133 | + $this->setQuoteMockData($productData['paymentProviderCode'], $shippingAddressMock, $billingAddressMock); |
| 134 | + $this->quoteItemMock->method('setQuote')->with($this->quoteMock)->willReturnSelf(); |
| 135 | + $this->quoteItemMock->method('getQuote')->willReturn($this->quoteMock); |
| 136 | + $extensionAttributesMock = $this->getMockBuilder(CartExtensionInterface::class) |
| 137 | + ->disableOriginalConstructor() |
| 138 | + ->addMethods(['getShippingAssignments']) |
| 139 | + ->getMockForAbstractClass(); |
| 140 | + $this->quoteMock->expects($this->any()) |
| 141 | + ->method('getIsMultiShipping') |
| 142 | + ->willReturn($isMultiShippingMode); |
| 143 | + $this->quoteMock->expects($this->any()) |
| 144 | + ->method('getExtensionAttributes') |
| 145 | + ->willReturn($extensionAttributesMock); |
| 146 | + $extensionAttributesMock->expects($this->any()) |
| 147 | + ->method('getShippingAssignments') |
| 148 | + ->willReturn($this->shippingAssignmentFactoryMock); |
| 149 | + |
| 150 | + $quote = $this->multishippingQuoteRepository->afterGet($this->cartMock, $this->quoteMock); |
| 151 | + $this->assertNotEmpty($quote); |
| 152 | + $this->assertEquals(1, count($quote->getItems())); |
| 153 | + $this->assertNotEmpty(current($quote->getItems())); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Return Product Mock. |
| 158 | + * |
| 159 | + * @param Simple|MockObject $simpleProductTypeMock |
| 160 | + * @return MockObject |
| 161 | + */ |
| 162 | + private function getProductMock($simpleProductTypeMock): MockObject |
| 163 | + { |
| 164 | + $productMock = $this->getMockBuilder(Product::class) |
| 165 | + ->disableOriginalConstructor() |
| 166 | + ->onlyMethods(['getTypeInstance']) |
| 167 | + ->getMock(); |
| 168 | + $productMock->method('getTypeInstance')->willReturn($simpleProductTypeMock); |
| 169 | + |
| 170 | + return $productMock; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Return Quote Item Mock. |
| 175 | + * |
| 176 | + * @param string $productType |
| 177 | + * @param Product|MockObject $productMock |
| 178 | + * @return void |
| 179 | + */ |
| 180 | + private function getQuoteItemMock(string $productType, Product|MockObject $productMock): void |
| 181 | + { |
| 182 | + $this->quoteItemMock->method('getProductType')->willReturn($productType); |
| 183 | + $this->quoteItemMock->method('getProduct')->willReturn($productMock); |
| 184 | + $this->quoteItemMock->method('getQty')->willReturn(1); |
| 185 | + $this->quoteItemMock->method('getPrice')->willReturn(10); |
| 186 | + $this->quoteItemMock->method('getQuote')->willReturn($this->quoteMock); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Return Quote Address Item Mock |
| 191 | + * |
| 192 | + * @param string $productType |
| 193 | + * @param array $productOptions |
| 194 | + * @return MockObject |
| 195 | + */ |
| 196 | + private function getQuoteAddressItemMock(string $productType, array $productOptions): MockObject |
| 197 | + { |
| 198 | + $quoteAddressItemMock = $this->getMockBuilder(Item::class) |
| 199 | + ->disableOriginalConstructor() |
| 200 | + ->addMethods(['getQuoteItem','setProductType', 'setProductOptions']) |
| 201 | + ->onlyMethods(['getParentItem']) |
| 202 | + ->getMock(); |
| 203 | + $quoteAddressItemMock->method('getQuoteItem')->willReturn($this->quoteItemMock); |
| 204 | + $quoteAddressItemMock->method('setProductType')->with($productType)->willReturnSelf(); |
| 205 | + $quoteAddressItemMock->method('setProductOptions')->willReturn($productOptions); |
| 206 | + $quoteAddressItemMock->method('getParentItem')->willReturn(false); |
| 207 | + |
| 208 | + return $quoteAddressItemMock; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Return Quote Addresses Mock |
| 213 | + * @param Item|MockObject $quoteAddressItemMock |
| 214 | + * @return array |
| 215 | + */ |
| 216 | + private function getQuoteAddressesMock(Item|MockObject $quoteAddressItemMock): array |
| 217 | + { |
| 218 | + $shippingAddressMock = $this->getMockBuilder(Address::class) |
| 219 | + ->disableOriginalConstructor() |
| 220 | + ->addMethods(['getAddressType', 'getGrandTotal']) |
| 221 | + ->onlyMethods( |
| 222 | + [ |
| 223 | + 'validate', |
| 224 | + 'getShippingMethod', |
| 225 | + 'getShippingRateByCode', |
| 226 | + 'getCountryId', |
| 227 | + 'getAllItems', |
| 228 | + ] |
| 229 | + )->getMock(); |
| 230 | + $shippingAddressMock->method('validate')->willReturn(true); |
| 231 | + $shippingAddressMock->method('getAllItems')->willReturn([$quoteAddressItemMock]); |
| 232 | + $shippingAddressMock->method('getAddressType')->willReturn('shipping'); |
| 233 | + |
| 234 | + $shippingRateMock = $this->getMockBuilder(Rate::class) |
| 235 | + ->disableOriginalConstructor() |
| 236 | + ->addMethods([ 'getPrice' ]) |
| 237 | + ->getMock(); |
| 238 | + $shippingAddressMock->method('getShippingRateByCode')->willReturn($shippingRateMock); |
| 239 | + |
| 240 | + $billingAddressMock = $this->getMockBuilder(Address::class) |
| 241 | + ->disableOriginalConstructor() |
| 242 | + ->onlyMethods(['validate']) |
| 243 | + ->getMock(); |
| 244 | + $billingAddressMock->method('validate')->willReturn(true); |
| 245 | + |
| 246 | + return [$shippingAddressMock, $billingAddressMock]; |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Set data for Quote Mock. |
| 251 | + * |
| 252 | + * @param string $paymentProviderCode |
| 253 | + * @param Address|MockObject $shippingAddressMock |
| 254 | + * @param Address|MockObject $billingAddressMock |
| 255 | + * @return void |
| 256 | + */ |
| 257 | + private function setQuoteMockData( |
| 258 | + string $paymentProviderCode, |
| 259 | + Address|MockObject $shippingAddressMock, |
| 260 | + Address|MockObject $billingAddressMock |
| 261 | + ): void { |
| 262 | + $paymentMock = $this->getPaymentMock($paymentProviderCode); |
| 263 | + $this->quoteMock->method('getPayment') |
| 264 | + ->willReturn($paymentMock); |
| 265 | + $this->quoteMock->method('getAllShippingAddresses') |
| 266 | + ->willReturn([$shippingAddressMock]); |
| 267 | + $this->quoteMock->method('getBillingAddress') |
| 268 | + ->willReturn($billingAddressMock); |
| 269 | + $this->quoteMock->method('hasVirtualItems') |
| 270 | + ->willReturn(false); |
| 271 | + $this->quoteMock->expects($this->any())->method('reserveOrderId')->willReturnSelf(); |
| 272 | + $this->quoteMock->method('setIsActive')->with(false)->willReturnSelf(); |
| 273 | + $this->quoteMock->method('setItems')->with([$this->quoteItemMock])->willReturnSelf(); |
| 274 | + $this->quoteMock->method('getItems')->willReturn([$this->quoteItemMock]); |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Return Payment Mock. |
| 279 | + * |
| 280 | + * @param string $paymentProviderCode |
| 281 | + * @return MockObject |
| 282 | + */ |
| 283 | + private function getPaymentMock(string $paymentProviderCode): MockObject |
| 284 | + { |
| 285 | + $abstractMethod = $this->getMockBuilder(AbstractMethod::class) |
| 286 | + ->disableOriginalConstructor() |
| 287 | + ->onlyMethods(['isAvailable']) |
| 288 | + ->getMockForAbstractClass(); |
| 289 | + $abstractMethod->method('isAvailable')->willReturn(true); |
| 290 | + |
| 291 | + $paymentMock = $this->getMockBuilder(Payment::class) |
| 292 | + ->disableOriginalConstructor() |
| 293 | + ->onlyMethods(['getMethodInstance', 'getMethod']) |
| 294 | + ->getMock(); |
| 295 | + $paymentMock->method('getMethodInstance')->willReturn($abstractMethod); |
| 296 | + $paymentMock->method('getMethod')->willReturn($paymentProviderCode); |
| 297 | + |
| 298 | + return $paymentMock; |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * DataProvider for pluginForAfterGetMultiShippingModeDataProvider(). |
| 303 | + * |
| 304 | + * @return array |
| 305 | + */ |
| 306 | + public function pluginForAfterGetMultiShippingModeDataProvider(): array |
| 307 | + { |
| 308 | + $productData = [ |
| 309 | + 'productType' => Type::TYPE_SIMPLE, |
| 310 | + 'paymentProviderCode' => 'checkmo', |
| 311 | + 'productOptions' => [ |
| 312 | + 'info_buyRequest' => [ |
| 313 | + 'product' => '1', |
| 314 | + 'qty' => 1, |
| 315 | + ], |
| 316 | + ] |
| 317 | + ]; |
| 318 | + return [ |
| 319 | + 'test case for multi shipping quote' => [true, $productData], |
| 320 | + 'test case for single shipping quote' => [false, $productData] |
| 321 | + ]; |
| 322 | + } |
| 323 | +} |
0 commit comments