/** * @param int|null $integrationId * @param array $oauthRequest * @return void */ public function setupUserId($integrationId, $oauthRequest) { $integration = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock(); $this->integrationService->expects($this->any())->method('findActiveIntegrationByConsumerId')->will($this->returnValue($integration)); $this->oauthRequestHelper->expects($this->once())->method('prepareRequest')->will($this->returnValue($oauthRequest)); $this->oauthService->expects($this->any())->method('validateAccessTokenRequest')->will($this->returnValue(1)); $integration->expects($this->any())->method('getId')->will($this->returnValue($integrationId)); }
/** * @dataProvider getValidTokenData */ public function testValidToken($userType, $userId, $expectedUserType, $expectedUserId) { $bearerToken = 'bearer1234'; $this->request->expects($this->once())->method('getHeader')->with('Authorization')->will($this->returnValue("Bearer {$bearerToken}")); $token = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token')->disableOriginalConstructor()->setMethods(['loadByToken', 'getId', 'getUserType', 'getCustomerId', 'getAdminId', '__wakeup'])->getMock(); $this->tokenFactory->expects($this->once())->method('create')->will($this->returnValue($token)); $token->expects($this->once())->method('loadByToken')->with($bearerToken)->will($this->returnSelf()); $token->expects($this->once())->method('getId')->will($this->returnValue(1)); $token->expects($this->once())->method('getUserType')->will($this->returnValue($userType)); $integration = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock(); switch ($userType) { case UserContextInterface::USER_TYPE_INTEGRATION: $integration->expects($this->once())->method('getId')->will($this->returnValue($userId)); $this->integrationService->expects($this->once())->method('findByConsumerId')->will($this->returnValue($integration)); break; case UserContextInterface::USER_TYPE_ADMIN: $token->expects($this->once())->method('getAdminId')->will($this->returnValue($userId)); break; case UserContextInterface::USER_TYPE_CUSTOMER: $token->expects($this->once())->method('getCustomerId')->will($this->returnValue($userId)); break; } $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType()); $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId()); /* check again to make sure that the above methods were only called once */ $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType()); $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId()); }