scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 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 Monolog\TestCase;
15 use Monolog\Logger;
16  
17 class DeduplicationHandlerTest extends TestCase
18 {
19 /**
20 * @covers Monolog\Handler\DeduplicationHandler::flush
21 */
22 public function testFlushPassthruIfAllRecordsUnderTrigger()
23 {
24 $test = new TestHandler();
25 @unlink(sys_get_temp_dir().'/monolog_dedup.log');
26 $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
27  
28 $handler->handle($this->getRecord(Logger::DEBUG));
29 $handler->handle($this->getRecord(Logger::INFO));
30  
31 $handler->flush();
32  
33 $this->assertTrue($test->hasInfoRecords());
34 $this->assertTrue($test->hasDebugRecords());
35 $this->assertFalse($test->hasWarningRecords());
36 }
37  
38 /**
39 * @covers Monolog\Handler\DeduplicationHandler::flush
40 * @covers Monolog\Handler\DeduplicationHandler::appendRecord
41 */
42 public function testFlushPassthruIfEmptyLog()
43 {
44 $test = new TestHandler();
45 @unlink(sys_get_temp_dir().'/monolog_dedup.log');
46 $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
47  
48 $handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
49 $handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
50  
51 $handler->flush();
52  
53 $this->assertTrue($test->hasErrorRecords());
54 $this->assertTrue($test->hasCriticalRecords());
55 $this->assertFalse($test->hasWarningRecords());
56 }
57  
58 /**
59 * @covers Monolog\Handler\DeduplicationHandler::flush
60 * @covers Monolog\Handler\DeduplicationHandler::appendRecord
61 * @covers Monolog\Handler\DeduplicationHandler::isDuplicate
62 * @depends testFlushPassthruIfEmptyLog
63 */
64 public function testFlushSkipsIfLogExists()
65 {
66 $test = new TestHandler();
67 $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
68  
69 $handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
70 $handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
71  
72 $handler->flush();
73  
74 $this->assertFalse($test->hasErrorRecords());
75 $this->assertFalse($test->hasCriticalRecords());
76 $this->assertFalse($test->hasWarningRecords());
77 }
78  
79 /**
80 * @covers Monolog\Handler\DeduplicationHandler::flush
81 * @covers Monolog\Handler\DeduplicationHandler::appendRecord
82 * @covers Monolog\Handler\DeduplicationHandler::isDuplicate
83 * @depends testFlushPassthruIfEmptyLog
84 */
85 public function testFlushPassthruIfLogTooOld()
86 {
87 $test = new TestHandler();
88 $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
89  
90 $record = $this->getRecord(Logger::ERROR);
91 $record['datetime']->modify('+62seconds');
92 $handler->handle($record);
93 $record = $this->getRecord(Logger::CRITICAL);
94 $record['datetime']->modify('+62seconds');
95 $handler->handle($record);
96  
97 $handler->flush();
98  
99 $this->assertTrue($test->hasErrorRecords());
100 $this->assertTrue($test->hasCriticalRecords());
101 $this->assertFalse($test->hasWarningRecords());
102 }
103  
104 /**
105 * @covers Monolog\Handler\DeduplicationHandler::flush
106 * @covers Monolog\Handler\DeduplicationHandler::appendRecord
107 * @covers Monolog\Handler\DeduplicationHandler::isDuplicate
108 * @covers Monolog\Handler\DeduplicationHandler::collectLogs
109 */
110 public function testGcOldLogs()
111 {
112 $test = new TestHandler();
113 @unlink(sys_get_temp_dir().'/monolog_dedup.log');
114 $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
115  
116 // handle two records from yesterday, and one recent
117 $record = $this->getRecord(Logger::ERROR);
118 $record['datetime']->modify('-1day -10seconds');
119 $handler->handle($record);
120 $record2 = $this->getRecord(Logger::CRITICAL);
121 $record2['datetime']->modify('-1day -10seconds');
122 $handler->handle($record2);
123 $record3 = $this->getRecord(Logger::CRITICAL);
124 $record3['datetime']->modify('-30seconds');
125 $handler->handle($record3);
126  
127 // log is written as none of them are duplicate
128 $handler->flush();
129 $this->assertSame(
130 $record['datetime']->getTimestamp() . ":ERROR:test\n" .
131 $record2['datetime']->getTimestamp() . ":CRITICAL:test\n" .
132 $record3['datetime']->getTimestamp() . ":CRITICAL:test\n",
133 file_get_contents(sys_get_temp_dir() . '/monolog_dedup.log')
134 );
135 $this->assertTrue($test->hasErrorRecords());
136 $this->assertTrue($test->hasCriticalRecords());
137 $this->assertFalse($test->hasWarningRecords());
138  
139 // clear test handler
140 $test->clear();
141 $this->assertFalse($test->hasErrorRecords());
142 $this->assertFalse($test->hasCriticalRecords());
143  
144 // log new records, duplicate log gets GC'd at the end of this flush call
145 $handler->handle($record = $this->getRecord(Logger::ERROR));
146 $handler->handle($record2 = $this->getRecord(Logger::CRITICAL));
147 $handler->flush();
148  
149 // log should now contain the new errors and the previous one that was recent enough
150 $this->assertSame(
151 $record3['datetime']->getTimestamp() . ":CRITICAL:test\n" .
152 $record['datetime']->getTimestamp() . ":ERROR:test\n" .
153 $record2['datetime']->getTimestamp() . ":CRITICAL:test\n",
154 file_get_contents(sys_get_temp_dir() . '/monolog_dedup.log')
155 );
156 $this->assertTrue($test->hasErrorRecords());
157 $this->assertTrue($test->hasCriticalRecords());
158 $this->assertFalse($test->hasWarningRecords());
159 }
160  
161 public static function tearDownAfterClass()
162 {
163 @unlink(sys_get_temp_dir().'/monolog_dedup.log');
164 }
165 }