‪TYPO3CMS  10.4
LoggerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪LoggerTest extends UnitTestCase
30 {
34  public function ‪getNameGetsLoggerName()
35  {
36  $logger = new ‪Logger('test.core.log');
37  self::assertSame('test.core.log', $logger->getName());
38  }
39 
44  {
45  $logger = new ‪Logger('test.core.log');
46  $writer = new ‪WriterFixture();
47  $logger->addWriter(LogLevel::ERROR, $writer);
48  // warning < error, thus must not be logged
49  $logger->log(LogLevel::WARNING, 'test message');
50  self::assertEmpty($writer->getRecords());
51  }
52 
57  {
58  $logger = new ‪Logger('test.core.log');
59  $writer = new ‪WriterFixture();
60  $logger->addWriter(LogLevel::DEBUG, $writer);
61  $returnValue = $logger->log(LogLevel::WARNING, 'test message');
62  self::assertInstanceOf(Logger::class, $returnValue);
63  }
64 
69  {
70  $logger = new ‪Logger('test.core.log');
71  $returnValue = $logger->log(LogLevel::WARNING, 'test message');
72  self::assertInstanceOf(Logger::class, $returnValue);
73  }
74 
79  {
80  $logger = new ‪Logger('test.core.log');
81  $writer = new ‪WriterFixture();
82  $logger->addWriter(LogLevel::EMERGENCY, $writer);
83  $returnValue = $logger->log(LogLevel::WARNING, 'test message');
84  self::assertInstanceOf(Logger::class, $returnValue);
85  }
86 
90  public function ‪loggerCallsProcessor()
91  {
92  $component = 'test.core.log';
93  $level = LogLevel::DEBUG;
94  $message = 'test';
95  $logger = new ‪Logger($component);
97  $processor = $this->getMockBuilder(NullProcessor::class)
98  ->setMethods(['processLogRecord'])
99  ->getMock();
100  $processor->expects(self::once())->method('processLogRecord')->willReturn(new ‪LogRecord($component, $level, $message));
101  $logger->addProcessor($level, $processor);
102  // we need a writer, otherwise we will not process log records
103  $logger->addWriter($level, new ‪NullWriter());
104  $logger->warning($message);
105  }
106 
110  public function ‪loggerLogsRecord()
111  {
112  $logger = new ‪Logger('test.core.log');
114  $writer = $this->getMockBuilder(NullWriter::class)
115  ->setMethods(['writeLog'])
116  ->getMock();
117  $writer->expects(self::once())->method('writeLog');
118  $logger->addWriter(LogLevel::DEBUG, $writer);
119  $logger->warning('test');
120  }
121 
126  {
127  $logger = new ‪Logger('test.core.log');
128  $writer = new ‪WriterFixture();
129  $logger->addWriter(LogLevel::NOTICE, $writer);
130  // notice == notice, thus must be logged
131  $logger->log(LogLevel::NOTICE, 'test message');
132  self::assertNotEmpty($writer->getRecords());
133  }
134 
139  {
140  return [
141  ['emergency'],
142  ['alert'],
143  ['critical'],
144  ['error'],
145  ['warning'],
146  ['notice'],
147  ['info'],
148  ['debug']
149  ];
150  }
151 
157  public function ‪loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
158  {
159  $logger = new ‪Logger('test.core.log');
160  $writer = new ‪WriterFixture();
161  $logger->addWriter(LogLevel::DEBUG, $writer);
162  call_user_func([$logger, $shorthandMethod], 'test message');
163  self::assertNotEmpty($writer->getRecords());
164  }
165 
170  {
171  $logger = new ‪Logger('test.core.log');
172  $writer = new ‪WriterFixture();
173  $logger->addWriter(LogLevel::NOTICE, $writer);
174  // warning > notice, thus must be logged
175  $logger->log(LogLevel::WARNING, 'test message');
176  self::assertNotEmpty($writer->getRecords());
177  }
178 
183  {
184  $logger = new ‪Logger('test.core.log');
185  $writer = new ‪WriterFixture();
186  $logger->addWriter(LogLevel::NOTICE, $writer);
187  $writers = $logger->getWriters();
188  self::assertContains($writer, $writers[LogLevel::NOTICE]);
189  }
190 
195  {
196  $logger = new ‪Logger('test.core.log');
197  $writer = new ‪WriterFixture();
198  $logger->addWriter(LogLevel::NOTICE, $writer);
199  $writers = $logger->getWriters();
200  self::assertContains($writer, $writers[LogLevel::EMERGENCY]);
201  }
202 }
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerReturnsItselfAfterLoggingLessCritical
‪loggerReturnsItselfAfterLoggingLessCritical()
Definition: LoggerTest.php:78
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsThroughShorthandMethod
‪loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
Definition: LoggerTest.php:157
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerReturnsItselfAfterLogging
‪loggerReturnsItselfAfterLogging()
Definition: LoggerTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsAtLeastAsCriticalAsLogLevel
‪loggerLogsRecordsAtLeastAsCriticalAsLogLevel()
Definition: LoggerTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\getNameGetsLoggerName
‪getNameGetsLoggerName()
Definition: LoggerTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsThroughShorthandMethodDataProvider
‪array loggerLogsRecordsThroughShorthandMethodDataProvider()
Definition: LoggerTest.php:138
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecordsMoreCriticalThanLogLevel
‪loggerLogsRecordsMoreCriticalThanLogLevel()
Definition: LoggerTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\addWriterAddsWriterAlsoToHigherLevelsThanSpecified
‪addWriterAddsWriterAlsoToHigherLevelsThanSpecified()
Definition: LoggerTest.php:194
‪TYPO3\CMS\Core\Tests\Unit\Log
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerCallsProcessor
‪loggerCallsProcessor()
Definition: LoggerTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\addWriterAddsWriterToTheSpecifiedLevel
‪addWriterAddsWriterToTheSpecifiedLevel()
Definition: LoggerTest.php:182
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerLogsRecord
‪loggerLogsRecord()
Definition: LoggerTest.php:110
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:22
‪TYPO3\CMS\Core\Tests\Unit\Log\Fixtures\WriterFixture
Definition: WriterFixture.php:25
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest
Definition: LoggerTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerReturnsItselfAfterLoggingWithoutWriter
‪loggerReturnsItselfAfterLoggingWithoutWriter()
Definition: LoggerTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Log\LoggerTest\loggerDoesNotLogRecordsLessCriticalThanLogLevel
‪loggerDoesNotLogRecordsLessCriticalThanLogLevel()
Definition: LoggerTest.php:43
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Log\Writer\NullWriter
Definition: NullWriter.php:24
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24
‪TYPO3\CMS\Core\Log\Processor\NullProcessor
Definition: NullProcessor.php:24