2016-07-30 23:17:13 +02:00
|
|
|
<?php
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-30 23:17:13 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\ErrorHandler;
|
|
|
|
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 09:46:29 +01:00
|
|
|
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator;
|
2016-07-30 23:17:13 +02:00
|
|
|
use Zend\Diactoros\Response;
|
2018-12-25 23:01:30 +01:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2019-02-16 21:58:14 +01:00
|
|
|
use function array_map;
|
|
|
|
use function range;
|
2016-07-30 23:17:13 +02:00
|
|
|
|
2017-03-25 09:46:29 +01:00
|
|
|
class JsonErrorResponseGeneratorTest extends TestCase
|
2016-07-30 23:17:13 +02:00
|
|
|
{
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var JsonErrorResponseGenerator */
|
2018-11-20 19:37:22 +01:00
|
|
|
private $errorHandler;
|
2016-07-30 23:17:13 +02:00
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
public function setUp(): void
|
2016-07-30 23:17:13 +02:00
|
|
|
{
|
2017-03-25 09:46:29 +01:00
|
|
|
$this->errorHandler = new JsonErrorResponseGenerator();
|
2016-07-30 23:17:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 21:58:14 +01:00
|
|
|
/** @test */
|
|
|
|
public function noErrorStatusReturnsInternalServerError(): void
|
2016-07-30 23:17:13 +02:00
|
|
|
{
|
2019-02-16 21:58:14 +01:00
|
|
|
/** @var Response\JsonResponse $response */
|
2018-12-25 23:01:30 +01:00
|
|
|
$response = $this->errorHandler->__invoke(null, new ServerRequest(), new Response());
|
2019-02-16 21:58:14 +01:00
|
|
|
$payload = $response->getPayload();
|
|
|
|
|
2016-07-30 23:17:13 +02:00
|
|
|
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
2017-03-24 21:38:43 +01:00
|
|
|
$this->assertEquals(500, $response->getStatusCode());
|
2019-02-16 21:58:14 +01:00
|
|
|
$this->assertEquals('Internal Server Error', $payload['message']);
|
2016-07-30 23:17:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-02-16 21:58:14 +01:00
|
|
|
* @dataProvider provideStatus
|
2016-07-30 23:17:13 +02:00
|
|
|
*/
|
2019-02-16 21:58:14 +01:00
|
|
|
public function errorStatusReturnsThatStatus(int $status, string $message): void
|
2016-07-30 23:17:13 +02:00
|
|
|
{
|
2019-02-16 21:58:14 +01:00
|
|
|
/** @var Response\JsonResponse $response */
|
2016-07-30 23:17:13 +02:00
|
|
|
$response = $this->errorHandler->__invoke(
|
2017-03-24 21:38:43 +01:00
|
|
|
null,
|
2018-12-25 23:01:30 +01:00
|
|
|
new ServerRequest(),
|
2019-02-16 21:58:14 +01:00
|
|
|
(new Response())->withStatus($status, $message)
|
2016-07-30 23:17:13 +02:00
|
|
|
);
|
2019-02-16 21:58:14 +01:00
|
|
|
$payload = $response->getPayload();
|
|
|
|
|
2016-07-30 23:17:13 +02:00
|
|
|
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
2019-02-16 21:58:14 +01:00
|
|
|
$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));
|
2016-07-30 23:17:13 +02:00
|
|
|
}
|
|
|
|
}
|