Browse Source

Merge pull request #11713 from cakephp/3.next-db-driver

3.next db driver
ADmad 8 years ago
parent
commit
390b415c95

+ 1 - 1
phpstan.neon

@@ -26,7 +26,7 @@ parameters:
         - '#Call to an undefined method object::__toString\(\)#'
         - '#Call to an undefined method object::toArray\(\)#'
         - '#Call to an undefined method object::__debugInfo\(\)#'
-        - '#Method PDO::lastInsertId\(\) invoked with 2 parameters, 0-1 required#'
+        - '#Cannot call method lastInsertId\(\) on null#'
     earlyTerminatingMethodCalls:
         Cake\Shell\Shell:
             - abort

+ 157 - 118
src/Database/Driver.php

@@ -14,17 +14,24 @@
  */
 namespace Cake\Database;
 
+use Cake\Database\Query;
+use Cake\Database\Statement\PDOStatement;
 use InvalidArgumentException;
 use PDO;
+use PDOException;
 
 /**
  * Represents a database driver containing all specificities for
  * a database engine including its SQL dialect.
- *
- * @property \PDO|null $_connection
  */
-abstract class Driver
+abstract class Driver implements DriverInterface
 {
+    /**
+     * Instance of PDO.
+     *
+     * @var \PDO|null
+     */
+    protected $_connection;
 
     /**
      * Configuration data.
@@ -72,112 +79,169 @@ abstract class Driver
     /**
      * Establishes a connection to the database server
      *
+     * @param string $dsn A Driver-specific PDO-DSN
+     * @param array $config configuration to be used for creating connection
      * @return bool true on success
      */
+    protected function _connect($dsn, array $config)
+    {
+        $connection = new PDO(
+            $dsn,
+            $config['username'],
+            $config['password'],
+            $config['flags']
+        );
+        $this->setConnection($connection);
+
+        return true;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     abstract public function connect();
 
     /**
-     * Disconnects from database server
-     *
-     * @return void
+     * {@inheritDoc}
      */
-    abstract public function disconnect();
+    public function disconnect()
+    {
+        $this->_connection = null;
+    }
 
     /**
      * Returns correct connection resource or object that is internally used
-     * If first argument is passed,
+     * 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;
+        }
+
+        return $this->_connection;
+    }
+
+    /**
+     * Get the internal PDO connection instance.
      *
-     * @param null|\PDO $connection The connection object
      * @return \PDO
      */
-    abstract public function connection($connection = null);
+    public function getConnection()
+    {
+        return $this->_connection;
+    }
 
     /**
-     * Returns whether php is able to use this driver for connecting to database
+     * Set the internal PDO connection instance.
      *
-     * @return bool true if it is valid to use this driver
+     * @param \PDO $connection PDO instance.
+     * @return $this
+     */
+    public function setConnection($connection)
+    {
+        $this->_connection = $connection;
+
+        return $this;
+    }
+
+    /**
+     * {@inheritDoc}
      */
     abstract public function enabled();
 
     /**
-     * Prepares a sql statement to be executed
-     *
-     * @param string|\Cake\Database\Query $query The query to convert into a statement.
-     * @return \Cake\Database\StatementInterface
+     * {@inheritDoc}
      */
-    abstract public function prepare($query);
+    public function prepare($query)
+    {
+        $this->connect();
+        $isObject = $query instanceof Query;
+        $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
+
+        return new PDOStatement($statement, $this);
+    }
 
     /**
-     * Starts a transaction
-     *
-     * @return bool true on success, false otherwise
+     * {@inheritDoc}
      */
-    abstract public function beginTransaction();
+    public function beginTransaction()
+    {
+        $this->connect();
+        if ($this->_connection->inTransaction()) {
+            return true;
+        }
+
+        return $this->_connection->beginTransaction();
+    }
 
     /**
-     * Commits a transaction
-     *
-     * @return bool true on success, false otherwise
+     * {@inheritDoc}
      */
-    abstract public function commitTransaction();
+    public function commitTransaction()
+    {
+        $this->connect();
+        if (!$this->_connection->inTransaction()) {
+            return false;
+        }
+
+        return $this->_connection->commit();
+    }
 
     /**
-     * Rollsback a transaction
-     *
-     * @return bool true on success, false otherwise
+     * {@inheritDoc}
      */
-    abstract public function rollbackTransaction();
+    public function rollbackTransaction()
+    {
+        $this->connect();
+        if (!$this->_connection->inTransaction()) {
+            return false;
+        }
+
+        return $this->_connection->rollBack();
+    }
 
     /**
-     * Get the SQL for releasing a save point.
-     *
-     * @param string $name The table name
-     * @return string
+     * {@inheritDoc}
      */
     abstract public function releaseSavePointSQL($name);
 
     /**
-     * Get the SQL for creating a save point.
-     *
-     * @param string $name The table name
-     * @return string
+     * {@inheritDoc}
      */
     abstract public function savePointSQL($name);
 
     /**
-     * Get the SQL for rollingback a save point.
-     *
-     * @param string $name The table name
-     * @return string
+     * {@inheritDoc}
      */
     abstract public function rollbackSavePointSQL($name);
 
     /**
-     * Get the SQL for disabling foreign keys
-     *
-     * @return string
+     * {@inheritDoc}
      */
     abstract public function disableForeignKeySQL();
 
     /**
-     * Get the SQL for enabling foreign keys
-     *
-     * @return string
+     * {@inheritDoc}
      */
     abstract public function enableForeignKeySQL();
 
     /**
-     * Returns whether the driver supports adding or dropping constraints
-     * to already created tables.
-     *
-     * @return bool true if driver supports dynamic constraints
+     * {@inheritDoc}
      */
     abstract public function supportsDynamicConstraints();
 
     /**
-     * Returns whether this driver supports save points for nested transactions
-     *
-     * @return bool true if save points are supported, false otherwise
+     * {@inheritDoc}
      */
     public function supportsSavePoints()
     {
@@ -185,62 +249,44 @@ abstract class Driver
     }
 
     /**
-     * Returns a value in a safe representation to be used in a query string
-     *
-     * @param mixed $value The value to quote.
-     * @param string $type Type to be used for determining kind of quoting to perform
-     * @return string
+     * {@inheritDoc}
      */
-    abstract public function quote($value, $type);
+    public function quote($value, $type)
+    {
+        $this->connect();
+
+        return $this->_connection->quote($value, $type);
+    }
 
     /**
-     * Checks if the driver supports quoting
+     * Checks if the driver supports quoting, as PDO_ODBC does not support it.
      *
      * @return bool
      */
     public function supportsQuoting()
     {
-        return true;
+        $this->connect();
+
+        return $this->_connection->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'odbc';
     }
 
     /**
-     * Returns a callable function that will be used to transform a passed Query object.
-     * This function, in turn, will return an instance of a Query object that has been
-     * transformed to accommodate any specificities of the SQL dialect in use.
-     *
-     * @param string $type the type of query to be transformed
-     * (select, insert, update, delete)
-     * @return callable
+     * {@inheritDoc}
      */
     abstract public function queryTranslator($type);
 
     /**
-     * Get the schema dialect.
-     *
-     * Used by Cake\Database\Schema package to reflect schema and
-     * generate schema.
-     *
-     * If all the tables that use this Driver specify their
-     * own schemas, then this may return null.
-     *
-     * @return \Cake\Database\Schema\BaseSchema
+     * {@inheritDoc}
      */
     abstract public function schemaDialect();
 
     /**
-     * Quotes a database identifier (a column name, table name, etc..) to
-     * be used safely in queries without the risk of using reserved words
-     *
-     * @param string $identifier The identifier expression to quote.
-     * @return string
+     * {@inheritDoc}
      */
     abstract public function quoteIdentifier($identifier);
 
     /**
-     * Escapes values for use in schema definitions.
-     *
-     * @param mixed $value The value to escape.
-     * @return string String for use in schema definitions.
+     * {@inheritDoc}
      */
     public function schemaValue($value)
     {
@@ -267,9 +313,7 @@ abstract class Driver
     }
 
     /**
-     * Returns the schema name that's being used
-     *
-     * @return string
+     * {@inheritDoc}
      */
     public function schema()
     {
@@ -277,33 +321,39 @@ abstract class Driver
     }
 
     /**
-     * Returns last id generated for a table or sequence in database
-     *
-     * @param string|null $table table name or sequence to get last insert value from
-     * @param string|null $column the name of the column representing the primary key
-     * @return string|int
+     * {@inheritDoc}
      */
     public function lastInsertId($table = null, $column = null)
     {
+        $this->connect();
+
+        if ($this->_connection instanceof PDO) {
+            return $this->_connection->lastInsertId($table);
+        }
+
         return $this->_connection->lastInsertId($table, $column);
     }
 
     /**
-     * Check whether or not the driver is connected.
-     *
-     * @return bool
+     * {@inheritDoc}
      */
     public function isConnected()
     {
-        return $this->_connection !== null;
+        if ($this->_connection === null) {
+            $connected = false;
+        } else {
+            try {
+                $connected = $this->_connection->query('SELECT 1');
+            } catch (PDOException $e) {
+                $connected = false;
+            }
+        }
+
+        return (bool)$connected;
     }
 
     /**
-     * Sets whether or not this driver should automatically quote identifiers
-     * in queries.
-     *
-     * @param bool $enable Whether to enable auto quoting
-     * @return $this
+     * {@inheritDoc}
      */
     public function enableAutoQuoting($enable = true)
     {
@@ -313,10 +363,7 @@ abstract class Driver
     }
 
     /**
-     * Returns whether or not this driver should automatically quote identifiers
-     * in queries
-     *
-     * @return bool
+     * {@inheritDoc}
      */
     public function isAutoQuotingEnabled()
     {
@@ -348,13 +395,7 @@ abstract class Driver
     }
 
     /**
-     * Transforms the passed query to this Driver's dialect and returns an instance
-     * of the transformed query and the full compiled SQL string
-     *
-     * @param \Cake\Database\Query $query The query to compile.
-     * @param \Cake\Database\ValueBinder $generator The value binder to use.
-     * @return array containing 2 entries. The first entity is the transformed query
-     * and the second one the compiled SQL
+     * {@inheritDoc}
      */
     public function compileQuery(Query $query, ValueBinder $generator)
     {
@@ -366,9 +407,7 @@ abstract class Driver
     }
 
     /**
-     * Returns an instance of a QueryCompiler
-     *
-     * @return \Cake\Database\QueryCompiler
+     * {@inheritDoc}
      */
     public function newCompiler()
     {

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

@@ -24,7 +24,6 @@ class Mysql extends Driver
 {
 
     use MysqlDialectTrait;
-    use PDODriverTrait;
 
     /**
      * Base configuration settings for MySQL driver
@@ -104,7 +103,7 @@ class Mysql extends Driver
         $this->_connect($dsn, $config);
 
         if (!empty($config['init'])) {
-            $connection = $this->connection();
+            $connection = $this->getConnection();
             foreach ((array)$config['init'] as $command) {
                 $connection->exec($command);
             }

+ 2 - 0
src/Database/Driver/PDODriverTrait.php

@@ -21,6 +21,8 @@ use PDOException;
 
 /**
  * PDO driver trait
+ *
+ * @deprecated 3.6.0 The methods of this trait have been added to `Driver` class.
  */
 trait PDODriverTrait
 {

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

@@ -21,7 +21,6 @@ use PDO;
 class Postgres extends Driver
 {
 
-    use PDODriverTrait;
     use PostgresDialectTrait;
 
     /**
@@ -66,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 - 2
src/Database/Driver/Sqlite.php

@@ -24,7 +24,6 @@ use PDO;
 class Sqlite extends Driver
 {
 
-    use PDODriverTrait;
     use SqliteDialectTrait;
 
     /**
@@ -75,7 +74,7 @@ class Sqlite extends Driver
 
         if (!empty($config['init'])) {
             foreach ((array)$config['init'] as $command) {
-                $this->connection()->exec($command);
+                $this->getConnection()->exec($command);
             }
         }
 

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

@@ -26,7 +26,6 @@ use PDO;
 class Sqlserver extends Driver
 {
 
-    use PDODriverTrait;
     use SqlserverDialectTrait;
 
     /**
@@ -106,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);

+ 260 - 0
src/Database/DriverInterface.php

@@ -0,0 +1,260 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Database;
+
+use Cake\Database\Query;
+use Cake\Database\Statement\PDOStatement;
+use InvalidArgumentException;
+use PDO;
+use PDOException;
+
+/**
+ * Interface for database driver.
+ */
+interface DriverInterface
+{
+    /**
+     * Establishes a connection to the database server.
+     *
+     * @return bool True on success, false on failure.
+     */
+    public function connect();
+
+    /**
+     * Disconnects from database server.
+     *
+     * @return void
+     */
+    public function disconnect();
+
+    /**
+     * Returns correct connection resource or object that is internally used.
+     *
+     * @return object Connection object used internally.
+     */
+    public function getConnection();
+
+    /**
+     * Set the internal connection object.
+     *
+     * @param object $connection The connection instance.
+     * @return $this
+     */
+    public function setConnection($connection);
+
+    /**
+     * Returns whether php is able to use this driver for connecting to database.
+     *
+     * @return bool True if it is valid to use this driver.
+     */
+    public function enabled();
+
+    /**
+     * Prepares a sql statement to be executed.
+     *
+     * @param string|\Cake\Database\Query $query The query to turn into a prepared statement.
+     * @return \Cake\Database\StatementInterface
+     */
+    public function prepare($query);
+
+    /**
+     * Starts a transaction.
+     *
+     * @return bool True on success, false otherwise.
+     */
+    public function beginTransaction();
+
+    /**
+     * Commits a transaction.
+     *
+     * @return bool True on success, false otherwise.
+     */
+    public function commitTransaction();
+
+    /**
+     * Rollbacks a transaction.
+     *
+     * @return bool True on success, false otherwise.
+     */
+    public function rollbackTransaction();
+
+    /**
+     * Get the SQL for releasing a save point.
+     *
+     * @param string $name The table name.
+     * @return string
+     */
+    public function releaseSavePointSQL($name);
+
+    /**
+     * Get the SQL for creating a save point.
+     *
+     * @param string $name The table name.
+     * @return string
+     */
+    public function savePointSQL($name);
+
+    /**
+     * Get the SQL for rollingback a save point.
+     *
+     * @param string $name The table name.
+     * @return string
+     */
+    public function rollbackSavePointSQL($name);
+
+    /**
+     * Get the SQL for disabling foreign keys.
+     *
+     * @return string
+     */
+    public function disableForeignKeySQL();
+
+    /**
+     * Get the SQL for enabling foreign keys.
+     *
+     * @return string
+     */
+    public function enableForeignKeySQL();
+
+    /**
+     * Returns whether the driver supports adding or dropping constraints
+     * to already created tables.
+     *
+     * @return bool true if driver supports dynamic constraints.
+     */
+    public function supportsDynamicConstraints();
+
+    /**
+     * Returns whether this driver supports save points for nested transactions.
+     *
+     * @return bool True if save points are supported, false otherwise.
+     */
+    public function supportsSavePoints();
+
+    /**
+     * Returns a value in a safe representation to be used in a query string
+     *
+     * @param mixed $value The value to quote.
+     * @param string $type Type to be used for determining kind of quoting to perform.
+     * @return string
+     */
+    public function quote($value, $type);
+
+    /**
+     * Checks if the driver supports quoting.
+     *
+     * @return bool
+     */
+    public function supportsQuoting();
+
+    /**
+     * Returns a callable function that will be used to transform a passed Query object.
+     * This function, in turn, will return an instance of a Query object that has been
+     * transformed to accommodate any specificities of the SQL dialect in use.
+     *
+     * @param string $type The type of query to be transformed
+     * (select, insert, update, delete).
+     * @return callable
+     */
+    public function queryTranslator($type);
+
+    /**
+     * Get the schema dialect.
+     *
+     * Used by Cake\Database\Schema package to reflect schema and
+     * generate schema.
+     *
+     * If all the tables that use this Driver specify their
+     * own schemas, then this may return null.
+     *
+     * @return \Cake\Database\Schema\BaseSchema
+     */
+    public function schemaDialect();
+
+    /**
+     * Quotes a database identifier (a column name, table name, etc..) to
+     * be used safely in queries without the risk of using reserved words.
+     *
+     * @param string $identifier The identifier expression to quote.
+     * @return string
+     */
+    public function quoteIdentifier($identifier);
+
+    /**
+     * Escapes values for use in schema definitions.
+     *
+     * @param mixed $value The value to escape.
+     * @return string String for use in schema definitions.
+     */
+    public function schemaValue($value);
+
+    /**
+     * Returns the schema name that's being used.
+     *
+     * @return string
+     */
+    public function schema();
+
+    /**
+     * Returns last id generated for a table or sequence in database.
+     *
+     * @param string|null $table table name or sequence to get last insert value from.
+     * @param string|null $column the name of the column representing the primary key.
+     * @return string|int
+     */
+    public function lastInsertId($table = null, $column = null);
+
+    /**
+     * Checks whether or not the driver is connected.
+     *
+     * @return bool
+     */
+    public function isConnected();
+
+    /**
+     * Sets whether or not this driver should automatically quote identifiers
+     * in queries.
+     *
+     * @param bool $enable Whether to enable auto quoting
+     * @return $this
+     */
+    public function enableAutoQuoting($enable = true);
+
+    /**
+     * Returns whether or not this driver should automatically quote identifiers
+     * in queries.
+     *
+     * @return bool
+     */
+    public function isAutoQuotingEnabled();
+
+    /**
+     * Transforms the passed query to this Driver's dialect and returns an instance
+     * of the transformed query and the full compiled SQL string.
+     *
+     * @param \Cake\Database\Query $query The query to compile.
+     * @param \Cake\Database\ValueBinder $generator The value binder to use.
+     * @return array containing 2 entries. The first entity is the transformed query
+     * and the second one the compiled SQL.
+     */
+    public function compileQuery(Query $query, ValueBinder $generator);
+
+    /**
+     * Returns an instance of a QueryCompiler.
+     *
+     * @return \Cake\Database\QueryCompiler
+     */
+    public function newCompiler();
+}

+ 1 - 1
src/Database/Statement/MysqlStatement.php

@@ -32,7 +32,7 @@ class MysqlStatement extends PDOStatement
      */
     public function execute($params = null)
     {
-        $connection = $this->_driver->connection();
+        $connection = $this->_driver->getConnection();
 
         try {
             $connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $this->_bufferResults);

+ 4 - 4
tests/TestCase/Database/Driver/MysqlTest.php

@@ -44,7 +44,7 @@ class MysqlTest extends TestCase
     public function testConnectionConfigDefault()
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
-            ->setMethods(['_connect', 'connection'])
+            ->setMethods(['_connect', 'getConnection'])
             ->getMock();
         $dsn = 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8mb4';
         $expected = [
@@ -73,7 +73,7 @@ class MysqlTest extends TestCase
             ->with($dsn, $expected);
 
         $driver->expects($this->any())
-            ->method('connection')
+            ->method('getConnection')
             ->will($this->returnValue($connection));
         $driver->connect([]);
     }
@@ -101,7 +101,7 @@ class MysqlTest extends TestCase
             ]
         ];
         $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
-            ->setMethods(['_connect', 'connection'])
+            ->setMethods(['_connect', 'getConnection'])
             ->setConstructorArgs([$config])
             ->getMock();
         $dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=some-encoding';
@@ -125,7 +125,7 @@ class MysqlTest 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($config);
     }

+ 6 - 6
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();
@@ -147,7 +147,7 @@ class PostgresTest extends TestCase
     public function testInsertReturning()
     {
         $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
-            ->setMethods(['_connect', 'connection'])
+            ->setMethods(['_connect', 'getConnection'])
             ->setConstructorArgs([[]])
             ->getMock();
         $connection = $this

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

@@ -73,7 +73,7 @@ class SqliteTest extends TestCase
             'mask' => 0666
         ];
         $driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
-            ->setMethods(['_connect', 'connection'])
+            ->setMethods(['_connect', 'getConnection'])
             ->setConstructorArgs([$config])
             ->getMock();
         $dsn = 'sqlite:bar.db';
@@ -95,7 +95,7 @@ class SqliteTest 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($config);
     }
@@ -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);
 

+ 33 - 9
tests/TestCase/Database/DriverTest.php

@@ -87,6 +87,19 @@ class DriverTest extends TestCase
      */
     public function testSupportsQuoting()
     {
+        $connection = $this->getMockBuilder(PDO::class)
+            ->disableOriginalConstructor()
+            ->setMethods(['getAttribute'])
+            ->getMock();
+
+        $connection
+            ->expects($this->once())
+            ->method('getAttribute')
+            ->with(PDO::ATTR_DRIVER_NAME)
+            ->willReturn('mysql');
+
+        $this->driver->setConnection($connection);
+
         $result = $this->driver->supportsQuoting();
         $this->assertTrue($result);
     }
@@ -114,16 +127,18 @@ class DriverTest extends TestCase
     {
         $value = 'string';
 
-        $this->driver->_connection = $this->getMockBuilder(Mysql::class)
+        $connection = $this->getMockBuilder(PDO::class)
             ->disableOriginalConstructor()
             ->setMethods(['quote'])
             ->getMock();
 
-        $this->driver->_connection
+        $connection
             ->expects($this->once())
             ->method('quote')
             ->with($value, PDO::PARAM_STR);
 
+        $this->driver->setConnection($connection);
+
         $this->driver->schemaValue($value);
     }
 
@@ -144,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());
     }
 
@@ -155,11 +170,20 @@ class DriverTest extends TestCase
      */
     public function testIsConnected()
     {
-        $this->driver->_connection = 'connection';
-        $this->assertTrue($this->driver->isConnected());
-
-        $this->driver->_connection = null;
         $this->assertFalse($this->driver->isConnected());
+
+        $connection = $this->getMockBuilder(PDO::class)
+            ->disableOriginalConstructor()
+            ->setMethods(['query'])
+            ->getMock();
+
+        $connection
+            ->expects($this->once())
+            ->method('query')
+            ->willReturn(true);
+
+        $this->driver->setConnection($connection);
+        $this->assertTrue($this->driver->isConnected());
     }
 
     /**
@@ -250,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;
     }

+ 1 - 1
tests/TestCase/ORM/CompositeKeysTest.php

@@ -754,7 +754,7 @@ class CompositeKeyTest extends TestCase
         if ($driver instanceof Sqlserver) {
             $this->markTestSkipped('Sqlserver does not support the requirements of this test.');
         } elseif ($driver instanceof Sqlite) {
-            $serverVersion = $driver->connection()->getAttribute(PDO::ATTR_SERVER_VERSION);
+            $serverVersion = $driver->getConnection()->getAttribute(PDO::ATTR_SERVER_VERSION);
             if (version_compare($serverVersion, '3.15.0', '<')) {
                 $this->markTestSkipped("Sqlite ($serverVersion) does not support the requirements of this test.");
             }