‪TYPO3CMS  9.5
FileWriterTest.php
Go to the documentation of this file.
1 <?php
2 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 
18 use org\bovigo\vfs\vfsStream;
19 use org\bovigo\vfs\vfsStreamWrapper;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪FileWriterTest extends UnitTestCase
31 {
35  protected ‪$logFileDirectory = 'Log';
36 
40  protected ‪$logFileName = 'test.log';
41 
42  protected function ‪setUpVfsStream(): void
43  {
44  vfsStream::setup('LogRoot');
45  }
46 
54  protected function ‪createLogger($name = ''): ‪Logger
55  {
56  if (empty($name)) {
57  $name = $this->getUniqueId('test.core.log.');
58  }
59  GeneralUtility::makeInstance(LogManager::class)->registerLogger($name);
61  $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger($name);
62  return $logger;
63  }
64 
71  protected function ‪createWriter($prependName = ''): FileWriter
72  {
74  $writer = GeneralUtility::makeInstance(FileWriter::class, [
75  'logFile' => $this->‪getDefaultFileName($prependName)
76  ]);
77  return $writer;
78  }
79 
80  protected function ‪getDefaultFileName($prependName = ''): string
81  {
82  return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . ‪$this->logFileName;
83  }
84 
88  public function ‪setLogFileSetsLogFile(): void
89  {
90  $this->‪setUpVfsStream();
91  vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
92  $writer = GeneralUtility::makeInstance(FileWriter::class);
93  $writer->setLogFile($this->‪getDefaultFileName());
94  $this->assertAttributeEquals($this->‪getDefaultFileName(), 'logFile', $writer);
95  }
96 
100  public function ‪setLogFileAcceptsAbsolutePath(): void
101  {
102  $writer = GeneralUtility::makeInstance(FileWriter::class);
103  $tempFile = rtrim(sys_get_temp_dir(), '/\\') . '/typo3.log';
104  $writer->setLogFile($tempFile);
105  $this->assertAttributeEquals($tempFile, 'logFile', $writer);
106  }
107 
111  public function ‪createsLogFileDirectory(): void
112  {
113  $this->‪setUpVfsStream();
114  $this->‪createWriter();
115  $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
116  }
117 
121  public function ‪createsLogFile(): void
122  {
123  $this->‪setUpVfsStream();
124  $this->‪createWriter();
125  $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
126  }
127 
131  public function ‪logsToFileDataProvider(): array
132  {
133  $simpleRecord = GeneralUtility::makeInstance(LogRecord::class, $this->getUniqueId('test.core.log.fileWriter.simpleRecord.'), \‪TYPO3\CMS\Core\Log\‪LogLevel::INFO, 'test record');
134  $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']]);
135  return [
136  'simple record' => [$simpleRecord, trim((string)$simpleRecord)],
137  'record with data' => [$recordWithData, trim((string)$recordWithData)]
138  ];
139  }
140 
147  public function ‪logsToFile(LogRecord $record, $expectedResult): void
148  {
149  $this->‪setUpVfsStream();
150  $this->‪createWriter()->‪writeLog($record);
151  $logFileContents = trim(file_get_contents($this->‪getDefaultFileName()));
152  $this->assertEquals($expectedResult, $logFileContents);
153  }
154 
158  public function ‪logsToFileWithUnescapedCharacters(): void
159  {
160  $this->‪setUpVfsStream();
161 
162  $recordWithData = GeneralUtility::makeInstance(
163  LogRecord::class,
164  $this->getUniqueId('test.core.log.fileWriter.recordWithData.'),
165  \‪TYPO3\CMS\Core\Log\‪LogLevel::INFO,
166  'test record with unicode and slash in data to encode',
167  ['foo' => ['bar' => 'I paid 0.00€ for open source projects/code']]
168  );
169 
170  $expectedResult = '{"foo":{"bar":"I paid 0.00€ for open source projects/code"}}';
171 
172  $this->‪createWriter('encoded-data')->‪writeLog($recordWithData);
173  $logFileContents = trim(file_get_contents($this->‪getDefaultFileName('encoded-data')));
174  $this->assertContains($expectedResult, $logFileContents);
175  }
176 
183  public function ‪differentWritersLogToDifferentFiles(LogRecord $record, $expectedResult): void
184  {
185  $this->‪setUpVfsStream();
186  $firstWriter = $this->‪createWriter();
187  $secondWriter = $this->‪createWriter('second-');
188 
189  $firstWriter->writeLog($record);
190  $secondWriter->writeLog($record);
191 
192  $firstLogFileContents = trim(file_get_contents($this->‪getDefaultFileName()));
193  $secondLogFileContents = trim(file_get_contents($this->‪getDefaultFileName('second-')));
194 
195  $this->assertEquals($expectedResult, $firstLogFileContents);
196  $this->assertEquals($expectedResult, $secondLogFileContents);
197  }
198 
203  {
204  $this->‪setUpVfsStream();
205 
206  $firstWriter = $this->getMockBuilder(FileWriter::class)
207  ->setMethods(['dummy'])
208  ->getMock();
209  $secondWriter = $this->getMockBuilder(FileWriter::class)
210  ->setMethods(['createLogFile'])
211  ->getMock();
212 
213  $secondWriter->expects($this->never())->method('createLogFile');
214 
215  $logFilePrefix = $this->getUniqueId('unique');
216  $firstWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
217  $secondWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
218  }
219 
224  {
225  $this->‪setUpVfsStream();
226 
227  $firstWriter = $this->getMockBuilder(FileWriter::class)
228  ->setMethods(['closeLogFile'])
229  ->getMock();
230  $secondWriter = $this->getMockBuilder(FileWriter::class)
231  ->setMethods(['closeLogFile'])
232  ->getMock();
233 
234  $firstWriter->expects($this->never())->method('closeLogFile');
235  $secondWriter->expects($this->once())->method('closeLogFile');
236 
237  $logFilePrefix = $this->getUniqueId('unique');
238  $firstWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
239  $secondWriter->setLogFile($this->‪getDefaultFileName($logFilePrefix));
240  $firstWriter->__destruct();
241  $secondWriter->__destruct();
242  }
243 }
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\logsToFileWithUnescapedCharacters
‪logsToFileWithUnescapedCharacters()
Definition: FileWriterTest.php:156
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createsLogFileDirectory
‪createsLogFileDirectory()
Definition: FileWriterTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createsLogFile
‪createsLogFile()
Definition: FileWriterTest.php:119
‪TYPO3\CMS\Core\Log\LogLevel\INFO
‪const INFO
Definition: LogLevel.php:85
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\fileHandleIsNotClosedIfSecondFileWriterIsStillUsingSameFile
‪fileHandleIsNotClosedIfSecondFileWriterIsStillUsingSameFile()
Definition: FileWriterTest.php:221
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\setLogFileAcceptsAbsolutePath
‪setLogFileAcceptsAbsolutePath()
Definition: FileWriterTest.php:98
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest
Definition: FileWriterTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\$logFileDirectory
‪string $logFileDirectory
Definition: FileWriterTest.php:34
‪TYPO3\CMS\Core\Log\LogLevel\ALERT
‪const ALERT
Definition: LogLevel.php:39
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:21
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createLogger
‪Logger createLogger($name='')
Definition: FileWriterTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\setLogFileSetsLogFile
‪setLogFileSetsLogFile()
Definition: FileWriterTest.php:86
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\setUpVfsStream
‪setUpVfsStream()
Definition: FileWriterTest.php:40
‪TYPO3\CMS\Core\Log\Writer\FileWriter
Definition: FileWriter.php:29
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer
Definition: AbstractWriterTest.php:2
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\logsToFileDataProvider
‪array logsToFileDataProvider()
Definition: FileWriterTest.php:129
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:25
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\$logFileName
‪string $logFileName
Definition: FileWriterTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice
‪aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice()
Definition: FileWriterTest.php:200
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\logsToFile
‪logsToFile(LogRecord $record, $expectedResult)
Definition: FileWriterTest.php:145
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\differentWritersLogToDifferentFiles
‪differentWritersLogToDifferentFiles(LogRecord $record, $expectedResult)
Definition: FileWriterTest.php:181
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\getDefaultFileName
‪getDefaultFileName($prependName='')
Definition: FileWriterTest.php:78
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Log\Writer\FileWriter\writeLog
‪WriterInterface writeLog(LogRecord $record)
Definition: FileWriter.php:140
‪TYPO3\CMS\Core\Tests\Unit\Log\Writer\FileWriterTest\createWriter
‪FileWriter createWriter($prependName='')
Definition: FileWriterTest.php:69