scratch – Blame information for rev 120

Subversion Repositories:
Rev:
Rev Author Line No. Line
120 office 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  
14 use Gelf\Message;
15 use Monolog\TestCase;
16 use Monolog\Logger;
17 use Monolog\Formatter\GelfMessageFormatter;
18  
19 class GelfHandlerTest extends TestCase
20 {
21 public function setUp()
22 {
23 if (!class_exists('Gelf\Publisher') || !class_exists('Gelf\Message')) {
24 $this->markTestSkipped("graylog2/gelf-php not installed");
25 }
26 }
27  
28 /**
29 * @covers Monolog\Handler\GelfHandler::__construct
30 */
31 public function testConstruct()
32 {
33 $handler = new GelfHandler($this->getMessagePublisher());
34 $this->assertInstanceOf('Monolog\Handler\GelfHandler', $handler);
35 }
36  
37 protected function getHandler($messagePublisher)
38 {
39 $handler = new GelfHandler($messagePublisher);
40  
41 return $handler;
42 }
43  
44 protected function getMessagePublisher()
45 {
46 return $this->getMock('Gelf\Publisher', array('publish'), array(), '', false);
47 }
48  
49 public function testDebug()
50 {
51 $record = $this->getRecord(Logger::DEBUG, "A test debug message");
52 $expectedMessage = new Message();
53 $expectedMessage
54 ->setLevel(7)
55 ->setFacility("test")
56 ->setShortMessage($record['message'])
57 ->setTimestamp($record['datetime'])
58 ;
59  
60 $messagePublisher = $this->getMessagePublisher();
61 $messagePublisher->expects($this->once())
62 ->method('publish')
63 ->with($expectedMessage);
64  
65 $handler = $this->getHandler($messagePublisher);
66  
67 $handler->handle($record);
68 }
69  
70 public function testWarning()
71 {
72 $record = $this->getRecord(Logger::WARNING, "A test warning message");
73 $expectedMessage = new Message();
74 $expectedMessage
75 ->setLevel(4)
76 ->setFacility("test")
77 ->setShortMessage($record['message'])
78 ->setTimestamp($record['datetime'])
79 ;
80  
81 $messagePublisher = $this->getMessagePublisher();
82 $messagePublisher->expects($this->once())
83 ->method('publish')
84 ->with($expectedMessage);
85  
86 $handler = $this->getHandler($messagePublisher);
87  
88 $handler->handle($record);
89 }
90  
91 public function testInjectedGelfMessageFormatter()
92 {
93 $record = $this->getRecord(Logger::WARNING, "A test warning message");
94 $record['extra']['blarg'] = 'yep';
95 $record['context']['from'] = 'logger';
96  
97 $expectedMessage = new Message();
98 $expectedMessage
99 ->setLevel(4)
100 ->setFacility("test")
101 ->setHost("mysystem")
102 ->setShortMessage($record['message'])
103 ->setTimestamp($record['datetime'])
104 ->setAdditional("EXTblarg", 'yep')
105 ->setAdditional("CTXfrom", 'logger')
106 ;
107  
108 $messagePublisher = $this->getMessagePublisher();
109 $messagePublisher->expects($this->once())
110 ->method('publish')
111 ->with($expectedMessage);
112  
113 $handler = $this->getHandler($messagePublisher);
114 $handler->setFormatter(new GelfMessageFormatter('mysystem', 'EXT', 'CTX'));
115 $handler->handle($record);
116 }
117 }