3f660e291bfea1d9414a4c843c8fe906a5c4da54
[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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class LoggerTest extends UnitTestCase
28 {
29 /**
30 * @test
31 */
32 public function getNameGetsLoggerName()
33 {
34 $logger = new Logger('test.core.log');
35 $this->assertSame('test.core.log', $logger->getName());
36 }
37
38 /**
39 * @test
40 */
41 public function loggerDoesNotLogRecordsLessCriticalThanLogLevel()
42 {
43 $logger = new Logger('test.core.log');
44 $writer = new Fixtures\WriterFixture();
45 $logger->addWriter(LogLevel::ERROR, $writer);
46 // warning < error, thus must not be logged
47 $logger->log(LogLevel::WARNING, 'test message');
48 $this->assertAttributeEmpty('records', $writer);
49 }
50
51 /**
52 * @test
53 */
54 public function loggerReturnsItselfAfterLogging()
55 {
56 $logger = new Logger('test.core.log');
57 $writer = new Fixtures\WriterFixture();
58 $logger->addWriter(LogLevel::DEBUG, $writer);
59 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
60 $this->assertInstanceOf(Logger::class, $returnValue);
61 }
62
63 /**
64 * @test
65 */
66 public function loggerReturnsItselfAfterLoggingWithoutWriter()
67 {
68 $logger = new Logger('test.core.log');
69 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
70 $this->assertInstanceOf(Logger::class, $returnValue);
71 }
72
73 /**
74 * @test
75 */
76 public function loggerReturnsItselfAfterLoggingLessCritical()
77 {
78 $logger = new Logger('test.core.log');
79 $writer = new Fixtures\WriterFixture();
80 $logger->addWriter(LogLevel::EMERGENCY, $writer);
81 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
82 $this->assertInstanceOf(Logger::class, $returnValue);
83 }
84
85 /**
86 * @test
87 */
88 public function loggerCallsProcessor()
89 {
90 $component = 'test.core.log';
91 $level = LogLevel::DEBUG;
92 $message = 'test';
93 $logger = new Logger($component);
94 /** @var $processor \TYPO3\CMS\Core\Log\Processor\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject */
95 $processor = $this->getMockBuilder(NullProcessor::class)
96 ->setMethods(['processLogRecord'])
97 ->getMock();
98 $processor->expects($this->once())->method('processLogRecord')->will($this->returnValue(new LogRecord($component, $level, $message)));
99 $logger->addProcessor($level, $processor);
100 // we need a writer, otherwise we will not process log records
101 $logger->addWriter($level, new NullWriter());
102 $logger->warning($message);
103 }
104
105 /**
106 * @test
107 */
108 public function loggerLogsRecord()
109 {
110 $logger = new Logger('test.core.log');
111 /** @var NullWriter|\PHPUnit_Framework_MockObject_MockObject $writer */
112 $writer = $this->getMockBuilder(NullWriter::class)
113 ->setMethods(['writeLog'])
114 ->getMock();
115 $writer->expects($this->once())->method('writeLog');
116 $logger->addWriter(LogLevel::DEBUG, $writer);
117 $logger->warning('test');
118 }
119
120 /**
121 * @test
122 */
123 public function loggerLogsRecordsAtLeastAsCriticalAsLogLevel()
124 {
125 $logger = new Logger('test.core.log');
126 $writer = new Fixtures\WriterFixture();
127 $logger->addWriter(LogLevel::NOTICE, $writer);
128 // notice == notice, thus must be logged
129 $logger->log(LogLevel::NOTICE, 'test message');
130 $this->assertAttributeNotEmpty('records', $writer);
131 }
132
133 /**
134 * @return array
135 */
136 public function loggerLogsRecordsThroughShorthandMethodDataProvider()
137 {
138 return [
139 ['emergency'],
140 ['alert'],
141 ['critical'],
142 ['error'],
143 ['warning'],
144 ['notice'],
145 ['info'],
146 ['debug']
147 ];
148 }
149
150 /**
151 * @test
152 * @param string $shorthandMethod
153 * @dataProvider loggerLogsRecordsThroughShorthandMethodDataProvider
154 */
155 public function loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
156 {
157 $logger = new Logger('test.core.log');
158 $writer = new Fixtures\WriterFixture();
159 $logger->addWriter(LogLevel::DEBUG, $writer);
160 call_user_func([$logger, $shorthandMethod], 'test message');
161 $this->assertAttributeNotEmpty('records', $writer);
162 }
163
164 /**
165 * @test
166 */
167 public function loggerLogsRecordsMoreCriticalThanLogLevel()
168 {
169 $logger = new Logger('test.core.log');
170 $writer = new Fixtures\WriterFixture();
171 $logger->addWriter(LogLevel::NOTICE, $writer);
172 // warning > notice, thus must be logged
173 $logger->log(LogLevel::WARNING, 'test message');
174 $this->assertAttributeNotEmpty('records', $writer);
175 }
176
177 /**
178 * @test
179 */
180 public function addWriterAddsWriterToTheSpecifiedLevel()
181 {
182 $logger = new Logger('test.core.log');
183 $writer = new Fixtures\WriterFixture();
184 $logger->addWriter(LogLevel::NOTICE, $writer);
185 $writers = $logger->getWriters();
186 $this->assertContains($writer, $writers[LogLevel::NOTICE]);
187 }
188
189 /**
190 * @test
191 */
192 public function addWriterAddsWriterAlsoToHigherLevelsThanSpecified()
193 {
194 $logger = new Logger('test.core.log');
195 $writer = new Fixtures\WriterFixture();
196 $logger->addWriter(LogLevel::NOTICE, $writer);
197 $writers = $logger->getWriters();
198 $this->assertContains($writer, $writers[LogLevel::EMERGENCY]);
199 }
200 }