Skip to content

Commit a52dad8

Browse files
author
Oleksandr Boiko
committed
PredisCheck
1 parent f84e628 commit a52dad8

File tree

6 files changed

+209
-6
lines changed

6 files changed

+209
-6
lines changed

src/Check/PredisCheck.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Check;
6+
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use SymfonyHealthCheckBundle\Dto\Response;
9+
use Throwable;
10+
11+
class PredisCheck implements CheckInterface
12+
{
13+
public const PREDIS_CLIENT_CLASS = 'Predis\ClientInterface';
14+
15+
private const NAME = 'predis';
16+
17+
private ContainerInterface $container;
18+
19+
public function __construct(ContainerInterface $container)
20+
{
21+
$this->container = $container;
22+
}
23+
24+
public function check(): Response
25+
{
26+
if (!$client = $this->getPredisClient()) {
27+
return new Response(self::NAME, false, 'Predis Client not found');
28+
}
29+
30+
$key = 'test-' . time() . random_int(0, 1000);
31+
try {
32+
if ($client->get($key) !== null) {
33+
return new Response(self::NAME, false, 'Unable to check predis status');
34+
}
35+
36+
$value = '1';
37+
$client->set($key, $value);
38+
if ($client->get($key) === $value) {
39+
$client->del($key);
40+
return new Response(self::NAME, true, 'ok');
41+
} else {
42+
return new Response(self::NAME, false, 'Predis is not functional');
43+
}
44+
} catch (Throwable $throwable) {
45+
return new Response(self::NAME, false, 'Could not check predis status: ' . $throwable->getMessage());
46+
}
47+
}
48+
49+
private function getPredisClient(): ?object
50+
{
51+
foreach (
52+
[
53+
self::PREDIS_CLIENT_CLASS,
54+
'SymfonyBundles\RedisBundle\Redis\ClientInterface',
55+
] as $class
56+
) {
57+
if ($this->container->has($class)) {
58+
return $this->container->get($class);
59+
}
60+
}
61+
62+
return null;
63+
}
64+
}

src/Resources/config/health_checks.xml

+3
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
<argument type="service" id="service_container"/>
1313
</service>
1414
<service id="symfony_health_check.status_up_check" class="SymfonyHealthCheckBundle\Check\StatusUpCheck"/>
15+
<service id="symfony_health_check.predis_check" class="SymfonyHealthCheckBundle\Check\PredisCheck">
16+
<argument type="service" id="service_container"/>
17+
</service>
1518
</services>
1619
</container>

tests/Integration/Controller/HealthControllerTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use SymfonyHealthCheckBundle\Check\DoctrineCheck;
1010
use SymfonyHealthCheckBundle\Check\EnvironmentCheck;
11+
use SymfonyHealthCheckBundle\Check\PredisCheck;
1112
use SymfonyHealthCheckBundle\Check\StatusUpCheck;
1213
use SymfonyHealthCheckBundle\Controller\HealthController;
1314
use TypeError;
@@ -77,6 +78,24 @@ public function testDoctrineCheckServiceNotFoundException(): void
7778
);
7879
}
7980

81+
public function testPredisCheckServiceNotFoundException(): void
82+
{
83+
$healthController = new HealthController();
84+
$healthController->addHealthCheck(new PredisCheck(new ContainerBuilder()));
85+
86+
$response = $healthController->check();
87+
self::assertSame(200, $response->getStatusCode());
88+
self::assertSame(
89+
json_encode([[
90+
'name' => 'predis',
91+
'result' => false,
92+
'message' => 'Predis Client not found',
93+
'params' => []
94+
]]),
95+
$response->getContent()
96+
);
97+
}
98+
8099
public function testTwoCheckSuccess(): void
81100
{
82101
$healthController = new HealthController();

tests/Integration/Controller/PingControllerTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use SymfonyHealthCheckBundle\Check\DoctrineCheck;
1010
use SymfonyHealthCheckBundle\Check\EnvironmentCheck;
11+
use SymfonyHealthCheckBundle\Check\PredisCheck;
1112
use SymfonyHealthCheckBundle\Check\StatusUpCheck;
1213
use SymfonyHealthCheckBundle\Controller\PingController;
1314
use TypeError;
@@ -77,6 +78,24 @@ public function testDoctrineCheckServiceNotFoundException(): void
7778
);
7879
}
7980

81+
public function testPredisCheckServiceNotFoundException(): void
82+
{
83+
$healthController = new PingController();
84+
$healthController->addHealthCheck(new PredisCheck(new ContainerBuilder()));
85+
86+
$response = $healthController->check();
87+
self::assertSame(200, $response->getStatusCode());
88+
self::assertSame(
89+
json_encode([[
90+
'name' => 'predis',
91+
'result' => false,
92+
'message' => 'Predis Client not found',
93+
'params' => []
94+
]]),
95+
$response->getContent()
96+
);
97+
}
98+
8099
public function testTwoCheckSuccess(): void
81100
{
82101
$pingController = new PingController();

tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,18 @@ public function testWithFullConfig(): void
5050
{
5151
$container = $this->createContainerFromFixture('filled_bundle_config');
5252

53-
self::assertCount(6, $container->getDefinitions());
54-
self::assertArrayHasKey(HealthController::class, $container->getDefinitions());
55-
self::assertArrayHasKey(PingController::class, $container->getDefinitions());
56-
self::assertArrayHasKey('symfony_health_check.doctrine_check', $container->getDefinitions());
57-
self::assertArrayHasKey('symfony_health_check.environment_check', $container->getDefinitions());
58-
self::assertArrayHasKey('symfony_health_check.status_up_check', $container->getDefinitions());
53+
self::assertSame(
54+
[
55+
'service_container',
56+
HealthController::class,
57+
PingController::class,
58+
'symfony_health_check.doctrine_check',
59+
'symfony_health_check.environment_check',
60+
'symfony_health_check.status_up_check',
61+
'symfony_health_check.predis_check',
62+
],
63+
array_keys($container->getDefinitions()),
64+
);
5965
}
6066

6167
private function createContainerFromFixture(string $fixtureFile): ContainerBuilder

tests/Unit/Check/PredisCheckTest.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Tests\Unit\Check;
6+
7+
use Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\DependencyInjection\Container;
10+
use SymfonyHealthCheckBundle\Check\PredisCheck;
11+
use SymfonyHealthCheckBundle\Dto\Response;
12+
13+
class PredisCheckTest extends TestCase
14+
{
15+
public static function dataProvider(): array
16+
{
17+
return [
18+
'no client' => [
19+
'predisClient' => null,
20+
'expectedResponse' => new Response('predis', false, 'Predis Client not found'),
21+
],
22+
'incorrectly initialized client' => [
23+
'predisClient' => new class {
24+
public function get(): string
25+
{
26+
return 'abracadabra';
27+
}
28+
},
29+
'expectedResponse' => new Response('predis', false, 'Unable to check predis status'),
30+
],
31+
'exception' => [
32+
'predisClient' => new class {
33+
public function __call(string $name, array $args): string
34+
{
35+
throw new Exception('test');
36+
}
37+
},
38+
'expectedResponse' => new Response('predis', false, 'Could not check predis status: test'),
39+
],
40+
'non-working client' => [
41+
'predisClient' => new class {
42+
public function set(): void
43+
{
44+
}
45+
46+
public function get(): ?string
47+
{
48+
return null;
49+
}
50+
},
51+
'expectedResponse' => new Response('predis', false, 'Predis is not functional'),
52+
],
53+
'success' => [
54+
'predisClient' => new class {
55+
private array $data = [];
56+
57+
public function set(string $key, string $value): void
58+
{
59+
$this->data[$key] = $value;
60+
}
61+
62+
public function get(string $key): ?string
63+
{
64+
return $this->data[$key] ?? null;
65+
}
66+
67+
public function del(string $key): void
68+
{
69+
unset($this->data[$key]);
70+
}
71+
},
72+
'expectedResponse' => new Response('predis', true, 'ok'),
73+
],
74+
];
75+
}
76+
77+
/**
78+
* @dataProvider dataProvider
79+
*/
80+
public function test(?object $predisClient, Response $expectedResponse): void
81+
{
82+
$container = new Container();
83+
if ($predisClient) {
84+
$container->set(PredisCheck::PREDIS_CLIENT_CLASS, $predisClient);
85+
}
86+
87+
self::assertEquals(
88+
$expectedResponse,
89+
(new PredisCheck($container))->check(),
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)