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

75 lines
2.6 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\DBAL\Platforms\AbstractPlatform;
2021-10-23 16:44:56 +03:00
use Doctrine\DBAL\Result;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\MockObject\MockObject;
2018-12-29 13:39:55 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Rest\Action\HealthAction;
class HealthActionTest extends TestCase
{
private HealthAction $action;
private MockObject $conn;
2018-12-29 13:39:55 +03:00
protected function setUp(): void
2018-12-29 13:39:55 +03:00
{
$this->conn = $this->createMock(Connection::class);
$dbPlatform = $this->createMock(AbstractPlatform::class);
$dbPlatform->method('getDummySelectSQL')->willReturn('');
$this->conn->method('getDatabasePlatform')->willReturn($dbPlatform);
$em = $this->createMock(EntityManagerInterface::class);
$em->method('getConnection')->willReturn($this->conn);
$this->action = new HealthAction($em, new AppOptions(version: '1.2.3'));
2018-12-29 13:39:55 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function passResponseIsReturnedWhenDummyQuerySucceeds(): void
2018-12-29 13:39:55 +03:00
{
$this->conn->expects($this->once())->method('executeQuery')->willReturn($this->createMock(Result::class));
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
}
2019-02-17 22:28:34 +03:00
/** @test */
public function failResponseIsReturnedWhenDummyQueryThrowsException(): void
{
$this->conn->expects($this->once())->method('executeQuery')->willThrowException(new Exception());
/** @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'));
}
2018-12-29 13:39:55 +03:00
}