ServerClientTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Commands;
  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\Commands\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 testParseResponseOfClientKill()
  58. {
  59. $command = $this->getCommand();
  60. $command->setArguments(array('kill'));
  61. $this->assertSame(true, $command->parseResponse(true));
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testParseResponseOfClientList()
  67. {
  68. $command = $this->getCommand();
  69. $command->setArguments(array('list'));
  70. $raw =<<<BUFFER
  71. addr=127.0.0.1:45393 fd=6 idle=0 flags=N db=0 sub=0 psub=0
  72. addr=127.0.0.1:45394 fd=7 idle=0 flags=N db=0 sub=0 psub=0
  73. addr=127.0.0.1:45395 fd=8 idle=0 flags=N db=0 sub=0 psub=0
  74. BUFFER;
  75. $parsed = array (
  76. array('addr'=>'127.0.0.1:45393','fd'=>'6','idle'=>'0','flags'=>'N','db'=>'0','sub'=>'0','psub'=>'0'),
  77. array('addr'=>'127.0.0.1:45394','fd'=>'7','idle'=>'0','flags'=>'N','db'=>'0','sub'=>'0','psub'=>'0'),
  78. array('addr'=>'127.0.0.1:45395','fd'=>'8','idle'=>'0','flags'=>'N','db'=>'0','sub'=>'0','psub'=>'0'),
  79. );
  80. $this->assertSame($parsed, $command->parseResponse($raw));
  81. }
  82. /**
  83. * @group connected
  84. */
  85. public function testReturnsListOfConnectedClients()
  86. {
  87. $redis = $this->getClient();
  88. $this->assertInternalType('array', $clients = $redis->client('LIST'));
  89. $this->assertGreaterThanOrEqual(1, count($clients));
  90. $this->assertInternalType('array', $clients[0]);
  91. $this->assertArrayHasKey('addr', $clients[0]);
  92. $this->assertArrayHasKey('fd', $clients[0]);
  93. $this->assertArrayHasKey('idle', $clients[0]);
  94. $this->assertArrayHasKey('flags', $clients[0]);
  95. $this->assertArrayHasKey('db', $clients[0]);
  96. $this->assertArrayHasKey('sub', $clients[0]);
  97. $this->assertArrayHasKey('psub', $clients[0]);
  98. }
  99. /**
  100. * @group connected
  101. * @expectedException Predis\ServerException
  102. */
  103. public function testThrowsExceptioOnWrongModifier()
  104. {
  105. $redis = $this->getClient();
  106. $this->assertTrue($redis->client('FOO'));
  107. }
  108. /**
  109. * @group connected
  110. * @expectedException Predis\ServerException
  111. * @expectedExceptionMessage ERR No such client
  112. */
  113. public function testThrowsExceptionWhenKillingUnknownClient()
  114. {
  115. $redis = $this->getClient();
  116. $this->assertTrue($redis->client('KILL', '127.0.0.1:65535'));
  117. }
  118. }