|
6 | 6 |
|
7 | 7 | namespace Magento\Multishipping\Test\Unit\Model\Cart;
|
8 | 8 |
|
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 11 | +use Magento\Quote\Model\Cart\CartTotalRepository; |
| 12 | +use Magento\Quote\Model\Cart\Totals as QuoteTotals; |
| 13 | +use Magento\Quote\Model\Quote\Address as QuoteAddress; |
| 14 | +use Magento\Quote\Model\Quote\Address\Rate as QuoteAddressRate; |
| 15 | +use Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin; |
| 16 | +use Magento\Store\Model\Store; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | + |
9 | 19 | class CartTotalRepositoryPluginTest extends \PHPUnit\Framework\TestCase
|
10 | 20 | {
|
11 | 21 | /**
|
12 |
| - * @var \Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin |
| 22 | + * Stub cart id |
| 23 | + */ |
| 24 | + private const STUB_CART_ID = 10; |
| 25 | + |
| 26 | + /** |
| 27 | + * Stub shipping method |
| 28 | + */ |
| 29 | + private const STUB_SHIPPING_METHOD = 'flatrate_flatrate'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Stub shipping price |
| 33 | + */ |
| 34 | + private const STUB_SHIPPING_PRICE = '10.00'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var CartTotalRepositoryPlugin |
13 | 38 | */
|
14 | 39 | private $modelRepository;
|
15 | 40 |
|
16 | 41 | /**
|
17 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 42 | + * @var CartTotalRepository|MockObject |
| 43 | + */ |
| 44 | + private $quoteTotalRepositoryMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var CartRepositoryInterface|MockObject |
18 | 48 | */
|
19 | 49 | private $quoteRepositoryMock;
|
20 | 50 |
|
21 |
| - protected function setUp() |
22 |
| - { |
23 |
| - $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class); |
24 |
| - $this->modelRepository = new \Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin( |
25 |
| - $this->quoteRepositoryMock |
26 |
| - ); |
27 |
| - } |
| 51 | + /** |
| 52 | + * @var QuoteTotals|MockObject |
| 53 | + */ |
| 54 | + private $quoteTotalsMock; |
28 | 55 |
|
29 | 56 | /**
|
30 |
| - * Test quotTotal from cartRepository after get($cartId) function is called |
| 57 | + * @var QuoteAddress|MockObject |
31 | 58 | */
|
32 |
| - public function testAfterGet() |
| 59 | + private $shippingAddressMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var QuoteAddressRate|MockObject |
| 63 | + */ |
| 64 | + private $shippingRateMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var Store|MockObject |
| 68 | + */ |
| 69 | + private $storeMock; |
| 70 | + |
| 71 | + protected function setUp() |
33 | 72 | {
|
34 |
| - $cartId = "10"; |
35 |
| - $shippingMethod = 'flatrate_flatrate'; |
36 |
| - $shippingPrice = '10.00'; |
37 |
| - $quoteMock = $this->createPartialMock( |
38 |
| - \Magento\Quote\Model\Cart\Totals::class, |
| 73 | + $objectManager = new ObjectManager($this); |
| 74 | + $this->quoteTotalsMock = $this->createPartialMock( |
| 75 | + QuoteTotals::class, |
39 | 76 | [
|
40 |
| - 'getStore', |
41 |
| - 'getShippingAddress', |
42 |
| - 'getIsMultiShipping' |
| 77 | + 'getStore', |
| 78 | + 'getShippingAddress', |
| 79 | + 'getIsMultiShipping' |
43 | 80 | ]
|
44 | 81 | );
|
45 |
| - $this->quoteRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock); |
46 |
| - $quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(true); |
47 |
| - $shippingAddressMock = $this->createPartialMock( |
48 |
| - \Magento\Quote\Model\Quote\Address::class, |
| 82 | + $this->shippingAddressMock = $this->createPartialMock( |
| 83 | + QuoteAddress::class, |
49 | 84 | [
|
50 |
| - 'getShippingMethod', |
51 |
| - 'getShippingRateByCode', |
52 |
| - 'getShippingAmount' |
| 85 | + 'getShippingMethod', |
| 86 | + 'getShippingRateByCode', |
| 87 | + 'getShippingAmount' |
53 | 88 | ]
|
54 | 89 | );
|
55 |
| - $quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($shippingAddressMock); |
56 |
| - |
57 |
| - $shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod); |
58 |
| - $shippingAddressMock->expects($this->any())->method('getShippingAmount')->willReturn($shippingPrice); |
59 |
| - $shippingRateMock = $this->createPartialMock( |
60 |
| - \Magento\Quote\Model\Quote\Address\Rate::class, |
| 90 | + $this->shippingRateMock = $this->createPartialMock( |
| 91 | + QuoteAddressRate::class, |
61 | 92 | [
|
62 |
| - 'getPrice' |
| 93 | + 'getPrice' |
63 | 94 | ]
|
64 | 95 | );
|
65 |
| - $shippingAddressMock->expects($this->once())->method('getShippingRateByCode')->willReturn($shippingRateMock); |
66 |
| - |
67 |
| - $shippingRateMock->expects($this->once())->method('getPrice')->willReturn($shippingPrice); |
68 |
| - |
69 |
| - $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) |
| 96 | + $this->storeMock = $this->getMockBuilder(Store::class) |
70 | 97 | ->disableOriginalConstructor()
|
71 | 98 | ->getMock();
|
72 |
| - $quoteMock->expects($this->any())->method('getStore')->willReturn($storeMock); |
73 |
| - $storeMock->expects($this->any())->method('getBaseCurrency')->willReturnSelf(); |
| 99 | + $this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class); |
| 100 | + $this->quoteTotalRepositoryMock = $this->createMock(CartTotalRepository::class); |
| 101 | + $this->modelRepository = $objectManager->getObject(CartTotalRepositoryPlugin::class, [ |
| 102 | + 'quoteRepository' => $this->quoteRepositoryMock |
| 103 | + ]); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test quoteTotal from cartRepository after get($cartId) function is called |
| 108 | + */ |
| 109 | + public function testAfterGetQuoteTotalAddedShippingPrice() |
| 110 | + { |
| 111 | + $this->quoteRepositoryMock->expects($this->once()) |
| 112 | + ->method('getActive') |
| 113 | + ->with(self::STUB_CART_ID) |
| 114 | + ->willReturn($this->quoteTotalsMock); |
| 115 | + $this->quoteTotalsMock->expects($this->once()) |
| 116 | + ->method('getIsMultiShipping') |
| 117 | + ->willReturn(true); |
| 118 | + $this->quoteTotalsMock->expects($this->any()) |
| 119 | + ->method('getShippingAddress') |
| 120 | + ->willReturn($this->shippingAddressMock); |
| 121 | + |
| 122 | + $this->shippingAddressMock->expects($this->once()) |
| 123 | + ->method('getShippingMethod') |
| 124 | + ->willReturn(self::STUB_SHIPPING_METHOD); |
| 125 | + $this->shippingAddressMock->expects($this->any()) |
| 126 | + ->method('getShippingAmount') |
| 127 | + ->willReturn(self::STUB_SHIPPING_PRICE); |
| 128 | + |
| 129 | + $this->shippingAddressMock->expects($this->once()) |
| 130 | + ->method('getShippingRateByCode') |
| 131 | + ->willReturn($this->shippingRateMock); |
| 132 | + |
| 133 | + $this->shippingRateMock->expects($this->once()) |
| 134 | + ->method('getPrice') |
| 135 | + ->willReturn(self::STUB_SHIPPING_PRICE); |
| 136 | + |
| 137 | + $this->quoteTotalsMock->expects($this->any()) |
| 138 | + ->method('getStore') |
| 139 | + ->willReturn($this->storeMock); |
| 140 | + $this->storeMock->expects($this->any()) |
| 141 | + ->method('getBaseCurrency') |
| 142 | + ->willReturnSelf(); |
74 | 143 |
|
75 | 144 | $this->modelRepository->afterGet(
|
76 |
| - $this->createMock(\Magento\Quote\Model\Cart\CartTotalRepository::class), |
77 |
| - $quoteMock, |
78 |
| - $cartId |
| 145 | + $this->quoteTotalRepositoryMock, |
| 146 | + $this->quoteTotalsMock, |
| 147 | + self::STUB_CART_ID |
79 | 148 | );
|
80 | 149 | }
|
81 | 150 | }
|
0 commit comments