File: WebdisConnectionTest.php

package info (click to toggle)
php-nrk-predis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,068 kB
  • sloc: php: 59,571; xml: 42; makefile: 5
file content (227 lines) | stat: -rw-r--r-- 7,152 bytes parent folder | download
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
<?php

/*
 * This file is part of the Predis package.
 *
 * (c) 2009-2020 Daniele Alessandri
 * (c) 2021-2025 Till Krüss
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Predis\Connection;

use PredisTestCase;

/**
 * @group ext-curl
 * @group ext-phpiredis
 * @group realm-connection
 * @group realm-webdis
 * @requires extension phpiredis
 * @requires extension curl
 */
class WebdisConnectionTest extends PredisTestCase
{
    /**
     * @group disconnected
     */
    public function testIsConnectedAlwaysReturnsTrue(): void
    {
        $connection = $this->createConnection();

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

    /**
     * @group disconnected
     */
    public function testSupportsSchemeUnix(): void
    {
        $connection = $this->createConnectionWithParams(['scheme' => 'http']);

        $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
    }

    /**
     * @group disconnected
     */
    public function testThrowsExceptionOnInvalidScheme(): void
    {
        $this->expectException('InvalidArgumentException');
        $this->expectExceptionMessage("Invalid scheme: 'tcp'");

        $connection = $this->createConnectionWithParams(['scheme' => 'tcp']);
    }

    /**
     * @group disconnected
     */
    public function testWritingCommandsIsNotSupported(): void
    {
        $this->expectException('Predis\NotSupportedException');
        $this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::writeRequest() is not supported");

        $connection = $this->createConnection();
        $connection->writeRequest($this->getCommandFactory()->create('ping'));
    }

    /**
     * @group disconnected
     */
    public function testReadingResponsesIsNotSupported(): void
    {
        $this->expectException('Predis\NotSupportedException');
        $this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::readResponse() is not supported");

        $connection = $this->createConnection();
        $connection->readResponse($this->getCommandFactory()->create('ping'));
    }

    /**
     * @group disconnected
     */
    public function testReadingFromConnectionIsNotSupported(): void
    {
        $this->expectException('Predis\NotSupportedException');
        $this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::read() is not supported");

        $connection = $this->createConnection();
        $connection->read();
    }

    /**
     * @group disconnected
     */
    public function testAddingConnectCommandsIsNotSupported(): void
    {
        $this->expectException('Predis\NotSupportedException');
        $this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::addConnectCommand() is not supported");

        $connection = $this->createConnection();
        $connection->addConnectCommand($this->getCommandFactory()->create('ping'));
    }

    /**
     * @group disconnected
     */
    public function testRejectCommandSelect(): void
    {
        $this->expectException('Predis\NotSupportedException');
        $this->expectExceptionMessage("Command 'SELECT' is not allowed by Webdis");

        $connection = $this->createConnection();
        $connection->executeCommand($this->getCommandFactory()->create('select', [0]));
    }

    /**
     * @group disconnected
     */
    public function testRejectCommandAuth(): void
    {
        $this->expectException('Predis\NotSupportedException');
        $this->expectExceptionMessage("Command 'AUTH' is not allowed by Webdis");

        $connection = $this->createConnection();
        $connection->executeCommand($this->getCommandFactory()->create('auth', ['foobar']));
    }

    /**
     * @group disconnected
     */
    public function testCanBeSerialized(): void
    {
        $parameters = $this->getParameters([
            'alias' => 'redis',
            'read_write_timeout' => 10,
        ]);

        $connection = $this->createConnectionWithParams($parameters);

        $unserialized = unserialize(serialize($connection));

        $this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
        $this->assertEquals($parameters, $unserialized->getParameters());
    }

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

    /**
     * @group connected
     */
    public function testExecutesMultipleCommandsOnServer(): void
    {
        $commands = $this->getCommandFactory();

        $cmdPing = $commands->create('ping');
        $cmdEcho = $commands->create('echo', ['echoed']);
        $cmdGet = $commands->create('get', ['foobar']);
        $cmdRpush = $commands->create('rpush', ['metavars', 'foo', 'hoge', 'lol']);
        $cmdLrange = $commands->create('lrange', ['metavars', 0, -1]);

        $connection = $this->createConnection(true);

        $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
        $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
        $this->assertNull($connection->executeCommand($cmdGet));
        $this->assertSame(3, $connection->executeCommand($cmdRpush));
        $this->assertSame(['foo', 'hoge', 'lol'], $connection->executeCommand($cmdLrange));
    }

    /**
     * @medium
     * @group disconnected
     * @group slow
     */
    public function testThrowExceptionWhenUnableToConnect(): void
    {
        $this->expectException('Predis\Connection\ConnectionException');

        $connection = $this->createConnectionWithParams(['host' => '169.254.10.10']);
        $connection->executeCommand($this->getCommandFactory()->create('ping'));
    }

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

    /**
     * Returns a named array with the default connection parameters and their values.
     *
     * @return array Default connection parameters
     */
    protected function getDefaultParametersArray(): array
    {
        return [
            'scheme' => 'http',
            'host' => constant('WEBDIS_SERVER_HOST'),
            'port' => constant('WEBDIS_SERVER_PORT'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function createConnection(): NodeConnectionInterface
    {
        return $this->createConnectionWithParams([]);
    }

    /**
     * {@inheritdoc}
     */
    protected function createConnectionWithParams($parameters): NodeConnectionInterface
    {
        if (!$parameters instanceof ParametersInterface) {
            $parameters = $this->getParameters($parameters);
        }

        $connection = new WebdisConnection($parameters);
        $connection->executeCommand($this->getCommandFactory()->create('flushdb'));

        return $connection;
    }
}