Completed
Push — master ( 366677...26f340 )
by Avtandil
06:23
created

RedisTest::it_should_set_custom_serializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Tests\Unit;
5
6
use Longman\LaravelLodash\Redis\RedisManager;
7
8
class RedisTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
9
{
10
    private const PHPREDIS_OPT_SERIALIZER = 1;
11
    private const PHPREDIS_SERIALIZER_IGBINARY = 2;
12
13
    /** @test */
14
    public function it_should_set_custom_serializer()
15
    {
16
        $redis = $this->createConnection('phpredis', [
17
            'default' => [
18
                'serializer' => 'igbinary',
19
            ],
20
        ]);
21
        $client = $redis->connection()->client();
22
23
        $data = ['name' => 'Georgia'];
24
        $redis->set('country', $data, null, 60);
0 ignored issues
show
Documentation Bug introduced by
The method set does not exist on object<Longman\LaravelLodash\Redis\RedisManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
25
26
        $this->assertEquals($client->getOption(self::PHPREDIS_OPT_SERIALIZER), self::PHPREDIS_SERIALIZER_IGBINARY);
0 ignored issues
show
Bug introduced by
The method getOption() does not exist on Predis\Client. Did you maybe mean getOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
27
        $this->assertEquals($redis->get('country'), $data);
0 ignored issues
show
Documentation Bug introduced by
The method get does not exist on object<Longman\LaravelLodash\Redis\RedisManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
28
    }
29
30
    public function createConnection(string $driver, array $config = []): RedisManager
31
    {
32
        $host = getenv('REDIS_HOST') ?: '127.0.0.1';
33
        $port = getenv('REDIS_PORT') ?: 6379;
34
        $defaultConfig = [
35
            'cluster' => false,
36
            'default' => [
37
                'host'         => $host,
38
                'port'         => $port,
39
                'database'     => 5,
40
                'options'      => ['prefix' => 'lodash:'],
41
                'timeout'      => 0.5,
42
                'read_timeout' => 1.5,
43
            ],
44
        ];
45
        $options = array_replace_recursive($defaultConfig, $config);
46
47
        $redis = new RedisManager($this->app, $driver, $options);
48
49
        return $redis;
50
    }
51
}
52