Skip to content

Commit

Permalink
🚿 coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Sep 19, 2024
1 parent 3a13a22 commit 71b0d66
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Core/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function set_expires(DateTime|DateInterval|int|null $expires = null):v

// clamp max expiry
if($this->expires > $max){
$this->expires = $max;
$this->expires = $max; // @codeCoverageIgnore
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/OAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ final protected function getMeResponseData(string $endpoint, array|null $params
$this->handleMeResponseError($response);

/** @noinspection PhpUnreachableStatementInspection this is here because phpstan silly */
return [];
return []; // @codeCoverageIgnore
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Core/PKCETrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ final public function setCodeChallenge(array $params, string $challengeMethod):a
*/
final public function setCodeVerifier(array $params):array{

if(!$this instanceof PKCE){
throw new ProviderException('PKCE challenge not supported');
}

if(!isset($params['grant_type'], $params['code']) || $params['grant_type'] !== 'authorization_code'){
throw new ProviderException('invalid authorization request body');
}
Expand Down Expand Up @@ -120,7 +116,7 @@ final public function generateChallenge(string $verifier, string $challengeMetho
$verifier = match($challengeMethod){
PKCE::CHALLENGE_METHOD_S256 => hash('sha256', $verifier, true),
// no other hash methods yet
default => throw new ProviderException('invalid PKCE challenge method'),
default => throw new ProviderException('invalid PKCE challenge method'), // @codeCoverageIgnore
};

return sodium_bin2base64($verifier, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING);
Expand Down
4 changes: 2 additions & 2 deletions src/Core/TokenInvalidateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul

// @link https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
if(!in_array($type, ['access_token', 'refresh_token'], true)){
throw new ProviderException(sprintf('invalid token type "%s"', $type));
throw new ProviderException(sprintf('invalid token type "%s"', $type)); // @codeCoverageIgnore
}

$tokenToInvalidate = ($token ?? $this->storage->getAccessToken($this->name));
Expand All @@ -59,7 +59,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul
// ok, let's see if we got a response body
// @link https://datatracker.ietf.org/doc/html/rfc7009#section-2.2.1
if(str_contains($response->getHeaderLine('content-type'), 'json')){
$json = MessageUtil::decodeJSON($response);
$json = MessageUtil::decodeJSON($response, true);

if(isset($json['error'])){
throw new ProviderException($json['error']);
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static function encrypt(string $data, string $keyHex, int $format = self:
self::ENCRYPT_FORMAT_BINARY => $nonce.$box,
self::ENCRYPT_FORMAT_BASE64 => sodium_bin2base64($nonce.$box, SODIUM_BASE64_VARIANT_ORIGINAL),
self::ENCRYPT_FORMAT_HEX => sodium_bin2hex($nonce.$box),
default => throw new InvalidArgumentException('invalid format'),
default => throw new InvalidArgumentException('invalid format'), // @codeCoverageIgnore
};

sodium_memzero($data);
Expand All @@ -125,7 +125,7 @@ public static function decrypt(string $encrypted, string $keyHex, int $format =
self::ENCRYPT_FORMAT_BINARY => $encrypted,
self::ENCRYPT_FORMAT_BASE64 => sodium_base642bin($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL),
self::ENCRYPT_FORMAT_HEX => sodium_hex2bin($encrypted),
default => throw new InvalidArgumentException('invalid format'),
default => throw new InvalidArgumentException('invalid format'), // @codeCoverageIgnore
};

$nonce = substr($bin, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/BigCartel.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul
return true;
}

return false;
return false; // @codeCoverageIgnore
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/DeviantArt.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul
return true;
}

return false;
return false; // @codeCoverageIgnore
}

}
2 changes: 1 addition & 1 deletion src/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected function loadFile(string $key, string $provider):string|null{
$contents = file_get_contents($path);

if($contents === false){
throw new OAuthStorageException('file_get_contents() error');
throw new OAuthStorageException('file_get_contents() error'); // @codeCoverageIgnore
}

return $contents;
Expand Down
11 changes: 5 additions & 6 deletions tests/Providers/Unit/BigCartelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace chillerlan\OAuthTest\Providers\Unit;

use chillerlan\OAuth\Core\{AccessToken, TokenInvalidate};
use chillerlan\OAuth\Core\AccessToken;
use chillerlan\OAuth\Providers\BigCartel;
use chillerlan\OAuthTest\Attributes\Provider;

Expand All @@ -22,11 +22,6 @@
final class BigCartelTest extends OAuth2ProviderUnitTestAbstract{

public function testTokenInvalidate():void{

if(!$this->provider instanceof TokenInvalidate){
$this::markTestSkipped('TokenInvalidate N/A');
}

// BigCartel expects the account id set in the token and responds with a 204
$this->setMockResponse($this->responseFactory->createResponse(204));

Expand All @@ -37,4 +32,8 @@ public function testTokenInvalidate():void{
$this::assertFalse($this->storage->hasAccessToken($this->provider->getName()));
}

public function testTokenInvalidateFailed():void{
$this::markTestIncomplete();
}

}
4 changes: 4 additions & 0 deletions tests/Providers/Unit/DeviantArtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ public function testTokenInvalidate():void{
$this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken);
}

public function testTokenInvalidateFailedWithException():void{
$this->markTestSkipped('N/A');
}

}
4 changes: 4 additions & 0 deletions tests/Providers/Unit/OAuth1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public function testMeUnknownErrorException():void{
$this->markTestSkipped('N/A');
}

public function testTokenInvalidateFailedWithException():void{
$this->markTestSkipped('N/A');
}

}
4 changes: 4 additions & 0 deletions tests/Providers/Unit/OAuth2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public function testMeUnknownErrorException():void{
$this->markTestSkipped('N/A');
}

public function testTokenInvalidateFailedWithException():void{
$this->markTestSkipped('N/A');
}

}
40 changes: 39 additions & 1 deletion tests/Providers/Unit/OAuthProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function testTokenInvalidate():void{

$token = $this->getTestToken();

$this->provider->storeAccessToken($this->getTestToken());
$this->provider->storeAccessToken($token);

$this::assertTrue($this->storage->hasAccessToken($this->provider->getName()));
$this::assertTrue($this->provider->invalidateAccessToken());
Expand All @@ -231,6 +231,44 @@ public function testTokenInvalidate():void{
$this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken);
}

public function testTokenInvalidateFailed():void{

if(!$this->provider instanceof TokenInvalidate){
$this::markTestSkipped('TokenInvalidate N/A');
}

$token = $this->getTestToken();

$this->provider->storeAccessToken($token);

$this->setMockResponse($this->responseFactory->createResponse(404));

$this::assertFalse($this->provider->invalidateAccessToken());
}

public function testTokenInvalidateFailedWithException():void{

if(!$this->provider instanceof TokenInvalidate){
$this::markTestSkipped('TokenInvalidate N/A');
}

$this->expectException(ProviderException::class);
$this->expectExceptionMessage('whatever');

$token = $this->getTestToken();

$this->provider->storeAccessToken($token);

$response = $this->responseFactory
->createResponse(404)
->withHeader('Content-Type', 'application/json')
->withBody($this->streamFactory->createStream('{"error":"whatever"}'));

$this->setMockResponse($response);

$this->provider->invalidateAccessToken();
}

public function testTokenInvalidateNoTokenException():void{

if(!$this->provider instanceof TokenInvalidate){
Expand Down
8 changes: 8 additions & 0 deletions tests/Providers/Unit/StripeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function testTokenInvalidate():void{
$this::markTestIncomplete();
}

public function testTokenInvalidateFailed():void{
$this::markTestIncomplete();
}

public function testTokenInvalidateFailedWithException():void{
$this::markTestIncomplete();
}

}
4 changes: 4 additions & 0 deletions tests/Providers/Unit/VimeoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ public function testTokenInvalidate():void{
$this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken);
}

public function testTokenInvalidateFailedWithException():void{
$this->markTestSkipped('N/A');
}

}

0 comments on commit 71b0d66

Please sign in to comment.