From 32c9d2b4bfcc89ce79c08d26cf018109c9c61ae0 Mon Sep 17 00:00:00 2001
From: Anja Leichsenring <aleichsenring@ab-softlab.de>
Date: Wed, 30 Sep 2020 13:17:42 +0200
Subject: [PATCH] [TASK] Improve mocking in ConnectionTest to prepare for dbal
 2.11

The tests need some stuff from the constructor to not throw errors now,
so the mock got the required objects.

Resolves: #92452
Releases: master, 10.4
Change-Id: Ibc5c6d6ce513d8d8247185710d43e481cf873ba7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65964
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Simon Gilli <typo3@gilbertsoft.org>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Simon Gilli <typo3@gilbertsoft.org>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../Tests/Unit/Database/ConnectionTest.php    | 54 ++++++++++++++-----
 1 file changed, 41 insertions(+), 13 deletions(-)

diff --git a/typo3/sysext/core/Tests/Unit/Database/ConnectionTest.php b/typo3/sysext/core/Tests/Unit/Database/ConnectionTest.php
index 42dbcc94d7a8..d3854ce4ef62 100644
--- a/typo3/sysext/core/Tests/Unit/Database/ConnectionTest.php
+++ b/typo3/sysext/core/Tests/Unit/Database/ConnectionTest.php
@@ -17,9 +17,11 @@ declare(strict_types=1);
 
 namespace TYPO3\CMS\Core\Tests\Unit\Database;
 
+use Doctrine\DBAL\Configuration;
 use Doctrine\DBAL\Driver\Mysqli\Driver;
 use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
 use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
+use Doctrine\DBAL\Platforms\MySqlPlatform;
 use Doctrine\DBAL\Statement;
 use Doctrine\DBAL\VersionAwarePlatformDriver;
 use Prophecy\Prophecy\ObjectProphecy;
@@ -58,18 +60,19 @@ class ConnectionTest extends UnitTestCase
         parent::setUp();
 
         $this->connection = $this->getMockBuilder(Connection::class)
-            ->disableOriginalConstructor()
             ->setMethods(
                 [
                     'connect',
                     'executeQuery',
                     'executeUpdate',
+                    'executeStatement',
                     'getDatabasePlatform',
                     'getDriver',
                     'getExpressionBuilder',
                     'getWrappedConnection',
                 ]
             )
+            ->setConstructorArgs([['platform' => $this->prophesize(MySqlPlatform::class)->reveal()], $this->prophesize(Driver::class)->reveal(), new Configuration(), null])
             ->getMock();
 
         $this->connection->expects(self::any())
@@ -218,10 +221,19 @@ class ConnectionTest extends UnitTestCase
      */
     public function insertQueries(array $args, string $expectedQuery, array $expectedValues, array $expectedTypes)
     {
-        $this->connection->expects(self::once())
-            ->method('executeUpdate')
-            ->with($expectedQuery, $expectedValues, $expectedTypes)
-            ->willReturn(1);
+
+        // @todo drop else branch and condition once doctrine/dbal is requried in version 2.11.0 minimum
+        if (method_exists(Connection::class, 'executeStatement')) {
+            $this->connection->expects(self::once())
+                ->method('executeStatement')
+                ->with($expectedQuery, $expectedValues, $expectedTypes)
+                ->willReturn(1);
+        } else {
+            $this->connection->expects(self::once())
+                ->method('executeUpdate')
+                ->with($expectedQuery, $expectedValues, $expectedTypes)
+                ->willReturn(1);
+        }
 
         $this->connection->insert(...$args);
     }
@@ -282,10 +294,18 @@ class ConnectionTest extends UnitTestCase
      */
     public function updateQueries(array $args, string $expectedQuery, array $expectedValues, array $expectedTypes)
     {
-        $this->connection->expects(self::once())
-            ->method('executeUpdate')
-            ->with($expectedQuery, $expectedValues, $expectedTypes)
-            ->willReturn(1);
+        // @todo drop else branch and condition once doctrine/dbal is requried in version 2.11.0 minimum
+        if (method_exists(Connection::class, 'executeStatement')) {
+            $this->connection->expects(self::once())
+                ->method('executeStatement')
+                ->with($expectedQuery, $expectedValues, $expectedTypes)
+                ->willReturn(1);
+        } else {
+            $this->connection->expects(self::once())
+                ->method('executeUpdate')
+                ->with($expectedQuery, $expectedValues, $expectedTypes)
+                ->willReturn(1);
+        }
 
         $this->connection->update(...$args);
     }
@@ -333,10 +353,18 @@ class ConnectionTest extends UnitTestCase
      */
     public function deleteQueries(array $args, string $expectedQuery, array $expectedValues, array $expectedTypes)
     {
-        $this->connection->expects(self::once())
-            ->method('executeUpdate')
-            ->with($expectedQuery, $expectedValues, $expectedTypes)
-            ->willReturn(1);
+        // @todo drop else branch and condition once doctrine/dbal is requried in version 2.11.0 minimum
+        if (method_exists(Connection::class, 'executeStatement')) {
+            $this->connection->expects(self::once())
+                ->method('executeStatement')
+                ->with($expectedQuery, $expectedValues, $expectedTypes)
+                ->willReturn(1);
+        } else {
+            $this->connection->expects(self::once())
+                ->method('executeUpdate')
+                ->with($expectedQuery, $expectedValues, $expectedTypes)
+                ->willReturn(1);
+        }
 
         $this->connection->delete(...$args);
     }
-- 
GitLab