_objectManager = new ObjectManager($this); $userId = 'userId'; $userType = 'userType'; $this->userContext = $this->getMockBuilder(CompositeUserContext::class) ->disableOriginalConstructor() ->setMethods(['getUserId', 'getUserType']) ->getMock(); $this->userContext->expects($this->once()) ->method('getUserId') ->willReturn($userId); $this->userContext->expects($this->once()) ->method('getUserType') ->willReturn($userType); $this->roleCollectionFactory = $this->getMockBuilder( \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class )->disableOriginalConstructor() ->setMethods(['create'])->getMock(); $this->roleCollection = $this->getMockBuilder(\Magento\Authorization\Model\ResourceModel\Role\Collection::class) ->disableOriginalConstructor() ->setMethods(['setUserFilter', 'getFirstItem']) ->getMock(); $this->roleCollectionFactory->expects($this->once()) ->method('create') ->willReturn($this->roleCollection); $this->roleCollection->expects($this->once()) ->method('setUserFilter') ->with($userId, $userType) ->willReturn($this->roleCollection); $this->role = $this->getMockBuilder(Role::class) ->disableOriginalConstructor() ->setMethods(['getId', '__wakeup']) ->getMock(); $this->roleCollection->expects($this->once()) ->method('getFirstItem') ->willReturn($this->role); $this->locator = $this->_objectManager->getObject( WebapiRoleLocator::class, [ 'userContext' => $this->userContext, 'roleCollectionFactory' => $this->roleCollectionFactory ] ); } public function testNoRoleId() { $this->role->expects($this->once()) ->method('getId') ->willReturn(null); $this->assertEquals('', $this->locator->getAclRoleId()); } public function testGetAclRoleId() { $roleId = 9; $this->role->expects($this->exactly(2)) ->method('getId') ->willReturn($roleId); $this->assertEquals($roleId, $this->locator->getAclRoleId()); } }