|
|
@@ -92,8 +92,10 @@ class ConnectionTest extends TestCase
|
|
|
$this->connection = ConnectionManager::get('test');
|
|
|
$this->defaultLogger = $this->connection->getLogger();
|
|
|
|
|
|
- $this->logState = $this->connection->isQueryLoggingEnabled();
|
|
|
- $this->connection->disableQueryLogging();
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->logState = $this->connection->isQueryLoggingEnabled();
|
|
|
+ $this->connection->disableQueryLogging();
|
|
|
+ });
|
|
|
|
|
|
static::setAppNamespace();
|
|
|
}
|
|
|
@@ -103,7 +105,9 @@ class ConnectionTest extends TestCase
|
|
|
parent::tearDown();
|
|
|
$this->connection->disableSavePoints();
|
|
|
$this->connection->setLogger($this->defaultLogger);
|
|
|
- $this->connection->enableQueryLogging($this->logState);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->connection->enableQueryLogging($this->logState);
|
|
|
+ });
|
|
|
|
|
|
Log::reset();
|
|
|
unset($this->connection);
|
|
|
@@ -130,8 +134,10 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testConnect(): void
|
|
|
{
|
|
|
- $this->assertTrue($this->connection->connect());
|
|
|
- $this->assertTrue($this->connection->isConnected());
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->assertTrue($this->connection->connect());
|
|
|
+ $this->assertTrue($this->connection->isConnected());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -249,25 +255,27 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testWrongCredentials(): void
|
|
|
{
|
|
|
- $config = ConnectionManager::getConfig('test');
|
|
|
- $this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
|
|
|
- $connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::getConfig('test'));
|
|
|
-
|
|
|
- $e = null;
|
|
|
- try {
|
|
|
- $connection->connect();
|
|
|
- } catch (MissingConnectionException $e) {
|
|
|
- }
|
|
|
-
|
|
|
- $this->assertNotNull($e);
|
|
|
- $this->assertStringStartsWith(
|
|
|
- sprintf(
|
|
|
- 'Connection to %s could not be established:',
|
|
|
- App::shortName(get_class($connection->getDriver()), 'Database/Driver')
|
|
|
- ),
|
|
|
- $e->getMessage()
|
|
|
- );
|
|
|
- $this->assertInstanceOf('PDOException', $e->getPrevious());
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $config = ConnectionManager::getConfig('test');
|
|
|
+ $this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
|
|
|
+ $connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::getConfig('test'));
|
|
|
+
|
|
|
+ $e = null;
|
|
|
+ try {
|
|
|
+ $connection->connect();
|
|
|
+ } catch (MissingConnectionException $e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->assertNotNull($e);
|
|
|
+ $this->assertStringStartsWith(
|
|
|
+ sprintf(
|
|
|
+ 'Connection to %s could not be established:',
|
|
|
+ App::shortName(get_class($connection->getDriver()), 'Database/Driver')
|
|
|
+ ),
|
|
|
+ $e->getMessage()
|
|
|
+ );
|
|
|
+ $this->assertInstanceOf('PDOException', $e->getPrevious());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public function testConnectRetry(): void
|
|
|
@@ -290,16 +298,18 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testPrepare(): void
|
|
|
{
|
|
|
- $sql = 'SELECT 1 + 1';
|
|
|
- $result = $this->connection->prepare($sql);
|
|
|
- $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
|
|
|
- $this->assertEquals($sql, $result->queryString);
|
|
|
-
|
|
|
- $query = $this->connection->selectQuery('1 + 1');
|
|
|
- $result = $this->connection->prepare($query);
|
|
|
- $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
|
|
|
- $sql = '#SELECT [`"\[]?1 \+ 1[`"\]]?#';
|
|
|
- $this->assertMatchesRegularExpression($sql, $result->queryString);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $sql = 'SELECT 1 + 1';
|
|
|
+ $result = $this->connection->prepare($sql);
|
|
|
+ $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
|
|
|
+ $this->assertEquals($sql, $result->queryString);
|
|
|
+
|
|
|
+ $query = $this->connection->selectQuery('1 + 1');
|
|
|
+ $result = $this->connection->prepare($query);
|
|
|
+ $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
|
|
|
+ $sql = '#SELECT [`"\[]?1 \+ 1[`"\]]?#';
|
|
|
+ $this->assertMatchesRegularExpression($sql, $result->queryString);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -351,18 +361,20 @@ class ConnectionTest extends TestCase
|
|
|
'Only required for SQLite driver which does not support buffered results natively'
|
|
|
);
|
|
|
|
|
|
- $statement = $this->connection->query('SELECT * FROM things LIMIT 3');
|
|
|
-
|
|
|
- $collection = new Collection($statement);
|
|
|
- $result = $collection->extract('id')->toArray();
|
|
|
- $this->assertEquals(['1', '2'], $result);
|
|
|
-
|
|
|
- // Check iteration after extraction
|
|
|
- $result = [];
|
|
|
- foreach ($collection as $v) {
|
|
|
- $result[] = $v['id'];
|
|
|
- }
|
|
|
- $this->assertEquals(['1', '2'], $result);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $statement = $this->connection->query('SELECT * FROM things LIMIT 3');
|
|
|
+
|
|
|
+ $collection = new Collection($statement);
|
|
|
+ $result = $collection->extract('id')->toArray();
|
|
|
+ $this->assertEquals(['1', '2'], $result);
|
|
|
+
|
|
|
+ // Check iteration after extraction
|
|
|
+ $result = [];
|
|
|
+ foreach ($collection as $v) {
|
|
|
+ $result[] = $v['id'];
|
|
|
+ }
|
|
|
+ $this->assertEquals(['1', '2'], $result);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -844,35 +856,37 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testInTransactionWithSavePoints(): void
|
|
|
{
|
|
|
- $this->skipIf(
|
|
|
- $this->connection->getDriver() instanceof Sqlserver,
|
|
|
- 'SQLServer fails when this test is included.'
|
|
|
- );
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->skipIf(
|
|
|
+ $this->connection->getDriver() instanceof Sqlserver,
|
|
|
+ 'SQLServer fails when this test is included.'
|
|
|
+ );
|
|
|
|
|
|
- $this->connection->enableSavePoints(true);
|
|
|
- $this->skipIf(!$this->connection->isSavePointsEnabled(), 'Database driver doesn\'t support save points');
|
|
|
+ $this->connection->enableSavePoints(true);
|
|
|
+ $this->skipIf(!$this->connection->isSavePointsEnabled(), 'Database driver doesn\'t support save points');
|
|
|
|
|
|
- $this->connection->begin();
|
|
|
- $this->assertTrue($this->connection->inTransaction());
|
|
|
+ $this->connection->begin();
|
|
|
+ $this->assertTrue($this->connection->inTransaction());
|
|
|
|
|
|
- $this->connection->begin();
|
|
|
- $this->assertTrue($this->connection->inTransaction());
|
|
|
+ $this->connection->begin();
|
|
|
+ $this->assertTrue($this->connection->inTransaction());
|
|
|
|
|
|
- $this->connection->commit();
|
|
|
- $this->assertTrue($this->connection->inTransaction());
|
|
|
+ $this->connection->commit();
|
|
|
+ $this->assertTrue($this->connection->inTransaction());
|
|
|
|
|
|
- $this->connection->commit();
|
|
|
- $this->assertFalse($this->connection->inTransaction());
|
|
|
+ $this->connection->commit();
|
|
|
+ $this->assertFalse($this->connection->inTransaction());
|
|
|
|
|
|
- $this->connection->begin();
|
|
|
- $this->assertTrue($this->connection->inTransaction());
|
|
|
+ $this->connection->begin();
|
|
|
+ $this->assertTrue($this->connection->inTransaction());
|
|
|
|
|
|
- $this->connection->begin();
|
|
|
- $this->connection->rollback();
|
|
|
- $this->assertTrue($this->connection->inTransaction());
|
|
|
+ $this->connection->begin();
|
|
|
+ $this->connection->rollback();
|
|
|
+ $this->assertTrue($this->connection->inTransaction());
|
|
|
|
|
|
- $this->connection->rollback();
|
|
|
- $this->assertFalse($this->connection->inTransaction());
|
|
|
+ $this->connection->rollback();
|
|
|
+ $this->assertFalse($this->connection->inTransaction());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -880,18 +894,20 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testQuote(): void
|
|
|
{
|
|
|
- $this->skipIf(!$this->connection->supportsQuoting());
|
|
|
- $expected = "'2012-01-01'";
|
|
|
- $result = $this->connection->quote(new DateTime('2012-01-01'), 'date');
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $expected = "'1'";
|
|
|
- $result = $this->connection->quote(1, 'string');
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $expected = "'hello'";
|
|
|
- $result = $this->connection->quote('hello', 'string');
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->skipIf(!$this->connection->supportsQuoting());
|
|
|
+ $expected = "'2012-01-01'";
|
|
|
+ $result = $this->connection->quote(new DateTime('2012-01-01'), 'date');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $expected = "'1'";
|
|
|
+ $result = $this->connection->quote(1, 'string');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $expected = "'hello'";
|
|
|
+ $result = $this->connection->quote('hello', 'string');
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -899,109 +915,111 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testQuoteIdentifier(): void
|
|
|
{
|
|
|
- $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
|
|
|
- ->onlyMethods(['enabled'])
|
|
|
- ->getMock();
|
|
|
- $driver->expects($this->once())
|
|
|
- ->method('enabled')
|
|
|
- ->will($this->returnValue(true));
|
|
|
- $connection = new Connection(['driver' => $driver]);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('name');
|
|
|
- $expected = '"name"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Model.*');
|
|
|
- $expected = '"Model".*';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Items.No_ 2');
|
|
|
- $expected = '"Items"."No_ 2"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Items.No_ 2 thing');
|
|
|
- $expected = '"Items"."No_ 2 thing"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Items.No_ 2 thing AS thing');
|
|
|
- $expected = '"Items"."No_ 2 thing" AS "thing"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Items.Item Category Code = :c1');
|
|
|
- $expected = '"Items"."Item Category Code" = :c1';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('MTD()');
|
|
|
- $expected = 'MTD()';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('(sm)');
|
|
|
- $expected = '(sm)';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('name AS x');
|
|
|
- $expected = '"name" AS "x"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Model.name AS x');
|
|
|
- $expected = '"Model"."name" AS "x"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Function(Something.foo)');
|
|
|
- $expected = 'Function("Something"."foo")';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Function(SubFunction(Something.foo))');
|
|
|
- $expected = 'Function(SubFunction("Something"."foo"))';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Function(Something.foo) AS x');
|
|
|
- $expected = 'Function("Something"."foo") AS "x"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('name-with-minus');
|
|
|
- $expected = '"name-with-minus"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('my-name');
|
|
|
- $expected = '"my-name"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Foo-Model.*');
|
|
|
- $expected = '"Foo-Model".*';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Team.P%');
|
|
|
- $expected = '"Team"."P%"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Team.G/G');
|
|
|
- $expected = '"Team"."G/G"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Model.name as y');
|
|
|
- $expected = '"Model"."name" AS "y"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('nämé');
|
|
|
- $expected = '"nämé"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('aßa.nämé');
|
|
|
- $expected = '"aßa"."nämé"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('aßa.*');
|
|
|
- $expected = '"aßa".*';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Modeß.nämé as y');
|
|
|
- $expected = '"Modeß"."nämé" AS "y"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
-
|
|
|
- $result = $connection->quoteIdentifier('Model.näme Datum as y');
|
|
|
- $expected = '"Model"."näme Datum" AS "y"';
|
|
|
- $this->assertEquals($expected, $result);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
|
|
|
+ ->onlyMethods(['enabled'])
|
|
|
+ ->getMock();
|
|
|
+ $driver->expects($this->once())
|
|
|
+ ->method('enabled')
|
|
|
+ ->will($this->returnValue(true));
|
|
|
+ $connection = new Connection(['driver' => $driver]);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('name');
|
|
|
+ $expected = '"name"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Model.*');
|
|
|
+ $expected = '"Model".*';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Items.No_ 2');
|
|
|
+ $expected = '"Items"."No_ 2"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Items.No_ 2 thing');
|
|
|
+ $expected = '"Items"."No_ 2 thing"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Items.No_ 2 thing AS thing');
|
|
|
+ $expected = '"Items"."No_ 2 thing" AS "thing"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Items.Item Category Code = :c1');
|
|
|
+ $expected = '"Items"."Item Category Code" = :c1';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('MTD()');
|
|
|
+ $expected = 'MTD()';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('(sm)');
|
|
|
+ $expected = '(sm)';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('name AS x');
|
|
|
+ $expected = '"name" AS "x"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Model.name AS x');
|
|
|
+ $expected = '"Model"."name" AS "x"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Function(Something.foo)');
|
|
|
+ $expected = 'Function("Something"."foo")';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Function(SubFunction(Something.foo))');
|
|
|
+ $expected = 'Function(SubFunction("Something"."foo"))';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Function(Something.foo) AS x');
|
|
|
+ $expected = 'Function("Something"."foo") AS "x"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('name-with-minus');
|
|
|
+ $expected = '"name-with-minus"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('my-name');
|
|
|
+ $expected = '"my-name"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Foo-Model.*');
|
|
|
+ $expected = '"Foo-Model".*';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Team.P%');
|
|
|
+ $expected = '"Team"."P%"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Team.G/G');
|
|
|
+ $expected = '"Team"."G/G"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Model.name as y');
|
|
|
+ $expected = '"Model"."name" AS "y"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('nämé');
|
|
|
+ $expected = '"nämé"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('aßa.nämé');
|
|
|
+ $expected = '"aßa"."nämé"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('aßa.*');
|
|
|
+ $expected = '"aßa".*';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Modeß.nämé as y');
|
|
|
+ $expected = '"Modeß"."nämé" AS "y"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+
|
|
|
+ $result = $connection->quoteIdentifier('Model.näme Datum as y');
|
|
|
+ $expected = '"Model"."näme Datum" AS "y"';
|
|
|
+ $this->assertEquals($expected, $result);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1029,16 +1047,18 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testLoggerDecorator(): void
|
|
|
{
|
|
|
- $logger = new QueryLogger();
|
|
|
- $this->connection->enableQueryLogging(true);
|
|
|
- $this->connection->setLogger($logger);
|
|
|
- $st = $this->connection->prepare('SELECT 1');
|
|
|
- $this->assertInstanceOf(LoggingStatement::class, $st);
|
|
|
- $this->assertSame($logger, $st->getLogger());
|
|
|
-
|
|
|
- $this->connection->enableQueryLogging(false);
|
|
|
- $st = $this->connection->prepare('SELECT 1');
|
|
|
- $this->assertNotInstanceOf('Cake\Database\Log\LoggingStatement', $st);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $logger = new QueryLogger();
|
|
|
+ $this->connection->enableQueryLogging(true);
|
|
|
+ $this->connection->setLogger($logger);
|
|
|
+ $st = $this->connection->prepare('SELECT 1');
|
|
|
+ $this->assertInstanceOf(LoggingStatement::class, $st);
|
|
|
+ $this->assertSame($logger, $st->getLogger());
|
|
|
+
|
|
|
+ $this->connection->enableQueryLogging(false);
|
|
|
+ $st = $this->connection->prepare('SELECT 1');
|
|
|
+ $this->assertNotInstanceOf('Cake\Database\Log\LoggingStatement', $st);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1046,11 +1066,13 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testEnableQueryLogging(): void
|
|
|
{
|
|
|
- $this->connection->enableQueryLogging(true);
|
|
|
- $this->assertTrue($this->connection->isQueryLoggingEnabled());
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->connection->enableQueryLogging(true);
|
|
|
+ $this->assertTrue($this->connection->isQueryLoggingEnabled());
|
|
|
|
|
|
- $this->connection->disableQueryLogging();
|
|
|
- $this->assertFalse($this->connection->isQueryLoggingEnabled());
|
|
|
+ $this->connection->disableQueryLogging();
|
|
|
+ $this->assertFalse($this->connection->isQueryLoggingEnabled());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1059,12 +1081,14 @@ class ConnectionTest extends TestCase
|
|
|
public function testLogFunction(): void
|
|
|
{
|
|
|
Log::setConfig('queries', ['className' => 'Array']);
|
|
|
- $this->connection->enableQueryLogging();
|
|
|
- $this->connection->log('SELECT 1');
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $this->connection->enableQueryLogging();
|
|
|
+ $this->connection->log('SELECT 1');
|
|
|
|
|
|
- $messages = Log::engine('queries')->read();
|
|
|
- $this->assertCount(1, $messages);
|
|
|
- $this->assertSame('debug: connection=test role= duration=0 rows=0 SELECT 1', $messages[0]);
|
|
|
+ $messages = Log::engine('queries')->read();
|
|
|
+ $this->assertCount(1, $messages);
|
|
|
+ $this->assertSame('debug: connection=test role= duration=0 rows=0 SELECT 1', $messages[0]);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1073,30 +1097,32 @@ class ConnectionTest extends TestCase
|
|
|
public function testLoggerDecoratorDoesNotPrematurelyFetchRecords(): void
|
|
|
{
|
|
|
Log::setConfig('queries', ['className' => 'Array']);
|
|
|
- $logger = new QueryLogger();
|
|
|
- $this->connection->enableQueryLogging(true);
|
|
|
- $this->connection->setLogger($logger);
|
|
|
- $st = $this->connection->execute('SELECT * FROM things');
|
|
|
- $this->assertInstanceOf(LoggingStatement::class, $st);
|
|
|
-
|
|
|
- $messages = Log::engine('queries')->read();
|
|
|
- $this->assertCount(0, $messages);
|
|
|
-
|
|
|
- $expected = [
|
|
|
- [1, 'a title', 'a body'],
|
|
|
- [2, 'another title', 'another body'],
|
|
|
- ];
|
|
|
- $results = $st->fetchAll();
|
|
|
- $this->assertEquals($expected, $results);
|
|
|
-
|
|
|
- $messages = Log::engine('queries')->read();
|
|
|
- $this->assertCount(1, $messages);
|
|
|
-
|
|
|
- $st = $this->connection->execute('SELECT * FROM things WHERE id = 0');
|
|
|
- $this->assertSame(0, $st->rowCount());
|
|
|
-
|
|
|
- $messages = Log::engine('queries')->read();
|
|
|
- $this->assertCount(2, $messages, 'Select queries without any matching rows should also be logged.');
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $logger = new QueryLogger();
|
|
|
+ $this->connection->enableQueryLogging(true);
|
|
|
+ $this->connection->setLogger($logger);
|
|
|
+ $st = $this->connection->execute('SELECT * FROM things');
|
|
|
+ $this->assertInstanceOf(LoggingStatement::class, $st);
|
|
|
+
|
|
|
+ $messages = Log::engine('queries')->read();
|
|
|
+ $this->assertCount(0, $messages);
|
|
|
+
|
|
|
+ $expected = [
|
|
|
+ [1, 'a title', 'a body'],
|
|
|
+ [2, 'another title', 'another body'],
|
|
|
+ ];
|
|
|
+ $results = $st->fetchAll();
|
|
|
+ $this->assertEquals($expected, $results);
|
|
|
+
|
|
|
+ $messages = Log::engine('queries')->read();
|
|
|
+ $this->assertCount(1, $messages);
|
|
|
+
|
|
|
+ $st = $this->connection->execute('SELECT * FROM things WHERE id = 0');
|
|
|
+ $this->assertSame(0, $st->rowCount());
|
|
|
+
|
|
|
+ $messages = Log::engine('queries')->read();
|
|
|
+ $this->assertCount(2, $messages, 'Select queries without any matching rows should also be logged.');
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1106,26 +1132,28 @@ class ConnectionTest extends TestCase
|
|
|
{
|
|
|
Log::setConfig('queries', ['className' => 'Array']);
|
|
|
|
|
|
- $connection = $this
|
|
|
- ->getMockBuilder(Connection::class)
|
|
|
- ->onlyMethods(['connect'])
|
|
|
- ->disableOriginalConstructor()
|
|
|
- ->getMock();
|
|
|
- $connection->enableQueryLogging(true);
|
|
|
-
|
|
|
- $this->deprecated(function () use ($connection) {
|
|
|
- $driver = $this->getMockFormDriver();
|
|
|
- $connection->setDriver($driver);
|
|
|
- });
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $connection = $this
|
|
|
+ ->getMockBuilder(Connection::class)
|
|
|
+ ->onlyMethods(['connect'])
|
|
|
+ ->disableOriginalConstructor()
|
|
|
+ ->getMock();
|
|
|
+ $connection->enableQueryLogging(true);
|
|
|
+
|
|
|
+ $this->deprecated(function () use ($connection) {
|
|
|
+ $driver = $this->getMockFormDriver();
|
|
|
+ $connection->setDriver($driver);
|
|
|
+ });
|
|
|
|
|
|
- $connection->begin();
|
|
|
- $connection->begin(); //This one will not be logged
|
|
|
- $connection->rollback();
|
|
|
+ $connection->begin();
|
|
|
+ $connection->begin(); //This one will not be logged
|
|
|
+ $connection->rollback();
|
|
|
|
|
|
- $messages = Log::engine('queries')->read();
|
|
|
- $this->assertCount(2, $messages);
|
|
|
- $this->assertSame('debug: connection= role= duration=0 rows=0 BEGIN', $messages[0]);
|
|
|
- $this->assertSame('debug: connection= role= duration=0 rows=0 ROLLBACK', $messages[1]);
|
|
|
+ $messages = Log::engine('queries')->read();
|
|
|
+ $this->assertCount(2, $messages);
|
|
|
+ $this->assertSame('debug: connection= role= duration=0 rows=0 BEGIN', $messages[0]);
|
|
|
+ $this->assertSame('debug: connection= role= duration=0 rows=0 ROLLBACK', $messages[1]);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1133,21 +1161,23 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testLogCommitTransaction(): void
|
|
|
{
|
|
|
- $driver = $this->getMockFormDriver();
|
|
|
- $connection = $this->getMockBuilder(Connection::class)
|
|
|
- ->onlyMethods(['connect'])
|
|
|
- ->setConstructorArgs([['driver' => $driver]])
|
|
|
- ->getMock();
|
|
|
-
|
|
|
- Log::setConfig('queries', ['className' => 'Array']);
|
|
|
- $connection->enableQueryLogging(true);
|
|
|
- $connection->begin();
|
|
|
- $connection->commit();
|
|
|
-
|
|
|
- $messages = Log::engine('queries')->read();
|
|
|
- $this->assertCount(2, $messages);
|
|
|
- $this->assertSame('debug: connection= role= duration=0 rows=0 BEGIN', $messages[0]);
|
|
|
- $this->assertSame('debug: connection= role= duration=0 rows=0 COMMIT', $messages[1]);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $driver = $this->getMockFormDriver();
|
|
|
+ $connection = $this->getMockBuilder(Connection::class)
|
|
|
+ ->onlyMethods(['connect'])
|
|
|
+ ->setConstructorArgs([['driver' => $driver]])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ Log::setConfig('queries', ['className' => 'Array']);
|
|
|
+ $connection->enableQueryLogging(true);
|
|
|
+ $connection->begin();
|
|
|
+ $connection->commit();
|
|
|
+
|
|
|
+ $messages = Log::engine('queries')->read();
|
|
|
+ $this->assertCount(2, $messages);
|
|
|
+ $this->assertSame('debug: connection= role= duration=0 rows=0 BEGIN', $messages[0]);
|
|
|
+ $this->assertSame('debug: connection= role= duration=0 rows=0 COMMIT', $messages[1]);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1420,28 +1450,30 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testAutomaticReconnect2(): void
|
|
|
{
|
|
|
- $conn = clone $this->connection;
|
|
|
- $statement = $conn->query('SELECT 1');
|
|
|
- $statement->execute();
|
|
|
- $statement->closeCursor();
|
|
|
-
|
|
|
- $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
- $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
- $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
-
|
|
|
- $newDriver->expects($this->exactly(2))
|
|
|
- ->method('prepare')
|
|
|
- ->will($this->onConsecutiveCalls(
|
|
|
- $this->throwException(new Exception('server gone away')),
|
|
|
- $this->returnValue($statement)
|
|
|
- ));
|
|
|
-
|
|
|
- $res = $conn->query('SELECT 1');
|
|
|
- $this->assertInstanceOf(StatementInterface::class, $res);
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $conn = clone $this->connection;
|
|
|
+ $statement = $conn->query('SELECT 1');
|
|
|
+ $statement->execute();
|
|
|
+ $statement->closeCursor();
|
|
|
+
|
|
|
+ $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
+ $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+ $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+
|
|
|
+ $newDriver->expects($this->exactly(2))
|
|
|
+ ->method('prepare')
|
|
|
+ ->will($this->onConsecutiveCalls(
|
|
|
+ $this->throwException(new Exception('server gone away')),
|
|
|
+ $this->returnValue($statement)
|
|
|
+ ));
|
|
|
+
|
|
|
+ $res = $conn->query('SELECT 1');
|
|
|
+ $this->assertInstanceOf(StatementInterface::class, $res);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1450,99 +1482,105 @@ class ConnectionTest extends TestCase
|
|
|
*/
|
|
|
public function testNoAutomaticReconnect(): void
|
|
|
{
|
|
|
- $conn = clone $this->connection;
|
|
|
- $statement = $conn->query('SELECT 1');
|
|
|
- $statement->execute();
|
|
|
- $statement->closeCursor();
|
|
|
-
|
|
|
- $conn->begin();
|
|
|
-
|
|
|
- $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
- $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
- $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $oldDriver = $prop->getValue($conn);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
-
|
|
|
- $newDriver->expects($this->once())
|
|
|
- ->method('prepare')
|
|
|
- ->will($this->throwException(new Exception('server gone away')));
|
|
|
-
|
|
|
- try {
|
|
|
- $conn->query('SELECT 1');
|
|
|
- } catch (Exception $e) {
|
|
|
- }
|
|
|
- $this->assertInstanceOf(Exception::class, $e ?? null);
|
|
|
-
|
|
|
- $prop->setValue($conn, $oldDriver);
|
|
|
- $conn->rollback();
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $conn = clone $this->connection;
|
|
|
+ $statement = $conn->query('SELECT 1');
|
|
|
+ $statement->execute();
|
|
|
+ $statement->closeCursor();
|
|
|
+
|
|
|
+ $conn->begin();
|
|
|
+
|
|
|
+ $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
+ $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+ $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $oldDriver = $prop->getValue($conn);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+
|
|
|
+ $newDriver->expects($this->once())
|
|
|
+ ->method('prepare')
|
|
|
+ ->will($this->throwException(new Exception('server gone away')));
|
|
|
+
|
|
|
+ try {
|
|
|
+ $conn->query('SELECT 1');
|
|
|
+ } catch (Exception $e) {
|
|
|
+ }
|
|
|
+ $this->assertInstanceOf(Exception::class, $e ?? null);
|
|
|
+
|
|
|
+ $prop->setValue($conn, $oldDriver);
|
|
|
+ $conn->rollback();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public function testAutomaticReconnectWithoutQueryLogging(): void
|
|
|
{
|
|
|
- $conn = clone $this->connection;
|
|
|
-
|
|
|
- $logger = new TestBaseLog();
|
|
|
- $conn->setLogger($logger);
|
|
|
- $conn->disableQueryLogging();
|
|
|
-
|
|
|
- $statement = $conn->query('SELECT 1');
|
|
|
- $statement->execute();
|
|
|
- $statement->closeCursor();
|
|
|
-
|
|
|
- $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
- $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
- $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
-
|
|
|
- $newDriver->expects($this->exactly(2))
|
|
|
- ->method('prepare')
|
|
|
- ->will($this->onConsecutiveCalls(
|
|
|
- $this->throwException(new Exception('server gone away')),
|
|
|
- $this->returnValue($statement)
|
|
|
- ));
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $conn = clone $this->connection;
|
|
|
+
|
|
|
+ $logger = new TestBaseLog();
|
|
|
+ $conn->setLogger($logger);
|
|
|
+ $conn->disableQueryLogging();
|
|
|
+
|
|
|
+ $statement = $conn->query('SELECT 1');
|
|
|
+ $statement->execute();
|
|
|
+ $statement->closeCursor();
|
|
|
+
|
|
|
+ $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
+ $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+ $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+
|
|
|
+ $newDriver->expects($this->exactly(2))
|
|
|
+ ->method('prepare')
|
|
|
+ ->will($this->onConsecutiveCalls(
|
|
|
+ $this->throwException(new Exception('server gone away')),
|
|
|
+ $this->returnValue($statement)
|
|
|
+ ));
|
|
|
|
|
|
- $conn->query('SELECT 1');
|
|
|
+ $conn->query('SELECT 1');
|
|
|
|
|
|
- $this->assertEmpty($logger->getMessage());
|
|
|
+ $this->assertEmpty($logger->getMessage());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public function testAutomaticReconnectWithQueryLogging(): void
|
|
|
{
|
|
|
- $conn = clone $this->connection;
|
|
|
-
|
|
|
- $logger = new TestBaseLog();
|
|
|
- $conn->setLogger($logger);
|
|
|
- $conn->enableQueryLogging();
|
|
|
+ $this->deprecated(function () {
|
|
|
+ $conn = clone $this->connection;
|
|
|
+
|
|
|
+ $logger = new TestBaseLog();
|
|
|
+ $conn->setLogger($logger);
|
|
|
+ $conn->enableQueryLogging();
|
|
|
+
|
|
|
+ $statement = $conn->query('SELECT 1');
|
|
|
+ $statement->execute();
|
|
|
+ $statement->closeCursor();
|
|
|
+
|
|
|
+ $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
+ $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+ $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
+ $prop->setAccessible(true);
|
|
|
+ $oldDriver = $prop->getValue($conn);
|
|
|
+ $prop->setValue($conn, $newDriver);
|
|
|
+
|
|
|
+ $newDriver->expects($this->exactly(2))
|
|
|
+ ->method('prepare')
|
|
|
+ ->will($this->onConsecutiveCalls(
|
|
|
+ $this->throwException(new Exception('server gone away')),
|
|
|
+ $this->returnValue($statement)
|
|
|
+ ));
|
|
|
|
|
|
- $statement = $conn->query('SELECT 1');
|
|
|
- $statement->execute();
|
|
|
- $statement->closeCursor();
|
|
|
+ $conn->query('SELECT 1');
|
|
|
|
|
|
- $newDriver = $this->getMockBuilder(Driver::class)->getMock();
|
|
|
- $prop = new ReflectionProperty($conn, 'readDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
- $prop = new ReflectionProperty($conn, 'writeDriver');
|
|
|
- $prop->setAccessible(true);
|
|
|
- $oldDriver = $prop->getValue($conn);
|
|
|
- $prop->setValue($conn, $newDriver);
|
|
|
-
|
|
|
- $newDriver->expects($this->exactly(2))
|
|
|
- ->method('prepare')
|
|
|
- ->will($this->onConsecutiveCalls(
|
|
|
- $this->throwException(new Exception('server gone away')),
|
|
|
- $this->returnValue($statement)
|
|
|
- ));
|
|
|
-
|
|
|
- $conn->query('SELECT 1');
|
|
|
-
|
|
|
- $this->assertSame('[RECONNECT]', $logger->getMessage());
|
|
|
+ $this->assertSame('[RECONNECT]', $logger->getMessage());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public function testNewQuery()
|