AmqpHandlerTest.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /**
  14. * @covers Monolog\Handler\RotatingFileHandler
  15. */
  16. class AmqpHandlerTest extends TestCase
  17. {
  18. public function setUp()
  19. {
  20. if (!class_exists('AMQPConnection') || !class_exists('AMQPExchange')) {
  21. $this->markTestSkipped("amqp-php not installed");
  22. }
  23. if (!class_exists('AMQPChannel')) {
  24. $this->markTestSkipped("Please update AMQP to version >= 1.0");
  25. }
  26. }
  27. public function testHandle()
  28. {
  29. $exchange = $this->getExchange();
  30. $handler = new AmqpHandler($exchange, 'log');
  31. $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
  32. $expected = array(
  33. array(
  34. 'message' => 'test',
  35. 'context' => array(
  36. 'data' => array(),
  37. 'foo' => 34,
  38. ),
  39. 'level' => 300,
  40. 'level_name' => 'WARNING',
  41. 'channel' => 'test',
  42. 'extra' => array(),
  43. ),
  44. 'warn.test',
  45. 0,
  46. array(
  47. 'delivery_mode' => 2,
  48. 'Content-type' => 'application/json'
  49. )
  50. );
  51. $handler->handle($record);
  52. $messages = $exchange->getMessages();
  53. $this->assertCount(1, $messages);
  54. $messages[0][0] = json_decode($messages[0][0], true);
  55. unset($messages[0][0]['datetime']);
  56. $this->assertEquals($expected, $messages[0]);
  57. }
  58. protected function getExchange()
  59. {
  60. return new AmqpExchangeMock();
  61. }
  62. }