ServerClientTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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;
  11. /**
  12. * @group commands
  13. * @group realm-server
  14. */
  15. class ServerClientTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ServerClient';
  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. */
  104. public function testReturnsListOfConnectedClients()
  105. {
  106. $redis = $this->getClient();
  107. $this->assertInternalType('array', $clients = $redis->client('LIST'));
  108. $this->assertGreaterThanOrEqual(1, count($clients));
  109. $this->assertInternalType('array', $clients[0]);
  110. $this->assertArrayHasKey('addr', $clients[0]);
  111. $this->assertArrayHasKey('fd', $clients[0]);
  112. $this->assertArrayHasKey('idle', $clients[0]);
  113. $this->assertArrayHasKey('flags', $clients[0]);
  114. $this->assertArrayHasKey('db', $clients[0]);
  115. $this->assertArrayHasKey('sub', $clients[0]);
  116. $this->assertArrayHasKey('psub', $clients[0]);
  117. }
  118. /**
  119. * @group connected
  120. * @requiresRedisVersion >= 2.6.9
  121. */
  122. public function testGetsNameOfConnection()
  123. {
  124. $redis = $this->getClient();
  125. $clientName = $redis->client('GETNAME');
  126. $this->assertNull($clientName);
  127. $expectedConnectionName = 'foo-bar';
  128. $this->assertEquals('OK', $redis->client('SETNAME', $expectedConnectionName));
  129. $this->assertEquals($expectedConnectionName, $redis->client('GETNAME'));
  130. }
  131. /**
  132. * @group connected
  133. * @requiresRedisVersion >= 2.6.9
  134. */
  135. public function testSetsNameOfConnection()
  136. {
  137. $redis = $this->getClient();
  138. $expectedConnectionName = 'foo-baz';
  139. $this->assertEquals('OK', $redis->client('SETNAME', $expectedConnectionName));
  140. $this->assertEquals($expectedConnectionName, $redis->client('GETNAME'));
  141. }
  142. /**
  143. * @return array
  144. */
  145. public function invalidConnectionNameProvider()
  146. {
  147. return array(
  148. array('foo space'),
  149. array('foo \n'),
  150. array('foo $'),
  151. );
  152. }
  153. /**
  154. * @group connected
  155. * @requiresRedisVersion >= 2.6.9
  156. * @dataProvider invalidConnectionNameProvider
  157. *
  158. * @expectedException \Predis\Response\ServerException
  159. *
  160. * @param string $invalidConnectionName
  161. */
  162. public function testInvalidSetNameOfConnection($invalidConnectionName)
  163. {
  164. $redis = $this->getClient();
  165. $redis->client('SETNAME', $invalidConnectionName);
  166. }
  167. /**
  168. * @group connected
  169. * @expectedException \Predis\Response\ServerException
  170. */
  171. public function testThrowsExceptioOnWrongModifier()
  172. {
  173. $redis = $this->getClient();
  174. $redis->client('FOO');
  175. }
  176. /**
  177. * @group connected
  178. * @expectedException \Predis\Response\ServerException
  179. * @expectedExceptionMessage ERR No such client
  180. */
  181. public function testThrowsExceptionWhenKillingUnknownClient()
  182. {
  183. $redis = $this->getClient();
  184. $redis->client('KILL', '127.0.0.1:65535');
  185. }
  186. }