‪TYPO3CMS  10.4
FileWriterTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use org\bovigo\vfs\vfsStream;
21 use org\bovigo\vfs\vfsStreamWrapper;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪FileWriterTest extends UnitTestCase
35 {
39  protected ‪$logFileDirectory = 'Log';
40 
44  protected ‪$logFileName = 'test.log';
45 
46  protected function ‪setUpVfsStream(): void
47  {
48  vfsStream::setup('LogRoot');
49  }
50 
58  protected function ‪createLogger($name = ''): ‪Logger
59  {
60  if (empty($name)) {
61  $name = ‪StringUtility::getUniqueId('test.core.log.');
62  }
63  GeneralUtility::makeInstance(LogManager::class)->registerLogger($name);
65  $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger($name);
66  return $logger;
67  }
68 
75  protected function ‪createWriter($prependName = ''): FileWriter
76  {
78  $writer = GeneralUtility::makeInstance(FileWriter::class, [
79  'logFile' => $this->‪getDefaultFileName($prependName)
80  ]);
81  return $writer;
82  }
83 
84  protected function ‪getDefaultFileName($prependName = ''): string
85  {
86  return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . ‪$this->logFileName;
87  }
88 
92  public function ‪setLogFileSetsLogFile(): void
93  {
94  $this->‪setUpVfsStream();
95  vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
96  $writer = GeneralUtility::makeInstance(FileWriter::class);
97  $writer->setLogFile($this->‪getDefaultFileName());
98  self::assertEquals($this->‪getDefaultFileName(), $writer->getLogFile());
99  }
100 
104  public function ‪setLogFileAcceptsAbsolutePath(): void
105  {
106  $writer = GeneralUtility::makeInstance(FileWriter::class);
107  $tempFile = rtrim(sys_get_temp_dir(), '/\\') . '/typo3.log';
108  $writer->setLogFile($tempFile);
109  self::assertEquals($tempFile, $writer->getLogFile());
110  }
111 
115  public function ‪createsLogFileDirectory(): void
116  {
117  $this->‪setUpVfsStream();
118  $this->‪createWriter();
119  self::assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
120  }
121 
125  public function ‪createsLogFile(): void
126  {
127  $this->‪setUpVfsStream();
128  $this->‪createWriter();
129  self::assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
130  }
131 
135  public function ‪logsToFileDataProvider(): array
136  {
137  $simpleRecord = GeneralUtility::makeInstance(LogRecord::class, ‪StringUtility::getUniqueId('test.core.log.fileWriter.simpleRecord.'), LogLevel::INFO, 'test record');
138  $recordWithData = GeneralUtility::makeInstance(LogRecord::class, ‪StringUtility::getUniqueId('test.core.log.fileWriter.recordWithData.'), LogLevel::ALERT, 'test record with data', ['foo' => ['bar' => 'baz']]);
139  return [
140  'simple record' => [$simpleRecord, trim((string)$simpleRecord)],
141  'record with data' => [$recordWithData, trim((string)$recordWithData)]
142  ];
143  }
144 
151  public function ‪logsToFile(LogRecord $record, $expectedResult): void
152  {
153  $this->‪setUpVfsStream();
154  $this->‪createWriter()->‪writeLog($record);
155  $logFileContents = trim(file_get_contents($this->‪getDefaultFileName()));
156  self::assertEquals($expectedResult, $logFileContents);
157  }
158 
162  public function ‪logsToFileWithUnescapedCharacters(): void
163  {
164  $this->‪setUpVfsStream();
165 
166  $recordWithData = GeneralUtility::makeInstance(
167  LogRecord::class,
168  ‪StringUtility::getUniqueId('test.core.log.fileWriter.recordWithData.'),
169  LogLevel::INFO,
170  'test record with unicode and slash in data to encode',
171  ['foo' => ['bar' => 'I paid 0.00€ for open source projects/code']]
172  );
173 
174  $expectedResult = '{"foo":{"bar":"I paid 0.00€ for open source projects/code"}}';
175 
176  $this->‪createWriter('encoded-data')->‪writeLog($recordWithData);
177  $logFileContents = trim(file_get_contents($this->‪getDefaultFileName('encoded-data')));
178  self::assertStringContainsString($expectedResult, $logFileContents);
179  }
180 
187  public function ‪differentWritersLogToDifferentFiles(LogRecord $record, $expectedResult): void
188  {
189  $this->‪setUpVfsStream();
190  $firstWriter = $this->‪createWriter();
191  $secondWriter = $this->‪createWriter('second-');
192 
193  $firstWriter->writeLog($record);
194  $secondWriter->writeLog($record);
195 
196  $firstLogFileContents = trim(file_get_contents($this->‪getDefaultFileName()));
197  $secondLogFileContents = trim(file_get_contents($this->‪getDefaultFileName('second-')));
198 
199  self::assertEquals($expectedResult, $firstLogFileContents);
200  self::assertEquals($expectedResult, $secondLogFileContents);
201  }
202 
207  {
208  $this->‪setUpVfsStream();
209 
210  $firstWriter = $this->getMockBuilder(FileWriter::class)
211  ->setMethods(['dummy'])
212  ->getMock();
213  $secondWriter = $this->getMockBuilder(FileWriter::class)
214  ->setMethods(['createLogFile'])
215  ->getMock();
216 
217  $secondWriter->expects(self::never())->method('createLogFile');
218 
219  $logFilePrefix = ‪StringUtility::getUniqueId('unique');
220  $firstWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
221  $secondWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
222  }
223 
228  {
229  $this->‪setUpVfsStream();
230 
231  $firstWriter = $this->getMockBuilder(FileWriter::class)
232  ->setMethods(['closeLogFile'])
233  ->getMock();
234  $secondWriter = $this->getMockBuilder(FileWriter::class)
235  ->setMethods(['closeLogFile'])
236  ->getMock();
237 
238  $firstWriter->expects(self::never())->method('closeLogFile');
239  $secondWriter->expects(self::once())->method('closeLogFile');
240 
241  $logFilePrefix = ‪StringUtility::getUniqueId('unique');
242  $firstWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
243  $secondWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
244  $firstWriter->__destruct();
245  $secondWriter->__destruct();
246  }
247 }
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\logsToFileWithUnescapedCharacters
‪logsToFileWithUnescapedCharacters()
Definition: FileWriterTest.php:160
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createsLogFileDirectory
‪createsLogFileDirectory()
Definition: FileWriterTest.php:113
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createsLogFile
‪createsLogFile()
Definition: FileWriterTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\fileHandleIsNotClosedIfSecondFileWriterIsStillUsingSameFile
‪fileHandleIsNotClosedIfSecondFileWriterIsStillUsingSameFile()
Definition: FileWriterTest.php:225
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\setLogFileAcceptsAbsolutePath
‪setLogFileAcceptsAbsolutePath()
Definition: FileWriterTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest
Definition: FileWriterTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\$logFileDirectory
‪string $logFileDirectory
Definition: FileWriterTest.php:38
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:22
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createLogger
‪Logger createLogger($name='')
Definition: FileWriterTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\setLogFileSetsLogFile
‪setLogFileSetsLogFile()
Definition: FileWriterTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\setUpVfsStream
‪setUpVfsStream()
Definition: FileWriterTest.php:44
‪TYPO3\CMS\Core\Log\Writer\FileWriter
Definition: FileWriter.php:28
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer
Definition: AbstractWriterTest.php:16
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\logsToFileDataProvider
‪array logsToFileDataProvider()
Definition: FileWriterTest.php:133
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:30
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\$logFileName
‪string $logFileName
Definition: FileWriterTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice
‪aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice()
Definition: FileWriterTest.php:204
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\logsToFile
‪logsToFile(LogRecord $record, $expectedResult)
Definition: FileWriterTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\differentWritersLogToDifferentFiles
‪differentWritersLogToDifferentFiles(LogRecord $record, $expectedResult)
Definition: FileWriterTest.php:185
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\getDefaultFileName
‪getDefaultFileName($prependName='')
Definition: FileWriterTest.php:82
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24
‪TYPO3\CMS\Core\Log\Writer\FileWriter\writeLog
‪WriterInterface writeLog(LogRecord $record)
Definition: FileWriter.php:139
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createWriter
‪FileWriter createWriter($prependName='')
Definition: FileWriterTest.php:73