[TASK] Doctrine: Migrate DatabaseWriter 54/49254/5
authorWouter Wolters <typo3@wouterwolters.nl>
Thu, 28 Jul 2016 15:00:21 +0000 (17:00 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Fri, 29 Jul 2016 10:37:29 +0000 (12:37 +0200)
Resolves: #77293
Releases: master
Change-Id: I78b6a3294e46127dbea3e1c2f249f93e1c6110e5
Reviewed-on: https://review.typo3.org/49254
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Classes/Log/Writer/DatabaseWriter.php
typo3/sysext/core/Tests/Functional/Log/Writer/DatabaseWriterTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php

index 9495d00..7916e91 100644 (file)
@@ -13,7 +13,9 @@ namespace TYPO3\CMS\Core\Log\Writer;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Log\LogRecord;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Log writer that writes the log records into a database table.
@@ -54,7 +56,6 @@ class DatabaseWriter extends AbstractWriter
      *
      * @param LogRecord $record Log record
      * @return \TYPO3\CMS\Core\Log\Writer\WriterInterface $this
-     * @throws \RuntimeException
      */
     public function writeLog(LogRecord $record)
     {
@@ -78,17 +79,10 @@ class DatabaseWriter extends AbstractWriter
             'data' => $data
         );
 
-        if (false === $this->getDatabaseConnection()->exec_INSERTquery($this->logTable, $fieldValues)) {
-            throw new \RuntimeException('Could not write log record to database', 1345036334);
-        }
-        return $this;
-    }
+        GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getConnectionForTable($this->logTable)
+            ->insert($this->logTable, $fieldValues);
 
-    /**
-     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
+        return $this;
     }
 }
diff --git a/typo3/sysext/core/Tests/Functional/Log/Writer/DatabaseWriterTest.php b/typo3/sysext/core/Tests/Functional/Log/Writer/DatabaseWriterTest.php
new file mode 100644 (file)
index 0000000..fc18df8
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Functional\Log\Writer;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Core\Bootstrap;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Log\LogLevel;
+use TYPO3\CMS\Core\Log\LogRecord;
+use TYPO3\CMS\Core\Log\Writer\DatabaseWriter;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+
+/**
+ * Test case
+ */
+class DatabaseWriterTest extends FunctionalTestCase
+{
+    /**
+     * @test
+     */
+    public function writeLogInsertsLogRecordWithGivenProperties()
+    {
+        $logRecordData = array(
+            'request_id' => Bootstrap::getInstance()->getRequestId(),
+            'time_micro' => 1469740000.0,
+            'component' => 'aComponent',
+            'level' => LogLevel::DEBUG,
+            'message' => 'aMessage',
+            'data' => '',
+        );
+        $logRecord = new LogRecord(
+            $logRecordData['component'],
+            $logRecordData['level'],
+            $logRecordData['message'],
+            []
+        );
+        $logRecord->setCreated($logRecordData['time_micro']);
+
+        (new DatabaseWriter())->writeLog($logRecord);
+
+        $rowInDatabase = (new ConnectionPool())->getConnectionForTable('sys_log')
+            ->select(
+                array_keys($logRecordData),
+                'sys_log',
+                ['request_id' => $logRecordData['request_id']]
+            )
+            ->fetch();
+
+        $this->assertEquals($logRecordData, $rowInDatabase);
+    }
+}
index 653b4c0..2624be0 100644 (file)
@@ -14,6 +14,11 @@ namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
+use TYPO3\CMS\Core\Database\Connection;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Test case
  */
@@ -37,71 +42,25 @@ class DatabaseWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @test
      */
-    public function writeLogThrowsExceptionIfDatabaseInsertFailed()
-    {
-        $this->expectException(\RuntimeException::class);
-        $this->expectExceptionCode(1345036334);
-
-        $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
-        $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->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
-        /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
-        $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
-            ->setMethods(array('dummy'))
-            ->disableOriginalConstructor()
-            ->getMock();
-        $subject->writeLog($logRecordMock);
-    }
-
-    /**
-     * @test
-     */
     public function writeLogInsertsToSpecifiedTable()
     {
         $logTable = $this->getUniqueId('logtable_');
-        $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($logTable, $this->anything());
-        /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
+
+        $connectionProphecy = $this->prophesize(Connection::class);
+        $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
+        $connectionPoolProphecy->getConnectionForTable(Argument::cetera())->willReturn($connectionProphecy->reveal());
+
+        GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
         $logRecordMock = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
-        /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
         $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
             ->setMethods(array('dummy'))
             ->disableOriginalConstructor()
             ->getMock();
         $subject->setLogTable($logTable);
-        $subject->writeLog($logRecordMock);
-    }
-
-    /**
-     * @test
-     */
-    public function writeLogInsertsLogRecordWithGivenProperties()
-    {
-        $logRecordData = array(
-            'request_id' => $this->getUniqueId('request_id'),
-            'time_micro' => $this->getUniqueId('time_micro'),
-            'component' => $this->getUniqueId('component'),
-            'level' => $this->getUniqueId('level'),
-            'message' => $this->getUniqueId('message'),
-            'data' => '',
-        );
-        /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
-        $logRecordFixture = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
-        $logRecordFixture->expects($this->any())->method('getRequestId')->will($this->returnValue($logRecordData['request_id']));
-        $logRecordFixture->expects($this->any())->method('getCreated')->will($this->returnValue($logRecordData['time_micro']));
-        $logRecordFixture->expects($this->any())->method('getComponent')->will($this->returnValue($logRecordData['component']));
-        $logRecordFixture->expects($this->any())->method('getLevel')->will($this->returnValue($logRecordData['level']));
-        $logRecordFixture->expects($this->any())->method('getMessage')->will($this->returnValue($logRecordData['message']));
-        $logRecordFixture->expects($this->any())->method('getData')->will($this->returnValue(array()));
 
-        /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
-        #$subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
-        $subject = new \TYPO3\CMS\Core\Log\Writer\DatabaseWriter();
-
-        $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
-        $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($this->anything(), $logRecordData);
+        // $logTable should end up as first insert argument
+        $connectionProphecy->insert($logTable, Argument::cetera())->willReturn(1);
 
-        $subject->writeLog($logRecordFixture);
+        $subject->writeLog($logRecordMock);
     }
 }