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;
|
2021-10-23 14:36:27 +03:00
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
2021-10-23 16:44:56 +03:00
|
|
|
use Doctrine\DBAL\Result;
|
2020-04-25 20:58:49 +03:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2018-12-29 15:50:42 +03:00
|
|
|
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;
|
2021-10-23 14:36:27 +03:00
|
|
|
use Prophecy\Argument;
|
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;
|
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
private HealthAction $action;
|
|
|
|
private ObjectProphecy $conn;
|
2018-12-29 13:39:55 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2018-12-29 13:39:55 +03:00
|
|
|
{
|
|
|
|
$this->conn = $this->prophesize(Connection::class);
|
2021-10-23 16:44:56 +03:00
|
|
|
$this->conn->executeQuery(Argument::cetera())->willReturn($this->prophesize(Result::class)->reveal());
|
2021-10-23 14:36:27 +03:00
|
|
|
$dbPlatform = $this->prophesize(AbstractPlatform::class);
|
|
|
|
$dbPlatform->getDummySelectSQL()->willReturn('');
|
|
|
|
$this->conn->getDatabasePlatform()->willReturn($dbPlatform->reveal());
|
|
|
|
|
2020-04-25 20:58:49 +03:00
|
|
|
$em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
$em->getConnection()->willReturn($this->conn->reveal());
|
|
|
|
|
2022-09-17 14:01:28 +03:00
|
|
|
$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 */
|
2021-10-23 14:36:27 +03:00
|
|
|
public function passResponseIsReturnedWhenDummyQuerySucceeds(): void
|
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'));
|
2021-10-23 14:36:27 +03:00
|
|
|
$this->conn->executeQuery(Argument::cetera())->shouldHaveBeenCalledOnce();
|
2018-12-29 13:39:55 +03:00
|
|
|
}
|
2018-12-29 15:50:42 +03:00
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2021-10-23 14:36:27 +03:00
|
|
|
public function failResponseIsReturnedWhenDummyQueryThrowsException(): void
|
2018-12-29 15:50:42 +03:00
|
|
|
{
|
2021-10-23 14:36:27 +03:00
|
|
|
$executeQuery = $this->conn->executeQuery(Argument::cetera())->willThrow(Exception::class);
|
2018-12-29 15:50:42 +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 15:50:42 +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'));
|
2021-10-23 14:36:27 +03:00
|
|
|
$executeQuery->shouldHaveBeenCalledOnce();
|
2018-12-29 15:50:42 +03:00
|
|
|
}
|
2018-12-29 13:39:55 +03:00
|
|
|
}
|