Skip to content

Commit 32feb84

Browse files
committed
Merge pull request #35 from clue-labs/select
Do not send empty SELECT statement when no database has been given
2 parents c3e380e + e9071a3 commit 32feb84

2 files changed

Lines changed: 41 additions & 41 deletions

File tree

src/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private function getDatabaseFromTarget($target)
135135
{
136136
$db = null;
137137
$path = parse_url($target, PHP_URL_PATH);
138-
if ($path !== null) {
138+
if ($path !== null && $path !== '') {
139139
// skip first slash
140140
$db = substr($path, 1);
141141
}

tests/FactoryTest.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,77 @@
22

33
use React\Socket\ConnectionInterface;
44

5-
use Clue\React\Redis\Server;
6-
7-
use Clue\React\Redis\StreamingClient;
8-
95
use Clue\React\Redis\Factory;
6+
use React\Promise;
107

118
class FactoryTest extends TestCase
129
{
10+
private $loop;
11+
private $connector;
12+
private $factory;
13+
1314
public function setUp()
1415
{
15-
$this->loop = new React\EventLoop\StreamSelectLoop();
16-
$this->factory = new Factory($this->loop);
16+
$this->loop = $this->getMock('React\EventLoop\LoopInterface');
17+
$this->connector = $this->getMock('React\SocketClient\ConnectorInterface');
18+
$this->factory = new Factory($this->loop, $this->connector);
1719
}
1820

19-
public function testPrequisiteServerAcceptsAnyPassword()
21+
public function testCtor()
2022
{
21-
$this->markTestSkipped();
23+
$this->factory = new Factory($this->loop);
2224
}
2325

24-
/**
25-
* @depends testPrequisiteServerAcceptsAnyPassword
26-
*/
27-
public function testClientDefaultSuccess()
26+
public function testWillConnectWithDefaultPort()
2827
{
29-
$promise = $this->factory->createClient();
30-
31-
$this->expectPromiseResolve($promise)->then(function (StreamingClient $client) {
32-
$client->end();
33-
});
28+
$this->connector->expects($this->once())->method('create')->with('redis.example.com', 6379)->willReturn(Promise\reject(new \RuntimeException()));
29+
$promise = $this->factory->createClient('redis.example.com');
30+
}
3431

35-
$this->loop->run();
32+
public function testWillConnectToLocalIpWhenTargetIsLocalhost()
33+
{
34+
$this->connector->expects($this->once())->method('create')->with('127.0.0.1', 1337)->willReturn(Promise\reject(new \RuntimeException()));
35+
$promise = $this->factory->createClient('tcp://localhost:1337');
3636
}
3737

38-
/**
39-
* @depends testPrequisiteServerAcceptsAnyPassword
40-
*/
41-
public function testClientAuthSelect()
38+
public function testWillResolveIfConnectorResolves()
4239
{
43-
$promise = $this->factory->createClient('tcp://authenticationpassword@127.0.0.1:6379/0');
40+
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
41+
$stream->expects($this->never())->method('write');
4442

45-
$this->expectPromiseResolve($promise)->then(function (StreamingClient $client) {
46-
$client->end();
47-
});
43+
$this->connector->expects($this->once())->method('create')->with('127.0.0.1', 2)->willReturn(Promise\resolve($stream));
44+
$promise = $this->factory->createClient('tcp://127.0.0.1:2');
4845

49-
$this->loop->run();
46+
$this->expectPromiseResolve($promise);
5047
}
5148

52-
/**
53-
* @depends testPrequisiteServerAcceptsAnyPassword
54-
*/
55-
public function testClientAuthenticationContainsColons()
49+
public function testWillWriteSelectCommandIfTargetContainsPath()
5650
{
57-
$promise = $this->factory->createClient('tcp://authentication:can:contain:colons@127.0.0.1:6379');
51+
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
52+
$stream->expects($this->once())->method('write')->with("*2\r\n$6\r\nselect\r\n$4\r\ndemo\r\n");
53+
54+
$this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream));
55+
$this->factory->createClient('tcp://127.0.0.1/demo');
56+
}
5857

59-
$this->expectPromiseResolve($promise)->then(function (StreamingClient $client) {
60-
$client->end();
61-
});
58+
public function testWillWriteAuthCommandIfTargetContainsUserInfo()
59+
{
60+
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
61+
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$11\r\nhello:world\r\n");
6262

63-
$this->loop->run();
63+
$this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream));
64+
$this->factory->createClient('tcp://hello:world@127.0.0.1');
6465
}
6566

66-
public function testClientUnconnectableAddress()
67+
public function testWillRejectIfConnectorRejects()
6768
{
69+
$this->connector->expects($this->once())->method('create')->with('127.0.0.1', 2)->willReturn(Promise\reject(new \RuntimeException()));
6870
$promise = $this->factory->createClient('tcp://127.0.0.1:2');
6971

7072
$this->expectPromiseReject($promise);
71-
72-
$this->loop->tick();
7373
}
7474

75-
public function testClientInvalidAddress()
75+
public function testWillRejectIfTargetIsInvalid()
7676
{
7777
$promise = $this->factory->createClient('http://invalid target');
7878

0 commit comments

Comments
 (0)