-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SymfonyHealthCheckBundle\Check; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use SymfonyHealthCheckBundle\Dto\Response; | ||
use Throwable; | ||
|
||
class PredisCheck implements CheckInterface | ||
{ | ||
public const PREDIS_CLIENT_CLASS = 'Predis\ClientInterface'; | ||
|
||
private const NAME = 'predis'; | ||
|
||
private ContainerInterface $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function check(): Response | ||
{ | ||
if (!$client = $this->getPredisClient()) { | ||
return new Response(self::NAME, false, 'Predis Client not found'); | ||
} | ||
|
||
$key = 'test-' . time() . random_int(0, 1000); | ||
try { | ||
if ($client->get($key) !== null) { | ||
return new Response(self::NAME, false, 'Unable to check predis status'); | ||
} | ||
|
||
$value = '1'; | ||
$client->set($key, $value); | ||
if ($client->get($key) === $value) { | ||
$client->del($key); | ||
return new Response(self::NAME, true, 'ok'); | ||
} else { | ||
return new Response(self::NAME, false, 'Predis is not functional'); | ||
} | ||
} catch (Throwable $throwable) { | ||
return new Response(self::NAME, false, 'Could not check predis status: ' . $throwable->getMessage()); | ||
} | ||
} | ||
|
||
private function getPredisClient(): ?object | ||
{ | ||
foreach ( | ||
[ | ||
self::PREDIS_CLIENT_CLASS, | ||
'SymfonyBundles\RedisBundle\Redis\ClientInterface', | ||
] as $class | ||
) { | ||
if ($this->container->has($class)) { | ||
return $this->container->get($class); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SymfonyHealthCheckBundle\Tests\Unit\Check; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use SymfonyHealthCheckBundle\Check\PredisCheck; | ||
use SymfonyHealthCheckBundle\Dto\Response; | ||
|
||
class PredisCheckTest extends TestCase | ||
{ | ||
/** | ||
* @return array{ | ||
* predisClient: ?object, | ||
* expectedResponse: Response, | ||
* }[] | ||
*/ | ||
public static function dataProvider(): array | ||
{ | ||
return [ | ||
'no client' => [ | ||
'predisClient' => null, | ||
'expectedResponse' => new Response('predis', false, 'Predis Client not found'), | ||
], | ||
'incorrectly initialized client' => [ | ||
'predisClient' => new class { | ||
public function get(): string | ||
{ | ||
return 'abracadabra'; | ||
} | ||
}, | ||
'expectedResponse' => new Response('predis', false, 'Unable to check predis status'), | ||
], | ||
'exception' => [ | ||
'predisClient' => new class { | ||
public function __call(string $name, array $args): string | ||
{ | ||
throw new Exception('test'); | ||
} | ||
}, | ||
'expectedResponse' => new Response('predis', false, 'Could not check predis status: test'), | ||
], | ||
'non-working client' => [ | ||
'predisClient' => new class { | ||
public function set(): void | ||
{ | ||
} | ||
|
||
public function get(): ?string | ||
{ | ||
return null; | ||
} | ||
}, | ||
'expectedResponse' => new Response('predis', false, 'Predis is not functional'), | ||
], | ||
'success' => [ | ||
'predisClient' => new class { | ||
private array $data = []; | ||
|
||
public function set(string $key, string $value): void | ||
{ | ||
$this->data[$key] = $value; | ||
} | ||
|
||
public function get(string $key): ?string | ||
{ | ||
return $this->data[$key] ?? null; | ||
} | ||
|
||
public function del(string $key): void | ||
{ | ||
unset($this->data[$key]); | ||
} | ||
}, | ||
'expectedResponse' => new Response('predis', true, 'ok'), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function test(?object $predisClient, Response $expectedResponse): void | ||
{ | ||
$container = new Container(); | ||
if ($predisClient) { | ||
$container->set(PredisCheck::PREDIS_CLIENT_CLASS, $predisClient); | ||
} | ||
|
||
self::assertEquals( | ||
$expectedResponse, | ||
(new PredisCheck($container))->check(), | ||
); | ||
} | ||
} |