RedisConnectionTest.php 2.15 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\redis;
4

5 6 7
/**
 * @group redis
 */
Carsten Brandt committed
8
class RedisConnectionTest extends RedisTestCase
9
{
10 11 12 13 14 15 16 17 18 19
    /**
     * test connection to redis and selection of db
     */
    public function testConnect()
    {
        $db = $this->getConnection(false);
        $db->open();
        $this->assertTrue($db->ping());
        $db->set('YIITESTKEY', 'YIITESTVALUE');
        $db->close();
20

21 22 23 24 25
        $db = $this->getConnection(false);
        $db->database = 0;
        $db->open();
        $this->assertEquals('YIITESTVALUE', $db->get('YIITESTKEY'));
        $db->close();
26

27 28 29 30 31 32
        $db = $this->getConnection(false);
        $db->database = 1;
        $db->open();
        $this->assertNull($db->get('YIITESTKEY'));
        $db->close();
    }
33

34 35 36 37 38 39 40 41 42 43 44
    public function keyValueData()
    {
        return [
            [123],
            [-123],
            [0],
            ['test'],
            ["test\r\ntest"],
            [''],
        ];
    }
45

46 47 48 49 50 51
    /**
     * @dataProvider keyValueData
     */
    public function testStoreGet($data)
    {
        $db = $this->getConnection(true);
52

53 54 55
        $db->set('hi', $data);
        $this->assertEquals($data, $db->get('hi'));
    }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    /**
     * https://github.com/yiisoft/yii2/issues/4745
     */
    public function testReturnType()
    {
        $redis = $this->getConnection();
        $redis->executeCommand('SET',['key1','val1']);
        $redis->executeCommand('HMSET',['hash1','hk3','hv3','hk4','hv4']);
        $redis->executeCommand('RPUSH',['newlist2','tgtgt','tgtt','44',11]);
        $redis->executeCommand('SADD',['newset2','segtggttval','sv1','sv2','sv3']);
        $redis->executeCommand('ZADD',['newz2',2,'ss',3,'pfpf']);
        $allKeys = $redis->executeCommand('KEYS',['*']);
        sort($allKeys);
        $this->assertEquals(['hash1', 'key1', 'newlist2', 'newset2', 'newz2'], $allKeys);
        $expected = [
            'hash1' => 'hash',
            'key1' => 'string',
            'newlist2' => 'list',
            'newset2' => 'set',
            'newz2' => 'zset',
        ];
        foreach($allKeys as $key) {
            $this->assertEquals($expected[$key], $redis->executeCommand('TYPE',[$key]));
        }
    }
Luciano Baraglia committed
82
}