|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FiveLab Diagnostic package. |
| 5 | + * |
| 6 | + * (c) FiveLab <mail@fivelab.org> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types = 1); |
| 13 | + |
| 14 | +namespace FiveLab\Component\Diagnostic\Tests\Check\Redis\Predis; |
| 15 | + |
| 16 | +use FiveLab\Component\Diagnostic\Check\Redis\Predis\PredisPingPongCheck; |
| 17 | +use FiveLab\Component\Diagnostic\Result\Failure; |
| 18 | +use FiveLab\Component\Diagnostic\Result\Success; |
| 19 | +use FiveLab\Component\Diagnostic\Tests\Check\AbstractRedisTestCase; |
| 20 | +use PHPUnit\Framework\Attributes\Test; |
| 21 | + |
| 22 | +class PredisPingPongCheckTest extends AbstractRedisTestCase |
| 23 | +{ |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + if (!$this->canTestingWithRedis()) { |
| 27 | + self::markTestSkipped('The Redis is not configured.'); |
| 28 | + } |
| 29 | + |
| 30 | + if (!\class_exists(\Redis::class)) { |
| 31 | + self::markTestSkipped('The ext-redis not installed.'); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + #[Test] |
| 36 | + public function shouldSuccessCheck(): void |
| 37 | + { |
| 38 | + $check = new PredisPingPongCheck( |
| 39 | + $this->getRedisHost(), |
| 40 | + $this->getRedisPort(), |
| 41 | + $this->getRedisPassword() |
| 42 | + ); |
| 43 | + |
| 44 | + $result = $check->check(); |
| 45 | + |
| 46 | + self::assertEquals(new Success('Success connect to Redis.'), $result); |
| 47 | + } |
| 48 | + |
| 49 | + #[Test] |
| 50 | + public function shouldSuccessGetExtraParameters(): void |
| 51 | + { |
| 52 | + $check = new PredisPingPongCheck( |
| 53 | + $this->getRedisHost(), |
| 54 | + $this->getRedisPort(), |
| 55 | + $this->getRedisPassword() |
| 56 | + ); |
| 57 | + |
| 58 | + self::assertEquals([ |
| 59 | + 'host' => $this->getRedisHost(), |
| 60 | + 'port' => $this->getRedisPort(), |
| 61 | + ], $check->getExtraParameters()); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function shouldFailIfHostIsWrong(): void |
| 66 | + { |
| 67 | + $check = new PredisPingPongCheck( |
| 68 | + $this->getRedisHost().'some', |
| 69 | + $this->getRedisPort(), |
| 70 | + $this->getRedisPassword() |
| 71 | + ); |
| 72 | + |
| 73 | + $result = $check->check(); |
| 74 | + |
| 75 | + self::assertInstanceOf(Failure::class, $result); |
| 76 | + self::assertStringStartsWith('Cannot connect to Redis: php_network_getaddresses:', $result->message); |
| 77 | + } |
| 78 | +} |
0 commit comments