AmqpHandlerTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. $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. }