Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteManagerTest.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class QuoteManagerTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
23 
28 
33 
37  protected $quoteMock;
38 
42  protected $sessionMock;
43 
48 
53 
54  protected function setUp()
55  {
56  $this->persistentSessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
57  $this->sessionMock =
58  $this->createPartialMock(\Magento\Persistent\Model\Session::class, [
59  'setLoadInactive',
60  'setCustomerData',
61  'clearQuote',
62  'clearStorage',
63  'getQuote',
64  'removePersistentCookie',
65  '__wakeup',
66  ]);
67  $this->persistentDataMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
68  $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
69 
70  $this->abstractCollectionMock =
71  $this->createMock(\Magento\Eav\Model\Entity\Collection\AbstractCollection::class);
72 
73  $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
74  $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
75  'getId',
76  'getIsPersistent',
77  'getPaymentsCollection',
78  'getAddressesCollection',
79  'setIsActive',
80  'setCustomerId',
81  'setCustomerEmail',
82  'setCustomerFirstname',
83  'setCustomerLastname',
84  'setCustomerGroupId',
85  'setIsPersistent',
86  'getShippingAddress',
87  'getBillingAddress',
88  'collectTotals',
89  'removeAllAddresses',
90  'getIsActive',
91  'getCustomerId',
92  '__wakeup'
93  ]);
94 
95  $this->model = new QuoteManager(
96  $this->persistentSessionMock,
97  $this->persistentDataMock,
98  $this->checkoutSessionMock,
99  $this->quoteRepositoryMock
100  );
101  }
102 
103  public function testSetGuestWithEmptyQuote()
104  {
105  $this->checkoutSessionMock->expects($this->once())
106  ->method('getQuote')->will($this->returnValue(null));
107  $this->quoteMock->expects($this->never())->method('getId');
108 
109  $this->persistentSessionMock->expects($this->once())
110  ->method('getSession')->will($this->returnValue($this->sessionMock));
111  $this->sessionMock->expects($this->once())
112  ->method('removePersistentCookie')->will($this->returnValue($this->sessionMock));
113 
114  $this->model->setGuest(false);
115  }
116 
118  {
119  $this->checkoutSessionMock->expects($this->once())
120  ->method('getQuote')->will($this->returnValue($this->quoteMock));
121  $this->quoteMock->expects($this->once())->method('getId')->will($this->returnValue(null));
122  $this->persistentDataMock->expects($this->never())->method('isShoppingCartPersist');
123 
124  $this->persistentSessionMock->expects($this->once())
125  ->method('getSession')->will($this->returnValue($this->sessionMock));
126  $this->sessionMock->expects($this->once())
127  ->method('removePersistentCookie')->will($this->returnValue($this->sessionMock));
128 
129  $this->model->setGuest(false);
130  }
131 
133  {
134  $this->checkoutSessionMock->expects($this->once())
135  ->method('getQuote')->will($this->returnValue($this->quoteMock));
136  $this->quoteMock->expects($this->once())->method('getId')->will($this->returnValue(11));
137  $this->persistentDataMock->expects($this->once())
138  ->method('isShoppingCartPersist')->will($this->returnValue(false));
139  $this->quoteMock->expects($this->once())->method('getIsPersistent')->will($this->returnValue(false));
140  $this->checkoutSessionMock->expects($this->once())
141  ->method('clearQuote')->will($this->returnValue($this->checkoutSessionMock));
142  $this->checkoutSessionMock->expects($this->once())->method('clearStorage');
143  $this->quoteMock->expects($this->never())->method('getPaymentsCollection');
144 
145  $this->model->setGuest(true);
146  }
147 
148  public function testSetGuest()
149  {
150  $this->checkoutSessionMock->expects($this->once())
151  ->method('getQuote')->will($this->returnValue($this->quoteMock));
152  $this->quoteMock->expects($this->once())->method('getId')->will($this->returnValue(11));
153  $this->persistentDataMock->expects($this->never())->method('isShoppingCartPersist');
154  $this->quoteMock->expects($this->once())
155  ->method('getPaymentsCollection')->will($this->returnValue($this->abstractCollectionMock));
156  $this->quoteMock->expects($this->once())
157  ->method('getAddressesCollection')->will($this->returnValue($this->abstractCollectionMock));
158  $this->abstractCollectionMock->expects($this->exactly(2))->method('walk')->with('delete');
159  $this->quoteMock->expects($this->once())
160  ->method('setIsActive')->with(true)->will($this->returnValue($this->quoteMock));
161  $this->quoteMock->expects($this->once())
162  ->method('setCustomerId')->with(null)->will($this->returnValue($this->quoteMock));
163  $this->quoteMock->expects($this->once())
164  ->method('setCustomerEmail')->with(null)->will($this->returnValue($this->quoteMock));
165  $this->quoteMock->expects($this->once())
166  ->method('setCustomerFirstname')->with(null)->will($this->returnValue($this->quoteMock));
167  $this->quoteMock->expects($this->once())
168  ->method('setCustomerLastname')->with(null)->will($this->returnValue($this->quoteMock));
169  $this->quoteMock->expects($this->once())->method('setCustomerGroupId')
170  ->with(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID)
171  ->will($this->returnValue($this->quoteMock));
172  $this->quoteMock->expects($this->once())
173  ->method('setIsPersistent')->with(false)->will($this->returnValue($this->quoteMock));
174  $this->quoteMock->expects($this->once())
175  ->method('removeAllAddresses')->will($this->returnValue($this->quoteMock));
176  $quoteAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
177  $this->quoteMock->expects($this->once())
178  ->method('getShippingAddress')->will($this->returnValue($quoteAddressMock));
179  $this->quoteMock->expects($this->once())
180  ->method('getBillingAddress')->will($this->returnValue($quoteAddressMock));
181  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
182  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
183  $this->persistentSessionMock->expects($this->once())
184  ->method('getSession')->will($this->returnValue($this->sessionMock));
185  $this->sessionMock->expects($this->once())
186  ->method('removePersistentCookie')->will($this->returnValue($this->sessionMock));
187 
188  $this->model->setGuest(false);
189  }
190 
192  {
193  $this->checkoutSessionMock->expects($this->once())
194  ->method('setLoadInactive')->will($this->returnValue($this->sessionMock));
195 
196  $this->sessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($this->quoteMock));
197 
198  $this->quoteMock->expects($this->once())->method('getIsActive')->will($this->returnValue(11));
199  $this->quoteMock->expects($this->once())->method('getCustomerId')->will($this->returnValue(22));
200 
201  $this->checkoutSessionMock->expects($this->once())
202  ->method('setCustomerData')->with(null)->will($this->returnValue($this->sessionMock));
203 
204  $this->sessionMock->expects($this->once())
205  ->method('clearQuote')->will($this->returnValue($this->sessionMock));
206  $this->sessionMock->expects($this->once())
207  ->method('clearStorage')->will($this->returnValue($this->sessionMock));
208  $this->quoteMock->expects($this->never())->method('setIsActive');
209 
210  $this->model->expire();
211  }
212 
213  public function testExpire()
214  {
215  $this->checkoutSessionMock->expects($this->once())
216  ->method('setLoadInactive')->will($this->returnValue($this->sessionMock));
217  $this->sessionMock->expects($this->once())->method('getQuote')->will($this->returnValue($this->quoteMock));
218  $this->quoteMock->expects($this->once())->method('getIsActive')->will($this->returnValue(0));
219  $this->checkoutSessionMock->expects($this->never())->method('setCustomerData');
220  $this->quoteMock->expects($this->once())
221  ->method('setIsActive')
222  ->with(true)
223  ->will($this->returnValue($this->quoteMock));
224  $this->quoteMock->expects($this->once())
225  ->method('setIsPersistent')
226  ->with(false)
227  ->will($this->returnValue($this->quoteMock));
228  $this->quoteMock->expects($this->once())
229  ->method('setCustomerId')
230  ->with(null)
231  ->will($this->returnValue($this->quoteMock));
232  $this->quoteMock->expects($this->once())
233  ->method('setCustomerGroupId')
234  ->with(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID)
235  ->will($this->returnValue($this->quoteMock));
236 
237  $this->model->expire();
238  }
239 
241  {
242  $quoteId = 1;
243  $addressArgs = ['customerAddressId' => null];
244  $customerIdArgs = ['customerId' => null];
245  $emailArgs = ['email' => null];
246 
247  $this->checkoutSessionMock->expects($this->once())
248  ->method('getQuoteId')->willReturn($quoteId);
249  $this->quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
250  $this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($this->quoteMock);
251  $this->quoteMock->expects($this->once())
252  ->method('setIsActive')->with(true)->willReturn($this->quoteMock);
253  $this->quoteMock->expects($this->once())
254  ->method('setCustomerId')->with(null)->willReturn($this->quoteMock);
255  $this->quoteMock->expects($this->once())
256  ->method('setCustomerEmail')->with(null)->willReturn($this->quoteMock);
257  $this->quoteMock->expects($this->once())
258  ->method('setCustomerFirstname')->with(null)->willReturn($this->quoteMock);
259  $this->quoteMock->expects($this->once())
260  ->method('setCustomerLastname')->with(null)->willReturn($this->quoteMock);
261  $this->quoteMock->expects($this->once())->method('setCustomerGroupId')
262  ->with(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID)
263  ->willReturn($this->quoteMock);
264  $this->quoteMock->expects($this->once())
265  ->method('setIsPersistent')->with(false)->willReturn($this->quoteMock);
266  $this->quoteMock->expects($this->exactly(3))
267  ->method('getAddressesCollection')->willReturn($this->abstractCollectionMock);
268  $this->abstractCollectionMock->expects($this->exactly(3))->method('walk')->with($this->logicalOr(
269  $this->equalTo('setCustomerAddressId'),
270  $this->equalTo($addressArgs),
271  $this->equalTo('setCustomerId'),
272  $this->equalTo($customerIdArgs),
273  $this->equalTo('setEmail'),
274  $this->equalTo($emailArgs)
275  ));
276  $this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock);
277  $this->persistentSessionMock->expects($this->once())
278  ->method('getSession')->willReturn($this->sessionMock);
279  $this->sessionMock->expects($this->once())
280  ->method('removePersistentCookie')->willReturn($this->sessionMock);
281  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
282 
283  $this->model->convertCustomerCartToGuest();
284  }
285 
287  {
288  $this->checkoutSessionMock->expects($this->once())
289  ->method('getQuoteId')->willReturn(null);
290  $this->quoteRepositoryMock->expects($this->once())->method('get')->with(null)->willReturn(null);
291  $this->model->convertCustomerCartToGuest();
292  }
293 
295  {
296  $this->checkoutSessionMock->expects($this->once())
297  ->method('getQuoteId')->willReturn(1);
298  $quoteWithNoId = $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
299  $quoteWithNoId->expects($this->once())->method('getId')->willReturn(null);
300  $this->quoteRepositoryMock->expects($this->once())->method('get')->with(1)->willReturn($quoteWithNoId);
301  $this->quoteMock->expects($this->once())->method('getId')->willReturn(1);
302  $this->model->convertCustomerCartToGuest();
303  }
304 }