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