[TASK] Streamline exceptions types in Logging API
[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 * @author Ingo Renner <ingo@typo3.org>
27 * @author Steffen Gebert <steffen.gebert@typo3.org>
28 */
29 class LoggerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
30
31 /**
32 * @test
33 */
34 public function getNameGetsLoggerName() {
35 $logger = new Logger('test.core.log');
36 $this->assertSame('test.core.log', $logger->getName());
37 }
38
39 /**
40 * @test
41 */
42 public function loggerDoesNotLogRecordsLessCriticalThanLogLevel() {
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 $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 $logger = new Logger('test.core.log');
67 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
68 $this->assertInstanceOf(Logger::class, $returnValue);
69 }
70
71 /**
72 * @test
73 */
74 public function loggerReturnsItselfAfterLoggingLessCritical() {
75 $logger = new Logger('test.core.log');
76 $writer = new Fixtures\WriterFixture();
77 $logger->addWriter(LogLevel::EMERGENCY, $writer);
78 $returnValue = $logger->log(LogLevel::WARNING, 'test message');
79 $this->assertInstanceOf(Logger::class, $returnValue);
80 }
81
82 /**
83 * @test
84 */
85 public function loggerCallsProcessor() {
86 $component = 'test.core.log';
87 $level = LogLevel::DEBUG;
88 $message = 'test';
89 $logger = new Logger($component);
90 /** @var $processor \TYPO3\CMS\Core\Log\Processor\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject */
91 $processor = $this->getMock(NullProcessor::class, array('processLogRecord'));
92 $processor->expects($this->once())->method('processLogRecord')->will($this->returnValue(new LogRecord($component, $level, $message)));
93 $logger->addProcessor($level, $processor);
94 // we need a writer, otherwise we will not process log records
95 $logger->addWriter($level, new NullWriter());
96 $logger->warning($message);
97 }
98
99 /**
100 * @test
101 */
102 public function loggerLogsRecord() {
103 $logger = new Logger('test.core.log');
104 /** @var NullWriter|\PHPUnit_Framework_MockObject_MockObject $writer */
105 $writer = $this->getMock(NullWriter::class, array('writeLog'));
106 $writer->expects($this->once())->method('writeLog');
107 $logger->addWriter(LogLevel::DEBUG, $writer);
108 $logger->warning('test');
109 }
110
111 /**
112 * @test
113 */
114 public function loggerLogsRecordsAtLeastAsCriticalAsLogLevel() {
115 $logger = new Logger('test.core.log');
116 $writer = new Fixtures\WriterFixture();
117 $logger->addWriter(LogLevel::NOTICE, $writer);
118 // notice == notice, thus must be logged
119 $logger->log(LogLevel::NOTICE, 'test message');
120 $this->assertAttributeNotEmpty('records', $writer);
121 }
122
123 /**
124 * @return array
125 */
126 public function loggerLogsRecordsThroughShorthandMethodDataProvider() {
127 return array(
128 array('emergency'),
129 array('alert'),
130 array('critical'),
131 array('error'),
132 array('warning'),
133 array('notice'),
134 array('info'),
135 array('debug')
136 );
137 }
138
139 /**
140 * @test
141 * @param string $shorthandMethod
142 * @dataProvider loggerLogsRecordsThroughShorthandMethodDataProvider
143 */
144 public function loggerLogsRecordsThroughShorthandMethod($shorthandMethod) {
145 $logger = new Logger('test.core.log');
146 $writer = new Fixtures\WriterFixture();
147 $logger->addWriter(LogLevel::DEBUG, $writer);
148 call_user_func(array($logger, $shorthandMethod), 'test message');
149 $this->assertAttributeNotEmpty('records', $writer);
150 }
151
152 /**
153 * @test
154 */
155 public function loggerLogsRecordsMoreCriticalThanLogLevel() {
156 $logger = new Logger('test.core.log');
157 $writer = new Fixtures\WriterFixture();
158 $logger->addWriter(LogLevel::NOTICE, $writer);
159 // warning > notice, thus must be logged
160 $logger->log(LogLevel::WARNING, 'test message');
161 $this->assertAttributeNotEmpty('records', $writer);
162 }
163
164 /**
165 * @test
166 */
167 public function addWriterAddsWriterToTheSpecifiedLevel() {
168 $logger = new Logger('test.core.log');
169 $writer = new Fixtures\WriterFixture();
170 $logger->addWriter(LogLevel::NOTICE, $writer);
171 $writers = $logger->getWriters();
172 $this->assertContains($writer, $writers[LogLevel::NOTICE]);
173 }
174
175 /**
176 * @test
177 */
178 public function addWriterAddsWriterAlsoToHigherLevelsThanSpecified() {
179 $logger = new Logger('test.core.log');
180 $writer = new Fixtures\WriterFixture();
181 $logger->addWriter(LogLevel::NOTICE, $writer);
182 $writers = $logger->getWriters();
183 $this->assertContains($writer, $writers[LogLevel::EMERGENCY]);
184 }
185
186 }