12 use \Magento\Checkout\Model\Session;
31 $this->_helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
42 $orderFactory = $this->getMockBuilder(\
Magento\Sales\Model\OrderFactory::class)
43 ->disableOriginalConstructor()
44 ->setMethods([
'create'])
46 $orderFactory->expects($this->once())->method(
'create')->will($this->returnValue($orderMock));
48 $messageCollectionFactory = $this->getMockBuilder(\
Magento\Framework\Message\CollectionFactory::class)
49 ->disableOriginalConstructor()
50 ->setMethods([
'create'])
54 $appState = $this->createPartialMock(\
Magento\Framework\
App\State::class, [
'isInstalled']);
55 $appState->expects($this->any())->method(
'isInstalled')->will($this->returnValue(
true));
58 $request->expects($this->any())->method(
'getHttpHost')->will($this->returnValue([]));
60 $constructArguments = $this->_helper->getConstructArguments(
61 \
Magento\Checkout\Model\Session::class,
64 'orderFactory' => $orderFactory,
65 'messageCollectionFactory' => $messageCollectionFactory,
70 $this->_session = $this->_helper->getObject(\
Magento\Checkout\Model\Session::class, $constructArguments);
71 $this->_session->setLastRealOrderId($orderId);
73 $this->assertSame($orderMock, $this->_session->getLastRealOrder());
74 if ($orderId == $incrementId) {
75 $this->assertSame($orderMock, $this->_session->getLastRealOrder());
85 [
null, 1, $this->_getOrderMock(1,
null)],
86 [1, 1, $this->_getOrderMock(1, 1)],
87 [1,
null, $this->_getOrderMock(
null, 1)]
96 protected function _getOrderMock($incrementId, $orderId)
99 $order = $this->getMockBuilder(
100 \
Magento\Sales\Model\Order::class
101 )->disableOriginalConstructor()->setMethods(
102 [
'getIncrementId',
'loadByIncrementId',
'__sleep',
'__wakeup']
105 if ($orderId && $incrementId) {
106 $order->expects($this->once())->method(
'getIncrementId')->will($this->returnValue($incrementId));
107 $order->expects($this->once())->method(
'loadByIncrementId')->with($orderId);
119 $storage = new \Magento\Framework\Session\Storage(
'default', [$paramToClear =>
'test_data']);
120 $this->_session = $this->_helper->getObject(\
Magento\Checkout\Model\Session::class, [
'storage' => $storage]);
122 $this->_session->clearHelperData();
123 $this->assertNull($this->_session->getData($paramToClear));
134 [
'last_real_order_id'],
135 [
'additional_messages']
145 public function testRestoreQuote($hasOrderId, $hasQuoteId)
147 $order = $this->createPartialMock(
148 \
Magento\Sales\Model\Order::class,
149 [
'getId',
'loadByIncrementId',
'__wakeup']
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));
155 ->setMethods([
'save'])
156 ->getMockForAbstractClass();
157 $storage = new \Magento\Framework\Session\Storage();
160 $storeManager->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
161 $eventManager = $this->getMockForAbstractClass(\
Magento\Framework\Event\ManagerInterface::class);
164 $session = $this->_helper->getObject(
165 \
Magento\Checkout\Model\Session::class,
167 'orderFactory' => $orderFactory,
169 'storage' => $storage,
171 'eventManager' => $eventManager
174 $lastOrderId =
'last order id';
176 $anotherQuoteId =
'another quote id';
177 $session->setLastRealOrderId($lastOrderId);
182 $quote = $this->createPartialMock(
184 [
'setIsActive',
'getId',
'setReservedOrderId',
'__wakeup',
'save']
193 $this->returnValue($anotherQuoteId)
195 $eventManager->expects(
226 ->willThrowException(
229 $quote->expects($this->never())->method(
'setIsActive');
230 $quote->expects($this->never())->method(
'setReservedOrderId');
231 $quote->expects($this->never())->method(
'save');
235 if ($hasOrderId && $hasQuoteId) {
236 $this->assertNull(
$session->getLastRealOrderId());
237 $this->assertEquals($anotherQuoteId,
$session->getQuoteId());
239 $this->assertEquals($lastOrderId,
$session->getLastRealOrderId());
242 $this->assertEquals(
$result, $hasOrderId && $hasQuoteId);
250 return [[
true,
true], [
true,
false], [
false,
true], [
false,
false]];
256 ->disableOriginalConstructor()
259 $this->assertFalse(
$session->hasQuote());
268 ->disableOriginalConstructor()
269 ->setMethods([
'getWebsiteId',
'__wakeup'])
271 $store->expects($this->any())
272 ->method(
'getWebsiteId')
278 ->will($this->returnValue(
$store));
281 ->disableOriginalConstructor()
283 $quote->expects($this->once())
285 ->will($this->returnValue($replaceQuoteId));
287 $storage = $this->getMockBuilder(\
Magento\Framework\
Session\Storage::class)
288 ->disableOriginalConstructor()
289 ->setMethods([
'setData',
'getData'])
292 $storage->expects($this->any())
294 ->willReturn($replaceQuoteId);
295 $storage->expects($this->any())
298 $quoteIdMaskMock = $this->createPartialMock(
300 [
'getMaskedId',
'load',
'setQuoteId',
'save']
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');
307 $quoteIdMaskFactoryMock = $this->createPartialMock(\
Magento\
Quote\Model\QuoteIdMaskFactory::class, [
'create']);
308 $quoteIdMaskFactoryMock->expects($this->once())->method(
'create')->willReturn($quoteIdMaskMock);
310 $session = $this->_helper->getObject(
311 \
Magento\Checkout\Model\Session::class,
314 'storage' => $storage,
315 'quoteIdMaskFactory' => $quoteIdMaskFactoryMock
322 $this->assertEquals($replaceQuoteId,
$session->getQuoteId());
327 $storage = $this->getMockBuilder(\
Magento\Framework\
Session\Storage::class)
328 ->disableOriginalConstructor()
329 ->setMethods([
'unsetData'])
331 $storage->expects($this->once())
332 ->method(
'unsetData');
334 $session = $this->_helper->getObject(
335 \
Magento\Checkout\Model\Session::class,
337 'storage' => $storage
341 $this->assertInstanceOf(\
Magento\Checkout\Model\Session::class,
$session->clearStorage());
342 $this->assertFalse(
$session->hasQuote());
345 public function testResetCheckout()
348 $session = $this->_helper->getObject(
349 \
Magento\Checkout\Model\Session::class,
356 public function testGetStepData()
365 $session = $this->_helper->getObject(
366 \
Magento\Checkout\Model\Session::class,
367 [
'storage' =>
new \
Magento\Framework\Session\Storage()]
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'));
377 public function testSetStepData()
385 $session = $this->_helper->getObject(
386 \
Magento\Checkout\Model\Session::class,
387 [
'storage' =>
new \
Magento\Framework\Session\Storage()]
391 $session->setStepData(
'complex',
'key2',
'value2');
392 $session->setStepData(
'simple', [
'key' =>
'value']);
393 $session->setStepData(
'simple',
'key2',
'value2');
404 $this->assertEquals($expectedResult,
$session->getSteps());
const CHECKOUT_STATE_BEGIN
restoreQuoteDataProvider()
testClearHelperData($paramToClear)
testGetLastRealOrder($orderId, $incrementId, $orderMock)
getLastRealOrderDataProvider()
clearHelperDataDataProvider()