helperMock = $this->createMock(Data::class); $this->sessionHelperMock = $this->createMock(Session::class); $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class); $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class); $this->observerMock = $this->createMock(Observer::class); $this->eventMock = $this->createPartialMock(Event::class, ['getData']); $this->persistentSessionMock = $this->getMockBuilder(\Magento\Persistent\Model\Session::class) ->addMethods(['getCustomerId']) ->disableOriginalConstructor() ->getMock(); $this->customerRepositoryMock = $this->createMock( CustomerRepositoryInterface::class ); $this->model = new SetCheckoutSessionPersistentDataObserver( $this->sessionHelperMock, $this->customerSessionMock, $this->helperMock, $this->customerRepositoryMock ); } /** * Test execute method when session is not persistent * * @throws LocalizedException * @throws NoSuchEntityException */ public function testExecuteWhenSessionIsNotPersistent() { $this->observerMock->expects($this->once()) ->method('getEvent') ->willReturn($this->eventMock); $this->eventMock->expects($this->once()) ->method('getData') ->willReturn($this->checkoutSessionMock); $this->sessionHelperMock->expects($this->once()) ->method('isPersistent') ->willReturn(false); $this->checkoutSessionMock->expects($this->never()) ->method('setLoadInactive'); $this->checkoutSessionMock->expects($this->never()) ->method('setCustomerData'); $this->model->execute($this->observerMock); } /** * Test execute method when session is persistent * * @throws LocalizedException * @throws NoSuchEntityException */ public function testExecute() { $this->observerMock->expects($this->once()) ->method('getEvent') ->willReturn($this->eventMock); $this->eventMock->expects($this->once()) ->method('getData') ->willReturn($this->checkoutSessionMock); $this->sessionHelperMock->expects($this->exactly(2)) ->method('isPersistent') ->willReturn(true); $this->customerSessionMock->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); $this->helperMock->expects($this->exactly(2)) ->method('isShoppingCartPersist') ->willReturn(true); $this->persistentSessionMock->expects($this->once()) ->method('getCustomerId') ->willReturn(123); $this->sessionHelperMock->expects($this->once()) ->method('getSession') ->willReturn($this->persistentSessionMock); $this->customerRepositoryMock->expects($this->once()) ->method('getById') ->willReturn(true); //? $this->checkoutSessionMock->expects($this->never()) ->method('setLoadInactive'); $this->checkoutSessionMock->expects($this->once()) ->method('setCustomerData'); $this->model->execute($this->observerMock); } }