mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Improved tests to increase MSI to 70%
This commit is contained in:
@@ -3,11 +3,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Exception;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class AuthenticationException extends RuntimeException
|
||||
{
|
||||
public static function expiredJWT(Exception $prev = null): self
|
||||
public static function expiredJWT(?Throwable $prev = null): self
|
||||
{
|
||||
return new self('The token has expired.', -1, $prev);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequest;
|
||||
use function array_map;
|
||||
use function range;
|
||||
|
||||
class JsonErrorResponseGeneratorTest extends TestCase
|
||||
{
|
||||
@@ -18,27 +20,41 @@ class JsonErrorResponseGeneratorTest extends TestCase
|
||||
$this->errorHandler = new JsonErrorResponseGenerator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function noErrorStatusReturnsInternalServerError()
|
||||
/** @test */
|
||||
public function noErrorStatusReturnsInternalServerError(): void
|
||||
{
|
||||
/** @var Response\JsonResponse $response */
|
||||
$response = $this->errorHandler->__invoke(null, new ServerRequest(), new Response());
|
||||
$payload = $response->getPayload();
|
||||
|
||||
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||
$this->assertEquals(500, $response->getStatusCode());
|
||||
$this->assertEquals('Internal Server Error', $payload['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideStatus
|
||||
*/
|
||||
public function errorStatusReturnsThatStatus()
|
||||
public function errorStatusReturnsThatStatus(int $status, string $message): void
|
||||
{
|
||||
/** @var Response\JsonResponse $response */
|
||||
$response = $this->errorHandler->__invoke(
|
||||
null,
|
||||
new ServerRequest(),
|
||||
(new Response())->withStatus(405)
|
||||
(new Response())->withStatus($status, $message)
|
||||
);
|
||||
$payload = $response->getPayload();
|
||||
|
||||
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||
$this->assertEquals(405, $response->getStatusCode());
|
||||
$this->assertEquals($status, $response->getStatusCode());
|
||||
$this->assertEquals($message, $payload['message']);
|
||||
}
|
||||
|
||||
public function provideStatus(): iterable
|
||||
{
|
||||
return array_map(function (int $status) {
|
||||
return [$status, 'Some message'];
|
||||
}, range(400, 500, 20));
|
||||
}
|
||||
}
|
||||
|
||||
31
module/Rest/test/Exception/AuthenticationExceptionTest.php
Normal file
31
module/Rest/test/Exception/AuthenticationExceptionTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Exception;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Rest\Exception\AuthenticationException;
|
||||
use Throwable;
|
||||
|
||||
class AuthenticationExceptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider providePrev
|
||||
*/
|
||||
public function expiredJWTCreatesExpectedException(?Throwable $prev): void
|
||||
{
|
||||
$e = AuthenticationException::expiredJWT($prev);
|
||||
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
$this->assertEquals(-1, $e->getCode());
|
||||
$this->assertEquals('The token has expired.', $e->getMessage());
|
||||
}
|
||||
|
||||
public function providePrev()
|
||||
{
|
||||
yield 'with previous exception' => [new Exception('Prev')];
|
||||
yield 'without previous exception' => [null];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Exception;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
|
||||
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
|
||||
use Throwable;
|
||||
use function array_map;
|
||||
use function random_int;
|
||||
use function range;
|
||||
use function sprintf;
|
||||
|
||||
class VerifyAuthenticationExceptionTest extends TestCase
|
||||
{
|
||||
use StringUtilsTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideExceptionData
|
||||
*/
|
||||
public function withErrorCreatesExpectedException(string $code, string $message, ?Throwable $prev): void
|
||||
{
|
||||
$e = VerifyAuthenticationException::withError($code, $message, $prev);
|
||||
|
||||
$this->assertEquals(0, $e->getCode());
|
||||
$this->assertEquals(
|
||||
sprintf('Authentication verification failed with the public message "%s"', $message),
|
||||
$e->getMessage()
|
||||
);
|
||||
$this->assertEquals($code, $e->getErrorCode());
|
||||
$this->assertEquals($message, $e->getPublicMessage());
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
}
|
||||
|
||||
public function provideExceptionData(): iterable
|
||||
{
|
||||
return array_map(function () {
|
||||
return [
|
||||
$this->generateRandomString(),
|
||||
$this->generateRandomString(50),
|
||||
random_int(0, 1) === 1 ? new Exception('Prev') : null,
|
||||
];
|
||||
}, range(1, 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideConstructorData
|
||||
*/
|
||||
public function constructCreatesExpectedException(
|
||||
string $errorCode,
|
||||
string $publicMessage,
|
||||
string $message,
|
||||
int $code,
|
||||
?Throwable $prev
|
||||
): void {
|
||||
$e = new VerifyAuthenticationException($errorCode, $publicMessage, $message, $code, $prev);
|
||||
|
||||
$this->assertEquals($code, $e->getCode());
|
||||
$this->assertEquals($message, $e->getMessage());
|
||||
$this->assertEquals($errorCode, $e->getErrorCode());
|
||||
$this->assertEquals($publicMessage, $e->getPublicMessage());
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
}
|
||||
|
||||
public function provideConstructorData(): iterable
|
||||
{
|
||||
return array_map(function (int $i) {
|
||||
return [
|
||||
$this->generateRandomString(),
|
||||
$this->generateRandomString(30),
|
||||
$this->generateRandomString(50),
|
||||
$i,
|
||||
random_int(0, 1) === 1 ? new Exception('Prev') : null,
|
||||
];
|
||||
}, range(10, 20));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function defaultConstructorValuesAreKept(): void
|
||||
{
|
||||
$e = new VerifyAuthenticationException('foo', 'bar');
|
||||
|
||||
$this->assertEquals(0, $e->getCode());
|
||||
$this->assertEquals('', $e->getMessage());
|
||||
$this->assertEquals('foo', $e->getErrorCode());
|
||||
$this->assertEquals('bar', $e->getPublicMessage());
|
||||
$this->assertNull($e->getPrevious());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user