actionMock = $this->createMock(Index::class); $this->observerMock = $this->createMock(Observer::class); $this->sessionHelperMock = $this->createMock(Session::class); $this->helperMock = $this->createMock(Data::class); $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class); $this->checkoutSession = $this->createMock(CheckoutSession::class); $this->eventManagerMock = $this->getMockBuilder(Event::class) ->addMethods(['getControllerAction']) ->disableOriginalConstructor() ->getMock(); $this->observerMock ->expects($this->once()) ->method('getEvent') ->willReturn($this->eventManagerMock); $this->model = new MakePersistentQuoteGuestObserver( $this->sessionHelperMock, $this->helperMock, $this->customerSessionMock, $this->checkoutSession ); } public function testExecute() { $this->eventManagerMock ->expects($this->once()) ->method('getControllerAction') ->willReturn($this->actionMock); $this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true); $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false); $this->helperMock->expects($this->never())->method('isShoppingCartPersist'); $this->checkoutSession->expects($this->once())->method('clearQuote')->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('clearStorage')->willReturnSelf(); $this->model->execute($this->observerMock); } public function testExecuteWhenShoppingCartIsPersist() { $this->eventManagerMock ->expects($this->once()) ->method('getControllerAction') ->willReturn($this->actionMock); $this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true); $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(true); $this->checkoutSession->expects($this->once())->method('clearQuote')->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('clearStorage')->willReturnSelf(); $this->model->execute($this->observerMock); } public function testExecuteWhenShoppingCartIsNotPersist() { $this->eventManagerMock ->expects($this->once()) ->method('getControllerAction') ->willReturn($this->actionMock); $this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true); $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(false); $this->checkoutSession->expects($this->never())->method('clearQuote')->willReturnSelf(); $this->checkoutSession->expects($this->never())->method('clearStorage')->willReturnSelf(); $this->model->execute($this->observerMock); } }