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

88 lines
3 KiB
PHP
Raw Normal View History

2018-12-29 13:39:55 +03:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action;
use Doctrine\DBAL\Connection;
use Exception;
2018-12-29 13:39:55 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Rest\Action\HealthAction;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequest;
class HealthActionTest extends TestCase
{
/** @var HealthAction */
private $action;
/** @var ObjectProphecy */
private $conn;
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);
$this->action = new HealthAction($this->conn->reveal(), new AppOptions(['version' => '1.2.3']));
}
2019-02-17 22:28:34 +03:00
/** @test */
2018-12-29 13:39:55 +03:00
public function passResponseIsReturnedWhenConnectionSucceeds()
{
$ping = $this->conn->ping()->willReturn(true);
/** @var JsonResponse $resp */
$resp = $this->action->handle(new ServerRequest());
$payload = $resp->getPayload();
$this->assertEquals(200, $resp->getStatusCode());
$this->assertEquals('pass', $payload['status']);
$this->assertEquals('1.2.3', $payload['version']);
$this->assertEquals([
'about' => 'https://shlink.io',
'project' => 'https://github.com/shlinkio/shlink',
], $payload['links']);
$this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
$ping->shouldHaveBeenCalledOnce();
}
2019-02-17 22:28:34 +03:00
/** @test */
2018-12-29 13:39:55 +03:00
public function failResponseIsReturnedWhenConnectionFails()
{
$ping = $this->conn->ping()->willReturn(false);
/** @var JsonResponse $resp */
$resp = $this->action->handle(new ServerRequest());
$payload = $resp->getPayload();
$this->assertEquals(503, $resp->getStatusCode());
$this->assertEquals('fail', $payload['status']);
$this->assertEquals('1.2.3', $payload['version']);
$this->assertEquals([
'about' => 'https://shlink.io',
'project' => 'https://github.com/shlinkio/shlink',
], $payload['links']);
$this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
$ping->shouldHaveBeenCalledOnce();
}
2019-02-17 22:28:34 +03:00
/** @test */
public function failResponseIsReturnedWhenConnectionThrowsException()
{
$ping = $this->conn->ping()->willThrow(Exception::class);
/** @var JsonResponse $resp */
$resp = $this->action->handle(new ServerRequest());
$payload = $resp->getPayload();
$this->assertEquals(503, $resp->getStatusCode());
$this->assertEquals('fail', $payload['status']);
$this->assertEquals('1.2.3', $payload['version']);
$this->assertEquals([
'about' => 'https://shlink.io',
'project' => 'https://github.com/shlinkio/shlink',
], $payload['links']);
$this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
$ping->shouldHaveBeenCalledOnce();
}
2018-12-29 13:39:55 +03:00
}