SLOWLOG_Test.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * In order to support the output of SLOWLOG, the backend connection must be
  13. * able to parse nested multibulk responses deeper than 2 levels.
  14. *
  15. * @group commands
  16. * @group realm-server
  17. */
  18. class SLOWLOG_Test extends PredisCommandTestCase
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function getExpectedCommand()
  24. {
  25. return 'Predis\Command\Redis\SLOWLOG';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function getExpectedId()
  31. {
  32. return 'SLOWLOG';
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testFilterArguments()
  38. {
  39. $arguments = array('GET', '2');
  40. $expected = array('GET', '2');
  41. $command = $this->getCommand();
  42. $command->setArguments($arguments);
  43. $this->assertSame($expected, $command->getArguments());
  44. }
  45. /**
  46. * This is the response type for SLOWLOG GET.
  47. *
  48. * @group disconnected
  49. */
  50. public function testParseResponse()
  51. {
  52. $raw = array(array(0, 1323163469, 12451, array('SORT', 'list:unordered')));
  53. $expected = array(
  54. array(
  55. 'id' => 0,
  56. 'timestamp' => 1323163469,
  57. 'duration' => 12451,
  58. 'command' => array('SORT', 'list:unordered'),
  59. ),
  60. );
  61. $command = $this->getCommand();
  62. $this->assertSame($expected, $command->parseResponse($raw));
  63. }
  64. /**
  65. * This is the response type for SLOWLOG LEN.
  66. *
  67. * @group disconnected
  68. */
  69. public function testParseResponseInteger()
  70. {
  71. $command = $this->getCommand();
  72. $this->assertSame(10, $command->parseResponse(10));
  73. }
  74. /**
  75. * @group connected
  76. * @requiresRedisVersion >= 2.2.12
  77. */
  78. public function testReturnsAnArrayOfLoggedCommands()
  79. {
  80. $redis = $this->getClient();
  81. $config = $redis->config('get', 'slowlog-log-slower-than');
  82. $threshold = array_pop($config);
  83. $redis->config('set', 'slowlog-log-slower-than', 0);
  84. $redis->set('foo', 'bar');
  85. $this->assertInternalType('array', $slowlog = $redis->slowlog('GET'));
  86. $this->assertGreaterThan(0, count($slowlog));
  87. $this->assertInternalType('array', $slowlog[0]);
  88. $this->assertGreaterThan(0, $slowlog[0]['id']);
  89. $this->assertGreaterThan(0, $slowlog[0]['timestamp']);
  90. $this->assertGreaterThan(0, $slowlog[0]['duration']);
  91. $this->assertInternalType('array', $slowlog[0]['command']);
  92. $redis->config('set', 'slowlog-log-slower-than', $threshold);
  93. }
  94. /**
  95. * @group connected
  96. * @requiresRedisVersion >= 2.2.12
  97. */
  98. public function testCanResetTheLog()
  99. {
  100. $redis = $this->getClient();
  101. $this->assertEquals('OK', $redis->slowlog('RESET'));
  102. }
  103. /**
  104. * @group connected
  105. * @requiresRedisVersion >= 2.2.12
  106. * @expectedException \Predis\Response\ServerException
  107. */
  108. public function testThrowsExceptionOnInvalidSubcommand()
  109. {
  110. $redis = $this->getClient();
  111. $redis->slowlog('INVALID');
  112. }
  113. }