|
2 | 2 |
|
3 | 3 | use React\Socket\ConnectionInterface; |
4 | 4 |
|
5 | | -use Clue\React\Redis\Server; |
6 | | - |
7 | | -use Clue\React\Redis\StreamingClient; |
8 | | - |
9 | 5 | use Clue\React\Redis\Factory; |
| 6 | +use React\Promise; |
10 | 7 |
|
11 | 8 | class FactoryTest extends TestCase |
12 | 9 | { |
| 10 | + private $loop; |
| 11 | + private $connector; |
| 12 | + private $factory; |
| 13 | + |
13 | 14 | public function setUp() |
14 | 15 | { |
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); |
17 | 19 | } |
18 | 20 |
|
19 | | - public function testPrequisiteServerAcceptsAnyPassword() |
| 21 | + public function testCtor() |
20 | 22 | { |
21 | | - $this->markTestSkipped(); |
| 23 | + $this->factory = new Factory($this->loop); |
22 | 24 | } |
23 | 25 |
|
24 | | - /** |
25 | | - * @depends testPrequisiteServerAcceptsAnyPassword |
26 | | - */ |
27 | | - public function testClientDefaultSuccess() |
| 26 | + public function testWillConnectWithDefaultPort() |
28 | 27 | { |
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 | + } |
34 | 31 |
|
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'); |
36 | 36 | } |
37 | 37 |
|
38 | | - /** |
39 | | - * @depends testPrequisiteServerAcceptsAnyPassword |
40 | | - */ |
41 | | - public function testClientAuthSelect() |
| 38 | + public function testWillResolveIfConnectorResolves() |
42 | 39 | { |
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'); |
44 | 42 |
|
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'); |
48 | 45 |
|
49 | | - $this->loop->run(); |
| 46 | + $this->expectPromiseResolve($promise); |
50 | 47 | } |
51 | 48 |
|
52 | | - /** |
53 | | - * @depends testPrequisiteServerAcceptsAnyPassword |
54 | | - */ |
55 | | - public function testClientAuthenticationContainsColons() |
| 49 | + public function testWillWriteSelectCommandIfTargetContainsPath() |
56 | 50 | { |
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 | + } |
58 | 57 |
|
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"); |
62 | 62 |
|
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'); |
64 | 65 | } |
65 | 66 |
|
66 | | - public function testClientUnconnectableAddress() |
| 67 | + public function testWillRejectIfConnectorRejects() |
67 | 68 | { |
| 69 | + $this->connector->expects($this->once())->method('create')->with('127.0.0.1', 2)->willReturn(Promise\reject(new \RuntimeException())); |
68 | 70 | $promise = $this->factory->createClient('tcp://127.0.0.1:2'); |
69 | 71 |
|
70 | 72 | $this->expectPromiseReject($promise); |
71 | | - |
72 | | - $this->loop->tick(); |
73 | 73 | } |
74 | 74 |
|
75 | | - public function testClientInvalidAddress() |
| 75 | + public function testWillRejectIfTargetIsInvalid() |
76 | 76 | { |
77 | 77 | $promise = $this->factory->createClient('http://invalid target'); |
78 | 78 |
|
|
0 commit comments