‪TYPO3CMS  9.5
LoggerTest.php
Go to the documentation of this file.
1 <?php
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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪LoggerTest extends UnitTestCase
28 {
32  public function ‪getNameGetsLoggerName()
33  {
34  $logger = new ‪Logger('test.core.log');
35  $this->assertSame('test.core.log', $logger->getName());
36  }
37 
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 
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 
67  {
68  $logger = new ‪Logger('test.core.log');
69  $returnValue = $logger->log(‪LogLevel::WARNING, 'test message');
70  $this->assertInstanceOf(Logger::class, $returnValue);
71  }
72 
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 
88  public function ‪loggerCallsProcessor()
89  {
90  $component = 'test.core.log';
91  $level = ‪LogLevel::DEBUG;
92  $message = 'test';
93  $logger = new ‪Logger($component);
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 
108  public function ‪loggerLogsRecord()
109  {
110  $logger = new ‪Logger('test.core.log');
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 
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 
137  {
138  return [
139  ['emergency'],
140  ['alert'],
141  ['critical'],
142  ['error'],
143  ['warning'],
144  ['notice'],
145  ['info'],
146  ['debug']
147  ];
148  }
149 
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 
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 
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 
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 }
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerReturnsItselfAfterLoggingLessCritical
‪loggerReturnsItselfAfterLoggingLessCritical()
Definition: LoggerTest.php:76
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsThroughShorthandMethod
‪loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
Definition: LoggerTest.php:155
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerReturnsItselfAfterLogging
‪loggerReturnsItselfAfterLogging()
Definition: LoggerTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsAtLeastAsCriticalAsLogLevel
‪loggerLogsRecordsAtLeastAsCriticalAsLogLevel()
Definition: LoggerTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\getNameGetsLoggerName
‪getNameGetsLoggerName()
Definition: LoggerTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsThroughShorthandMethodDataProvider
‪array loggerLogsRecordsThroughShorthandMethodDataProvider()
Definition: LoggerTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsMoreCriticalThanLogLevel
‪loggerLogsRecordsMoreCriticalThanLogLevel()
Definition: LoggerTest.php:167
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\addWriterAddsWriterAlsoToHigherLevelsThanSpecified
‪addWriterAddsWriterAlsoToHigherLevelsThanSpecified()
Definition: LoggerTest.php:192
‪TYPO3\CMS\Core\Tests\Unit\Log
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerCallsProcessor
‪loggerCallsProcessor()
Definition: LoggerTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\addWriterAddsWriterToTheSpecifiedLevel
‪addWriterAddsWriterToTheSpecifiedLevel()
Definition: LoggerTest.php:180
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecord
‪loggerLogsRecord()
Definition: LoggerTest.php:108
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:21
‪TYPO3\CMS\Core\Tests\Unit\Log\Fixtures\WriterFixture
Definition: WriterFixture.php:21
‪TYPO3\CMS\Core\Log\LogLevel\ERROR
‪const ERROR
Definition: LogLevel.php:57
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest
Definition: LoggerTest.php:28
‪TYPO3\CMS\Core\Log\LogLevel\EMERGENCY
‪const EMERGENCY
Definition: LogLevel.php:30
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerReturnsItselfAfterLoggingWithoutWriter
‪loggerReturnsItselfAfterLoggingWithoutWriter()
Definition: LoggerTest.php:66
‪TYPO3\CMS\Core\Log\LogLevel\WARNING
‪const WARNING
Definition: LogLevel.php:67
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerDoesNotLogRecordsLessCriticalThanLogLevel
‪loggerDoesNotLogRecordsLessCriticalThanLogLevel()
Definition: LoggerTest.php:41
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Core\Log\Writer\NullWriter
Definition: NullWriter.php:22
‪TYPO3\CMS\Core\Log\LogLevel\NOTICE
‪const NOTICE
Definition: LogLevel.php:76
‪TYPO3\CMS\Core\Log\LogLevel\DEBUG
‪const DEBUG
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:21
‪TYPO3\CMS\Core\Log\Processor\NullProcessor
Definition: NullProcessor.php:21