CLIENT_Test.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Command\Redis;
  11. /**
  12. * @group commands
  13. * @group realm-server
  14. */
  15. class CLIENT_Test extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\Redis\CLIENT';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'CLIENT';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArgumentsOfClientKill()
  35. {
  36. $arguments = array('kill', '127.0.0.1:45393');
  37. $expected = array('kill', '127.0.0.1:45393');
  38. $command = $this->getCommand();
  39. $command->setArguments($arguments);
  40. $this->assertSame($expected, $command->getArguments());
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testFilterArgumentsOfClientList()
  46. {
  47. $arguments = array('list');
  48. $expected = array('list');
  49. $command = $this->getCommand();
  50. $command->setArguments($arguments);
  51. $this->assertSame($expected, $command->getArguments());
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testFilterArgumentsOfClientGetname()
  57. {
  58. $arguments = $expected = array('getname');
  59. $command = $this->getCommand();
  60. $command->setArguments($arguments);
  61. $this->assertSame($expected, $command->getArguments());
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testFilterArgumentsOfClientSetname()
  67. {
  68. $arguments = $expected = array('setname', 'connection-a');
  69. $command = $this->getCommand();
  70. $command->setArguments($arguments);
  71. $this->assertSame($expected, $command->getArguments());
  72. }
  73. /**
  74. * @group disconnected
  75. */
  76. public function testParseResponseOfClientKill()
  77. {
  78. $command = $this->getCommand();
  79. $command->setArguments(array('kill'));
  80. $this->assertSame(true, $command->parseResponse(true));
  81. }
  82. /**
  83. * @group disconnected
  84. */
  85. public function testParseResponseOfClientList()
  86. {
  87. $command = $this->getCommand();
  88. $command->setArguments(array('list'));
  89. $raw = <<<BUFFER
  90. addr=127.0.0.1:45393 fd=6 idle=0 flags=N db=0 sub=0 psub=0
  91. addr=127.0.0.1:45394 fd=7 idle=0 flags=N db=0 sub=0 psub=0
  92. addr=127.0.0.1:45395 fd=8 idle=0 flags=N db=0 sub=0 psub=0
  93. BUFFER;
  94. $parsed = array(
  95. array('addr' => '127.0.0.1:45393', 'fd' => '6', 'idle' => '0', 'flags' => 'N', 'db' => '0', 'sub' => '0', 'psub' => '0'),
  96. array('addr' => '127.0.0.1:45394', 'fd' => '7', 'idle' => '0', 'flags' => 'N', 'db' => '0', 'sub' => '0', 'psub' => '0'),
  97. array('addr' => '127.0.0.1:45395', 'fd' => '8', 'idle' => '0', 'flags' => 'N', 'db' => '0', 'sub' => '0', 'psub' => '0'),
  98. );
  99. $this->assertSame($parsed, $command->parseResponse($raw));
  100. }
  101. /**
  102. * @group connected
  103. * @requiresRedisVersion >= 2.4.0
  104. */
  105. public function testReturnsListOfConnectedClients()
  106. {
  107. $redis = $this->getClient();
  108. $this->assertInternalType('array', $clients = $redis->client('LIST'));
  109. $this->assertGreaterThanOrEqual(1, count($clients));
  110. $this->assertInternalType('array', $clients[0]);
  111. $this->assertArrayHasKey('addr', $clients[0]);
  112. $this->assertArrayHasKey('fd', $clients[0]);
  113. $this->assertArrayHasKey('idle', $clients[0]);
  114. $this->assertArrayHasKey('flags', $clients[0]);
  115. $this->assertArrayHasKey('db', $clients[0]);
  116. $this->assertArrayHasKey('sub', $clients[0]);
  117. $this->assertArrayHasKey('psub', $clients[0]);
  118. }
  119. /**
  120. * @group connected
  121. * @requiresRedisVersion >= 2.6.9
  122. */
  123. public function testGetsNameOfConnection()
  124. {
  125. $redis = $this->getClient();
  126. $clientName = $redis->client('GETNAME');
  127. $this->assertNull($clientName);
  128. $expectedConnectionName = 'foo-bar';
  129. $this->assertEquals('OK', $redis->client('SETNAME', $expectedConnectionName));
  130. $this->assertEquals($expectedConnectionName, $redis->client('GETNAME'));
  131. }
  132. /**
  133. * @group connected
  134. * @requiresRedisVersion >= 2.6.9
  135. */
  136. public function testSetsNameOfConnection()
  137. {
  138. $redis = $this->getClient();
  139. $expectedConnectionName = 'foo-baz';
  140. $this->assertEquals('OK', $redis->client('SETNAME', $expectedConnectionName));
  141. $this->assertEquals($expectedConnectionName, $redis->client('GETNAME'));
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function invalidConnectionNameProvider()
  147. {
  148. return array(
  149. array('foo space'),
  150. array('foo \n'),
  151. array('foo $'),
  152. );
  153. }
  154. /**
  155. * @group connected
  156. * @requiresRedisVersion >= 2.6.9
  157. * @dataProvider invalidConnectionNameProvider
  158. *
  159. * @expectedException \Predis\Response\ServerException
  160. *
  161. * @param string $invalidConnectionName
  162. */
  163. public function testInvalidSetNameOfConnection($invalidConnectionName)
  164. {
  165. $redis = $this->getClient();
  166. $redis->client('SETNAME', $invalidConnectionName);
  167. }
  168. /**
  169. * @group connected
  170. * @requiresRedisVersion >= 2.4.0
  171. * @expectedException \Predis\Response\ServerException
  172. */
  173. public function testThrowsExceptioOnWrongModifier()
  174. {
  175. $redis = $this->getClient();
  176. $redis->client('FOO');
  177. }
  178. /**
  179. * @group connected
  180. * @requiresRedisVersion >= 2.4.0
  181. * @expectedException \Predis\Response\ServerException
  182. * @expectedExceptionMessage ERR No such client
  183. */
  184. public function testThrowsExceptionWhenKillingUnknownClient()
  185. {
  186. $redis = $this->getClient();
  187. $redis->client('KILL', '127.0.0.1:65535');
  188. }
  189. }