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