ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
AmqpHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
15 use Monolog\Logger;
18 
23 {
24  public function testHandleAmqpExt()
25  {
26  if (!class_exists('AMQPConnection') || !class_exists('AMQPExchange')) {
27  $this->markTestSkipped("amqp-php not installed");
28  }
29 
30  if (!class_exists('AMQPChannel')) {
31  $this->markTestSkipped("Please update AMQP to version >= 1.0");
32  }
33 
34  $messages = array();
35 
36  $exchange = $this->getMock('AMQPExchange', array('publish', 'setName'), array(), '', false);
37  $exchange->expects($this->once())
38  ->method('setName')
39  ->with('log')
40  ;
41  $exchange->expects($this->any())
42  ->method('publish')
43  ->will($this->returnCallback(function ($message, $routing_key, $flags = 0, $attributes = array()) use (&$messages) {
44  $messages[] = array($message, $routing_key, $flags, $attributes);
45  }))
46  ;
47 
48  $handler = new AmqpHandler($exchange, 'log');
49 
50  $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
51 
52  $expected = array(
53  array(
54  'message' => 'test',
55  'context' => array(
56  'data' => array(),
57  'foo' => 34,
58  ),
59  'level' => 300,
60  'level_name' => 'WARNING',
61  'channel' => 'test',
62  'extra' => array(),
63  ),
64  'warn.test',
65  0,
66  array(
67  'delivery_mode' => 2,
68  'content_type' => 'application/json',
69  ),
70  );
71 
72  $handler->handle($record);
73 
74  $this->assertCount(1, $messages);
75  $messages[0][0] = json_decode($messages[0][0], true);
76  unset($messages[0][0]['datetime']);
77  $this->assertEquals($expected, $messages[0]);
78  }
79 
80  public function testHandlePhpAmqpLib()
81  {
82  if (!class_exists('PhpAmqpLib\Connection\AMQPConnection')) {
83  $this->markTestSkipped("php-amqplib not installed");
84  }
85 
86  $messages = array();
87 
88  $exchange = $this->getMock('PhpAmqpLib\Channel\AMQPChannel', array('basic_publish', '__destruct'), array(), '', false);
89 
90  $exchange->expects($this->any())
91  ->method('basic_publish')
92  ->will($this->returnCallback(function (AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null) use (&$messages) {
93  $messages[] = array($msg, $exchange, $routing_key, $mandatory, $immediate, $ticket);
94  }))
95  ;
96 
97  $handler = new AmqpHandler($exchange, 'log');
98 
99  $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
100 
101  $expected = array(
102  array(
103  'message' => 'test',
104  'context' => array(
105  'data' => array(),
106  'foo' => 34,
107  ),
108  'level' => 300,
109  'level_name' => 'WARNING',
110  'channel' => 'test',
111  'extra' => array(),
112  ),
113  'log',
114  'warn.test',
115  false,
116  false,
117  null,
118  array(
119  'delivery_mode' => 2,
120  'content_type' => 'application/json',
121  ),
122  );
123 
124  $handler->handle($record);
125 
126  $this->assertCount(1, $messages);
127 
128  /* @var $msg AMQPMessage */
129  $msg = $messages[0][0];
130  $messages[0][0] = json_decode($msg->body, true);
131  $messages[0][] = $msg->get_properties();
132  unset($messages[0][0]['datetime']);
133 
134  $this->assertEquals($expected, $messages[0]);
135  }
136 }
$attributes
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
catch(Exception $e) $message
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
Create styles array
The data for the language used.
$messages
Definition: en-x-test.php:7
$handler