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;
|
2021-10-23 13:36:27 +02:00
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
2021-10-23 15:44:56 +02:00
|
|
|
use Doctrine\DBAL\Result;
|
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;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 22:02:31 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-12-29 11:39:55 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
|
|
|
use Shlinkio\Shlink\Rest\Action\HealthAction;
|
|
|
|
|
|
|
|
class HealthActionTest extends TestCase
|
|
|
|
{
|
2019-12-29 22:48:40 +01:00
|
|
|
private HealthAction $action;
|
2022-10-24 19:53:13 +02:00
|
|
|
private MockObject & Connection $conn;
|
2018-12-29 11:39:55 +01:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2018-12-29 11:39:55 +01:00
|
|
|
{
|
2022-10-23 22:02:31 +02:00
|
|
|
$this->conn = $this->createMock(Connection::class);
|
|
|
|
$dbPlatform = $this->createMock(AbstractPlatform::class);
|
|
|
|
$dbPlatform->method('getDummySelectSQL')->willReturn('');
|
|
|
|
$this->conn->method('getDatabasePlatform')->willReturn($dbPlatform);
|
2021-10-23 13:36:27 +02:00
|
|
|
|
2022-10-23 22:02:31 +02:00
|
|
|
$em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$em->method('getConnection')->willReturn($this->conn);
|
2020-04-25 19:58:49 +02:00
|
|
|
|
2022-10-23 22:02:31 +02:00
|
|
|
$this->action = new HealthAction($em, new AppOptions(version: '1.2.3'));
|
2018-12-29 11:39:55 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2021-10-23 13:36:27 +02:00
|
|
|
public function passResponseIsReturnedWhenDummyQuerySucceeds(): void
|
2018-12-29 11:39:55 +01:00
|
|
|
{
|
2022-10-23 22:02:31 +02:00
|
|
|
$this->conn->expects($this->once())->method('executeQuery')->willReturn($this->createMock(Result::class));
|
|
|
|
|
2018-12-29 11:39:55 +01:00
|
|
|
/** @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
|
|
|
}
|
2018-12-29 13:50:42 +01:00
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2021-10-23 13:36:27 +02:00
|
|
|
public function failResponseIsReturnedWhenDummyQueryThrowsException(): void
|
2018-12-29 13:50:42 +01:00
|
|
|
{
|
2022-10-23 22:02:31 +02:00
|
|
|
$this->conn->expects($this->once())->method('executeQuery')->willThrowException(new Exception());
|
2018-12-29 13:50:42 +01:00
|
|
|
|
|
|
|
/** @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
|
|
|
}
|
2018-12-29 11:39:55 +01:00
|
|
|
}
|