RedisPubSubHandlerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 testConstructorShouldThrowExceptionForInvalidRedis()
  18. {
  19. $this->expectException(\InvalidArgumentException::class);
  20. new RedisPubSubHandler(new \stdClass(), 'key');
  21. }
  22. public function testConstructorShouldWorkWithPredis()
  23. {
  24. $redis = $this->createMock('Predis\Client');
  25. $this->assertInstanceof('Monolog\Handler\RedisPubSubHandler', new RedisPubSubHandler($redis, 'key'));
  26. }
  27. public function testConstructorShouldWorkWithRedis()
  28. {
  29. if (!class_exists('Redis')) {
  30. $this->markTestSkipped('The redis ext is required to run this test');
  31. }
  32. $redis = $this->createMock('Redis');
  33. $this->assertInstanceof('Monolog\Handler\RedisPubSubHandler', new RedisPubSubHandler($redis, 'key'));
  34. }
  35. public function testPredisHandle()
  36. {
  37. $redis = $this->prophesize('Predis\Client');
  38. $redis->publish('key', 'test')->shouldBeCalled();
  39. $redis = $redis->reveal();
  40. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass(), 'foo' => 34]);
  41. $handler = new RedisPubSubHandler($redis, 'key');
  42. $handler->setFormatter(new LineFormatter("%message%"));
  43. $handler->handle($record);
  44. }
  45. public function testRedisHandle()
  46. {
  47. if (!class_exists('Redis')) {
  48. $this->markTestSkipped('The redis ext is required to run this test');
  49. }
  50. $redis = $this->createPartialMock('Redis', ['publish']);
  51. $redis->expects($this->once())
  52. ->method('publish')
  53. ->with('key', 'test');
  54. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass(), 'foo' => 34]);
  55. $handler = new RedisPubSubHandler($redis, 'key');
  56. $handler->setFormatter(new LineFormatter("%message%"));
  57. $handler->handle($record);
  58. }
  59. }