Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php

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

namespace Predis\Connection;

use PredisTestCase;
use Predis\Profile\ServerProfile;

/**
 * @group realm-connection
 */
abstract class PredisConnectionTestCase extends PredisTestCase
{
    /**
     * @group disconnected
     * @group slow
     * @expectedException Predis\Connection\ConnectionException
     */
    public function testThrowExceptionWhenUnableToConnect()
    {
        $parameters = array('host' => '169.254.10.10', 'timeout' => 0.5);
        $connection = $this->getConnection($profile, false, $parameters);
        $connection->executeCommand($this->getProfile()->createCommand('ping'));
    }

    // ******************************************************************** //
    // ---- INTEGRATION TESTS --------------------------------------------- //
    // ******************************************************************** //

    /**
     * @group connected
     */
    public function testConnectForcesConnection()
    {
        $connection = $this->getConnection();

        $this->assertFalse($connection->isConnected());
        $connection->connect();
        $this->assertTrue($connection->isConnected());
    }

    /**
     * @group connected
     * @expectedException Predis\ClientException
     * @expectedExceptionMessage Connection already estabilished
     */
    public function testThrowsExceptionOnConnectWhenAlreadyConnected()
    {
        $connection = $this->getConnection();

        $connection->connect();
        $connection->connect();
    }

    /**
     * @group connected
     */
    public function testDisconnectForcesDisconnection()
    {
        $connection = $this->getConnection();

        $connection->connect();
        $this->assertTrue($connection->isConnected());

        $connection->disconnect();
        $this->assertFalse($connection->isConnected());
    }

    /**
     * @group disconnected
     */
    public function testDoesNotThrowExceptionOnDisconnectWhenAlreadyDisconnected()
    {
        $connection = $this->getConnection();

        $this->assertFalse($connection->isConnected());
        $connection->disconnect();
        $this->assertFalse($connection->isConnected());
    }

    /**
     * @group connected
     */
    public function testGetResourceForcesConnection()
    {
        $connection = $this->getConnection();

        $this->assertFalse($connection->isConnected());
        $this->assertInternalType('resource', $connection->getResource());
        $this->assertTrue($connection->isConnected());
    }

    /**
     * @group connected
     */
    public function testSendingCommandForcesConnection()
    {
        $connection = $this->getConnection($profile);
        $cmdPing = $profile->createCommand('ping');

        $this->assertSame('PONG', $connection->executeCommand($cmdPing));
        $this->assertTrue($connection->isConnected());
    }

    /**
     * @group connected
     */
    public function testExecutesCommandOnServer()
    {
        $connection = $this->getConnection($profile);

        $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
        $cmdPing->expects($this->never())
                ->method('parseResponse');

        $this->assertSame('PONG', $connection->executeCommand($cmdPing));
    }

    /**
     * @group connected
     */
    public function testWritesCommandToServer()
    {
        $connection = $this->getConnection($profile);

        $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
        $cmdPing->expects($this->never())
                ->method('parseResponse');

        $connection->writeCommand($cmdPing);
        $connection->disconnect();
    }

    /**
     * @group connected
     */
    public function testReadsCommandFromServer()
    {
        $connection = $this->getConnection($profile);

        $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
        $cmdPing->expects($this->never())
                ->method('parseResponse');

        $connection->writeCommand($cmdPing);
        $this->assertSame('PONG', $connection->readResponse($cmdPing));
    }

    /**
     * @group connected
     */
    public function testIsAbleToWriteMultipleCommandsAndReadThemBackForPipelining()
    {
        $connection = $this->getConnection($profile);

        $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
        $cmdPing->expects($this->never())
                ->method('parseResponse');

        $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse'));
        $cmdEcho->setArguments(array('ECHOED'));
        $cmdEcho->expects($this->never())
                ->method('parseResponse');

        $connection = $this->getConnection();

        $connection->writeCommand($cmdPing);
        $connection->writeCommand($cmdEcho);

        $this->assertSame('PONG', $connection->readResponse($cmdPing));
        $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
    }

    /**
     * @group connected
     */
    public function testSendsInitializationCommandsOnConnection()
    {
        $connection = $this->getConnection($profile, true);

        $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('getArguments'));
        $cmdPing->expects($this->once())
                ->method('getArguments')
                ->will($this->returnValue(array()));

        $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('getArguments'));
        $cmdEcho->expects($this->once())
                ->method('getArguments')
                ->will($this->returnValue(array('ECHOED')));

        $connection->pushInitCommand($cmdPing);
        $connection->pushInitCommand($cmdEcho);

        $connection->connect();
    }

    /**
     * @group connected
     */
    public function testReadsStatusReplies()
    {
        $connection = $this->getConnection($profile, true);

        $connection->writeCommand($profile->createCommand('set', array('foo', 'bar')));
        $this->assertTrue($connection->read());

        $connection->writeCommand($profile->createCommand('ping'));
        $this->assertSame('PONG', $connection->read());

        $connection->writeCommand($profile->createCommand('multi'));
        $connection->writeCommand($profile->createCommand('ping'));
        $this->assertTrue($connection->read());
        $this->assertInstanceOf('Predis\ResponseQueued', $connection->read());
    }

    /**
     * @group connected
     */
    public function testReadsBulkReplies()
    {
        $connection = $this->getConnection($profile, true);

        $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));

        $connection->writeCommand($profile->createCommand('get', array('foo')));
        $this->assertSame('bar', $connection->read());

        $connection->writeCommand($profile->createCommand('get', array('hoge')));
        $this->assertNull($connection->read());
    }

    /**
     * @group connected
     */
    public function testReadsIntegerReplies()
    {
        $connection = $this->getConnection($profile, true);

        $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
        $connection->writeCommand($profile->createCommand('llen', array('metavars')));

        $this->assertSame(3, $connection->read());
    }

    /**
     * @group connected
     */
    public function testReadsErrorRepliesAsResponseErrorObjects()
    {
        $connection = $this->getConnection($profile, true);

        $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));
        $connection->writeCommand($profile->createCommand('rpush', array('foo', 'baz')));

        $this->assertInstanceOf('Predis\ResponseError', $error = $connection->read());
        $this->assertRegExp('/[ERR|WRONGTYPE] Operation against a key holding the wrong kind of value/', $error->getMessage());
    }

    /**
     * @group connected
     */
    public function testReadsMultibulkRepliesAsArrays()
    {
        $connection = $this->getConnection($profile, true);

        $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
        $connection->writeCommand($profile->createCommand('lrange', array('metavars', 0, -1)));

        $this->assertSame(array('foo', 'hoge', 'lol'), $connection->read());
    }

    /**
     * @group connected
     * @group slow
     * @expectedException Predis\Connection\ConnectionException
     */
    public function testThrowsExceptionOnConnectionTimeout()
    {
        $connection = $this->getConnection($_, false, array('host' => '169.254.10.10', 'timeout' => 0.5));

        $connection->connect();
    }

    /**
     * @group connected
     * @group slow
     * @expectedException Predis\Connection\ConnectionException
     */
    public function testThrowsExceptionOnReadWriteTimeout()
    {
        $connection = $this->getConnection($profile, true, array('read_write_timeout' => 0.5));

        $connection->executeCommand($profile->createCommand('brpop', array('foo', 3)));
    }

    // ******************************************************************** //
    // ---- HELPER METHODS ------------------------------------------------ //
    // ******************************************************************** //

    /**
     * Returns a named array with the default connection parameters and their values.
     *
     * @return Array Default connection parameters.
     */
    protected function getDefaultParametersArray()
    {
        return array(
            'scheme' => 'tcp',
            'host' => REDIS_SERVER_HOST,
            'port' => REDIS_SERVER_PORT,
            'database' => REDIS_SERVER_DBNUM,
            'read_write_timeout' => 2,
        );
    }

    /**
     * Returns a new instance of a connection instance.
     *
     * @param  ServerProfile    $profile    Reference to the server profile instance.
     * @param  bool             $initialize Push default initialization commands (SELECT and FLUSHDB).
     * @param  array            $parameters Additional connection parameters.
     * @return StreamConnection
     */
    abstract protected function getConnection(&$profile = null, $initialize = false, Array $parameters = array());
}

Commits for Nextrek/Aiba_backup/vendor/predis/predis/tests/PHPUnit/PredisConnectionTestCase.php

Diff revisions: vs.
Revision Author Commited Message
1464 MOliva picture MOliva Tue 13 Oct, 2020 11:16:56 +0000