RedisPubSubHandlerTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Monolog package.
  5. *
  6. * (c) Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Monolog\Handler;
  12. use Monolog\Test\TestCase;
  13. use Monolog\Level;
  14. use Monolog\Formatter\LineFormatter;
  15. class RedisPubSubHandlerTest extends TestCase
  16. {
  17. public function testConstructorShouldWorkWithPredis()
  18. {
  19. $redis = $this->createMock('Predis\Client');
  20. $this->assertInstanceof('Monolog\Handler\RedisPubSubHandler', new RedisPubSubHandler($redis, 'key'));
  21. }
  22. public function testConstructorShouldWorkWithRedis()
  23. {
  24. if (!class_exists('Redis')) {
  25. $this->markTestSkipped('The redis ext is required to run this test');
  26. }
  27. $redis = $this->createMock('Redis');
  28. $this->assertInstanceof('Monolog\Handler\RedisPubSubHandler', new RedisPubSubHandler($redis, 'key'));
  29. }
  30. public function testPredisHandle()
  31. {
  32. $redis = $this->getMockBuilder('Predis\Client')->getMock();
  33. $redis->expects($this->atLeastOnce())
  34. ->method('__call')
  35. ->with(self::equalTo('publish'), self::equalTo(['key', 'test']));
  36. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass(), 'foo' => 34]);
  37. $handler = new RedisPubSubHandler($redis, 'key');
  38. $handler->setFormatter(new LineFormatter("%message%"));
  39. $handler->handle($record);
  40. }
  41. public function testRedisHandle()
  42. {
  43. if (!class_exists('Redis')) {
  44. $this->markTestSkipped('The redis ext is required to run this test');
  45. }
  46. $redis = $this->createPartialMock('Redis', ['publish']);
  47. $redis->expects($this->once())
  48. ->method('publish')
  49. ->with('key', 'test');
  50. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass(), 'foo' => 34]);
  51. $handler = new RedisPubSubHandler($redis, 'key');
  52. $handler->setFormatter(new LineFormatter("%message%"));
  53. $handler->handle($record);
  54. }
  55. }