RedisTest::it_should_set_custom_serializer()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Unit;
6
7
use Longman\LaravelLodash\Redis\RedisManager;
8
use Redis;
9
use RedisArray;
10
11
use function getenv;
12
use function version_compare;
13
14
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...
15
{
16
    /** @test */
17
    public function it_should_set_custom_serializer()
18
    {
19
        $redis = $this->createConnection('phpredis', [
20
            'cluster' => false,
21
            'default' => [
22
                'host'         => getenv('REDIS_HOST') ?: '127.0.0.1',
23
                'port'         => getenv('REDIS_PORT') ?: 6379,
24
                'database'     => 5,
25
                'options'      => ['prefix' => 'lodash:'],
26
                'timeout'      => 0.5,
27
                'read_timeout' => 1.5,
28
                'serializer'   => 'igbinary',
29
            ],
30
        ]);
31
        /** @var \Redis $client */
32
        $client = $redis->connection()->client();
33
34
        $data = ['name' => 'Georgia'];
35
        $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...
36
37
        $this->assertInstanceOf(Redis::class, $client);
38
        $this->assertEquals($client->getOption(Redis::OPT_SERIALIZER), Redis::SERIALIZER_IGBINARY);
39
        $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...
40
    }
41
42
    /** @test */
43
    public function it_should_set_custom_serializer_for_cluster()
44
    {
45
        $redis = $this->createConnection('phpredis', [
46
            'clusters' => [
47
                'options' => [
48
                    'lazy_connect'    => true,
49
                    'connect_timeout' => 1,
50
                    'read_timeout'    => 3,
51
                    'password'        => getenv('REDIS_PASSWORD') ?: null,
52
                    'database'        => 5,
53
                    'prefix'          => 'lodash:',
54
                    'serializer'      => 'igbinary',
55
                ],
56
57
                'default' => [
58
                    [
59
                        'host' => getenv('REDIS_HOST') ?: '127.0.0.1',
60
                        'port' => getenv('REDIS_PORT') ?: 6379,
61
                    ],
62
                ],
63
            ],
64
        ]);
65
        /** @var \RedisArray $client */
66
        $client = $redis->connection()->client();
67
68
        $data = ['name' => 'Georgia'];
69
        $redis->set('country2', $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...
70
71
        $this->assertInstanceOf(RedisArray::class, $client);
72
        $this->assertEquals($redis->get('country2'), $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...
73
        foreach ($client->getOption(Redis::OPT_SERIALIZER) as $serializer) {
74
            $this->assertEquals($serializer, Redis::SERIALIZER_IGBINARY);
75
        }
76
    }
77
78
    private function createConnection(string $driver, array $config = []): RedisManager
79
    {
80
        if (version_compare($this->app->version(), '5.7', '<')) {
81
            $redis = new RedisManager($driver, $config);
0 ignored issues
show
Bug introduced by
The call to RedisManager::__construct() misses a required argument $config.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$driver is of type string, but the function expects a object<Illuminate\Contra...Foundation\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$config is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        } else {
83
            $redis = new RedisManager($this->app, $driver, $config);
84
        }
85
86
        return $redis;
87
    }
88
}
89