Browse Source

Add DriverInterface::getConnection()/setConnection().

ADmad 8 years ago
parent
commit
dd72625808

+ 35 - 2
src/Database/Driver.php

@@ -91,7 +91,7 @@ abstract class Driver implements DriverInterface
             $config['password'],
             $config['flags']
         );
-        $this->connection($connection);
+        $this->setConnection($connection);
 
         return true;
     }
@@ -110,10 +110,20 @@ abstract class Driver implements DriverInterface
     }
 
     /**
-     * {@inheritDoc}
+     * Returns correct connection resource or object that is internally used
+     * If first argument is passed, it will set internal connection object or
+     * result to the value passed.
+     *
+     * @param mixed $connection The PDO connection instance.
+     * @return mixed Connection object used internally.
+     * @deprecated 3.6.0 Use getConnection()/setConnection() instead.
      */
     public function connection($connection = null)
     {
+        deprecationWarning(
+            get_called_class() . '::connection() is deprecated. ' .
+            'Use setConnection()/getConnection() instead.'
+        );
         if ($connection !== null) {
             $this->_connection = $connection;
         }
@@ -122,6 +132,29 @@ abstract class Driver implements DriverInterface
     }
 
     /**
+     * Get the internal PDO connection instance.
+     *
+     * @return \PDO
+     */
+    public function getConnection()
+    {
+        return $this->_connection;
+    }
+
+    /**
+     * Set the internal PDO connection instance.
+     *
+     * @param \PDO $connection PDO instance.
+     * @return $this
+     */
+    public function setConnection($connection)
+    {
+        $this->_connection = $connection;
+
+        return $this;
+    }
+
+    /**
      * {@inheritDoc}
      */
     abstract public function enabled();

+ 1 - 1
src/Database/Driver/Postgres.php

@@ -65,7 +65,7 @@ class Postgres extends Driver
         }
 
         $this->_connect($dsn, $config);
-        $this->_connection = $connection = $this->connection();
+        $this->_connection = $connection = $this->getConnection();
         if (!empty($config['encoding'])) {
             $this->setEncoding($config['encoding']);
         }

+ 1 - 1
src/Database/Driver/Sqlserver.php

@@ -105,7 +105,7 @@ class Sqlserver extends Driver
         }
         $this->_connect($dsn, $config);
 
-        $connection = $this->connection();
+        $connection = $this->getConnection();
         if (!empty($config['init'])) {
             foreach ((array)$config['init'] as $command) {
                 $connection->exec($command);

+ 10 - 5
src/Database/DriverInterface.php

@@ -40,14 +40,19 @@ interface DriverInterface
     public function disconnect();
 
     /**
-     * Returns correct connection resource or object that is internally used
-     * If first argument is passed, it will set internal connection object or
-     * result to the value passed.
+     * Returns correct connection resource or object that is internally used.
      *
-     * @param mixed $connection The PDO connection instance.
      * @return mixed Connection object used internally.
      */
-    public function connection($connection = null);
+    public function getConnection();
+
+    /**
+     * Set the internal connection object.
+     *
+     * @param mixed $connection The connection instance.
+     * @return $this
+     */
+    public function setConnection($connection);
 
     /**
      * Returns whether php is able to use this driver for connecting to database.

+ 5 - 5
tests/TestCase/Database/Driver/PostgresTest.php

@@ -32,7 +32,7 @@ class PostgresTest extends TestCase
     public function testConnectionConfigDefault()
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
-            ->setMethods(['_connect', 'connection'])
+            ->setMethods(['_connect', 'getConnection'])
             ->getMock();
         $dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
         $expected = [
@@ -72,7 +72,7 @@ class PostgresTest extends TestCase
 
         $driver->expects($this->once())->method('_connect')
             ->with($dsn, $expected);
-        $driver->expects($this->any())->method('connection')
+        $driver->expects($this->any())->method('getConnection')
             ->will($this->returnValue($connection));
 
         $driver->connect();
@@ -99,7 +99,7 @@ class PostgresTest extends TestCase
             'init' => ['Execute this', 'this too']
         ];
         $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
-            ->setMethods(['_connect', 'connection'])
+            ->setMethods(['_connect', 'getConnection', 'setConnection'])
             ->setConstructorArgs([$config])
             ->getMock();
         $dsn = 'pgsql:host=foo;port=3440;dbname=bar';
@@ -129,11 +129,11 @@ class PostgresTest extends TestCase
         $connection->expects($this->at(7))->method('exec')->with('SET timezone = Antarctica');
         $connection->expects($this->exactly(5))->method('exec');
 
-        $driver->connection($connection);
+        $driver->setConnection($connection);
         $driver->expects($this->once())->method('_connect')
             ->with($dsn, $expected);
 
-        $driver->expects($this->any())->method('connection')
+        $driver->expects($this->any())->method('getConnection')
             ->will($this->returnValue($connection));
 
         $driver->connect();

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

@@ -138,7 +138,7 @@ class SqliteTest extends TestCase
             ->will($this->returnCallback(function ($value) {
                 return '"' . $value . '"';
             }));
-        $driver->connection($mock);
+        $driver->setConnection($mock);
         $this->assertEquals($expected, $driver->schemaValue($input));
     }
 }

+ 1 - 1
tests/TestCase/Database/Driver/SqlserverTest.php

@@ -161,7 +161,7 @@ class SqlserverTest extends TestCase
         $connection->expects($this->at(2))->method('exec')->with('SET config1 value1');
         $connection->expects($this->at(3))->method('exec')->with('SET config2 value2');
 
-        $driver->connection($connection);
+        $driver->setConnection($connection);
         $driver->expects($this->once())->method('_connect')
             ->with($dsn, $expected);
 

+ 6 - 6
tests/TestCase/Database/DriverTest.php

@@ -98,7 +98,7 @@ class DriverTest extends TestCase
             ->with(PDO::ATTR_DRIVER_NAME)
             ->willReturn('mysql');
 
-        $this->driver->connection($connection);
+        $this->driver->setConnection($connection);
 
         $result = $this->driver->supportsQuoting();
         $this->assertTrue($result);
@@ -137,7 +137,7 @@ class DriverTest extends TestCase
             ->method('quote')
             ->with($value, PDO::PARAM_STR);
 
-        $this->driver->connection($connection);
+        $this->driver->setConnection($connection);
 
         $this->driver->schemaValue($value);
     }
@@ -159,7 +159,7 @@ class DriverTest extends TestCase
             ->method('lastInsertId')
             ->willReturn('all-the-bears');
 
-        $this->driver->connection($connection);
+        $this->driver->setConnection($connection);
         $this->assertSame('all-the-bears', $this->driver->lastInsertId());
     }
 
@@ -182,7 +182,7 @@ class DriverTest extends TestCase
             ->method('query')
             ->willReturn(true);
 
-        $this->driver->connection($connection);
+        $this->driver->setConnection($connection);
         $this->assertTrue($this->driver->isConnected());
     }
 
@@ -274,10 +274,10 @@ class DriverTest extends TestCase
      */
     public function testDestructor()
     {
-        $this->driver->connection(true);
+        $this->driver->setConnection(true);
         $this->driver->__destruct();
 
-        $this->assertNull($this->driver->connection());
+        $this->assertNull($this->driver->getConnection());
     }
 
     /**

+ 3 - 3
tests/TestCase/Database/Schema/MysqlSchemaTest.php

@@ -1038,7 +1038,7 @@ SQL;
         $connection->expects($this->any())->method('getDriver')
             ->will($this->returnValue($driver));
 
-        $driver->connection()
+        $driver->getConnection()
             ->expects($this->any())
             ->method('getAttribute')
             ->will($this->returnValue('5.6.0'));
@@ -1108,7 +1108,7 @@ SQL;
             ->method('getDriver')
             ->will($this->returnValue($driver));
 
-        $driver->connection()
+        $driver->getConnection()
             ->expects($this->any())
             ->method('getAttribute')
             ->will($this->returnValue('5.7.0'));
@@ -1329,7 +1329,7 @@ SQL;
             ->will($this->returnCallback(function ($value) {
                 return "'$value'";
             }));
-        $driver->connection($mock);
+        $driver->setConnection($mock);
 
         return $driver;
     }

+ 1 - 1
tests/TestCase/Database/Schema/PostgresSchemaTest.php

@@ -1264,7 +1264,7 @@ SQL;
             ->will($this->returnCallback(function ($value) {
                 return "'$value'";
             }));
-        $driver->connection($mock);
+        $driver->setConnection($mock);
 
         return $driver;
     }

+ 3 - 3
tests/TestCase/Database/Schema/SqliteSchemaTest.php

@@ -1026,7 +1026,7 @@ SQL;
         $statement = $this->getMockBuilder('\PDOStatement')
             ->setMethods(['execute', 'rowCount', 'closeCursor', 'fetch'])
             ->getMock();
-        $driver->connection()->expects($this->once())->method('prepare')
+        $driver->getConnection()->expects($this->once())->method('prepare')
             ->with('SELECT 1 FROM sqlite_master WHERE name = "sqlite_sequence"')
             ->will($this->returnValue($statement));
         $statement->expects($this->at(0))->method('fetch')
@@ -1058,7 +1058,7 @@ SQL;
         $statement = $this->getMockBuilder('\PDOStatement')
             ->setMethods(['execute', 'rowCount', 'closeCursor', 'fetch'])
             ->getMock();
-        $driver->connection()->expects($this->once())->method('prepare')
+        $driver->getConnection()->expects($this->once())->method('prepare')
             ->with('SELECT 1 FROM sqlite_master WHERE name = "sqlite_sequence"')
             ->will($this->returnValue($statement));
         $statement->expects($this->once())->method('fetch')
@@ -1087,7 +1087,7 @@ SQL;
             ->will($this->returnCallback(function ($value) {
                 return '"' . $value . '"';
             }));
-        $driver->connection($mock);
+        $driver->setConnection($mock);
 
         return $driver;
     }

+ 1 - 1
tests/TestCase/Database/Schema/SqlserverSchemaTest.php

@@ -1021,7 +1021,7 @@ SQL;
             ->will($this->returnCallback(function ($value) {
                 return "'$value'";
             }));
-        $driver->connection($mock);
+        $driver->setConnection($mock);
 
         return $driver;
     }