shlink/module/Rest/test/Action/HealthActionTest.php

94 lines
3.2 KiB
PHP
Raw Normal View History

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