Passed
Pull Request — master (#16)
by Mihail
11:07 queued 02:40
created

RedisConnectionTest::test_redis_invalid_option_exception()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 17
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Koded\Caching;
4
5
use Koded\Caching\Client\CacheClientFactory;
6
use Koded\Caching\Configuration\ConfigFactory;
7
use Koded\Stdlib\Interfaces\Serializer;
8
use PHPUnit\Framework\TestCase;
9
10
class RedisConnectionTest extends TestCase
11
{
12
13
    public function test_should_throw_exception_on_connection_error()
14
    {
15
        $this->expectException(CacheException::class);
16
17
        if (!getenv('CI')) {
18
            $this->expectExceptionCode(Cache::E_CONNECTION_ERROR);
19
            $this->expectExceptionMessage('[Cache Exception] Failed to connect the Redis client');
20
        }
21
22
        (new CacheClientFactory(new ConfigFactory([
23
            'host' => 'invalid-redis-host'
24
        ])))->new('redis');
25
    }
26
27
    public function test_redis_invalid_option_exception()
28
    {
29
        $this->expectException(CacheException::class);
30
31
        if (!getenv('CI')) {
32
            $this->expectExceptionCode(2);
33
            $this->expectExceptionMessage('Redis::setOption() expects parameter 2 to be string, object given');
34
        }
35
36
        (new CacheClientFactory(new ConfigFactory([
37
            'serializer' => Serializer::JSON,
38
            'prefix' => new \stdClass(), // invalid prefix to test the catch block
39
40
            'host' => getenv('REDIS_SERVER_HOST'),
41
            'port' => getenv('REDIS_SERVER_PORT'),
42
43
        ])))->new();
44
    }
45
46
    public function test_predis_auth_exception()
47
    {
48
        $redis = (new CacheClientFactory(new ConfigFactory([
49
            'auth' => 'fubar',
50
51
            'host' => getenv('REDIS_SERVER_HOST'),
52
            'port' => getenv('REDIS_SERVER_PORT')
53
54
        ])))->new();
55
56
        $this->assertTrue($redis->client()->isConnected(), 'The auth is ignored, even it is not set in Redis');
57
    }
58
59
    protected function setUp(): void
60
    {
61
        if (false === extension_loaded('redis')) {
62
            $this->markTestSkipped('Redis extension is not loaded.');
63
        }
64
65
        putenv('CACHE_CLIENT=redis');
66
    }
67
68
    protected function tearDown(): void
69
    {
70
        putenv('CACHE_CLIENT=');
71
    }
72
}
73