ServerClientTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. * @group commands
  14. * @group realm-server
  15. */
  16. class ServerClientTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Command\ServerClient';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function getExpectedId()
  29. {
  30. return 'CLIENT';
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testFilterArgumentsOfClientKill()
  36. {
  37. $arguments = array('kill', '127.0.0.1:45393');
  38. $expected = array('kill', '127.0.0.1:45393');
  39. $command = $this->getCommand();
  40. $command->setArguments($arguments);
  41. $this->assertSame($expected, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testFilterArgumentsOfClientList()
  47. {
  48. $arguments = array('list');
  49. $expected = array('list');
  50. $command = $this->getCommand();
  51. $command->setArguments($arguments);
  52. $this->assertSame($expected, $command->getArguments());
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testFilterArgumentsOfClientGetname()
  58. {
  59. $arguments = $expected = array('getname');
  60. $command = $this->getCommand();
  61. $command->setArguments($arguments);
  62. $this->assertSame($expected, $command->getArguments());
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testFilterArgumentsOfClientSetname()
  68. {
  69. $arguments = $expected = array('setname', 'connection-a');
  70. $command = $this->getCommand();
  71. $command->setArguments($arguments);
  72. $this->assertSame($expected, $command->getArguments());
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testParseResponseOfClientKill()
  78. {
  79. $command = $this->getCommand();
  80. $command->setArguments(array('kill'));
  81. $this->assertSame(true, $command->parseResponse(true));
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testParseResponseOfClientList()
  87. {
  88. $command = $this->getCommand();
  89. $command->setArguments(array('list'));
  90. $raw =<<<BUFFER
  91. addr=127.0.0.1:45393 fd=6 idle=0 flags=N db=0 sub=0 psub=0
  92. addr=127.0.0.1:45394 fd=7 idle=0 flags=N db=0 sub=0 psub=0
  93. addr=127.0.0.1:45395 fd=8 idle=0 flags=N db=0 sub=0 psub=0
  94. BUFFER;
  95. $parsed = array (
  96. array('addr'=>'127.0.0.1:45393','fd'=>'6','idle'=>'0','flags'=>'N','db'=>'0','sub'=>'0','psub'=>'0'),
  97. array('addr'=>'127.0.0.1:45394','fd'=>'7','idle'=>'0','flags'=>'N','db'=>'0','sub'=>'0','psub'=>'0'),
  98. array('addr'=>'127.0.0.1:45395','fd'=>'8','idle'=>'0','flags'=>'N','db'=>'0','sub'=>'0','psub'=>'0'),
  99. );
  100. $this->assertSame($parsed, $command->parseResponse($raw));
  101. }
  102. /**
  103. * @group connected
  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. */
  122. public function testGetsNameOfConnection()
  123. {
  124. $this->markTestSkippedOnRedisVersionBelow('2.6.9');
  125. $redis = $this->getClient();
  126. $clientName = $redis->client('GETNAME');
  127. $this->assertNull($clientName);
  128. $expectedConnectionName = 'foo-bar';
  129. $this->assertTrue($redis->client('SETNAME', $expectedConnectionName));
  130. $this->assertEquals($expectedConnectionName, $redis->client('GETNAME'));
  131. }
  132. /**
  133. * @group connected
  134. */
  135. public function testSetsNameOfConnection()
  136. {
  137. $this->markTestSkippedOnRedisVersionBelow('2.6.9');
  138. $redis = $this->getClient();
  139. $expectedConnectionName = 'foo-baz';
  140. $this->assertTrue($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. * @expectedException Predis\ServerException
  157. * @dataProvider invalidConnectionNameProvider
  158. */
  159. public function testInvalidSetNameOfConnection($invalidConnectionName)
  160. {
  161. $this->markTestSkippedOnRedisVersionBelow('2.6.9');
  162. $redis = $this->getClient();
  163. $redis->client('SETNAME', $invalidConnectionName);
  164. }
  165. /**
  166. * @group connected
  167. * @expectedException Predis\ServerException
  168. */
  169. public function testThrowsExceptioOnWrongModifier()
  170. {
  171. $redis = $this->getClient();
  172. $this->assertTrue($redis->client('FOO'));
  173. }
  174. /**
  175. * @group connected
  176. * @expectedException Predis\ServerException
  177. * @expectedExceptionMessage ERR No such client
  178. */
  179. public function testThrowsExceptionWhenKillingUnknownClient()
  180. {
  181. $redis = $this->getClient();
  182. $this->assertTrue($redis->client('KILL', '127.0.0.1:65535'));
  183. }
  184. }