|
| 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\Cart; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Customer\Api\Data\AddressInterface; |
| 13 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 14 | +use Magento\Customer\Api\Data\RegionInterface; |
| 15 | +use Magento\Checkout\Model\Cart\CollectQuote; |
| 16 | +use Magento\Customer\Model\Session as CustomerSession; |
| 17 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 18 | +use Magento\Quote\Api\Data\EstimateAddressInterface; |
| 19 | +use Magento\Quote\Api\Data\EstimateAddressInterfaceFactory; |
| 20 | +use Magento\Quote\Api\ShippingMethodManagementInterface; |
| 21 | +use Magento\Quote\Model\Quote; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * Class CollectQuoteTest |
| 26 | + */ |
| 27 | +class CollectQuoteTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var CollectQuote |
| 31 | + */ |
| 32 | + private $model; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + private $customerSessionMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $customerRepositoryMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + private $addressRepositoryMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var EstimateAddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject |
| 51 | + */ |
| 52 | + private $estimateAddressFactoryMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var EstimateAddressInterface|\PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + private $estimateAddressMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var ShippingMethodManagementInterface|\PHPUnit_Framework_MockObject_MockObject |
| 61 | + */ |
| 62 | + private $shippingMethodManagerMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 66 | + */ |
| 67 | + private $quoteRepositoryMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var Quote|\PHPUnit_Framework_MockObject_MockObject |
| 71 | + */ |
| 72 | + private $quoteMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 76 | + */ |
| 77 | + private $customerMock; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var AddressInterface|\PHPUnit_Framework_MockObject_MockObject |
| 81 | + */ |
| 82 | + private $addressMock; |
| 83 | + |
| 84 | + /** |
| 85 | + * Set up |
| 86 | + */ |
| 87 | + protected function setUp() |
| 88 | + { |
| 89 | + $this->customerSessionMock = $this->createMock(CustomerSession::class); |
| 90 | + $this->customerRepositoryMock = $this->getMockForAbstractClass( |
| 91 | + CustomerRepositoryInterface::class, |
| 92 | + [], |
| 93 | + '', |
| 94 | + false, |
| 95 | + true, |
| 96 | + true, |
| 97 | + ['getById'] |
| 98 | + ); |
| 99 | + $this->addressRepositoryMock = $this->createMock(AddressRepositoryInterface::class); |
| 100 | + $this->estimateAddressMock = $this->createMock(EstimateAddressInterface::class); |
| 101 | + $this->estimateAddressFactoryMock = |
| 102 | + $this->createPartialMock(EstimateAddressInterfaceFactory::class, ['create']); |
| 103 | + $this->shippingMethodManagerMock = $this->createMock(ShippingMethodManagementInterface::class); |
| 104 | + $this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class); |
| 105 | + $this->quoteMock = $this->createMock(Quote::class); |
| 106 | + $this->customerMock = $this->createMock(CustomerInterface::class); |
| 107 | + $this->addressMock = $this->createMock(AddressInterface::class); |
| 108 | + |
| 109 | + $this->model = new CollectQuote( |
| 110 | + $this->customerSessionMock, |
| 111 | + $this->customerRepositoryMock, |
| 112 | + $this->addressRepositoryMock, |
| 113 | + $this->estimateAddressFactoryMock, |
| 114 | + $this->shippingMethodManagerMock, |
| 115 | + $this->quoteRepositoryMock |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test collect method |
| 121 | + */ |
| 122 | + public function testCollect() |
| 123 | + { |
| 124 | + $customerId = 1; |
| 125 | + $defaultAddressId = 999; |
| 126 | + $countryId = 'USA'; |
| 127 | + $regionId = 'CA'; |
| 128 | + $regionMock = $this->createMock(RegionInterface::class); |
| 129 | + |
| 130 | + $this->customerSessionMock->expects(self::once()) |
| 131 | + ->method('isLoggedIn') |
| 132 | + ->willReturn(true); |
| 133 | + $this->customerSessionMock->expects(self::once()) |
| 134 | + ->method('getCustomerId') |
| 135 | + ->willReturn($customerId); |
| 136 | + $this->customerRepositoryMock->expects(self::once()) |
| 137 | + ->method('getById') |
| 138 | + ->willReturn($this->customerMock); |
| 139 | + $this->customerMock->expects(self::once()) |
| 140 | + ->method('getDefaultShipping') |
| 141 | + ->willReturn($defaultAddressId); |
| 142 | + $this->addressMock->expects(self::once()) |
| 143 | + ->method('getCountryId') |
| 144 | + ->willReturn($countryId); |
| 145 | + $regionMock->expects(self::once()) |
| 146 | + ->method('getRegion') |
| 147 | + ->willReturn($regionId); |
| 148 | + $this->addressMock->expects(self::once()) |
| 149 | + ->method('getRegion') |
| 150 | + ->willReturn($regionMock); |
| 151 | + $this->addressRepositoryMock->expects(self::once()) |
| 152 | + ->method('getById') |
| 153 | + ->with($defaultAddressId) |
| 154 | + ->willReturn($this->addressMock); |
| 155 | + $this->estimateAddressFactoryMock->expects(self::once()) |
| 156 | + ->method('create') |
| 157 | + ->willReturn($this->estimateAddressMock); |
| 158 | + $this->quoteRepositoryMock->expects(self::once()) |
| 159 | + ->method('save') |
| 160 | + ->with($this->quoteMock); |
| 161 | + |
| 162 | + $this->model->collect($this->quoteMock); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Test with a not logged in customer |
| 167 | + */ |
| 168 | + public function testCollectWhenCustomerIsNotLoggedIn() |
| 169 | + { |
| 170 | + $this->customerSessionMock->expects(self::once()) |
| 171 | + ->method('isLoggedIn') |
| 172 | + ->willReturn(false); |
| 173 | + $this->customerRepositoryMock->expects(self::never()) |
| 174 | + ->method('getById'); |
| 175 | + |
| 176 | + $this->model->collect($this->quoteMock); |
| 177 | + } |
| 178 | +} |
0 commit comments