TYPO3 CMS  TYPO3_8-7
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 
26 class LoggerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
31  public function getNameGetsLoggerName()
32  {
33  $logger = new Logger('test.core.log');
34  $this->assertSame('test.core.log', $logger->getName());
35  }
36 
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 
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 
66  {
67  $logger = new Logger('test.core.log');
68  $returnValue = $logger->log(LogLevel::WARNING, 'test message');
69  $this->assertInstanceOf(Logger::class, $returnValue);
70  }
71 
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 
87  public function loggerCallsProcessor()
88  {
89  $component = 'test.core.log';
90  $level = LogLevel::DEBUG;
91  $message = 'test';
92  $logger = new Logger($component);
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 
107  public function loggerLogsRecord()
108  {
109  $logger = new Logger('test.core.log');
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 
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 
136  {
137  return [
138  ['emergency'],
139  ['alert'],
140  ['critical'],
141  ['error'],
142  ['warning'],
143  ['notice'],
144  ['info'],
145  ['debug']
146  ];
147  }
148 
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 
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 
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 
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 }
loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
Definition: LoggerTest.php:154