[TASK] Fix CGL errors
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / Writer / FileWriterTest.php
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
20 /**
21 * Test case
22 */
23 class FileWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
25 /**
26 * @var string
27 */
28 protected $logFileDirectory = 'Log';
29
30 /**
31 * @var string
32 */
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
43 /**
44 * Creates a test logger
45 *
46 * @param string $name
47 * @internal param string $component Component key
48 * @return \TYPO3\CMS\Core\Log\Logger
49 */
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);
56 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
57 $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger($name);
58 return $logger;
59 }
60
61 /**
62 * Creates a file writer
63 *
64 * @param string $prependName
65 * @return \TYPO3\CMS\Core\Log\Writer\FileWriter
66 */
67 protected function createWriter($prependName = '')
68 {
69 /** @var \TYPO3\CMS\Core\Log\Writer\FileWriter $writer */
70 $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\Writer\FileWriter::class, array(
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
81 /**
82 * @test
83 */
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
93 /**
94 * @test
95 */
96 public function setLogFileAcceptsAbsolutePath()
97 {
98 $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\Writer\FileWriter::class);
99 $writer->setLogFile('/tmp/typo3.log');
100 $this->assertAttributeEquals('/tmp/typo3.log', 'logFile', $writer);
101 }
102
103 /**
104 * @test
105 */
106 public function createsLogFileDirectory()
107 {
108 $this->setUpVfsStream();
109 $this->createWriter();
110 $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
111 }
112
113 /**
114 * @test
115 */
116 public function createsLogFile()
117 {
118 $this->setUpVfsStream();
119 $this->createWriter();
120 $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
121 }
122
123 /**
124 * @return array
125 */
126 public function logsToFileDataProvider()
127 {
128 $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');
129 $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', array('foo' => array('bar' => 'baz')));
130 return array(
131 'simple record' => array($simpleRecord, trim((string)$simpleRecord)),
132 'record with data' => array($recordWithData, trim((string)$recordWithData))
133 );
134 }
135
136 /**
137 * @test
138 * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
139 * @param string $expectedResult Needle
140 * @dataProvider logsToFileDataProvider
141 */
142 public function logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
143 {
144 $this->setUpVfsStream();
145 $this->createWriter()->writeLog($record);
146 $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
147 $this->assertEquals($expectedResult, $logFileContents);
148 }
149
150 /**
151 * @test
152 * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
153 * @param string $expectedResult Needle
154 * @dataProvider logsToFileDataProvider
155 */
156 public function differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
157 {
158 $this->setUpVfsStream();
159 $firstWriter = $this->createWriter();
160 $secondWriter = $this->createWriter('second-');
161
162 $firstWriter->writeLog($record);
163 $secondWriter->writeLog($record);
164
165 $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
166 $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
167
168 $this->assertEquals($expectedResult, $firstLogFileContents);
169 $this->assertEquals($expectedResult, $secondLogFileContents);
170 }
171
172 /**
173 * @test
174 */
175 public function aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice()
176 {
177 $this->setUpVfsStream();
178
179 $firstWriter = $this->getMock(\TYPO3\CMS\Core\Log\Writer\FileWriter::class, array('dummy'));
180 $secondWriter = $this->getMock(\TYPO3\CMS\Core\Log\Writer\FileWriter::class, array('createLogFile'));
181
182 $secondWriter->expects($this->never())->method('createLogFile');
183
184 $logFilePrefix = $this->getUniqueId('unique');
185 $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
186 $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
187 }
188 }