RedisHandlerTest.php 4.1 KB

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