2018-12-29 11:39:55 +01:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2018-12-29 11:39:55 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Connection;
|
2020-04-25 19:58:49 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2018-12-29 13:50:42 +01:00
|
|
|
use Exception;
|
2020-01-01 21:11:53 +01:00
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
2018-12-29 11:39:55 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-11-02 11:50:19 +01:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2018-12-29 11:39:55 +01:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
|
|
|
use Shlinkio\Shlink\Rest\Action\HealthAction;
|
|
|
|
|
|
|
|
class HealthActionTest extends TestCase
|
|
|
|
{
|
2020-11-02 11:50:19 +01:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 22:48:40 +01:00
|
|
|
private HealthAction $action;
|
|
|
|
private ObjectProphecy $conn;
|
2018-12-29 11:39:55 +01:00
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
public function setUp(): void
|
2018-12-29 11:39:55 +01:00
|
|
|
{
|
|
|
|
$this->conn = $this->prophesize(Connection::class);
|
2020-04-25 19:58:49 +02:00
|
|
|
$em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
$em->getConnection()->willReturn($this->conn->reveal());
|
|
|
|
|
|
|
|
$this->action = new HealthAction($em->reveal(), new AppOptions(['version' => '1.2.3']));
|
2018-12-29 11:39:55 +01:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2020-01-01 20:48:31 +01:00
|
|
|
public function passResponseIsReturnedWhenConnectionSucceeds(): void
|
2018-12-29 11:39:55 +01:00
|
|
|
{
|
|
|
|
$ping = $this->conn->ping()->willReturn(true);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $this->action->handle(new ServerRequest());
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
|
|
|
self::assertEquals('pass', $payload['status']);
|
|
|
|
self::assertEquals('1.2.3', $payload['version']);
|
|
|
|
self::assertEquals([
|
2018-12-29 11:39:55 +01:00
|
|
|
'about' => 'https://shlink.io',
|
|
|
|
'project' => 'https://github.com/shlinkio/shlink',
|
|
|
|
], $payload['links']);
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
|
2018-12-29 11:39:55 +01:00
|
|
|
$ping->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2020-01-01 20:48:31 +01:00
|
|
|
public function failResponseIsReturnedWhenConnectionFails(): void
|
2018-12-29 11:39:55 +01:00
|
|
|
{
|
|
|
|
$ping = $this->conn->ping()->willReturn(false);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $this->action->handle(new ServerRequest());
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals(503, $resp->getStatusCode());
|
|
|
|
self::assertEquals('fail', $payload['status']);
|
|
|
|
self::assertEquals('1.2.3', $payload['version']);
|
|
|
|
self::assertEquals([
|
2018-12-29 11:39:55 +01:00
|
|
|
'about' => 'https://shlink.io',
|
|
|
|
'project' => 'https://github.com/shlinkio/shlink',
|
|
|
|
], $payload['links']);
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
|
2018-12-29 11:39:55 +01:00
|
|
|
$ping->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2018-12-29 13:50:42 +01:00
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2020-01-01 20:48:31 +01:00
|
|
|
public function failResponseIsReturnedWhenConnectionThrowsException(): void
|
2018-12-29 13:50:42 +01:00
|
|
|
{
|
|
|
|
$ping = $this->conn->ping()->willThrow(Exception::class);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $this->action->handle(new ServerRequest());
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals(503, $resp->getStatusCode());
|
|
|
|
self::assertEquals('fail', $payload['status']);
|
|
|
|
self::assertEquals('1.2.3', $payload['version']);
|
|
|
|
self::assertEquals([
|
2018-12-29 13:50:42 +01:00
|
|
|
'about' => 'https://shlink.io',
|
|
|
|
'project' => 'https://github.com/shlinkio/shlink',
|
|
|
|
], $payload['links']);
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
|
2018-12-29 13:50:42 +01:00
|
|
|
$ping->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2018-12-29 11:39:55 +01:00
|
|
|
}
|