TYPO3 CMS  TYPO3_6-2
FileWriterTest.php
Go to the documentation of this file.
1 <?php
3 
17 use \org\bovigo\vfs\vfsStream;
18 use \org\bovigo\vfs\vfsStreamDirectory;
19 use \org\bovigo\vfs\vfsStreamWrapper;
20 
27 
31  protected $logFileDirectory = 'Log';
32 
36  protected $logFileName = 'test.log';
37 
38  protected function setUpVfsStream() {
39  if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
40  $this->markTestSkipped('File backend tests are not available with this phpunit version.');
41  }
42  vfsStream::setup('LogRoot');
43  }
44 
52  protected function createLogger($name = '') {
53  if (empty($name)) {
54  $name = $this->getUniqueId('test.core.log.');
55  }
56  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->registerLogger($name);
58  $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger($name);
59  return $logger;
60  }
61 
68  protected function createWriter($prependName = '') {
70  $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array(
71  'logFile' => $this->getDefaultFileName($prependName)
72  ));
73  return $writer;
74  }
75 
76  protected function getDefaultFileName($prependName = '') {
77  return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . $this->logFileName;
78  }
79 
83  public function setLogFileSetsLogFile() {
84  $this->setUpVfsStream();
85  vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
86  $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
87  $writer->setLogFile($this->getDefaultFileName());
88  $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
89  }
90 
94  public function setLogFileAcceptsAbsolutePath() {
95  $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
96  $tempFile = rtrim(sys_get_temp_dir(), '/\\') . '/typo3.log';
97  $writer->setLogFile($tempFile);
98  $this->assertAttributeEquals($tempFile, 'logFile', $writer);
99  }
100 
104  public function createsLogFileDirectory() {
105  $this->setUpVfsStream();
106  $this->createWriter();
107  $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
108  }
109 
113  public function createsLogFile() {
114  $this->setUpVfsStream();
115  $this->createWriter();
116  $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
117  }
118 
122  public function logsToFileDataProvider() {
123  $simpleRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', $this->getUniqueId('test.core.log.fileWriter.simpleRecord.'), \TYPO3\CMS\Core\Log\LogLevel::INFO, 'test record');
124  $recordWithData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', $this->getUniqueId('test.core.log.fileWriter.recordWithData.'), \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test record with data', array('foo' => array('bar' => 'baz')));
125  return array(
126  'simple record' => array($simpleRecord, trim((string) $simpleRecord)),
127  'record with data' => array($recordWithData, trim((string) $recordWithData))
128  );
129  }
130 
137  public function logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
138  $this->setUpVfsStream();
139  $this->createWriter()->writeLog($record);
140  $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
141  $this->assertEquals($expectedResult, $logFileContents);
142  }
143 
150  public function differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
151  $this->setUpVfsStream();
152  $firstWriter = $this->createWriter();
153  $secondWriter = $this->createWriter('second-');
154 
155  $firstWriter->writeLog($record);
156  $secondWriter->writeLog($record);
157 
158  $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
159  $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
160 
161  $this->assertEquals($expectedResult, $firstLogFileContents);
162  $this->assertEquals($expectedResult, $secondLogFileContents);
163  }
164 
169  $this->setUpVfsStream();
170 
171  $firstWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('dummy'));
172  $secondWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('createLogFile'));
173 
174  $secondWriter->expects($this->never())->method('createLogFile');
175 
176  $logFilePrefix = $this->getUniqueId('unique');
177  $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
178  $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
179 
180  }
181 }
differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)
logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult)