Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SessionTest.php
Go to the documentation of this file.
1 <?php
11 
12 use \Magento\Checkout\Model\Session;
13 
17 class SessionTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $_helper;
23 
27  protected $_session;
28 
29  protected function setUp()
30  {
31  $this->_helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
32  }
33 
40  public function testGetLastRealOrder($orderId, $incrementId, $orderMock)
41  {
42  $orderFactory = $this->getMockBuilder(\Magento\Sales\Model\OrderFactory::class)
43  ->disableOriginalConstructor()
44  ->setMethods(['create'])
45  ->getMock();
46  $orderFactory->expects($this->once())->method('create')->will($this->returnValue($orderMock));
47 
48  $messageCollectionFactory = $this->getMockBuilder(\Magento\Framework\Message\CollectionFactory::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['create'])
51  ->getMock();
52  $quoteRepository = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
53 
54  $appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['isInstalled']);
55  $appState->expects($this->any())->method('isInstalled')->will($this->returnValue(true));
56 
57  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
58  $request->expects($this->any())->method('getHttpHost')->will($this->returnValue([]));
59 
60  $constructArguments = $this->_helper->getConstructArguments(
61  \Magento\Checkout\Model\Session::class,
62  [
63  'request' => $request,
64  'orderFactory' => $orderFactory,
65  'messageCollectionFactory' => $messageCollectionFactory,
66  'quoteRepository' => $quoteRepository,
67  'storage' => new \Magento\Framework\Session\Storage()
68  ]
69  );
70  $this->_session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, $constructArguments);
71  $this->_session->setLastRealOrderId($orderId);
72 
73  $this->assertSame($orderMock, $this->_session->getLastRealOrder());
74  if ($orderId == $incrementId) {
75  $this->assertSame($orderMock, $this->_session->getLastRealOrder());
76  }
77  }
78 
82  public function getLastRealOrderDataProvider()
83  {
84  return [
85  [null, 1, $this->_getOrderMock(1, null)],
86  [1, 1, $this->_getOrderMock(1, 1)],
87  [1, null, $this->_getOrderMock(null, 1)]
88  ];
89  }
90 
96  protected function _getOrderMock($incrementId, $orderId)
97  {
99  $order = $this->getMockBuilder(
100  \Magento\Sales\Model\Order::class
101  )->disableOriginalConstructor()->setMethods(
102  ['getIncrementId', 'loadByIncrementId', '__sleep', '__wakeup']
103  )->getMock();
104 
105  if ($orderId && $incrementId) {
106  $order->expects($this->once())->method('getIncrementId')->will($this->returnValue($incrementId));
107  $order->expects($this->once())->method('loadByIncrementId')->with($orderId);
108  }
109 
110  return $order;
111  }
112 
117  public function testClearHelperData($paramToClear)
118  {
119  $storage = new \Magento\Framework\Session\Storage('default', [$paramToClear => 'test_data']);
120  $this->_session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, ['storage' => $storage]);
121 
122  $this->_session->clearHelperData();
123  $this->assertNull($this->_session->getData($paramToClear));
124  }
125 
129  public function clearHelperDataDataProvider()
130  {
131  return [
132  ['redirect_url'],
133  ['last_order_id'],
134  ['last_real_order_id'],
135  ['additional_messages']
136  ];
137  }
138 
145  public function testRestoreQuote($hasOrderId, $hasQuoteId)
146  {
147  $order = $this->createPartialMock(
148  \Magento\Sales\Model\Order::class,
149  ['getId', 'loadByIncrementId', '__wakeup']
150  );
151  $order->expects($this->once())->method('getId')->will($this->returnValue($hasOrderId ? 'order id' : null));
152  $orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
153  $orderFactory->expects($this->once())->method('create')->will($this->returnValue($order));
154  $quoteRepository = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)
155  ->setMethods(['save'])
156  ->getMockForAbstractClass();
157  $storage = new \Magento\Framework\Session\Storage();
158  $store = $this->createMock(\Magento\Store\Model\Store::class);
159  $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
160  $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
161  $eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
162 
164  $session = $this->_helper->getObject(
165  \Magento\Checkout\Model\Session::class,
166  [
167  'orderFactory' => $orderFactory,
168  'quoteRepository' => $quoteRepository,
169  'storage' => $storage,
170  'storeManager' => $storeManager,
171  'eventManager' => $eventManager
172  ]
173  );
174  $lastOrderId = 'last order id';
175  $quoteId = 'quote id';
176  $anotherQuoteId = 'another quote id';
177  $session->setLastRealOrderId($lastOrderId);
178  $session->setQuoteId($quoteId);
179 
180  if ($hasOrderId) {
181  $order->setQuoteId($quoteId);
182  $quote = $this->createPartialMock(
183  \Magento\Quote\Model\Quote::class,
184  ['setIsActive', 'getId', 'setReservedOrderId', '__wakeup', 'save']
185  );
186  if ($hasQuoteId) {
187  $quoteRepository->expects($this->once())->method('get')->with($quoteId)->willReturn($quote);
188  $quote->expects(
189  $this->any()
190  )->method(
191  'getId'
192  )->will(
193  $this->returnValue($anotherQuoteId)
194  );
195  $eventManager->expects(
196  $this->once()
197  )->method(
198  'dispatch'
199  )->with(
200  'restore_quote',
201  ['order' => $order, 'quote' => $quote]
202  );
203  $quote->expects(
204  $this->once()
205  )->method(
206  'setIsActive'
207  )->with(
208  $this->equalTo(1)
209  )->will(
210  $this->returnSelf()
211  );
212  $quote->expects(
213  $this->once()
214  )->method(
215  'setReservedOrderId'
216  )->with(
217  $this->isNull()
218  )->will(
219  $this->returnSelf()
220  );
221  $quoteRepository->expects($this->once())->method('save')->with($quote);
222  } else {
223  $quoteRepository->expects($this->once())
224  ->method('get')
225  ->with($quoteId)
226  ->willThrowException(
227  new \Magento\Framework\Exception\NoSuchEntityException()
228  );
229  $quote->expects($this->never())->method('setIsActive');
230  $quote->expects($this->never())->method('setReservedOrderId');
231  $quote->expects($this->never())->method('save');
232  }
233  }
234  $result = $session->restoreQuote();
235  if ($hasOrderId && $hasQuoteId) {
236  $this->assertNull($session->getLastRealOrderId());
237  $this->assertEquals($anotherQuoteId, $session->getQuoteId());
238  } else {
239  $this->assertEquals($lastOrderId, $session->getLastRealOrderId());
240  $this->assertEquals($quoteId, $session->getQuoteId());
241  }
242  $this->assertEquals($result, $hasOrderId && $hasQuoteId);
243  }
244 
248  public function restoreQuoteDataProvider()
249  {
250  return [[true, true], [true, false], [false, true], [false, false]];
251  }
252 
253  public function testHasQuote()
254  {
255  $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
256  ->disableOriginalConstructor()
257  ->getMock();
258  $session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, ['quote' => $quote]);
259  $this->assertFalse($session->hasQuote());
260  }
261 
262  public function testReplaceQuote()
263  {
264  $replaceQuoteId = 3;
265  $websiteId = 1;
266 
267  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
268  ->disableOriginalConstructor()
269  ->setMethods(['getWebsiteId', '__wakeup'])
270  ->getMock();
271  $store->expects($this->any())
272  ->method('getWebsiteId')
273  ->will($this->returnValue($websiteId));
274 
275  $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
276  $storeManager->expects($this->any())
277  ->method('getStore')
278  ->will($this->returnValue($store));
279 
280  $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
281  ->disableOriginalConstructor()
282  ->getMock();
283  $quote->expects($this->once())
284  ->method('getId')
285  ->will($this->returnValue($replaceQuoteId));
286 
287  $storage = $this->getMockBuilder(\Magento\Framework\Session\Storage::class)
288  ->disableOriginalConstructor()
289  ->setMethods(['setData', 'getData'])
290  ->getMock();
291 
292  $storage->expects($this->any())
293  ->method('getData')
294  ->willReturn($replaceQuoteId);
295  $storage->expects($this->any())
296  ->method('setData');
297 
298  $quoteIdMaskMock = $this->createPartialMock(
299  \Magento\Quote\Model\QuoteIdMask::class,
300  ['getMaskedId', 'load', 'setQuoteId', 'save']
301  );
302  $quoteIdMaskMock->expects($this->once())->method('load')->with($replaceQuoteId, 'quote_id')->willReturnSelf();
303  $quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willReturn(null);
304  $quoteIdMaskMock->expects($this->once())->method('setQuoteId')->with($replaceQuoteId)->willReturnSelf();
305  $quoteIdMaskMock->expects($this->once())->method('save');
306 
307  $quoteIdMaskFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteIdMaskFactory::class, ['create']);
308  $quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($quoteIdMaskMock);
309 
310  $session = $this->_helper->getObject(
311  \Magento\Checkout\Model\Session::class,
312  [
313  'storeManager' => $storeManager,
314  'storage' => $storage,
315  'quoteIdMaskFactory' => $quoteIdMaskFactoryMock
316  ]
317  );
318 
319  $session->replaceQuote($quote);
320 
321  $this->assertSame($quote, $session->getQuote());
322  $this->assertEquals($replaceQuoteId, $session->getQuoteId());
323  }
324 
325  public function testClearStorage()
326  {
327  $storage = $this->getMockBuilder(\Magento\Framework\Session\Storage::class)
328  ->disableOriginalConstructor()
329  ->setMethods(['unsetData'])
330  ->getMock();
331  $storage->expects($this->once())
332  ->method('unsetData');
333 
334  $session = $this->_helper->getObject(
335  \Magento\Checkout\Model\Session::class,
336  [
337  'storage' => $storage
338  ]
339  );
340 
341  $this->assertInstanceOf(\Magento\Checkout\Model\Session::class, $session->clearStorage());
342  $this->assertFalse($session->hasQuote());
343  }
344 
345  public function testResetCheckout()
346  {
348  $session = $this->_helper->getObject(
349  \Magento\Checkout\Model\Session::class,
350  ['storage' => new \Magento\Framework\Session\Storage()]
351  );
352  $session->resetCheckout();
353  $this->assertEquals(\Magento\Checkout\Model\Session::CHECKOUT_STATE_BEGIN, $session->getCheckoutState());
354  }
355 
356  public function testGetStepData()
357  {
358  $stepData = [
359  'simple' => 'data',
360  'complex' => [
361  'key' => 'value',
362  ],
363  ];
365  $session = $this->_helper->getObject(
366  \Magento\Checkout\Model\Session::class,
367  ['storage' => new \Magento\Framework\Session\Storage()]
368  );
369  $session->setSteps($stepData);
370  $this->assertEquals($stepData, $session->getStepData());
371  $this->assertFalse($session->getStepData('invalid_key'));
372  $this->assertEquals($stepData['complex'], $session->getStepData('complex'));
373  $this->assertFalse($session->getStepData('simple', 'invalid_sub_key'));
374  $this->assertEquals($stepData['complex']['key'], $session->getStepData('complex', 'key'));
375  }
376 
377  public function testSetStepData()
378  {
379  $stepData = [
380  'complex' => [
381  'key' => 'value',
382  ],
383  ];
385  $session = $this->_helper->getObject(
386  \Magento\Checkout\Model\Session::class,
387  ['storage' => new \Magento\Framework\Session\Storage()]
388  );
389  $session->setSteps($stepData);
390 
391  $session->setStepData('complex', 'key2', 'value2');
392  $session->setStepData('simple', ['key' => 'value']);
393  $session->setStepData('simple', 'key2', 'value2');
394  $expectedResult = [
395  'complex' => [
396  'key' => 'value',
397  'key2' => 'value2',
398  ],
399  'simple' => [
400  'key' => 'value',
401  'key2' => 'value2',
402  ],
403  ];
404  $this->assertEquals($expectedResult, $session->getSteps());
405  }
406 }
$quote
$order
Definition: order.php:55
$storeManager
testGetLastRealOrder($orderId, $incrementId, $orderMock)
Definition: SessionTest.php:40
$quoteRepository