da1fc224d386d06736807bb09cc9b9252d6b6caa
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / Writer / FileWriterTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use org\bovigo\vfs\vfsStream;
19 use org\bovigo\vfs\vfsStreamWrapper;
20 use TYPO3\CMS\Core\Log\Logger;
21 use TYPO3\CMS\Core\Log\LogManager;
22 use TYPO3\CMS\Core\Log\LogRecord;
23 use TYPO3\CMS\Core\Log\Writer\FileWriter;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class FileWriterTest extends UnitTestCase
31 {
32 /**
33 * @var string
34 */
35 protected $logFileDirectory = 'Log';
36
37 /**
38 * @var string
39 */
40 protected $logFileName = 'test.log';
41
42 protected function setUpVfsStream(): void
43 {
44 if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
45 $this->markTestSkipped('File backend tests are not available with this phpunit version.');
46 }
47 vfsStream::setup('LogRoot');
48 }
49
50 /**
51 * Creates a test logger
52 *
53 * @param string $name
54 * @internal param string $component Component key
55 * @return Logger
56 */
57 protected function createLogger($name = ''): Logger
58 {
59 if (empty($name)) {
60 $name = $this->getUniqueId('test.core.log.');
61 }
62 GeneralUtility::makeInstance(LogManager::class)->registerLogger($name);
63 /** @var Logger $logger */
64 $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger($name);
65 return $logger;
66 }
67
68 /**
69 * Creates a file writer
70 *
71 * @param string $prependName
72 * @return FileWriter
73 */
74 protected function createWriter($prependName = ''): FileWriter
75 {
76 /** @var FileWriter $writer */
77 $writer = GeneralUtility::makeInstance(FileWriter::class, [
78 'logFile' => $this->getDefaultFileName($prependName)
79 ]);
80 return $writer;
81 }
82
83 protected function getDefaultFileName($prependName = ''): string
84 {
85 return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . $this->logFileName;
86 }
87
88 /**
89 * @test
90 */
91 public function setLogFileSetsLogFile(): void
92 {
93 $this->setUpVfsStream();
94 vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
95 $writer = GeneralUtility::makeInstance(FileWriter::class);
96 $writer->setLogFile($this->getDefaultFileName());
97 $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
98 }
99
100 /**
101 * @test
102 */
103 public function setLogFileAcceptsAbsolutePath(): void
104 {
105 $writer = GeneralUtility::makeInstance(FileWriter::class);
106 $tempFile = rtrim(sys_get_temp_dir(), '/\\') . '/typo3.log';
107 $writer->setLogFile($tempFile);
108 $this->assertAttributeEquals($tempFile, 'logFile', $writer);
109 }
110
111 /**
112 * @test
113 */
114 public function createsLogFileDirectory(): void
115 {
116 $this->setUpVfsStream();
117 $this->createWriter();
118 $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
119 }
120
121 /**
122 * @test
123 */
124 public function createsLogFile(): void
125 {
126 $this->setUpVfsStream();
127 $this->createWriter();
128 $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
129 }
130
131 /**
132 * @return array
133 */
134 public function logsToFileDataProvider(): array
135 {
136 $simpleRecord = GeneralUtility::makeInstance(LogRecord::class, $this->getUniqueId('test.core.log.fileWriter.simpleRecord.'), \TYPO3\CMS\Core\Log\LogLevel::INFO, 'test record');
137 $recordWithData = GeneralUtility::makeInstance(LogRecord::class, $this->getUniqueId('test.core.log.fileWriter.recordWithData.'), \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test record with data', ['foo' => ['bar' => 'baz']]);
138 return [
139 'simple record' => [$simpleRecord, trim((string)$simpleRecord)],
140 'record with data' => [$recordWithData, trim((string)$recordWithData)]
141 ];
142 }
143
144 /**
145 * @test
146 * @param LogRecord $record Record Test Data
147 * @param string $expectedResult Needle
148 * @dataProvider logsToFileDataProvider
149 */
150 public function logsToFile(LogRecord $record, $expectedResult): void
151 {
152 $this->setUpVfsStream();
153 $this->createWriter()->writeLog($record);
154 $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
155 $this->assertEquals($expectedResult, $logFileContents);
156 }
157
158 /**
159 * @test
160 * @param LogRecord $record Record Test Data
161 * @param string $expectedResult Needle
162 * @dataProvider logsToFileDataProvider
163 */
164 public function differentWritersLogToDifferentFiles(LogRecord $record, $expectedResult): void
165 {
166 $this->setUpVfsStream();
167 $firstWriter = $this->createWriter();
168 $secondWriter = $this->createWriter('second-');
169
170 $firstWriter->writeLog($record);
171 $secondWriter->writeLog($record);
172
173 $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
174 $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
175
176 $this->assertEquals($expectedResult, $firstLogFileContents);
177 $this->assertEquals($expectedResult, $secondLogFileContents);
178 }
179
180 /**
181 * @test
182 */
183 public function aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice()
184 {
185 $this->setUpVfsStream();
186
187 $firstWriter = $this->getMockBuilder(FileWriter::class)
188 ->setMethods(['dummy'])
189 ->getMock();
190 $secondWriter = $this->getMockBuilder(FileWriter::class)
191 ->setMethods(['createLogFile'])
192 ->getMock();
193
194 $secondWriter->expects($this->never())->method('createLogFile');
195
196 $logFilePrefix = $this->getUniqueId('unique');
197 $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
198 $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
199 }
200 }