/** * Create a new token (and generate the token) * * @param TokenOwnerInterface $owner * @param Client $client * @param string|string[]|Scope[] $scopes * @return AccessToken * @throws OAuth2Exception */ public function createToken($owner, $client, $scopes) : AccessToken { if (empty($scopes)) { $scopes = $this->scopeService->getDefaultScopes(); } else { $this->validateTokenScopes($scopes); } do { $token = AccessToken::createNewAccessToken($this->serverOptions->getAccessTokenTtl(), $owner, $client, $scopes); } while ($this->tokenRepository->tokenExists($token->getToken())); return $this->tokenRepository->save($token); }
public function testCreateNewTokenUntilOneDoesNotExist() { $this->scopeService->expects($this->once())->method('getDefaultScopes')->will($this->returnValue(['read'])); $this->tokenRepository->expects($this->at(0))->method('tokenExists')->with($this->isType('string'))->willReturn(true); $this->tokenRepository->expects($this->at(1))->method('tokenExists')->with($this->isType('string'))->willReturn(false); $this->tokenRepository->expects($this->once())->method('save')->will($this->returnArgument(0)); $owner = $this->createMock(TokenOwnerInterface::class); $client = $this->createMock(Client::class); $token = $this->tokenService->createToken($owner, $client, []); $this->assertEquals(40, strlen($token->getToken())); }