|
8 | 8 | * For the full copyright and license information, please view the LICENSE |
9 | 9 | * file that was distributed with this source code. |
10 | 10 | */ |
11 | | -namespace Test\Unit; |
| 11 | +namespace Test\Unit\Client; |
12 | 12 |
|
13 | 13 | use RedisClient\Client\AbstractRedisClient; |
| 14 | +use RedisClient\Exception\EmptyResponseException; |
| 15 | +use RedisClient\Exception\MovedResponseException; |
| 16 | +use RedisClient\RedisClient; |
| 17 | +use Test\Unit\GlobalFunctionMock; |
14 | 18 |
|
15 | 19 | /** |
16 | 20 | * @see AbstractRedisClient |
| 21 | + * @runTestsInSeparateProcesses |
17 | 22 | */ |
18 | 23 | class AbstractRedisClientTest extends \PHPUnit_Framework_TestCase { |
19 | 24 |
|
@@ -95,4 +100,65 @@ public function test_getStructure($expect, $command, $params) { |
95 | 100 | $this->assertSame($expect, $Method->invoke($Client, $command, $params)); |
96 | 101 | } |
97 | 102 |
|
| 103 | + protected function mockStream() { |
| 104 | + include_once(__DIR__ . '/../GlobalFunctionMock.php'); |
| 105 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'stream_socket_client', function() {return true;}); |
| 106 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'stream_set_timeout', function() {return true;}); |
| 107 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fwrite', function($h, $m, $c) {return $c;}); |
| 108 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fgets', function() {return '';}); |
| 109 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fread', function() {return '';}); |
| 110 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fclose', function() {return true;}); |
| 111 | + } |
| 112 | + |
| 113 | + public function test_mockStream() { |
| 114 | + $this->mockStream(); |
| 115 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fgets', function() { |
| 116 | + return "+TEST\r\n"; |
| 117 | + }); |
| 118 | + |
| 119 | + $Redis = new RedisClient(); |
| 120 | + $this->assertSame('TEST', $Redis->ping()); |
| 121 | + unset($Redis); |
| 122 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_socket_client')); |
| 123 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_set_timeout')); |
| 124 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('fwrite')); |
| 125 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('fgets')); |
| 126 | + $this->assertSame(0, GlobalFunctionMock::getCountCalls('fread')); |
| 127 | + $this->assertSame(0, GlobalFunctionMock::getCountCalls('fclose')); |
| 128 | + } |
| 129 | + |
| 130 | + public function test_stream() { |
| 131 | + $Redis = new RedisClient(); |
| 132 | + $this->assertSame('PONG', $Redis->ping()); |
| 133 | + } |
| 134 | + |
| 135 | + public function test_MovedErrorResponse() { |
| 136 | + $this->mockStream(); |
| 137 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fwrite', function($h, $m, $c) { |
| 138 | + $this->assertSame(true, $h); |
| 139 | + $this->assertSame("*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n", $m); |
| 140 | + $this->assertSame(22, $c); |
| 141 | + return $c; |
| 142 | + }); |
| 143 | + GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fgets', function() { |
| 144 | + return "-MOVED 42 server\r\n"; |
| 145 | + }); |
| 146 | + |
| 147 | + $Redis = new RedisClient(); |
| 148 | + try { |
| 149 | + $Redis->get('key'); |
| 150 | + $this->assertTrue(false, 'Expect MovedResponseException'); |
| 151 | + } catch (\Exception $Ex) { |
| 152 | + /** @var MovedResponseException $Ex*/ |
| 153 | + $this->assertSame(true, $Ex instanceof MovedResponseException); |
| 154 | + $this->assertSame(42, $Ex->getSlot()); |
| 155 | + $this->assertSame('server', $Ex->getServer()); |
| 156 | + } |
| 157 | + |
| 158 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_socket_client')); |
| 159 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_set_timeout')); |
| 160 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('fwrite')); |
| 161 | + $this->assertSame(1, GlobalFunctionMock::getCountCalls('fgets')); |
| 162 | + $this->assertSame(0, GlobalFunctionMock::getCountCalls('fread')); |
| 163 | + } |
98 | 164 | } |
0 commit comments