[BUGFIX] Tests: Refactor log writer tests in ext:core 96/28396/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Fri, 14 Mar 2014 18:24:24 +0000 (19:24 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 14 Mar 2014 19:00:51 +0000 (20:00 +0100)
Test files and class naming is adapted to subject class names in
ext:core Log/Writer and the DatabaseWriterTests are rewritten to
not depend on initialized database connection anymore.

Change-Id: Ifff4e5a064018bb5b0db3bf9eeb1b9232adfb262
Resolves: #56921
Related: #51436
Releases: 6.2
Reviewed-on: https://review.typo3.org/28396
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/core/Classes/Log/LogRecord.php
typo3/sysext/core/Tests/Unit/Log/Writer/AbstractTest.php [deleted file]
typo3/sysext/core/Tests/Unit/Log/Writer/AbstractWriterTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseTest.php [deleted file]
typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Log/Writer/FileTest.php [deleted file]
typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php [new file with mode: 0644]

index 4f458e4..d49ed52 100644 (file)
@@ -161,7 +161,7 @@ class LogRecord implements \ArrayAccess {
         *
         * @param integer $level Severity level
         * @return \TYPO3\CMS\Core\Log\LogRecord
-        * @throws RangeException if the given log level is invalid
+        * @throws \RangeException if the given log level is invalid
         * @see \TYPO3\CMS\Core\Log\Level
         */
        public function setLevel($level) {
diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/AbstractTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/AbstractTest.php
deleted file mode 100644 (file)
index e7af519..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
-
-/***************************************************************
- * Copyright notice
- *
- * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
-
-/**
- * Testcase for \TYPO3\CMS\Core\Log\Writer\AbstractWriter
- *
- * @author Steffen Gebert <steffen.gebert@typo3.org>
- */
-class AbstractTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
-
-       /**
-        * @test
-        * @expectedException \InvalidArgumentException
-        */
-       public function refusesInvalidConfigurationOptions() {
-               $invalidConfiguration = array(
-                       'foo' => 'bar'
-               );
-               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tests\\Unit\\Log\\Fixtures\\WriterFixture', $invalidConfiguration);
-       }
-
-}
diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/AbstractWriterTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/AbstractWriterTest.php
new file mode 100644 (file)
index 0000000..c5af0d6
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
+
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Test case
+ *
+ * @author Steffen Gebert <steffen.gebert@typo3.org>
+ */
+class AbstractWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function constructThrowsExceptionWithInvalidConfigurationOption() {
+               $invalidConfiguration = array(
+                       'foo' => 'bar'
+               );
+               $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Log\\Writer\\AbstractWriter', array($invalidConfiguration));
+       }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseTest.php
deleted file mode 100644 (file)
index 585f514..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
-
-/***************************************************************
- * Copyright notice
- *
- * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
-
-/**
- * Testcase for \TYPO3\CMS\Core\Log\Writer\DatabaseWriter
- *
- * @author Steffen Gebert <steffen.gebert@typo3.org>
- */
-class DatabaseTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
-
-       /**
-        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Database\DatabaseConnection
-        */
-       protected $databaseStub;
-
-       /**
-        * Create a new database mock object for every test
-        * and backup the original global database object.
-        *
-        * @return void
-        */
-       public function setUp() {
-               $this->databaseStub = $this->setUpAndReturnDatabaseStub();
-       }
-
-       //////////////////////
-       // Utility functions
-       //////////////////////
-       /**
-        * Set up the stub to be able to get the result of the prepared statement.
-        *
-        * @return PHPUnit_Framework_MockObject_MockObject
-        */
-       private function setUpAndReturnDatabaseStub() {
-               $databaseLink = $GLOBALS['TYPO3_DB']->getDatabaseHandle();
-               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('exec_INSERTquery'), array(), '', FALSE, FALSE);
-               $GLOBALS['TYPO3_DB']->setDatabaseHandle($databaseLink);
-               return $GLOBALS['TYPO3_DB'];
-       }
-
-       /**
-        * Creates a test logger
-        *
-        * @return \TYPO3\CMS\Core\Log\Logger
-        */
-       protected function createLogger() {
-               $loggerName = uniqid('test.core.datbaseWriter');
-               /** @var \TYPO3\CMS\Core\Log\Logger $logger */
-               $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger($loggerName);
-               return $logger;
-       }
-
-       /**
-        * Creates a database writer
-        *
-        * @return \TYPO3\CMS\Core\Log\Writer\DatabaseWriter
-        */
-       protected function createWriter() {
-               /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter $writer */
-               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\DatabaseWriter');
-               return $writer;
-       }
-
-       /**
-        * @test
-        */
-       public function setLogTableSetsLogTable() {
-               $logTable = uniqid('logtable_');
-               $this->assertSame($logTable, $this->createWriter()->setLogTable($logTable)->getLogTable());
-       }
-
-       /**
-        * @return array
-        */
-       public function writerLogsToDatabaseDataProvider() {
-               $simpleRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', 'test.core.databaseWriter.simpleRecord', \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test entry');
-               $recordWithData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', 'test.core.databaseWriter.recordWithData', \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test entry with data', array('foo' => array('bar' => 'baz')));
-               return array(
-                       'simple record' => array($simpleRecord),
-                       'record with data' => array($recordWithData)
-               );
-       }
-
-       /**
-        * @test
-        * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
-        * @dataProvider writerLogsToDatabaseDataProvider
-        */
-       public function writerLogsToDatabase(\TYPO3\CMS\Core\Log\LogRecord $record) {
-               $logger = $this->createLogger();
-               $databaseWriter = $this->createWriter();
-               $logger->addWriter(\TYPO3\CMS\Core\Log\LogLevel::NOTICE, $databaseWriter);
-               $this->databaseStub->expects($this->once())->method('exec_INSERTquery');
-               $logger->log($record->getLevel(), $record->getMessage());
-       }
-
-}
diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php
new file mode 100644 (file)
index 0000000..76266ca
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
+
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Test case
+ *
+ * @author Steffen Gebert <steffen.gebert@typo3.org>
+ */
+class DatabaseWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @test
+        */
+       public function getTableReturnsPreviouslySetTable() {
+               $logTable = uniqid('logtable_');
+               /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
+               $subject->setLogTable($logTable);
+               $this->assertSame($logTable, $subject->getLogTable());
+       }
+
+       /**
+        * @test
+        * @expectedException \RuntimeException
+        */
+       public function writeLogThrowsExceptionIfDatabaseInsertFailed() {
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->will($this->returnValue(FALSE));
+               /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $logRecordMock = $this->getMock('TYPO3\\CMS\\Core\\Log\\LogRecord', array(), array(), '', FALSE);
+               /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
+               $subject->writeLog($logRecordMock);
+       }
+
+       /**
+        * @test
+        */
+       public function writeLogInsertsToSpecifiedTable() {
+               $logTable = uniqid('logtable_');
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($logTable, $this->anything());
+               /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $logRecordMock = $this->getMock('TYPO3\\CMS\\Core\\Log\\LogRecord', array(), array(), '', FALSE);
+               /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
+               $subject->setLogTable($logTable);
+               $subject->writeLog($logRecordMock);
+       }
+
+       /**
+        * @test
+        */
+       public function writeLogInsertsLogRecordWithGivenProperties() {
+               $logRecordData = array(
+                       'request_id' => uniqid('request_id'),
+                       'time_micro' => uniqid('time_micro'),
+                       'component' => uniqid('component'),
+                       'level' => uniqid('level'),
+                       'message' => uniqid('message'),
+                       'data' => '',
+               );
+               /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $logRecordMock = $this->getMock('TYPO3\\CMS\\Core\\Log\\LogRecord', array(), array(), '', FALSE);
+               $logRecordMock->expects($this->at(0))->method('offsetGet')->with('requestId')->will($this->returnValue($logRecordData['request_id']));
+               $logRecordMock->expects($this->at(1))->method('offsetGet')->with('created')->will($this->returnValue($logRecordData['time_micro']));
+               $logRecordMock->expects($this->at(2))->method('offsetGet')->with('component')->will($this->returnValue($logRecordData['component']));
+               $logRecordMock->expects($this->at(3))->method('offsetGet')->with('level')->will($this->returnValue($logRecordData['level']));
+               $logRecordMock->expects($this->at(4))->method('offsetGet')->with('message')->will($this->returnValue($logRecordData['message']));
+
+               /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
+               $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
+
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($this->anything(), $logRecordData);
+
+               $subject->writeLog($logRecordMock);
+       }
+}
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/FileTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/FileTest.php
deleted file mode 100644 (file)
index a6ec2e7..0000000
+++ /dev/null
@@ -1,216 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
-
-/***************************************************************
- * Copyright notice
- *
- * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
-
-use \org\bovigo\vfs\vfsStream;
-use \org\bovigo\vfs\vfsStreamDirectory;
-use \org\bovigo\vfs\vfsStreamWrapper;
-
-/**
- * Testcase for \TYPO3\CMS\Core\Log\Writer\FileWriter
- *
- * @author Steffen Gebert <steffen.gebert@typo3.org>
- */
-class FileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
-
-       /**
-        * @var string
-        */
-       protected $logFileDirectory = 'Log';
-
-       /**
-        * @var string
-        */
-       protected $logFileName = 'test.log';
-
-       protected function setUpVfsStream() {
-               if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
-                       $this->markTestSkipped('File backend tests are not available with this phpunit version.');
-               }
-               vfsStream::setup('LogRoot');
-       }
-
-       /**
-        * Creates a test logger
-        *
-        * @param string $name
-        * @internal param string $component Component key
-        * @return \TYPO3\CMS\Core\Log\Logger
-        */
-       protected function createLogger($name = '') {
-               if (empty($name)) {
-                       $name = uniqid('test.core.log.');
-               }
-               \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->registerLogger($name);
-               /** @var \TYPO3\CMS\Core\Log\Logger $logger */
-               $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger($name);
-               return $logger;
-       }
-
-       /**
-        * Creates a file writer
-        *
-        * @param string $prependName
-        * @return \TYPO3\CMS\Core\Log\Writer\FileWriter
-        */
-       protected function createWriter($prependName = '') {
-               /** @var \TYPO3\CMS\Core\Log\Writer\FileWriter $writer */
-               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array(
-                       'logFile' => $this->getDefaultFileName($prependName)
-               ));
-               return $writer;
-       }
-
-       protected function getDefaultFileName($prependName = '') {
-               return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . $this->logFileName;
-       }
-
-       /**
-        * @test
-        */
-       public function setLogFileSetsLogFile() {
-               $this->setUpVfsStream();
-               vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
-               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
-               $writer->setLogFile($this->getDefaultFileName());
-               $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
-       }
-
-       /**
-        * @test
-        * @expectedException \InvalidArgumentException
-        */
-       public function setLogFileRefusesIllegalPath() {
-               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
-               $writer->setLogFile('/tmp/typo3.log');
-       }
-
-       /**
-        * @test
-        */
-       public function createsLogFileDirectory() {
-               $this->setUpVfsStream();
-               $this->createWriter();
-               $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
-       }
-
-       /**
-        * @test
-        */
-       public function createsLogFile() {
-               $this->setUpVfsStream();
-               $this->createWriter();
-               $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
-       }
-
-       /**
-        * @return array
-        */
-       public function logsToFileDataProvider() {
-               $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');
-               $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')));
-               return array(
-                       'simple record' => array($simpleRecord, trim((string) $simpleRecord)),
-                       'record with data' => array($recordWithData, trim((string) $recordWithData))
-               );
-       }
-
-       /**
-        * @test
-        * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
-        * @param string $expectedResult Needle
-        * @dataProvider logsToFileDataProvider
-        */
-       public function logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
-               $this->setUpVfsStream();
-               $this->createWriter()->writeLog($record);
-               $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
-               $this->assertEquals($expectedResult, $logFileContents);
-       }
-
-       /**
-        * @test
-        * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
-        * @param string $expectedResult Needle
-        * @dataProvider logsToFileDataProvider
-        */
-       public function differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
-               $this->setUpVfsStream();
-               $firstWriter = $this->createWriter();
-               $secondWriter = $this->createWriter('second-');
-
-               $firstWriter->writeLog($record);
-               $secondWriter->writeLog($record);
-
-               $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
-               $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
-
-               $this->assertEquals($expectedResult, $firstLogFileContents);
-               $this->assertEquals($expectedResult, $secondLogFileContents);
-       }
-
-       /**
-        * @test
-        */
-       public function aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice() {
-               $this->setUpVfsStream();
-
-               $firstWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('dummy'));
-               $secondWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('createLogFile'));
-
-               $secondWriter->expects($this->never())->method('createLogFile');
-
-               $logFilePrefix = uniqid('unique');
-               $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
-               $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
-
-       }
-
-       /**
-        * @test
-        */
-       public function createsHtaccessForNewDirectory() {
-               $this->setUpVfsStream();
-               $directory = uniqid('Log');
-               $logFile = 'vfs://LogRoot/' . $directory . '/' . $this->logFileName;
-               $this->createWriter()->setLogFile($logFile);
-               $this->assertFileExists('vfs://LogRoot/' . $directory . '/.htaccess');
-       }
-
-       /**
-        * @test
-        */
-       public function createsNoHtaccessForExistingDirectory() {
-               $this->setUpVfsStream();
-               $directory = uniqid('Log');
-                       // create a directory
-               vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory($directory));
-               $logFile = 'vfs://LogRoot/' . $directory . '/' . $this->logFileName;
-               $this->assertTrue(is_dir('vfs://LogRoot/' . $directory));
-               $this->createWriter()->setLogFile($logFile);
-               $this->assertFileNotExists('vfs://LogRoot/' . $directory . '/.htaccess');
-       }
-
-}
diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php
new file mode 100644 (file)
index 0000000..bdd6a9b
--- /dev/null
@@ -0,0 +1,216 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
+
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use \org\bovigo\vfs\vfsStream;
+use \org\bovigo\vfs\vfsStreamDirectory;
+use \org\bovigo\vfs\vfsStreamWrapper;
+
+/**
+ * Test case
+ *
+ * @author Steffen Gebert <steffen.gebert@typo3.org>
+ */
+class FileWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var string
+        */
+       protected $logFileDirectory = 'Log';
+
+       /**
+        * @var string
+        */
+       protected $logFileName = 'test.log';
+
+       protected function setUpVfsStream() {
+               if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
+                       $this->markTestSkipped('File backend tests are not available with this phpunit version.');
+               }
+               vfsStream::setup('LogRoot');
+       }
+
+       /**
+        * Creates a test logger
+        *
+        * @param string $name
+        * @internal param string $component Component key
+        * @return \TYPO3\CMS\Core\Log\Logger
+        */
+       protected function createLogger($name = '') {
+               if (empty($name)) {
+                       $name = uniqid('test.core.log.');
+               }
+               \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->registerLogger($name);
+               /** @var \TYPO3\CMS\Core\Log\Logger $logger */
+               $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger($name);
+               return $logger;
+       }
+
+       /**
+        * Creates a file writer
+        *
+        * @param string $prependName
+        * @return \TYPO3\CMS\Core\Log\Writer\FileWriter
+        */
+       protected function createWriter($prependName = '') {
+               /** @var \TYPO3\CMS\Core\Log\Writer\FileWriter $writer */
+               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array(
+                       'logFile' => $this->getDefaultFileName($prependName)
+               ));
+               return $writer;
+       }
+
+       protected function getDefaultFileName($prependName = '') {
+               return 'vfs://LogRoot/' . $this->logFileDirectory . '/' . $prependName . $this->logFileName;
+       }
+
+       /**
+        * @test
+        */
+       public function setLogFileSetsLogFile() {
+               $this->setUpVfsStream();
+               vfsStream::newFile($this->logFileName)->at(vfsStreamWrapper::getRoot());
+               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
+               $writer->setLogFile($this->getDefaultFileName());
+               $this->assertAttributeEquals($this->getDefaultFileName(), 'logFile', $writer);
+       }
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function setLogFileRefusesIllegalPath() {
+               $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter');
+               $writer->setLogFile('/tmp/typo3.log');
+       }
+
+       /**
+        * @test
+        */
+       public function createsLogFileDirectory() {
+               $this->setUpVfsStream();
+               $this->createWriter();
+               $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($this->logFileDirectory));
+       }
+
+       /**
+        * @test
+        */
+       public function createsLogFile() {
+               $this->setUpVfsStream();
+               $this->createWriter();
+               $this->assertTrue(vfsStreamWrapper::getRoot()->getChild($this->logFileDirectory)->hasChild($this->logFileName));
+       }
+
+       /**
+        * @return array
+        */
+       public function logsToFileDataProvider() {
+               $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');
+               $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')));
+               return array(
+                       'simple record' => array($simpleRecord, trim((string) $simpleRecord)),
+                       'record with data' => array($recordWithData, trim((string) $recordWithData))
+               );
+       }
+
+       /**
+        * @test
+        * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
+        * @param string $expectedResult Needle
+        * @dataProvider logsToFileDataProvider
+        */
+       public function logsToFile(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
+               $this->setUpVfsStream();
+               $this->createWriter()->writeLog($record);
+               $logFileContents = trim(file_get_contents($this->getDefaultFileName()));
+               $this->assertEquals($expectedResult, $logFileContents);
+       }
+
+       /**
+        * @test
+        * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
+        * @param string $expectedResult Needle
+        * @dataProvider logsToFileDataProvider
+        */
+       public function differentWritersLogToDifferentFiles(\TYPO3\CMS\Core\Log\LogRecord $record, $expectedResult) {
+               $this->setUpVfsStream();
+               $firstWriter = $this->createWriter();
+               $secondWriter = $this->createWriter('second-');
+
+               $firstWriter->writeLog($record);
+               $secondWriter->writeLog($record);
+
+               $firstLogFileContents = trim(file_get_contents($this->getDefaultFileName()));
+               $secondLogFileContents = trim(file_get_contents($this->getDefaultFileName('second-')));
+
+               $this->assertEquals($expectedResult, $firstLogFileContents);
+               $this->assertEquals($expectedResult, $secondLogFileContents);
+       }
+
+       /**
+        * @test
+        */
+       public function aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice() {
+               $this->setUpVfsStream();
+
+               $firstWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('dummy'));
+               $secondWriter = $this->getMock('TYPO3\\CMS\\Core\\Log\\Writer\\FileWriter', array('createLogFile'));
+
+               $secondWriter->expects($this->never())->method('createLogFile');
+
+               $logFilePrefix = uniqid('unique');
+               $firstWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
+               $secondWriter->setLogFile($this->getDefaultFileName($logFilePrefix));
+
+       }
+
+       /**
+        * @test
+        */
+       public function createsHtaccessForNewDirectory() {
+               $this->setUpVfsStream();
+               $directory = uniqid('Log');
+               $logFile = 'vfs://LogRoot/' . $directory . '/' . $this->logFileName;
+               $this->createWriter()->setLogFile($logFile);
+               $this->assertFileExists('vfs://LogRoot/' . $directory . '/.htaccess');
+       }
+
+       /**
+        * @test
+        */
+       public function createsNoHtaccessForExistingDirectory() {
+               $this->setUpVfsStream();
+               $directory = uniqid('Log');
+                       // create a directory
+               vfsStreamWrapper::getRoot()->addChild(new vfsStreamDirectory($directory));
+               $logFile = 'vfs://LogRoot/' . $directory . '/' . $this->logFileName;
+               $this->assertTrue(is_dir('vfs://LogRoot/' . $directory));
+               $this->createWriter()->setLogFile($logFile);
+               $this->assertFileNotExists('vfs://LogRoot/' . $directory . '/.htaccess');
+       }
+
+}