RedisCollectionTest::handleDsnWithAuth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Liip\MonitorBundle\Tests\Check;
4
5
use Laminas\Diagnostics\Check\Redis;
6
use Liip\MonitorBundle\Check\RedisCollection;
7
use PHPUnit\Framework\TestCase;
8
use ReflectionClass;
9
use ReflectionException;
10
11
final class RedisCollectionTest extends TestCase
12
{
13
    const AUTH = 'my-super-secret-password';
14
15
    /**
16
     * @test
17
     * @dataProvider provideDsnWithAut
18
     */
19
    public function handleDsnWithAuth(string $dsn)
20
    {
21
        $config = [
22
          'dsn' => $dsn,
23
          'host' => 'localhost',
24
          'port' => 6379,
25
          'password' => null,
26
        ];
27
28
        $collection = new RedisCollection(['default' => $config]);
29
        $checks = $collection->getChecks();
30
31
        /** @var Redis $check */
32
        $check = $checks['redis_default'];
33
34
        $this->assertAuthPropertyValue($check, self::AUTH);
35
    }
36
37
    private function assertAuthPropertyValue(Redis $check, string $auth)
38
    {
39
        try {
40
            $refClass = new ReflectionClass($check);
41
            $authProp = $refClass->getProperty('auth');
42
            $authProp->setAccessible(true);
43
            self::assertSame($auth, $authProp->getValue($check));
44
        } catch (ReflectionException $e) {
45
            self::fail($e->getMessage());
46
        }
47
    }
48
49
    public function provideDsnWithAut(): array
50
    {
51
        return [
52
          'incompatible with parse_url' => [sprintf('redis://%[email protected]:6379', static::AUTH)],
53
          'compatible with parse_url' => [sprintf('redis://irrelevant-user:%[email protected]:6379', static::AUTH)],
54
        ];
55
    }
56
}
57