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