AmqpHandlerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 PhpAmqpLib\Message\AMQPMessage;
  14. /**
  15. * @covers Monolog\Handler\RotatingFileHandler
  16. */
  17. class AmqpHandlerTest extends TestCase
  18. {
  19. public function testHandleAmqpExt()
  20. {
  21. if (!class_exists('AMQPConnection') || !class_exists('AMQPExchange')) {
  22. $this->markTestSkipped("amqp-php not installed");
  23. }
  24. if (!class_exists('AMQPChannel')) {
  25. $this->markTestSkipped("Please update AMQP to version >= 1.0");
  26. }
  27. $messages = [];
  28. $exchange = $this->getMockBuilder('AMQPExchange')
  29. ->onlyMethods(['publish', 'setName'])
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $exchange->expects($this->any())
  33. ->method('publish')
  34. ->willReturnCallback(function ($message, $routing_key, $flags = 0, $attributes = []) use (&$messages) {
  35. $messages[] = [$message, $routing_key, $flags, $attributes];
  36. })
  37. ;
  38. $handler = new AmqpHandler($exchange);
  39. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  40. $expected = [
  41. [
  42. 'message' => 'test',
  43. 'context' => [
  44. 'data' => [],
  45. 'foo' => 34,
  46. ],
  47. 'level' => 300,
  48. 'level_name' => 'WARNING',
  49. 'channel' => 'test',
  50. 'extra' => [],
  51. ],
  52. 'warning.test',
  53. 0,
  54. [
  55. 'delivery_mode' => 2,
  56. 'content_type' => 'application/json',
  57. ],
  58. ];
  59. $handler->handle($record);
  60. $this->assertCount(1, $messages);
  61. $messages[0][0] = json_decode($messages[0][0], true);
  62. unset($messages[0][0]['datetime']);
  63. $this->assertEquals($expected, $messages[0]);
  64. }
  65. public function testHandlePhpAmqpLib()
  66. {
  67. if (!class_exists('PhpAmqpLib\Channel\AMQPChannel')) {
  68. $this->markTestSkipped("php-amqplib not installed");
  69. }
  70. $messages = [];
  71. $methodsToMock = ['basic_publish'];
  72. if (method_exists('PhpAmqpLib\Channel\AMQPChannel', '__destruct')) {
  73. $methodsToMock[] = '__destruct';
  74. }
  75. $exchange = $this->getMockBuilder('PhpAmqpLib\Channel\AMQPChannel')
  76. ->onlyMethods($methodsToMock)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $exchange->expects($this->any())
  80. ->method('basic_publish')
  81. ->willReturnCallback(function (AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null) use (&$messages) {
  82. $messages[] = [$msg, $exchange, $routing_key, $mandatory, $immediate, $ticket];
  83. })
  84. ;
  85. $handler = new AmqpHandler($exchange, 'log');
  86. $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]);
  87. $expected = [
  88. [
  89. 'message' => 'test',
  90. 'context' => [
  91. 'data' => [],
  92. 'foo' => 34,
  93. ],
  94. 'level' => 300,
  95. 'level_name' => 'WARNING',
  96. 'channel' => 'test',
  97. 'extra' => [],
  98. ],
  99. 'log',
  100. 'warning.test',
  101. false,
  102. false,
  103. null,
  104. [
  105. 'delivery_mode' => 2,
  106. 'content_type' => 'application/json',
  107. ],
  108. ];
  109. $handler->handle($record);
  110. $this->assertCount(1, $messages);
  111. /* @var $msg AMQPMessage */
  112. $msg = $messages[0][0];
  113. $messages[0][0] = json_decode($msg->body, true);
  114. $messages[0][] = $msg->get_properties();
  115. unset($messages[0][0]['datetime']);
  116. $this->assertEquals($expected, $messages[0]);
  117. }
  118. }