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
use PredisTestCase;
15

    
16
/**
17
 *
18
 */
19
class CommandTest extends PredisTestCase
20
{
21
    /**
22
     * @group disconnected
23
     */
24
    public function testImplementsCorrectInterface()
25
    {
26
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
27

    
28
        $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
29
    }
30

    
31
    /**
32
     * @group disconnected
33
     */
34
    public function testGetEmptyArguments()
35
    {
36
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
37

    
38
        $this->assertEmpty($command->getArguments());
39
    }
40

    
41
    /**
42
     * @group disconnected
43
     */
44
    public function testSetRawArguments()
45
    {
46
        $arguments = array('1st', '2nd', '3rd');
47

    
48
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
49
        $command->setRawArguments($arguments);
50

    
51
        $this->assertEquals($arguments, $command->getArguments());
52
    }
53

    
54
    /**
55
     * @group disconnected
56
     *
57
     * @todo Since AbstractCommand::filterArguments is protected we cannot set an expectation
58
     *       for it when AbstractCommand::setArguments() is invoked. I wonder how we can do that.
59
     */
60
    public function testSetArguments()
61
    {
62
        $arguments = array('1st', '2nd', '3rd');
63

    
64
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
65
        $command->setArguments($arguments);
66

    
67
        $this->assertEquals($arguments, $command->getArguments());
68
    }
69

    
70
    /**
71
     * @group disconnected
72
     */
73
    public function testGetArgumentAtIndex()
74
    {
75
        $arguments = array('1st', '2nd', '3rd');
76

    
77
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
78
        $command->setArguments($arguments);
79

    
80
        $this->assertEquals($arguments[0], $command->getArgument(0));
81
        $this->assertEquals($arguments[2], $command->getArgument(2));
82
        $this->assertNull($command->getArgument(10));
83
    }
84

    
85
    /**
86
     * @group disconnected
87
     */
88
    public function testParseResponse()
89
    {
90
        $response = 'response-buffer';
91
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
92

    
93
        $this->assertEquals($response, $command->parseResponse($response));
94
    }
95

    
96
    /**
97
     * @group disconnected
98
     */
99
    public function testSetAndGetHash()
100
    {
101
        $hash = "key-hash";
102

    
103
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
104
        $command->setRawArguments(array('key'));
105

    
106
        $this->assertNull($command->getHash());
107

    
108
        $command->setHash($hash);
109
        $this->assertSame($hash, $command->getHash());
110

    
111
        $command->setArguments(array('key'));
112
        $this->assertNull($command->getHash());
113

    
114
        $command->setHash($hash);
115
        $command->setRawArguments(array('key'));
116
        $this->assertNull($command->getHash());
117
    }
118
    /**
119
     * @group disconnected
120
     */
121
    public function testToString()
122
    {
123
        $expected = 'SET key value';
124
        $arguments = array('key', 'value');
125

    
126
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
127
        $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
128

    
129
        $command->setRawArguments($arguments);
130

    
131
        $this->assertEquals($expected, (string) $command);
132
    }
133

    
134
    /**
135
     * @group disconnected
136
     */
137
    public function testToStringWithLongArguments()
138
    {
139
        $expected = 'SET key abcdefghijklmnopqrstuvwxyz012345[...]';
140
        $arguments = array('key', 'abcdefghijklmnopqrstuvwxyz0123456789');
141

    
142
        $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
143
        $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
144

    
145
        $command->setRawArguments($arguments);
146

    
147
        $this->assertEquals($expected, (string) $command);
148
    }
149

    
150
    /**
151
     * @group disconnected
152
     */
153
    public function testNormalizeArguments()
154
    {
155
        $arguments = array('arg1', 'arg2', 'arg3', 'arg4');
156

    
157
        $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
158
        $this->assertSame($arguments, AbstractCommand::normalizeArguments(array($arguments)));
159

    
160
        $arguments = array(array(), array());
161
        $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
162

    
163
        $arguments = array(new \stdClass());
164
        $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
165
    }
166

    
167
    /**
168
     * @group disconnected
169
     */
170
    public function testNormalizeVariadic()
171
    {
172
        $arguments = array('key', 'value1', 'value2', 'value3');
173

    
174
        $this->assertSame($arguments, AbstractCommand::normalizeVariadic($arguments));
175
        $this->assertSame($arguments, AbstractCommand::normalizeVariadic(array('key', array('value1', 'value2', 'value3'))));
176

    
177
        $arguments = array(new \stdClass());
178
        $this->assertSame($arguments, AbstractCommand::normalizeVariadic($arguments));
179
    }
180
}
(1-1/146)