AmqpHandlerTest.php 4.1 KB

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