RedisHandlerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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->prophesize('Predis\Client');
  32. $redis->rpush('key', 'test')->shouldBeCalled();
  33. $redis = $redis->reveal();
  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. ->will($this->returnSelf());
  64. $redis->expects($this->once())
  65. ->method('rPush')
  66. ->will($this->returnSelf());
  67. $redis->expects($this->once())
  68. ->method('lTrim')
  69. ->will($this->returnSelf());
  70. $redis->expects($this->once())
  71. ->method('exec')
  72. ->will($this->returnSelf());
  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 = $this->createPartialMock('Predis\Client', ['transaction']);
  81. $redisTransaction = $this->getMockBuilder('Predis\Client')
  82. ->disableOriginalConstructor()
  83. ->addMethods(['rPush', 'lTrim'])
  84. ->getMock();
  85. $redisTransaction->expects($this->once())
  86. ->method('rPush')
  87. ->will($this->returnSelf());
  88. $redisTransaction->expects($this->once())
  89. ->method('lTrim')
  90. ->will($this->returnSelf());
  91. // Redis uses multi
  92. $redis->expects($this->once())
  93. ->method('transaction')
  94. ->will($this->returnCallback(function ($cb) use ($redisTransaction) {
  95. $cb($redisTransaction);
  96. }));
  97. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  98. $handler = new RedisHandler($redis, 'key', Level::Debug, true, 10);
  99. $handler->setFormatter(new LineFormatter("%message%"));
  100. $handler->handle($record);
  101. }
  102. }