2
0

AmqpHandlerTest.php 4.0 KB

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