|
| 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\Checkout\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Checkout\Api\Data\TotalsInformationInterface; |
| 11 | +use Magento\Checkout\Model\TotalsInformationManagement; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 14 | +use Magento\Quote\Api\CartTotalRepositoryInterface; |
| 15 | +use Magento\Quote\Model\Quote\Address; |
| 16 | + |
| 17 | +class TotalsInformationManagementTest extends \PHPUnit\Framework\TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ObjectManager |
| 21 | + */ |
| 22 | + private $objectManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var CartRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject |
| 26 | + */ |
| 27 | + private $cartRepositoryMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var CartTotalRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject |
| 31 | + */ |
| 32 | + private $cartTotalRepositoryMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var TotalsInformationManagement |
| 36 | + */ |
| 37 | + private $totalsInformationManagement; |
| 38 | + |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $this->objectManager = new ObjectManager($this); |
| 42 | + $this->cartRepositoryMock = $this->createMock( |
| 43 | + CartRepositoryInterface::class |
| 44 | + ); |
| 45 | + $this->cartTotalRepositoryMock = $this->createMock( |
| 46 | + CartTotalRepositoryInterface::class |
| 47 | + ); |
| 48 | + |
| 49 | + $this->totalsInformationManagement = $this->objectManager->getObject( |
| 50 | + TotalsInformationManagement::class, |
| 51 | + [ |
| 52 | + 'cartRepository' => $this->cartRepositoryMock, |
| 53 | + 'cartTotalRepository' => $this->cartTotalRepositoryMock, |
| 54 | + ] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test for \Magento\Checkout\Model\TotalsInformationManagement::calculate. |
| 60 | + * |
| 61 | + * @param string|null $carrierCode |
| 62 | + * @param string|null $carrierMethod |
| 63 | + * @param int $methodSetCount |
| 64 | + * @dataProvider dataProviderCalculate |
| 65 | + */ |
| 66 | + public function testCalculate(?string $carrierCode, ?string $carrierMethod, int $methodSetCount) |
| 67 | + { |
| 68 | + $cartId = 1; |
| 69 | + $cartMock = $this->createMock( |
| 70 | + \Magento\Quote\Model\Quote::class |
| 71 | + ); |
| 72 | + $cartMock->expects($this->once())->method('getItemsCount')->willReturn(1); |
| 73 | + $cartMock->expects($this->once())->method('getIsVirtual')->willReturn(false); |
| 74 | + $this->cartRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($cartMock); |
| 75 | + $this->cartTotalRepositoryMock->expects($this->once())->method('get')->with($cartId); |
| 76 | + |
| 77 | + $addressInformationMock = $this->createMock( |
| 78 | + TotalsInformationInterface::class |
| 79 | + ); |
| 80 | + $addressMock = $this->getMockBuilder(Address::class) |
| 81 | + ->addMethods( |
| 82 | + [ |
| 83 | + 'setShippingMethod', |
| 84 | + 'setCollectShippingRates', |
| 85 | + ] |
| 86 | + ) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + |
| 90 | + $addressInformationMock->expects($this->once())->method('getAddress')->willReturn($addressMock); |
| 91 | + $addressInformationMock->expects($this->any())->method('getShippingCarrierCode')->willReturn($carrierCode); |
| 92 | + $addressInformationMock->expects($this->any())->method('getShippingMethodCode')->willReturn($carrierMethod); |
| 93 | + $cartMock->expects($this->once())->method('setShippingAddress')->with($addressMock); |
| 94 | + $cartMock->expects($this->exactly($methodSetCount))->method('getShippingAddress')->willReturn($addressMock); |
| 95 | + $addressMock->expects($this->exactly($methodSetCount)) |
| 96 | + ->method('setCollectShippingRates')->with(true)->willReturn($addressMock); |
| 97 | + $addressMock->expects($this->exactly($methodSetCount)) |
| 98 | + ->method('setShippingMethod')->with($carrierCode . '_' . $carrierMethod); |
| 99 | + $cartMock->expects($this->once())->method('collectTotals'); |
| 100 | + |
| 101 | + $this->totalsInformationManagement->calculate($cartId, $addressInformationMock); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Data provider for testCalculate. |
| 106 | + * |
| 107 | + * @return array |
| 108 | + */ |
| 109 | + public function dataProviderCalculate(): array |
| 110 | + { |
| 111 | + return [ |
| 112 | + [ |
| 113 | + null, |
| 114 | + null, |
| 115 | + 0 |
| 116 | + ], |
| 117 | + [ |
| 118 | + null, |
| 119 | + 'carrier_method', |
| 120 | + 0 |
| 121 | + ], |
| 122 | + [ |
| 123 | + 'carrier_code', |
| 124 | + null, |
| 125 | + 0 |
| 126 | + ], |
| 127 | + [ |
| 128 | + 'carrier_code', |
| 129 | + 'carrier_method', |
| 130 | + 1 |
| 131 | + ] |
| 132 | + ]; |
| 133 | + } |
| 134 | +} |
0 commit comments