RedisHandlerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Handler;
  11. use Monolog\Level;
  12. use Monolog\Formatter\LineFormatter;
  13. class RedisHandlerTest extends \Monolog\Test\MonologTestCase
  14. {
  15. public function testConstructorShouldWorkWithPredis()
  16. {
  17. $redis = $this->createMock('Predis\Client');
  18. $this->assertInstanceof('Monolog\Handler\RedisHandler', new RedisHandler($redis, 'key'));
  19. }
  20. public function testConstructorShouldWorkWithRedis()
  21. {
  22. if (!class_exists('Redis')) {
  23. $this->markTestSkipped('The redis ext is required to run this test');
  24. }
  25. $redis = $this->createMock('Redis');
  26. $this->assertInstanceof('Monolog\Handler\RedisHandler', new RedisHandler($redis, 'key'));
  27. }
  28. public function testPredisHandle()
  29. {
  30. $redis = $this->getMockBuilder('Predis\Client')->getMock();
  31. $redis->expects($this->atLeastOnce())
  32. ->method('__call')
  33. ->with(self::equalTo('rpush'), self::equalTo(['key', 'test']));
  34. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  35. $handler = new RedisHandler($redis, 'key');
  36. $handler->setFormatter(new LineFormatter("%message%"));
  37. $handler->handle($record);
  38. }
  39. public function testRedisHandle()
  40. {
  41. if (!class_exists('Redis')) {
  42. $this->markTestSkipped('The redis ext is required to run this test');
  43. }
  44. $redis = $this->createPartialMock('Redis', ['rPush']);
  45. // Redis uses rPush
  46. $redis->expects($this->once())
  47. ->method('rPush')
  48. ->with('key', 'test');
  49. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  50. $handler = new RedisHandler($redis, 'key');
  51. $handler->setFormatter(new LineFormatter("%message%"));
  52. $handler->handle($record);
  53. }
  54. public function testRedisHandleCapped()
  55. {
  56. if (!class_exists('Redis')) {
  57. $this->markTestSkipped('The redis ext is required to run this test');
  58. }
  59. $redis = $this->createPartialMock('Redis', ['multi', 'rPush', 'lTrim', 'exec']);
  60. // Redis uses multi
  61. $redis->expects($this->once())
  62. ->method('multi')
  63. ->willReturnSelf();
  64. $redis->expects($this->once())
  65. ->method('rPush')
  66. ->willReturnSelf();
  67. $redis->expects($this->once())
  68. ->method('lTrim')
  69. ->willReturnSelf();
  70. $redis->expects($this->once())
  71. ->method('exec')
  72. ->willReturnSelf();
  73. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  74. $handler = new RedisHandler($redis, 'key', Level::Debug, true, 10);
  75. $handler->setFormatter(new LineFormatter("%message%"));
  76. $handler->handle($record);
  77. }
  78. public function testPredisHandleCapped()
  79. {
  80. $redis = new class extends \Predis\Client {
  81. public array $testResults = [];
  82. public function rpush(...$args)
  83. {
  84. $this->testResults[] = ['rpush', ...$args];
  85. return $this;
  86. }
  87. public function ltrim(...$args)
  88. {
  89. $this->testResults[] = ['ltrim', ...$args];
  90. return $this;
  91. }
  92. public function transaction(...$args)
  93. {
  94. $this->testResults[] = ['transaction start'];
  95. return ($args[0])($this);
  96. }
  97. };
  98. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  99. $handler = new RedisHandler($redis, 'key', Level::Debug, true, 10);
  100. $handler->setFormatter(new LineFormatter("%message%"));
  101. $handler->handle($record);
  102. self::assertsame([
  103. ['transaction start'],
  104. ['rpush', 'key', 'test'],
  105. ['ltrim', 'key', -10, -1],
  106. ], $redis->testResults);
  107. }
  108. }