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

MemcachedConfigurationTest::test_should_build_default_arguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Koded\Caching\Configuration;
4
5
use Memcached;
6
use PHPUnit\Framework\TestCase;
7
8
class MemcachedConfigurationTest extends TestCase
9
{
10
11
    public function test_with_servers_array_and_removing_some_options()
12
    {
13
        $config = new MemcachedConfiguration([
14
            'id' => 'test',
15
            'servers' => [
16
                ['memcached123', 11211]
17
            ],
18
            'options' => [
19
                Memcached::OPT_REMOVE_FAILED_SERVERS => false,
20
                Memcached::OPT_RETRY_TIMEOUT => 5,
21
                Memcached::OPT_PREFIX_KEY => 'test-prefix',
22
23
                // mark for removal
24
                Memcached::OPT_DISTRIBUTION => null,
25
                Memcached::OPT_SERVER_FAILURE_LIMIT => null,
26
            ]
27
        ]);
28
29
        $expected = $config->getOptions();
30
31
        $this->assertFalse($expected[Memcached::OPT_REMOVE_FAILED_SERVERS]);
32
        $this->assertSame('test-prefix', $expected[Memcached::OPT_PREFIX_KEY]);
33
        $this->assertSame(5, $expected[Memcached::OPT_RETRY_TIMEOUT]);
34
        $this->assertSame([['memcached123', 11211]], $config->getServers());
35
36
        // these 2 should be removed
37
        $this->assertArrayNotHasKey(Memcached::OPT_DISTRIBUTION, $expected);
38
        $this->assertArrayNotHasKey(Memcached::OPT_SERVER_FAILURE_LIMIT, $expected);
39
40
    }
41
42
    public function test_with_env_variable()
43
    {
44
        putenv('MEMCACHED_POOL=[["mem", 11212]]');
45
        $config = new MemcachedConfiguration;
46
47
        $this->assertNull($config->id);
48
        $this->assertSame([], $config->servers);
49
        $this->assertSame([["mem", 11212]], $config->getServers());
50
    }
51
52
    public function test_should_build_default_arguments()
53
    {
54
        $config = new MemcachedConfiguration;
55
56
        $this->assertNull($config->id);
57
        $this->assertSame([], $config->servers);
58
        $this->assertSame([['127.0.0.1', 11211]], $config->getServers());
59
60
        $this->assertEquals([
61
            Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
62
            Memcached::OPT_CONNECT_TIMEOUT => 10,
63
            Memcached::OPT_SERVER_FAILURE_LIMIT => 2,
64
            Memcached::OPT_REMOVE_FAILED_SERVERS => true,
65
            Memcached::OPT_RETRY_TIMEOUT => 1,
66
            // OPT_PREFIX_KEY is filtered out
67
68
        ], $config->getOptions());
69
    }
70
71
    public function test_should_set_the_ttl()
72
    {
73
        $config = new MemcachedConfiguration(['ttl' => 120]);
74
        $this->assertSame(120, $config->getTtl());
75
76
        $config = new MemcachedConfiguration(['ttl' => '120']);
77
        $this->assertSame(120, $config->getTtl());
78
79
        $config = new MemcachedConfiguration;
80
        $this->assertNull($config->getTtl());
81
    }
82
83
    protected function tearDown(): void
84
    {
85
        putenv('MEMCACHED_POOL=');
86
    }
87
}
88