Browse Source

Remove unnecessary method calls.

ADmad 4 years ago
parent
commit
e4b7be693b

+ 6 - 6
src/Database/Driver.php

@@ -113,22 +113,22 @@ abstract class Driver implements DriverInterface
      *
      * @param string $dsn A Driver-specific PDO-DSN
      * @param array<string, mixed> $config configuration to be used for creating connection
-     * @return void
+     * @return \PDO
      */
-    protected function _connect(string $dsn, array $config): void
+    protected function _connect(string $dsn, array $config): PDO
     {
-        $action = function () use ($dsn, $config): void {
-            $this->setConnection(new PDO(
+        $action = function () use ($dsn, $config): PDO {
+            return new PDO(
                 $dsn,
                 $config['username'] ?: null,
                 $config['password'] ?: null,
                 $config['flags']
-            ));
+            );
         };
 
         $retry = new CommandRetry(new ErrorCodeWaitStrategy(static::RETRY_ERROR_CODES, 5), 4);
         try {
-            $retry->run($action);
+            return $retry->run($action);
         } catch (PDOException $e) {
             throw new MissingConnectionException(
                 [

+ 2 - 3
src/Database/Driver/Mysql.php

@@ -159,12 +159,11 @@ class Mysql extends Driver
             $dsn .= ";charset={$config['encoding']}";
         }
 
-        $this->_connect($dsn, $config);
+        $this->_connection = $this->_connect($dsn, $config);
 
         if (!empty($config['init'])) {
-            $connection = $this->getConnection();
             foreach ((array)$config['init'] as $command) {
-                $connection->exec($command);
+                $this->_connection->exec($command);
             }
         }
     }

+ 3 - 4
src/Database/Driver/Postgres.php

@@ -99,8 +99,7 @@ class Postgres extends Driver
             $dsn = "pgsql:dbname={$config['database']}";
         }
 
-        $this->_connect($dsn, $config);
-        $this->_connection = $connection = $this->getConnection();
+        $this->_connection = $this->_connect($dsn, $config);
         if (!empty($config['encoding'])) {
             $this->setEncoding($config['encoding']);
         }
@@ -110,11 +109,11 @@ class Postgres extends Driver
         }
 
         if (!empty($config['timezone'])) {
-            $config['init'][] = sprintf('SET timezone = %s', $connection->quote($config['timezone']));
+            $config['init'][] = sprintf('SET timezone = %s', $this->_connection->quote($config['timezone']));
         }
 
         foreach ($config['init'] as $command) {
-            $connection->exec($command);
+            $this->_connection->exec($command);
         }
     }
 

+ 2 - 2
src/Database/Driver/Sqlite.php

@@ -133,7 +133,7 @@ class Sqlite extends Driver
         $databaseExists = file_exists($config['database']);
 
         $dsn = "sqlite:{$config['database']}";
-        $this->_connect($dsn, $config);
+        $this->_connection = $this->_connect($dsn, $config);
 
         if (!$databaseExists && $config['database'] !== ':memory:') {
             // phpcs:disable
@@ -143,7 +143,7 @@ class Sqlite extends Driver
 
         if (!empty($config['init'])) {
             foreach ((array)$config['init'] as $command) {
-                $this->getConnection()->exec($command);
+                $this->_connection->exec($command);
             }
         }
     }

+ 4 - 5
src/Database/Driver/Sqlserver.php

@@ -151,22 +151,21 @@ class Sqlserver extends Driver
         if ($config['multiSubnetFailover'] !== null) {
             $dsn .= ";MultiSubnetFailover={$config['multiSubnetFailover']}";
         }
-        $this->_connect($dsn, $config);
 
-        $connection = $this->getConnection();
+        $this->_connection = $this->_connect($dsn, $config);
         if (!empty($config['init'])) {
             foreach ((array)$config['init'] as $command) {
-                $connection->exec($command);
+                $this->_connection->exec($command);
             }
         }
         if (!empty($config['settings']) && is_array($config['settings'])) {
             foreach ($config['settings'] as $key => $value) {
-                $connection->exec("SET {$key} {$value}");
+                $this->_connection->exec("SET {$key} {$value}");
             }
         }
         if (!empty($config['attributes']) && is_array($config['attributes'])) {
             foreach ($config['attributes'] as $key => $value) {
-                $connection->setAttribute($key, $value);
+                $this->_connection->setAttribute($key, $value);
             }
         }
     }

+ 6 - 5
tests/TestCase/Database/Driver/MysqlTest.php

@@ -98,7 +98,7 @@ class MysqlTest extends TestCase
             ],
         ];
         $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
-            ->onlyMethods(['_connect', 'getConnection'])
+            ->onlyMethods(['_connect'])
             ->setConstructorArgs([$config])
             ->getMock();
         $dsn = 'mysql:host=foo;port=3440;dbname=bar';
@@ -120,8 +120,7 @@ class MysqlTest extends TestCase
             ->withConsecutive(['Execute this'], ['this too'], ["SET time_zone = 'Antarctica'"]);
 
         $driver->expects($this->once())->method('_connect')
-            ->with($dsn, $expected);
-        $driver->expects($this->any())->method('getConnection')
+            ->with($dsn, $expected)
             ->will($this->returnValue($connection));
         $driver->connect($config);
     }
@@ -187,10 +186,12 @@ class MysqlTest extends TestCase
 
         /** @var \PHPUnit\Framework\MockObject\MockObject&\Cake\Database\Driver\Mysql $driver */
         $driver = $this->getMockBuilder(Mysql::class)
-            ->onlyMethods(['connect'])
+            ->onlyMethods(['_connect'])
             ->getMock();
 
-        $driver->setConnection($connection);
+        $driver->expects($this->once())
+            ->method('_connect')
+            ->willReturn($connection);
 
         $result = $driver->version();
         $this->assertSame($expectedVersion, $result);

+ 4 - 8
tests/TestCase/Database/Driver/PostgresTest.php

@@ -34,7 +34,7 @@ class PostgresTest extends TestCase
     public function testConnectionConfigDefault(): void
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
-            ->onlyMethods(['_connect', 'getConnection'])
+            ->onlyMethods(['_connect'])
             ->getMock();
         $dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
         $expected = [
@@ -77,8 +77,7 @@ class PostgresTest extends TestCase
             );
 
         $driver->expects($this->once())->method('_connect')
-            ->with($dsn, $expected);
-        $driver->expects($this->any())->method('getConnection')
+            ->with($dsn, $expected)
             ->will($this->returnValue($connection));
 
         $driver->connect();
@@ -103,7 +102,7 @@ class PostgresTest extends TestCase
             'init' => ['Execute this', 'this too'],
         ];
         $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
-            ->onlyMethods(['_connect', 'getConnection', 'setConnection'])
+            ->onlyMethods(['_connect'])
             ->setConstructorArgs([$config])
             ->getMock();
         $dsn = 'pgsql:host=foo;port=3440;dbname=bar';
@@ -137,11 +136,8 @@ class PostgresTest extends TestCase
                 ['SET timezone = Antarctica']
             );
 
-        $driver->setConnection($connection);
         $driver->expects($this->once())->method('_connect')
-            ->with($dsn, $expected);
-
-        $driver->expects($this->any())->method('getConnection')
+            ->with($dsn, $expected)
             ->will($this->returnValue($connection));
 
         $driver->connect();

+ 3 - 3
tests/TestCase/Database/Driver/SqliteTest.php

@@ -72,7 +72,7 @@ class SqliteTest extends TestCase
             'mask' => 0666,
         ];
         $driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
-            ->onlyMethods(['_connect', 'getConnection'])
+            ->onlyMethods(['_connect'])
             ->setConstructorArgs([$config])
             ->getMock();
         $dsn = 'sqlite:bar.db';
@@ -94,9 +94,9 @@ class SqliteTest extends TestCase
             ->withConsecutive(['Execute this'], ['this too']);
 
         $driver->expects($this->once())->method('_connect')
-            ->with($dsn, $expected);
-        $driver->expects($this->any())->method('getConnection')
+            ->with($dsn, $expected)
             ->will($this->returnValue($connection));
+
         $driver->connect($config);
     }
 

+ 2 - 10
tests/TestCase/Database/Driver/SqlserverTest.php

@@ -101,7 +101,7 @@ class SqlserverTest extends TestCase
     public function testDnsString($constructorArgs, $dnsString): void
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
-            ->onlyMethods(['_connect', 'getConnection'])
+            ->onlyMethods(['_connect'])
             ->setConstructorArgs([$constructorArgs])
             ->getMock();
 
@@ -110,15 +110,7 @@ class SqlserverTest extends TestCase
                 $this->assertSame($dns, $dnsString);
 
                 return true;
-            }))
-            ->will($this->returnValue(true));
-
-        $connection = $this->getMockBuilder('PDO')
-            ->disableOriginalConstructor()
-            ->getMock();
-
-        $driver->method('getConnection')
-            ->will($this->returnValue($connection));
+            }));
 
         $driver->connect();
     }