PhpAmqpLibHandlerTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /**
  15. * @covers Monolog\Handler\RotatingFileHandler
  16. */
  17. class PhpAmqpLibHandlerTest extends TestCase
  18. {
  19. public function setUp()
  20. {
  21. if (!class_exists('PhpAmqpLib\Connection\AMQPConnection')) {
  22. $this->markTestSkipped("php-amqplib not installed");
  23. }
  24. }
  25. public function testHandle()
  26. {
  27. $messages = array();
  28. $exchange = $this->getMock('PhpAmqpLib\Channel\AMQPChannel', array('basic_publish', '__destruct'), array(), '', false);
  29. $exchange->expects($this->any())
  30. ->method('basic_publish')
  31. ->will($this->returnCallback(function (AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null) use (&$messages) {
  32. $messages[] = array($msg, $exchange, $routing_key, $mandatory, $immediate, $ticket);
  33. }))
  34. ;
  35. $handler = new PhpAmqpLibHandler($exchange, 'log');
  36. $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
  37. $expected = array(
  38. array(
  39. 'message' => 'test',
  40. 'context' => array(
  41. 'data' => array(),
  42. 'foo' => 34,
  43. ),
  44. 'level' => 300,
  45. 'level_name' => 'WARNING',
  46. 'channel' => 'test',
  47. 'extra' => array(),
  48. ),
  49. 'log',
  50. 'warn.test',
  51. false,
  52. false,
  53. null,
  54. array(
  55. 'delivery_mode' => 2,
  56. 'content_type' => 'application/json'
  57. )
  58. );
  59. $handler->handle($record);
  60. $this->assertCount(1, $messages);
  61. /* @var $msg AMQPMessage */
  62. $msg = $messages[0][0];
  63. $messages[0][0] = json_decode($msg->body, true);
  64. $messages[0][] = $msg->get_properties();
  65. unset($messages[0][0]['datetime']);
  66. $this->assertEquals($expected, $messages[0]);
  67. }
  68. }