TYPO3 CMS  TYPO3_7-6
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 
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->getMock(NullProcessor::class, ['processLogRecord']);
95  $processor->expects($this->once())->method('processLogRecord')->will($this->returnValue(new LogRecord($component, $level, $message)));
96  $logger->addProcessor($level, $processor);
97  // we need a writer, otherwise we will not process log records
98  $logger->addWriter($level, new NullWriter());
99  $logger->warning($message);
100  }
101 
105  public function loggerLogsRecord()
106  {
107  $logger = new Logger('test.core.log');
109  $writer = $this->getMock(NullWriter::class, ['writeLog']);
110  $writer->expects($this->once())->method('writeLog');
111  $logger->addWriter(LogLevel::DEBUG, $writer);
112  $logger->warning('test');
113  }
114 
119  {
120  $logger = new Logger('test.core.log');
121  $writer = new Fixtures\WriterFixture();
122  $logger->addWriter(LogLevel::NOTICE, $writer);
123  // notice == notice, thus must be logged
124  $logger->log(LogLevel::NOTICE, 'test message');
125  $this->assertAttributeNotEmpty('records', $writer);
126  }
127 
132  {
133  return [
134  ['emergency'],
135  ['alert'],
136  ['critical'],
137  ['error'],
138  ['warning'],
139  ['notice'],
140  ['info'],
141  ['debug']
142  ];
143  }
144 
150  public function loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
151  {
152  $logger = new Logger('test.core.log');
153  $writer = new Fixtures\WriterFixture();
154  $logger->addWriter(LogLevel::DEBUG, $writer);
155  call_user_func([$logger, $shorthandMethod], 'test message');
156  $this->assertAttributeNotEmpty('records', $writer);
157  }
158 
163  {
164  $logger = new Logger('test.core.log');
165  $writer = new Fixtures\WriterFixture();
166  $logger->addWriter(LogLevel::NOTICE, $writer);
167  // warning > notice, thus must be logged
168  $logger->log(LogLevel::WARNING, 'test message');
169  $this->assertAttributeNotEmpty('records', $writer);
170  }
171 
176  {
177  $logger = new Logger('test.core.log');
178  $writer = new Fixtures\WriterFixture();
179  $logger->addWriter(LogLevel::NOTICE, $writer);
180  $writers = $logger->getWriters();
181  $this->assertContains($writer, $writers[LogLevel::NOTICE]);
182  }
183 
188  {
189  $logger = new Logger('test.core.log');
190  $writer = new Fixtures\WriterFixture();
191  $logger->addWriter(LogLevel::NOTICE, $writer);
192  $writers = $logger->getWriters();
193  $this->assertContains($writer, $writers[LogLevel::EMERGENCY]);
194  }
195 }
loggerLogsRecordsThroughShorthandMethod($shorthandMethod)
Definition: LoggerTest.php:150