Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectQuoteTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 use Magento\Quote\Api\Data\EstimateAddressInterfaceFactory;
22 use PHPUnit\Framework\TestCase;
23 
27 class CollectQuoteTest extends TestCase
28 {
32  private $model;
33 
37  private $customerSessionMock;
38 
42  private $customerRepositoryMock;
43 
47  private $addressRepositoryMock;
48 
52  private $estimateAddressFactoryMock;
53 
57  private $estimateAddressMock;
58 
62  private $shippingMethodManagerMock;
63 
67  private $quoteRepositoryMock;
68 
72  private $quoteMock;
73 
77  private $customerMock;
78 
82  private $addressMock;
83 
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 
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 
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 }