[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / Writer / DatabaseWriterTest.php
index 06b80a9..d38dbf7 100644 (file)
@@ -14,79 +14,54 @@ 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;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
 /**
  * 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::class, 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::class, 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::class, array(), array(), '', FALSE);
-               /** @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->writeLog($logRecordMock);
-       }
+class DatabaseWriterTest extends UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function getTableReturnsPreviouslySetTable()
+    {
+        $logTable = $this->getUniqueId('logtable_');
+        /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
+        $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
+            ->setMethods(['dummy'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $subject->setLogTable($logTable);
+        $this->assertSame($logTable, $subject->getLogTable());
+    }
 
-       /**
-        * @test
-        */
-       public function writeLogInsertsToSpecifiedTable() {
-               $logTable = uniqid('logtable_');
-               $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, 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::class, array(), array(), '', FALSE);
-               /** @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->setLogTable($logTable);
-               $subject->writeLog($logRecordMock);
-       }
+    /**
+     * @test
+     */
+    public function writeLogInsertsToSpecifiedTable()
+    {
+        $logTable = $this->getUniqueId('logtable_');
 
-       /**
-        * @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::class, 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']));
+        $connectionProphecy = $this->prophesize(Connection::class);
+        $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
+        $connectionPoolProphecy->getConnectionForTable(Argument::cetera())->willReturn($connectionProphecy->reveal());
 
-               /** @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);
+        GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
+        $logRecordMock = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
+        $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
+            ->setMethods(['dummy'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $subject->setLogTable($logTable);
 
-               $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', FALSE);
-               $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($logRecordMock);
-       }
-}
\ No newline at end of file
+        $subject->writeLog($logRecordMock);
+    }
+}