mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-28 09:03:07 +03:00
Created health action related tests
This commit is contained in:
parent
0f86123ccb
commit
d58e24bce5
2 changed files with 115 additions and 0 deletions
45
module/Rest/test/Action/HealthActionFactoryTest.php
Normal file
45
module/Rest/test/Action/HealthActionFactoryTest.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Action;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||
use Shlinkio\Shlink\Rest\Action;
|
||||
use Zend\ServiceManager\ServiceManager;
|
||||
|
||||
class HealthActionFactoryTest extends TestCase
|
||||
{
|
||||
/** @var Action\HealthActionFactory */
|
||||
private $factory;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->factory = new Action\HealthActionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function serviceIsCreatedExtractingConnectionFromEntityManager()
|
||||
{
|
||||
$em = $this->prophesize(EntityManager::class);
|
||||
$conn = $this->prophesize(Connection::class);
|
||||
|
||||
$getConnection = $em->getConnection()->willReturn($conn->reveal());
|
||||
|
||||
$sm = new ServiceManager(['services' => [
|
||||
'Logger_Shlink' => $this->prophesize(LoggerInterface::class)->reveal(),
|
||||
AppOptions::class => $this->prophesize(AppOptions::class)->reveal(),
|
||||
EntityManager::class => $em->reveal(),
|
||||
]]);
|
||||
|
||||
$instance = ($this->factory)($sm, '');
|
||||
|
||||
$this->assertInstanceOf(Action\HealthAction::class, $instance);
|
||||
$getConnection->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
}
|
70
module/Rest/test/Action/HealthActionTest.php
Normal file
70
module/Rest/test/Action/HealthActionTest.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Action;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
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;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->conn = $this->prophesize(Connection::class);
|
||||
$this->action = new HealthAction($this->conn->reveal(), new AppOptions(['version' => '1.2.3']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue