Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of the Predis package.
5
 *
6
 * (c) Daniele Alessandri <suppakilla@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
namespace Predis\Command;
13

    
14
/**
15
 * @group commands
16
 * @group realm-pubsub
17
 */
18
class PubSubUnsubscribeTest extends PredisCommandTestCase
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function getExpectedCommand()
24
    {
25
        return 'Predis\Command\PubSubUnsubscribe';
26
    }
27

    
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function getExpectedId()
32
    {
33
        return 'UNSUBSCRIBE';
34
    }
35

    
36
    /**
37
     * @group disconnected
38
     */
39
    public function testFilterArguments()
40
    {
41
        $arguments = array('channel1', 'channel2', 'channel3');
42
        $expected = array('channel1', 'channel2', 'channel3');
43

    
44
        $command = $this->getCommand();
45
        $command->setArguments($arguments);
46

    
47
        $this->assertSame($expected, $command->getArguments());
48
    }
49

    
50
    /**
51
     * @group disconnected
52
     */
53
    public function testFilterArgumentsAsSingleArray()
54
    {
55
        $arguments = array(array('channel1', 'channel2', 'channel3'));
56
        $expected = array('channel1', 'channel2', 'channel3');
57

    
58
        $command = $this->getCommand();
59
        $command->setArguments($arguments);
60

    
61
        $this->assertSame($expected, $command->getArguments());
62
    }
63

    
64
    /**
65
     * @group disconnected
66
     */
67
    public function testParseResponse()
68
    {
69
        $raw = array('unsubscribe', 'channel', 1);
70
        $expected = array('unsubscribe', 'channel', 1);
71

    
72
        $command = $this->getCommand();
73

    
74
        $this->assertSame($expected, $command->parseResponse($raw));
75
    }
76

    
77
    /**
78
     * @group disconnected
79
     */
80
    public function testPrefixKeys()
81
    {
82
        $arguments = array(array('channel1', 'channel2', 'channel3'));
83
        $expected = array('prefix:channel1', 'prefix:channel2', 'prefix:channel3');
84

    
85
        $command = $this->getCommandWithArgumentsArray($arguments);
86
        $command->prefixKeys('prefix:');
87

    
88
        $this->assertSame($expected, $command->getArguments());
89
    }
90

    
91
    /**
92
     * @group disconnected
93
     */
94
    public function testPrefixKeysIgnoredOnEmptyArguments()
95
    {
96
        $command = $this->getCommand();
97
        $command->prefixKeys('prefix:');
98

    
99
        $this->assertSame(array(), $command->getArguments());
100
    }
101

    
102
    /**
103
     * @group connected
104
     */
105
    public function testDoesNotSwitchToSubscribeMode()
106
    {
107
        $redis = $this->getClient();
108

    
109
        $this->assertSame(array('unsubscribe', 'channel', 0), $redis->unsubscribe('channel'));
110
        $this->assertSame('echoed', $redis->echo('echoed'));
111
    }
112

    
113
    /**
114
     * @group connected
115
     */
116
    public function testUnsubscribesFromNotSubscribedChannels()
117
    {
118
        $redis = $this->getClient();
119

    
120
        $this->assertSame(array('unsubscribe', 'channel', 0), $redis->unsubscribe('channel'));
121
    }
122

    
123
    /**
124
     * @group connected
125
     */
126
    public function testUnsubscribesFromSubscribedChannels()
127
    {
128
        $redis = $this->getClient();
129

    
130
        $this->assertSame(array('subscribe', 'channel', 1), $redis->subscribe('channel'));
131
        $this->assertSame(array('unsubscribe', 'channel', 0), $redis->unsubscribe('channel'));
132
    }
133

    
134
    /**
135
     * @group connected
136
     */
137
    public function testUnsubscribesFromAllSubscribedChannels()
138
    {
139
        $redis = $this->getClient();
140

    
141
        $this->assertSame(array('subscribe', 'channel:foo', 1), $redis->subscribe('channel:foo'));
142
        $this->assertSame(array('subscribe', 'channel:bar', 2), $redis->subscribe('channel:bar'));
143

    
144
        list($_, $unsubscribed1, $_) = $redis->unsubscribe();
145
        list($_, $unsubscribed2, $_) = $redis->getConnection()->read();
146
        $this->assertSameValues(array('channel:foo', 'channel:bar'), array($unsubscribed1, $unsubscribed2));
147

    
148
        $this->assertSame('echoed', $redis->echo('echoed'));
149
    }
150
}
(64-64/146)