8e9796255869700630e067e097fc3fc8e45797fe
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / LoggerTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Log;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Log\Logger;
18 use TYPO3\CMS\Core\Log\LogLevel;
19 use TYPO3\CMS\Core\Log\LogRecord;
20 use TYPO3\CMS\Core\Log\Processor\NullProcessor;
21 use TYPO3\CMS\Core\Log\Writer\NullWriter;
22
23 /**
24 * Test case
25 */
26 class LoggerTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * @test
30 */
31 public function getNameGetsLoggerName()
32 {
33 $logger = new Logger('test.core.log');
34 $this->assertSame('test.core.log', $logger->getName());
35 }
36
37 /**
38 * @test
39 */
40 public function loggerDoesNotLogRecordsLessCriticalThanLogLevel()
41 {
42 $logger = new Logger('test.core.log');
43 $writer = new Fixtures\WriterFixture();
44 $logger->addWriter(LogLevel::ERROR, $writer);
45 // warning < error, thus must not be logged
46 $logger->log(LogLevel::WARNING, 'test message');
47 $this->assertAttributeEmpty('records', $writer);
48 }
49
50 /**
51 * @test
52 */
53 public function loggerReturnsItselfAfterLogging()
54 {
55 $logger = new Logger('test.core.log');
56 $writer = new Fixtures\WriterFixture();
57 $logger->addWriter(LogLevel::DEBUG, $writer);
58 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
59 $this->assertInstanceOf(Logger::class, $returnValue);
60 }
61
62 /**
63 * @test
64 */
65 public function loggerReturnsItselfAfterLoggingWithoutWriter()
66 {
67 $logger = new Logger('test.core.log');
68 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
69 $this->assertInstanceOf(Logger::class, $returnValue);
70 }
71
72 /**
73 * @test
74 */
75 public function loggerReturnsItselfAfterLoggingLessCritical()
76 {
77 $logger = new Logger('test.core.log');
78 $writer = new Fixtures\WriterFixture();
79 $logger->addWriter(LogLevel::EMERGENCY, $writer);
80 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
81 $this->assertInstanceOf(Logger::class, $returnValue);
82 }
83
84 /**
85 * @test
86 */
87 public function loggerCallsProcessor()
88 {
89 $component = 'test.core.log';
90 $level = LogLevel::DEBUG;
91 $message = 'test';
92 $logger = new Logger($component);
93 /** @var $processor \TYPO3\CMS\Core\Log\Processor\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject */
94 $processor = $this->getMockBuilder(NullProcessor::class)
95 ->setMethods(['processLogRecord'])
96 ->getMock();
97 $processor->expects($this->once())->method('processLogRecord')->will($this->returnValue(new LogRecord($component, $level, $message)));
98 $logger->addProcessor($level, $processor);
99 // we need a writer, otherwise we will not process log records
100 $logger->addWriter($level, new NullWriter());
101 $logger->warning($message);
102 }
103
104 /**
105 * @test
106 */
107 public function loggerLogsRecord()
108 {
109 $logger = new Logger('test.core.log');
110 /** @var NullWriter|\PHPUnit_Framework_MockObject_MockObject $writer */
111 $writer = $this->getMockBuilder(NullWriter::class)
112 ->setMethods(['writeLog'])
113 ->getMock();
114 $writer->expects($this->once())->method('writeLog');
115 $logger->addWriter(LogLevel::DEBUG, $writer);
116 $logger->warning('test');
117 }
118
119 /**
120 * @test
121 */
122 public function loggerLogsRecordsAtLeastAsCriticalAsLogLevel()
123 {
124 $logger = new Logger('test.core.log');
125 $writer = new Fixtures\WriterFixture();
126 $logger->addWriter(LogLevel::NOTICE, $writer);
127 // notice == notice, thus must be logged
128 $logger->log(LogLevel::NOTICE, 'test message');
129 $this->assertAttributeNotEmpty('records', $writer);
130 }
131
132 /**
133 * @return array
134 */
135 public function loggerLogsRecordsThroughShorthandMethodDataProvider()
136 {
137 return [
138 ['emergency'],
139 ['alert'],
140 ['critical'],
141 ['error'],
142 ['warning'],
143 ['notice'],
144 ['info'],
145 ['debug']
146 ];
147 }
148
149 /**
150 * @test
151 * @param string $shorthandMethod
152 * @dataProvider loggerLogsRecordsThroughShorthandMethodDataProvider
153 */
154 public function loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
155 {
156 $logger = new Logger('test.core.log');
157 $writer = new Fixtures\WriterFixture();
158 $logger->addWriter(LogLevel::DEBUG, $writer);
159 call_user_func([$logger, $shorthandMethod], 'test message');
160 $this->assertAttributeNotEmpty('records', $writer);
161 }
162
163 /**
164 * @test
165 */
166 public function loggerLogsRecordsMoreCriticalThanLogLevel()
167 {
168 $logger = new Logger('test.core.log');
169 $writer = new Fixtures\WriterFixture();
170 $logger->addWriter(LogLevel::NOTICE, $writer);
171 // warning > notice, thus must be logged
172 $logger->log(LogLevel::WARNING, 'test message');
173 $this->assertAttributeNotEmpty('records', $writer);
174 }
175
176 /**
177 * @test
178 */
179 public function addWriterAddsWriterToTheSpecifiedLevel()
180 {
181 $logger = new Logger('test.core.log');
182 $writer = new Fixtures\WriterFixture();
183 $logger->addWriter(LogLevel::NOTICE, $writer);
184 $writers = $logger->getWriters();
185 $this->assertContains($writer, $writers[LogLevel::NOTICE]);
186 }
187
188 /**
189 * @test
190 */
191 public function addWriterAddsWriterAlsoToHigherLevelsThanSpecified()
192 {
193 $logger = new Logger('test.core.log');
194 $writer = new Fixtures\WriterFixture();
195 $logger->addWriter(LogLevel::NOTICE, $writer);
196 $writers = $logger->getWriters();
197 $this->assertContains($writer, $writers[LogLevel::EMERGENCY]);
198 }
199 }