Skip to content

Commit dd72625

Browse files
committed
Add DriverInterface::getConnection()/setConnection().
1 parent 1279d95 commit dd72625

File tree

12 files changed

+68
-30
lines changed

12 files changed

+68
-30
lines changed

src/Database/Driver.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function _connect($dsn, array $config)
9191
$config['password'],
9292
$config['flags']
9393
);
94-
$this->connection($connection);
94+
$this->setConnection($connection);
9595

9696
return true;
9797
}
@@ -110,17 +110,50 @@ public function disconnect()
110110
}
111111

112112
/**
113-
* {@inheritDoc}
113+
* Returns correct connection resource or object that is internally used
114+
* If first argument is passed, it will set internal connection object or
115+
* result to the value passed.
116+
*
117+
* @param mixed $connection The PDO connection instance.
118+
* @return mixed Connection object used internally.
119+
* @deprecated 3.6.0 Use getConnection()/setConnection() instead.
114120
*/
115121
public function connection($connection = null)
116122
{
123+
deprecationWarning(
124+
get_called_class() . '::connection() is deprecated. ' .
125+
'Use setConnection()/getConnection() instead.'
126+
);
117127
if ($connection !== null) {
118128
$this->_connection = $connection;
119129
}
120130

121131
return $this->_connection;
122132
}
123133

134+
/**
135+
* Get the internal PDO connection instance.
136+
*
137+
* @return \PDO
138+
*/
139+
public function getConnection()
140+
{
141+
return $this->_connection;
142+
}
143+
144+
/**
145+
* Set the internal PDO connection instance.
146+
*
147+
* @param \PDO $connection PDO instance.
148+
* @return $this
149+
*/
150+
public function setConnection($connection)
151+
{
152+
$this->_connection = $connection;
153+
154+
return $this;
155+
}
156+
124157
/**
125158
* {@inheritDoc}
126159
*/

src/Database/Driver/Postgres.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function connect()
6565
}
6666

6767
$this->_connect($dsn, $config);
68-
$this->_connection = $connection = $this->connection();
68+
$this->_connection = $connection = $this->getConnection();
6969
if (!empty($config['encoding'])) {
7070
$this->setEncoding($config['encoding']);
7171
}

src/Database/Driver/Sqlserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function connect()
105105
}
106106
$this->_connect($dsn, $config);
107107

108-
$connection = $this->connection();
108+
$connection = $this->getConnection();
109109
if (!empty($config['init'])) {
110110
foreach ((array)$config['init'] as $command) {
111111
$connection->exec($command);

src/Database/DriverInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ public function connect();
4040
public function disconnect();
4141

4242
/**
43-
* Returns correct connection resource or object that is internally used
44-
* If first argument is passed, it will set internal connection object or
45-
* result to the value passed.
43+
* Returns correct connection resource or object that is internally used.
4644
*
47-
* @param mixed $connection The PDO connection instance.
4845
* @return mixed Connection object used internally.
4946
*/
50-
public function connection($connection = null);
47+
public function getConnection();
48+
49+
/**
50+
* Set the internal connection object.
51+
*
52+
* @param mixed $connection The connection instance.
53+
* @return $this
54+
*/
55+
public function setConnection($connection);
5156

5257
/**
5358
* Returns whether php is able to use this driver for connecting to database.

tests/TestCase/Database/Driver/PostgresTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PostgresTest extends TestCase
3232
public function testConnectionConfigDefault()
3333
{
3434
$driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
35-
->setMethods(['_connect', 'connection'])
35+
->setMethods(['_connect', 'getConnection'])
3636
->getMock();
3737
$dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
3838
$expected = [
@@ -72,7 +72,7 @@ public function testConnectionConfigDefault()
7272

7373
$driver->expects($this->once())->method('_connect')
7474
->with($dsn, $expected);
75-
$driver->expects($this->any())->method('connection')
75+
$driver->expects($this->any())->method('getConnection')
7676
->will($this->returnValue($connection));
7777

7878
$driver->connect();
@@ -99,7 +99,7 @@ public function testConnectionConfigCustom()
9999
'init' => ['Execute this', 'this too']
100100
];
101101
$driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
102-
->setMethods(['_connect', 'connection'])
102+
->setMethods(['_connect', 'getConnection', 'setConnection'])
103103
->setConstructorArgs([$config])
104104
->getMock();
105105
$dsn = 'pgsql:host=foo;port=3440;dbname=bar';
@@ -129,11 +129,11 @@ public function testConnectionConfigCustom()
129129
$connection->expects($this->at(7))->method('exec')->with('SET timezone = Antarctica');
130130
$connection->expects($this->exactly(5))->method('exec');
131131

132-
$driver->connection($connection);
132+
$driver->setConnection($connection);
133133
$driver->expects($this->once())->method('_connect')
134134
->with($dsn, $expected);
135135

136-
$driver->expects($this->any())->method('connection')
136+
$driver->expects($this->any())->method('getConnection')
137137
->will($this->returnValue($connection));
138138

139139
$driver->connect();

tests/TestCase/Database/Driver/SqliteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function testSchemaValue($input, $expected)
138138
->will($this->returnCallback(function ($value) {
139139
return '"' . $value . '"';
140140
}));
141-
$driver->connection($mock);
141+
$driver->setConnection($mock);
142142
$this->assertEquals($expected, $driver->schemaValue($input));
143143
}
144144
}

tests/TestCase/Database/Driver/SqlserverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function testConnectionConfigCustom()
161161
$connection->expects($this->at(2))->method('exec')->with('SET config1 value1');
162162
$connection->expects($this->at(3))->method('exec')->with('SET config2 value2');
163163

164-
$driver->connection($connection);
164+
$driver->setConnection($connection);
165165
$driver->expects($this->once())->method('_connect')
166166
->with($dsn, $expected);
167167

tests/TestCase/Database/DriverTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testSupportsQuoting()
9898
->with(PDO::ATTR_DRIVER_NAME)
9999
->willReturn('mysql');
100100

101-
$this->driver->connection($connection);
101+
$this->driver->setConnection($connection);
102102

103103
$result = $this->driver->supportsQuoting();
104104
$this->assertTrue($result);
@@ -137,7 +137,7 @@ public function testSchemaValueConnectionQuoting()
137137
->method('quote')
138138
->with($value, PDO::PARAM_STR);
139139

140-
$this->driver->connection($connection);
140+
$this->driver->setConnection($connection);
141141

142142
$this->driver->schemaValue($value);
143143
}
@@ -159,7 +159,7 @@ public function testLastInsertId()
159159
->method('lastInsertId')
160160
->willReturn('all-the-bears');
161161

162-
$this->driver->connection($connection);
162+
$this->driver->setConnection($connection);
163163
$this->assertSame('all-the-bears', $this->driver->lastInsertId());
164164
}
165165

@@ -182,7 +182,7 @@ public function testIsConnected()
182182
->method('query')
183183
->willReturn(true);
184184

185-
$this->driver->connection($connection);
185+
$this->driver->setConnection($connection);
186186
$this->assertTrue($this->driver->isConnected());
187187
}
188188

@@ -274,10 +274,10 @@ public function testNewCompiler()
274274
*/
275275
public function testDestructor()
276276
{
277-
$this->driver->connection(true);
277+
$this->driver->setConnection(true);
278278
$this->driver->__destruct();
279279

280-
$this->assertNull($this->driver->connection());
280+
$this->assertNull($this->driver->getConnection());
281281
}
282282

283283
/**

tests/TestCase/Database/Schema/MysqlSchemaTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ public function testCreateSql()
10381038
$connection->expects($this->any())->method('getDriver')
10391039
->will($this->returnValue($driver));
10401040

1041-
$driver->connection()
1041+
$driver->getConnection()
10421042
->expects($this->any())
10431043
->method('getAttribute')
10441044
->will($this->returnValue('5.6.0'));
@@ -1108,7 +1108,7 @@ public function testCreateSqlJson()
11081108
->method('getDriver')
11091109
->will($this->returnValue($driver));
11101110

1111-
$driver->connection()
1111+
$driver->getConnection()
11121112
->expects($this->any())
11131113
->method('getAttribute')
11141114
->will($this->returnValue('5.7.0'));
@@ -1329,7 +1329,7 @@ protected function _getMockedDriver()
13291329
->will($this->returnCallback(function ($value) {
13301330
return "'$value'";
13311331
}));
1332-
$driver->connection($mock);
1332+
$driver->setConnection($mock);
13331333

13341334
return $driver;
13351335
}

tests/TestCase/Database/Schema/PostgresSchemaTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ protected function _getMockedDriver()
12641264
->will($this->returnCallback(function ($value) {
12651265
return "'$value'";
12661266
}));
1267-
$driver->connection($mock);
1267+
$driver->setConnection($mock);
12681268

12691269
return $driver;
12701270
}

0 commit comments

Comments
 (0)