|
| 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