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