|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Persistent\Test\Unit\Observer; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test for SetCheckoutSessionPersistentDataObserver. |
| 13 | + */ |
| 14 | +class SetCheckoutSessionPersistentDataObserverTest extends \PHPUnit\Framework\TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Magento\Persistent\Observer\SetCheckoutSessionPersistentDataObserver |
| 18 | + */ |
| 19 | + private $model; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Persistent\Helper\Data| \PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + private $helperMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Persistent\Helper\Session| \PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + private $sessionHelperMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Checkout\Model\Session| \PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + private $checkoutSessionMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Customer\Model\Session| \PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $customerSessionMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Magento\Persistent\Model\Session| \PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $persistentSessionMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Customer\Api\CustomerRepositoryInterface| \PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $customerRepositoryMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $observerMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + private $eventMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritdoc |
| 63 | + */ |
| 64 | + protected function setUp() |
| 65 | + { |
| 66 | + $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class); |
| 67 | + $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class); |
| 68 | + $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class); |
| 69 | + $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class); |
| 70 | + $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class); |
| 71 | + $this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getData']); |
| 72 | + $this->persistentSessionMock = $this->createPartialMock( |
| 73 | + \Magento\Persistent\Model\Session::class, |
| 74 | + ['getCustomerId'] |
| 75 | + ); |
| 76 | + $this->customerRepositoryMock = $this->createMock( |
| 77 | + \Magento\Customer\Api\CustomerRepositoryInterface::class |
| 78 | + ); |
| 79 | + $this->model = new \Magento\Persistent\Observer\SetCheckoutSessionPersistentDataObserver( |
| 80 | + $this->sessionHelperMock, |
| 81 | + $this->customerSessionMock, |
| 82 | + $this->helperMock, |
| 83 | + $this->customerRepositoryMock |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test execute method when session is not persistent. |
| 89 | + * |
| 90 | + * @return void |
| 91 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 92 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 93 | + */ |
| 94 | + public function testExecuteWhenSessionIsNotPersistent() |
| 95 | + { |
| 96 | + $this->observerMock->expects($this->once()) |
| 97 | + ->method('getEvent') |
| 98 | + ->willReturn($this->eventMock); |
| 99 | + $this->eventMock->expects($this->once()) |
| 100 | + ->method('getData') |
| 101 | + ->willReturn($this->checkoutSessionMock); |
| 102 | + $this->sessionHelperMock->expects($this->once()) |
| 103 | + ->method('isPersistent') |
| 104 | + ->willReturn(false); |
| 105 | + $this->checkoutSessionMock->expects($this->never()) |
| 106 | + ->method('setLoadInactive'); |
| 107 | + $this->checkoutSessionMock->expects($this->never()) |
| 108 | + ->method('setCustomerData'); |
| 109 | + $this->model->execute($this->observerMock); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Test execute method when session is persistent. |
| 114 | + * |
| 115 | + * @return void |
| 116 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 117 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 118 | + */ |
| 119 | + public function testExecute() |
| 120 | + { |
| 121 | + $this->observerMock->expects($this->once()) |
| 122 | + ->method('getEvent') |
| 123 | + ->willReturn($this->eventMock); |
| 124 | + $this->eventMock->expects($this->once()) |
| 125 | + ->method('getData') |
| 126 | + ->willReturn($this->checkoutSessionMock); |
| 127 | + $this->sessionHelperMock->expects($this->exactly(2)) |
| 128 | + ->method('isPersistent') |
| 129 | + ->willReturn(true); |
| 130 | + $this->customerSessionMock->expects($this->once()) |
| 131 | + ->method('isLoggedIn') |
| 132 | + ->willReturn(false); |
| 133 | + $this->helperMock->expects($this->exactly(2)) |
| 134 | + ->method('isShoppingCartPersist') |
| 135 | + ->willReturn(true); |
| 136 | + $this->persistentSessionMock->expects($this->once()) |
| 137 | + ->method('getCustomerId') |
| 138 | + ->willReturn(123); |
| 139 | + $this->sessionHelperMock->expects($this->once()) |
| 140 | + ->method('getSession') |
| 141 | + ->willReturn($this->persistentSessionMock); |
| 142 | + $this->customerRepositoryMock->expects($this->once()) |
| 143 | + ->method('getById') |
| 144 | + ->willReturn(1); |
| 145 | + $this->checkoutSessionMock->expects($this->never()) |
| 146 | + ->method('setLoadInactive'); |
| 147 | + $this->checkoutSessionMock->expects($this->once()) |
| 148 | + ->method('setCustomerData'); |
| 149 | + $this->model->execute($this->observerMock); |
| 150 | + } |
| 151 | +} |
0 commit comments