Skip to content

Commit

Permalink
Merge pull request #47 from MacPaw/feat/addResponseDto
Browse files Browse the repository at this point in the history
Feat: remove custom error if service not found and create response standart response dto
  • Loading branch information
Yozhef authored Jan 17, 2022
2 parents 783aba9 + 94c85ee commit ce8335d
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 185 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ declare(strict_types=1);
namespace YourProject\Check;
use SymfonyHealthCheckBundle\Dto\Response;
class CustomCheck implements CheckInterface
{
private const CHECK_RESULT_KEY = 'customConnection';
public function check(): array
public function check(): Response
{
return [self::CHECK_RESULT_KEY => true];
return new Response('status', true, 'up');
}
}
```
Expand Down
7 changes: 3 additions & 4 deletions src/Check/CheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace SymfonyHealthCheckBundle\Check;

use SymfonyHealthCheckBundle\Dto\Response;

interface CheckInterface
{
/**
* @return array<string, mixed>
*/
public function check(): array;
public function check(): Response;
}
24 changes: 7 additions & 17 deletions src/Check/DoctrineCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace SymfonyHealthCheckBundle\Check;

use Symfony\Component\DependencyInjection\ContainerInterface;
use SymfonyHealthCheckBundle\Exception\ServiceNotFoundException;
use SymfonyHealthCheckBundle\Dto\Response;
use Throwable;

class DoctrineCheck implements CheckInterface
{
private const CHECK_RESULT_KEY = 'connection';
private const CHECK_RESULT_NAME = 'doctrine';

private ContainerInterface $container;

Expand All @@ -19,35 +19,25 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

/**
* @throws ServiceNotFoundException
*/
public function check(): array
public function check(): Response
{
$result = ['name' => 'doctrine'];

if ($this->container->has('doctrine.orm.entity_manager') === false) {
throw new ServiceNotFoundException(
'Entity Manager Not Found.',
[
'class' => 'doctrine.orm.entity_manager',
]
);
return new Response(self::CHECK_RESULT_NAME, false, 'Entity Manager Not Found.');
}

$entityManager = $this->container->get('doctrine.orm.entity_manager');

if ($entityManager === null) {
throw new ServiceNotFoundException('Entity Manager Not Found.');
return new Response(self::CHECK_RESULT_NAME, false, 'Entity Manager Not Found.');
}

try {
$con = $entityManager->getConnection();
$con->executeQuery($con->getDatabasePlatform()->getDummySelectSQL())->free();
} catch (Throwable $e) {
return array_merge($result, [self::CHECK_RESULT_KEY => false]);
return new Response(self::CHECK_RESULT_NAME, false, $e->getMessage());
}

return array_merge($result, [self::CHECK_RESULT_KEY => true]);
return new Response(self::CHECK_RESULT_NAME, true, 'ok');
}
}
9 changes: 4 additions & 5 deletions src/Check/EnvironmentCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SymfonyHealthCheckBundle\Check;

use Symfony\Component\DependencyInjection\ContainerInterface;
use SymfonyHealthCheckBundle\Dto\Response;
use Throwable;

class EnvironmentCheck implements CheckInterface
Expand All @@ -18,16 +19,14 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

public function check(): array
public function check(): Response
{
$result = ['name' => self::CHECK_RESULT_KEY];

try {
$env = $this->container->getParameter('kernel.environment');
} catch (Throwable $e) {
return array_merge($result, [self::CHECK_RESULT_KEY => 'Could not determine']);
return new Response(self::CHECK_RESULT_KEY, false, 'Could not determine');
}

return array_merge($result, [self::CHECK_RESULT_KEY => $env]);
return new Response(self::CHECK_RESULT_KEY, true, 'ok', [$env]);
}
}
6 changes: 4 additions & 2 deletions src/Check/StatusUpCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace SymfonyHealthCheckBundle\Check;

use SymfonyHealthCheckBundle\Dto\Response;

class StatusUpCheck implements CheckInterface
{
public function check(): array
public function check(): Response
{
return ['status' => 'up'];
return new Response('status', true, 'up');
}
}
2 changes: 1 addition & 1 deletion src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function healthCheckAction(): JsonResponse
{
$resultHealthCheck = [];
foreach ($this->healthChecks as $healthCheck) {
$resultHealthCheck[] = $healthCheck->check();
$resultHealthCheck[] = $healthCheck->check()->toArray();
}

return new JsonResponse($resultHealthCheck, Response::HTTP_OK);
Expand Down
64 changes: 64 additions & 0 deletions src/Dto/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Dto;

class Response
{
private string $name;
private bool $result;
private string $message;

/**
* @var mixed[]
*/
private array $params;

/**
* @param mixed[] $params
*/
public function __construct(string $name, bool $result, string $message, array $params = [])
{
$this->name = $name;
$this->result = $result;
$this->message = $message;
$this->params = $params;
}

public function getName(): string
{
return $this->name;
}

public function getResult(): bool
{
return $this->result;
}

public function getMessage(): string
{
return $this->message;
}

/**
* @return mixed[]
*/
public function getParams(): array
{
return $this->params;
}

/**
* @return mixed[]
*/
public function toArray(): array
{
return [
'name' => $this->getName(),
'result' => $this->getResult(),
'message' => $this->getMessage(),
'params' => $this->getParams(),
];
}
}
40 changes: 0 additions & 40 deletions src/Exception/AbstractException.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Exception/ParameterExceptionInterface.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Exception/ServiceNotFoundException.php

This file was deleted.

64 changes: 44 additions & 20 deletions tests/Integration/Controller/HealthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use SymfonyHealthCheckBundle\Check\EnvironmentCheck;
use SymfonyHealthCheckBundle\Check\StatusUpCheck;
use SymfonyHealthCheckBundle\Controller\HealthController;
use SymfonyHealthCheckBundle\Exception\ServiceNotFoundException;
use TypeError;

class HealthControllerTest extends WebTestCase
Expand All @@ -33,7 +32,12 @@ public function testAddCheckStatusUpSuccess(): void
$response = $healthController->healthCheckAction();

self::assertSame(200, $response->getStatusCode());
self::assertSame(json_encode([['status' => 'up']]), $response->getContent());
self::assertSame(
json_encode([[
'name' => 'status', 'result' => true, 'message' => 'up', 'params' => [],
]]),
$response->getContent()
);
}

public function testEnvironmentCheckCouldNotDetermine(): void
Expand All @@ -45,31 +49,32 @@ public function testEnvironmentCheckCouldNotDetermine(): void

self::assertSame(200, $response->getStatusCode());
self::assertSame(
json_encode([['name' => 'environment', 'environment' => 'Could not determine']]),
json_encode([[
'name' => 'environment',
'result' => false,
'message' => 'Could not determine',
'params' => []
]]),
$response->getContent()
);
}

public function testDoctrineCheckServiceNotFoundException(): void
{
self::expectException(ServiceNotFoundException::class);

$healthController = new HealthController();
$healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder()));

$healthController->healthCheckAction();
}

public function testDoctrineCheckGetParameters(): void
{
$healthController = new HealthController();
$healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder()));

try {
$healthController->healthCheckAction();
} catch (ServiceNotFoundException $exception) {
self::assertSame(["class" => "doctrine.orm.entity_manager"], $exception->getParameters());
}
$response = $healthController->healthCheckAction();
self::assertSame(200, $response->getStatusCode());
self::assertSame(
json_encode([[
'name' => 'doctrine',
'result' => false,
'message' => 'Entity Manager Not Found.',
'params' => []
]]),
$response->getContent()
);
}

public function testTwoCheckSuccess(): void
Expand All @@ -82,7 +87,19 @@ public function testTwoCheckSuccess(): void

self::assertSame(200, $response->getStatusCode());
self::assertSame(
json_encode([['status' => 'up'], ['name' => 'environment', 'environment' => 'Could not determine']]),
json_encode([
[
'name' => 'status',
'result' => true,
'message' => 'up',
'params' => [],
],
[
'name' => 'environment',
'result' => false,
'message' => 'Could not determine',
'params' => []
]]),
$response->getContent()
);
}
Expand All @@ -95,7 +112,14 @@ public function testEnvironmentCheckSuccess(): void

self::assertSame(200, $response->getStatusCode());
self::assertSame(
json_encode([['name' => 'environment', 'environment' => 'testing']]),
json_encode([
[
'name' => 'environment',
'result' => true,
'message' => 'ok',
'params' => ['testing']
]
]),
$response->getContent()
);
}
Expand Down
Loading

0 comments on commit ce8335d

Please sign in to comment.