[TASK] vfsStream: Use namespaced version from phpunit follow-up
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / Writer / FileTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use \org\bovigo\vfs\vfsStream;
28 use \org\bovigo\vfs\vfsStreamDirectory;
29 use \org\bovigo\vfs\vfsStreamWrapper;
30
31 /**
32 * Testcase for \TYPO3\CMS\Core\Log\Writer\FileWriter
33 *
34 * @author Steffen Gebert <steffen.gebert@typo3.org>
35 */
36 class FileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
37
38 /**
39 * @var string
40 */
41 protected $logFileDirectory = 'Log';
42
43 /**
44 * @var string
45 */
46 protected $logFileName = 'test.log';
47
48 protected function setUpVfsStream() {
49 if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
50 $this->markTestSkipped('File backend tests are not available with this phpunit version.');
51 }
52 vfsStream::setup('LogRoot');
53 }
54
55 /**
56 * Creates a test logger
57 *
58 * @param string $name
59 * @internal param string $component Component key
60 * @return \TYPO3\CMS\Core\Log\Logger
61 */
62 protected function createLogger($name = '') {
63 if (empty($name)) {
64 $name = uniqid('test.core.log.');
65 }
66 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->registerLogger($name);
67 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
68 $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger($name);
69 return $logger;
70 }
71
72 /**
73 * Creates a file writer
74 *
75 * @param string $prependName
76 * @return \TYPO3\CMS\Core\Log\Writer\FileWriter
77 */
78 protected function createWriter($prependName = '') {
79 /** @var \TYPO3\CMS\Core\Log\Writer\FileWriter $writer */
80 $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array(
81 'logFile' => $this->getDefaultFileName($prependName)
82 ));
83 return $writer;
84 }
85
86 protected function getDefaultFileName($prependName = '') {
87 return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . $this->logFileName;
88 }
89
90 /**
91 * @test
92 */
93 public function setLogFileSetsLogFile() {
94 $this->setUpVfsStream();
95 vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
96 $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
97 $writer->setLogFile($this->getDefaultFileName());
98 $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
99 }
100
101 /**
102 * @test
103 * @expectedException \InvalidArgumentException
104 */
105 public function setLogFileRefusesIllegalPath() {
106 $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
107 $writer->setLogFile('/tmp/typo3.log');
108 }
109
110 /**
111 * @test
112 */
113 public function createsLogFileDirectory() {
114 $this->setUpVfsStream();
115 $this->createWriter();
116 $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
117 }
118
119 /**
120 * @test
121 */
122 public function createsLogFile() {
123 $this->setUpVfsStream();
124 $this->createWriter();
125 $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
126 }
127
128 /**
129 * @return array
130 */
131 public function logsToFileDataProvider() {
132 $simpleRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', uniqid('test.core.log.fileWriter.simpleRecord.'), \TYPO3\CMS\Core\Log\LogLevel::INFO, 'test record');
133 $recordWithData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', uniqid('test.core.log.fileWriter.recordWithData.'), \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test record with data', array('foo' => array('bar' => 'baz')));
134 return array(
135 'simple record' => array($simpleRecord, trim((string) $simpleRecord)),
136 'record with data' => array($recordWithData, trim((string) $recordWithData))
137 );
138 }
139
140 /**
141 * @test
142 * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
143 * @param string $expectedResult Needle
144 * @dataProvider logsToFileDataProvider
145 */
146 public function logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
147 $this->setUpVfsStream();
148 $this->createWriter()->writeLog($record);
149 $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
150 $this->assertEquals($expectedResult, $logFileContents);
151 }
152
153 /**
154 * @test
155 * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
156 * @param string $expectedResult Needle
157 * @dataProvider logsToFileDataProvider
158 */
159 public function differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
160 $this->setUpVfsStream();
161 $firstWriter = $this->createWriter();
162 $secondWriter = $this->createWriter('second-');
163
164 $firstWriter->writeLog($record);
165 $secondWriter->writeLog($record);
166
167 $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
168 $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
169
170 $this->assertEquals($expectedResult, $firstLogFileContents);
171 $this->assertEquals($expectedResult, $secondLogFileContents);
172 }
173
174 /**
175 * @test
176 */
177 public function aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice() {
178 $this->setUpVfsStream();
179
180 $firstWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('dummy'));
181 $secondWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('createLogFile'));
182
183 $secondWriter->expects($this->never())->method('createLogFile');
184
185 $logFilePrefix = uniqid('unique');
186 $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
187 $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
188
189 }
190
191 /**
192 * @test
193 */
194 public function createsHtaccessForNewDirectory() {
195 $this->setUpVfsStream();
196 $directory = uniqid('Log');
197 $logFile = 'vfs://LogRoot/' . $directory . '/' . $this->logFileName;
198 $this->createWriter()->setLogFile($logFile);
199 $this->assertFileExists('vfs://LogRoot/' . $directory . '/.htaccess');
200 }
201
202 /**
203 * @test
204 */
205 public function createsNoHtaccessForExistingDirectory() {
206 $this->setUpVfsStream();
207 $directory = uniqid('Log');
208 // create a directory
209 vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory($directory));
210 $logFile = 'vfs://LogRoot/' . $directory . '/' . $this->logFileName;
211 $this->assertTrue(is_dir('vfs://LogRoot/' . $directory));
212 $this->createWriter()->setLogFile($logFile);
213 $this->assertFileNotExists('vfs://LogRoot/' . $directory . '/.htaccess');
214 }
215
216 }
217
218 ?>