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->prophesize('Predis\Client');
  37. $redis->rpush('key', 'test')->shouldBeCalled();
  38. $redis = $redis->reveal();
  39. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  40. $handler = new RedisHandler($redis, 'key');
  41. $handler->setFormatter(new LineFormatter("%message%"));
  42. $handler->handle($record);
  43. }
  44. public function testRedisHandle()
  45. {
  46. if (!class_exists('Redis')) {
  47. $this->markTestSkipped('The redis ext is required to run this test');
  48. }
  49. $redis = $this->createPartialMock('Redis', ['rPush']);
  50. // Redis uses rPush
  51. $redis->expects($this->once())
  52. ->method('rPush')
  53. ->with('key', 'test');
  54. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  55. $handler = new RedisHandler($redis, 'key');
  56. $handler->setFormatter(new LineFormatter("%message%"));
  57. $handler->handle($record);
  58. }
  59. public function testRedisHandleCapped()
  60. {
  61. if (!class_exists('Redis')) {
  62. $this->markTestSkipped('The redis ext is required to run this test');
  63. }
  64. $redis = $this->createPartialMock('Redis', ['multi', 'rPush', 'lTrim', 'exec']);
  65. // Redis uses multi
  66. $redis->expects($this->once())
  67. ->method('multi')
  68. ->will($this->returnSelf());
  69. $redis->expects($this->once())
  70. ->method('rPush')
  71. ->will($this->returnSelf());
  72. $redis->expects($this->once())
  73. ->method('lTrim')
  74. ->will($this->returnSelf());
  75. $redis->expects($this->once())
  76. ->method('exec')
  77. ->will($this->returnSelf());
  78. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  79. $handler = new RedisHandler($redis, 'key', Logger::DEBUG, true, 10);
  80. $handler->setFormatter(new LineFormatter("%message%"));
  81. $handler->handle($record);
  82. }
  83. public function testPredisHandleCapped()
  84. {
  85. $redis = $this->createPartialMock('Predis\Client', ['transaction']);
  86. $redisTransaction = $this->getMockBuilder('Predis\Client')
  87. ->disableOriginalConstructor()
  88. ->addMethods(['rPush', 'lTrim'])
  89. ->getMock();
  90. $redisTransaction->expects($this->once())
  91. ->method('rPush')
  92. ->will($this->returnSelf());
  93. $redisTransaction->expects($this->once())
  94. ->method('lTrim')
  95. ->will($this->returnSelf());
  96. // Redis uses multi
  97. $redis->expects($this->once())
  98. ->method('transaction')
  99. ->will($this->returnCallback(function ($cb) use ($redisTransaction) {
  100. $cb($redisTransaction);
  101. }));
  102. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  103. $handler = new RedisHandler($redis, 'key', Logger::DEBUG, true, 10);
  104. $handler->setFormatter(new LineFormatter("%message%"));
  105. $handler->handle($record);
  106. }
  107. }