Skip to content

Commit

Permalink
Add DriverInterface::getConnection()/setConnection().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 20, 2018
1 parent 1279d95 commit dd72625
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 30 deletions.
37 changes: 35 additions & 2 deletions src/Database/Driver.php
Expand Up @@ -91,7 +91,7 @@ protected function _connect($dsn, array $config)
$config['password'],
$config['flags']
);
$this->connection($connection);
$this->setConnection($connection);

return true;
}
Expand All @@ -110,17 +110,50 @@ public function disconnect()
}

/**
* {@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;
}

return $this->_connection;
}

/**
* 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}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Driver/Postgres.php
Expand Up @@ -65,7 +65,7 @@ public function connect()
}

$this->_connect($dsn, $config);
$this->_connection = $connection = $this->connection();
$this->_connection = $connection = $this->getConnection();
if (!empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Driver/Sqlserver.php
Expand Up @@ -105,7 +105,7 @@ public function connect()
}
$this->_connect($dsn, $config);

$connection = $this->connection();
$connection = $this->getConnection();
if (!empty($config['init'])) {
foreach ((array)$config['init'] as $command) {
$connection->exec($command);
Expand Down
15 changes: 10 additions & 5 deletions src/Database/DriverInterface.php
Expand Up @@ -40,14 +40,19 @@ public function connect();
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.
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Database/Driver/PostgresTest.php
Expand Up @@ -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 = [
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testConnectionConfigDefault()

$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();
Expand All @@ -99,7 +99,7 @@ public function testConnectionConfigCustom()
'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';
Expand Down Expand Up @@ -129,11 +129,11 @@ public function testConnectionConfigCustom()
$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();
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Driver/SqliteTest.php
Expand Up @@ -138,7 +138,7 @@ public function testSchemaValue($input, $expected)
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));
$driver->connection($mock);
$driver->setConnection($mock);
$this->assertEquals($expected, $driver->schemaValue($input));
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Driver/SqlserverTest.php
Expand Up @@ -161,7 +161,7 @@ public function testConnectionConfigCustom()
$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);

Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/Database/DriverTest.php
Expand Up @@ -98,7 +98,7 @@ public function testSupportsQuoting()
->with(PDO::ATTR_DRIVER_NAME)
->willReturn('mysql');

$this->driver->connection($connection);
$this->driver->setConnection($connection);

$result = $this->driver->supportsQuoting();
$this->assertTrue($result);
Expand Down Expand Up @@ -137,7 +137,7 @@ public function testSchemaValueConnectionQuoting()
->method('quote')
->with($value, PDO::PARAM_STR);

$this->driver->connection($connection);
$this->driver->setConnection($connection);

$this->driver->schemaValue($value);
}
Expand All @@ -159,7 +159,7 @@ public function testLastInsertId()
->method('lastInsertId')
->willReturn('all-the-bears');

$this->driver->connection($connection);
$this->driver->setConnection($connection);
$this->assertSame('all-the-bears', $this->driver->lastInsertId());
}

Expand All @@ -182,7 +182,7 @@ public function testIsConnected()
->method('query')
->willReturn(true);

$this->driver->connection($connection);
$this->driver->setConnection($connection);
$this->assertTrue($this->driver->isConnected());
}

Expand Down Expand Up @@ -274,10 +274,10 @@ public function testNewCompiler()
*/
public function testDestructor()
{
$this->driver->connection(true);
$this->driver->setConnection(true);
$this->driver->__destruct();

$this->assertNull($this->driver->connection());
$this->assertNull($this->driver->getConnection());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -1038,7 +1038,7 @@ public function testCreateSql()
$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'));
Expand Down Expand Up @@ -1108,7 +1108,7 @@ public function testCreateSqlJson()
->method('getDriver')
->will($this->returnValue($driver));

$driver->connection()
$driver->getConnection()
->expects($this->any())
->method('getAttribute')
->will($this->returnValue('5.7.0'));
Expand Down Expand Up @@ -1329,7 +1329,7 @@ protected function _getMockedDriver()
->will($this->returnCallback(function ($value) {
return "'$value'";
}));
$driver->connection($mock);
$driver->setConnection($mock);

return $driver;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -1264,7 +1264,7 @@ protected function _getMockedDriver()
->will($this->returnCallback(function ($value) {
return "'$value'";
}));
$driver->connection($mock);
$driver->setConnection($mock);

return $driver;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Database/Schema/SqliteSchemaTest.php
Expand Up @@ -1026,7 +1026,7 @@ public function testTruncateSql()
$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')
Expand Down Expand Up @@ -1058,7 +1058,7 @@ public function testTruncateSqlNoSequences()
$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')
Expand Down Expand Up @@ -1087,7 +1087,7 @@ protected function _getMockedDriver()
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));
$driver->connection($mock);
$driver->setConnection($mock);

return $driver;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -1021,7 +1021,7 @@ protected function _getMockedDriver()
->will($this->returnCallback(function ($value) {
return "'$value'";
}));
$driver->connection($mock);
$driver->setConnection($mock);

return $driver;
}
Expand Down

0 comments on commit dd72625

Please sign in to comment.