TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
FileWriterTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
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 use org\bovigo\vfs\vfsStream;
18 use org\bovigo\vfs\vfsStreamWrapper;
19 
24 {
28  protected $logFileDirectory = 'Log';
29 
33  protected $logFileName = 'test.log';
34 
35  protected function setUpVfsStream()
36  {
37  if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
38  $this->markTestSkipped('File backend tests are not available with this phpunit version.');
39  }
40  vfsStream::setup('LogRoot');
41  }
42 
50  protected function createLogger($name = '')
51  {
52  if (empty($name)) {
53  $name = $this->getUniqueId('test.core.log.');
54  }
55  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->registerLogger($name);
57  $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger($name);
58  return $logger;
59  }
60 
67  protected function createWriter($prependName = '')
68  {
70  $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\Writer\FileWriter::class, [
71  'logFile' => $this->getDefaultFileName($prependName)
72  ]);
73  return $writer;
74  }
75 
76  protected function getDefaultFileName($prependName = '')
77  {
78  return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . $this->logFileName;
79  }
80 
84  public function setLogFileSetsLogFile()
85  {
86  $this->setUpVfsStream();
87  vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
88  $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\Writer\FileWriter::class);
89  $writer->setLogFile($this->getDefaultFileName());
90  $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
91  }
92 
97  {
98  $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\Writer\FileWriter::class);
99  $tempFile = rtrim(sys_get_temp_dir(), '/\\') . '/typo3.log';
100  $writer->setLogFile($tempFile);
101  $this->assertAttributeEquals($tempFile, 'logFile', $writer);
102  }
103 
107  public function createsLogFileDirectory()
108  {
109  $this->setUpVfsStream();
110  $this->createWriter();
111  $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
112  }
113 
117  public function createsLogFile()
118  {
119  $this->setUpVfsStream();
120  $this->createWriter();
121  $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
122  }
123 
127  public function logsToFileDataProvider()
128  {
129  $simpleRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogRecord::class, $this->getUniqueId('test.core.log.fileWriter.simpleRecord.'), \TYPO3\CMS\Core\Log\LogLevel::INFO, 'test record');
130  $recordWithData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogRecord::class, $this->getUniqueId('test.core.log.fileWriter.recordWithData.'), \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test record with data', ['foo' => ['bar' => 'baz']]);
131  return [
132  'simple record' => [$simpleRecord, trim((string)$simpleRecord)],
133  'record with data' => [$recordWithData, trim((string)$recordWithData)]
134  ];
135  }
136 
143  public function logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
144  {
145  $this->setUpVfsStream();
146  $this->createWriter()->writeLog($record);
147  $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
148  $this->assertEquals($expectedResult, $logFileContents);
149  }
150 
157  public function differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
158  {
159  $this->setUpVfsStream();
160  $firstWriter = $this->createWriter();
161  $secondWriter = $this->createWriter('second-');
162 
163  $firstWriter->writeLog($record);
164  $secondWriter->writeLog($record);
165 
166  $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
167  $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
168 
169  $this->assertEquals($expectedResult, $firstLogFileContents);
170  $this->assertEquals($expectedResult, $secondLogFileContents);
171  }
172 
177  {
178  $this->setUpVfsStream();
179 
180  $firstWriter = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\FileWriter::class)
181  ->setMethods(['dummy'])
182  ->getMock();
183  $secondWriter = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\FileWriter::class)
184  ->setMethods(['createLogFile'])
185  ->getMock();
186 
187  $secondWriter->expects($this->never())->method('createLogFile');
188 
189  $logFilePrefix = $this->getUniqueId('unique');
190  $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
191  $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
192  }
193 }
differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
static makeInstance($className,...$constructorArguments)