RedisHandlerTest.php 4.3 KB

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