|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\Tests\React\Redis; |
| 4 | + |
| 5 | +use Clue\React\Redis\Factory; |
| 6 | +use React\Promise; |
| 7 | + |
| 8 | +class FactoryLazyStreamingClientTest extends TestCase |
| 9 | +{ |
| 10 | + private $loop; |
| 11 | + private $connector; |
| 12 | + private $factory; |
| 13 | + |
| 14 | + public function setUp() |
| 15 | + { |
| 16 | + $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 17 | + $this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); |
| 18 | + $this->factory = new Factory($this->loop, $this->connector); |
| 19 | + } |
| 20 | + |
| 21 | + public function testWillConnectWithDefaultPort() |
| 22 | + { |
| 23 | + $this->connector->expects($this->never())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException())); |
| 24 | + $this->factory->createLazyClient('redis.example.com'); |
| 25 | + } |
| 26 | + |
| 27 | + public function testWillConnectToLocalhost() |
| 28 | + { |
| 29 | + $this->connector->expects($this->never())->method('connect')->with('localhost:1337')->willReturn(Promise\reject(new \RuntimeException())); |
| 30 | + $this->factory->createLazyClient('localhost:1337'); |
| 31 | + } |
| 32 | + |
| 33 | + public function testWillResolveIfConnectorResolves() |
| 34 | + { |
| 35 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 36 | + $stream->expects($this->never())->method('write'); |
| 37 | + |
| 38 | + $this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream)); |
| 39 | + $client = $this->factory->createLazyClient('localhost'); |
| 40 | + |
| 41 | + $this->assertInstanceOf('Clue\React\Redis\Client', $client); |
| 42 | + } |
| 43 | + |
| 44 | + public function testWillWriteSelectCommandIfTargetContainsPath() |
| 45 | + { |
| 46 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 47 | + $stream->expects($this->never())->method('write')->with("*2\r\n$6\r\nselect\r\n$4\r\ndemo\r\n"); |
| 48 | + |
| 49 | + $this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream)); |
| 50 | + $this->factory->createLazyClient('redis://127.0.0.1/demo'); |
| 51 | + } |
| 52 | + |
| 53 | + public function testWillWriteSelectCommandIfTargetContainsDbQueryParameter() |
| 54 | + { |
| 55 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 56 | + $stream->expects($this->never())->method('write')->with("*2\r\n$6\r\nselect\r\n$1\r\n4\r\n"); |
| 57 | + |
| 58 | + $this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream)); |
| 59 | + $this->factory->createLazyClient('redis://127.0.0.1?db=4'); |
| 60 | + } |
| 61 | + |
| 62 | + public function testWillWriteAuthCommandIfRedisUriContainsUserInfo() |
| 63 | + { |
| 64 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 65 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n"); |
| 66 | + |
| 67 | + $this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream)); |
| 68 | + $this->factory->createLazyClient('redis://hello:world@example.com'); |
| 69 | + } |
| 70 | + |
| 71 | + public function testWillWriteAuthCommandIfRedisUriContainsEncodedUserInfo() |
| 72 | + { |
| 73 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 74 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nh@llo\r\n"); |
| 75 | + |
| 76 | + $this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream)); |
| 77 | + $this->factory->createLazyClient('redis://:h%40llo@example.com'); |
| 78 | + } |
| 79 | + |
| 80 | + public function testWillWriteAuthCommandIfTargetContainsPasswordQueryParameter() |
| 81 | + { |
| 82 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 83 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$6\r\nsecret\r\n"); |
| 84 | + |
| 85 | + $this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream)); |
| 86 | + $this->factory->createLazyClient('redis://example.com?password=secret'); |
| 87 | + } |
| 88 | + |
| 89 | + public function testWillWriteAuthCommandIfTargetContainsEncodedPasswordQueryParameter() |
| 90 | + { |
| 91 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 92 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nh@llo\r\n"); |
| 93 | + |
| 94 | + $this->connector->expects($this->never())->method('connect')->with('example.com:6379')->willReturn(Promise\resolve($stream)); |
| 95 | + $this->factory->createLazyClient('redis://example.com?password=h%40llo'); |
| 96 | + } |
| 97 | + |
| 98 | + public function testWillWriteAuthCommandIfRedissUriContainsUserInfo() |
| 99 | + { |
| 100 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 101 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n"); |
| 102 | + |
| 103 | + $this->connector->expects($this->never())->method('connect')->with('tls://example.com:6379')->willReturn(Promise\resolve($stream)); |
| 104 | + $this->factory->createLazyClient('rediss://hello:world@example.com'); |
| 105 | + } |
| 106 | + |
| 107 | + public function testWillWriteAuthCommandIfRedisUnixUriContainsPasswordQueryParameter() |
| 108 | + { |
| 109 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 110 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n"); |
| 111 | + |
| 112 | + $this->connector->expects($this->never())->method('connect')->with('unix:///tmp/redis.sock')->willReturn(Promise\resolve($stream)); |
| 113 | + $this->factory->createLazyClient('redis+unix:///tmp/redis.sock?password=world'); |
| 114 | + } |
| 115 | + |
| 116 | + public function testWillWriteAuthCommandIfRedisUnixUriContainsUserInfo() |
| 117 | + { |
| 118 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 119 | + $stream->expects($this->never())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n"); |
| 120 | + |
| 121 | + $this->connector->expects($this->never())->method('connect')->with('unix:///tmp/redis.sock')->willReturn(Promise\resolve($stream)); |
| 122 | + $this->factory->createLazyClient('redis+unix://hello:world@/tmp/redis.sock'); |
| 123 | + } |
| 124 | + |
| 125 | + public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter() |
| 126 | + { |
| 127 | + $stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 128 | + $stream->expects($this->never())->method('write')->with("*2\r\n$6\r\nselect\r\n$4\r\ndemo\r\n"); |
| 129 | + |
| 130 | + $this->connector->expects($this->never())->method('connect')->with('unix:///tmp/redis.sock')->willReturn(Promise\resolve($stream)); |
| 131 | + $this->factory->createLazyClient('redis+unix:///tmp/redis.sock?db=demo'); |
| 132 | + } |
| 133 | + |
| 134 | + public function testWillRejectIfConnectorRejects() |
| 135 | + { |
| 136 | + $this->connector->expects($this->never())->method('connect')->with('127.0.0.1:2')->willReturn(Promise\reject(new \RuntimeException())); |
| 137 | + $client = $this->factory->createLazyClient('redis://127.0.0.1:2'); |
| 138 | + |
| 139 | + $this->assertInstanceOf('Clue\React\Redis\Client', $client); |
| 140 | + } |
| 141 | + |
| 142 | + public function testWillRejectIfTargetIsInvalid() |
| 143 | + { |
| 144 | + $client = $this->factory->createLazyClient('http://invalid target'); |
| 145 | + |
| 146 | + $this->assertInstanceOf('Clue\React\Redis\Client', $client); |
| 147 | + } |
| 148 | +} |
0 commit comments