RedisHandlerTest.php 4.3 KB

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