RedisHandlerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  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->getMock('Predis\Client');
  26. $this->assertInstanceof('Monolog\Handler\RedisHandler', new RedisHandler($redis, 'key'));
  27. }
  28. public function testConstructorShouldWorkWithRedis()
  29. {
  30. $redis = $this->getMock('Redis');
  31. $this->assertInstanceof('Monolog\Handler\RedisHandler', new RedisHandler($redis, 'key'));
  32. }
  33. public function testPredisHandle()
  34. {
  35. $redis = $this->getMock('Predis\Client', ['rpush']);
  36. // Predis\Client uses rpush
  37. $redis->expects($this->once())
  38. ->method('rpush')
  39. ->with('key', 'test');
  40. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  41. $handler = new RedisHandler($redis, 'key');
  42. $handler->setFormatter(new LineFormatter("%message%"));
  43. $handler->handle($record);
  44. }
  45. public function testRedisHandle()
  46. {
  47. $redis = $this->getMock('Redis', ['rpush']);
  48. // Redis uses rPush
  49. $redis->expects($this->once())
  50. ->method('rPush')
  51. ->with('key', 'test');
  52. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  53. $handler = new RedisHandler($redis, 'key');
  54. $handler->setFormatter(new LineFormatter("%message%"));
  55. $handler->handle($record);
  56. }
  57. public function testRedisHandleCapped()
  58. {
  59. $redis = $this->getMock('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(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  74. $handler = new RedisHandler($redis, 'key', Logger::DEBUG, true, 10);
  75. $handler->setFormatter(new LineFormatter("%message%"));
  76. $handler->handle($record);
  77. }
  78. public function testPredisHandleCapped()
  79. {
  80. $redis = $this->getMock('Predis\Client', ['transaction']);
  81. $redisTransaction = $this->getMock('Predis\Client', ['rpush', 'ltrim']);
  82. $redisTransaction->expects($this->once())
  83. ->method('rpush')
  84. ->will($this->returnSelf());
  85. $redisTransaction->expects($this->once())
  86. ->method('ltrim')
  87. ->will($this->returnSelf());
  88. // Redis uses multi
  89. $redis->expects($this->once())
  90. ->method('transaction')
  91. ->will($this->returnCallback(function ($cb) use ($redisTransaction) {
  92. $cb($redisTransaction);
  93. }));
  94. $record = $this->getRecord(Logger::WARNING, 'test', ['data' => new \stdClass, 'foo' => 34]);
  95. $handler = new RedisHandler($redis, 'key', Logger::DEBUG, true, 10);
  96. $handler->setFormatter(new LineFormatter("%message%"));
  97. $handler->handle($record);
  98. }
  99. }