sessionHelperMock = $this->createMock(Session::class); $this->customerRepository = $this->getMockForAbstractClass( CustomerRepositoryInterface::class, [], '', false ); $this->observerMock = $this->createMock(Observer::class); $this->eventManagerMock = $this->getMockBuilder(Event::class) ->addMethods(['getCustomerCookies']) ->disableOriginalConstructor() ->getMock(); $this->sessionMock = $this->getMockBuilder(PersistentSessionModel::class) ->addMethods(['getGroupId', 'getCustomerId']) ->onlyMethods(['getId']) ->disableOriginalConstructor() ->getMock(); $this->customerMock = $this->getMockForAbstractClass( CustomerInterface::class, [], '', false ); $this->model = new UpdateCustomerCookiesObserver( $this->sessionHelperMock, $this->customerRepository ); } public function testExecuteWhenSessionNotPersistent() { $this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(false); $this->observerMock->expects($this->never())->method('getEvent'); $this->model->execute($this->observerMock); } public function testExecuteWhenCustomerCookieExist() { $customerId = 1; $customerGroupId = 2; $cookieMock = $this->getMockBuilder(DataObject::class) ->addMethods(['setCustomerId', 'setCustomerGroupId']) ->disableOriginalConstructor() ->getMock(); $this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true); $this->observerMock ->expects($this->once()) ->method('getEvent') ->willReturn($this->eventManagerMock); $this->eventManagerMock ->expects($this->once()) ->method('getCustomerCookies') ->willReturn($cookieMock); $this->sessionHelperMock ->expects($this->once()) ->method('getSession') ->willReturn($this->sessionMock); $this->sessionMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->customerRepository ->expects($this->once()) ->method('getById') ->willReturn($this->customerMock); $this->customerMock->expects($this->once())->method('getId')->willReturn($customerId); $this->customerMock->expects($this->once())->method('getGroupId')->willReturn($customerGroupId); $cookieMock->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); $cookieMock ->expects($this->once()) ->method('setCustomerGroupId') ->with($customerGroupId)->willReturnSelf(); $this->model->execute($this->observerMock); } }