Skip to content

Commit 52f1bc5

Browse files
authored
Fix php85 mysql attr constant (#2405)
1 parent 05dc104 commit 52f1bc5

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
php-version: ['8.1', '8.4']
17+
php-version: ['8.1', '8.5']
1818
db-type: [sqlite, mysql, pgsql]
1919
prefer-lowest: ['']
2020
include:
@@ -27,7 +27,7 @@ jobs:
2727

2828
steps:
2929
- name: Setup MySQL latest
30-
if: matrix.db-type == 'mysql' && matrix.php-version == '8.4'
30+
if: matrix.db-type == 'mysql' && matrix.php-version == '8.5'
3131
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp -p 3306:3306 -d mysql:8.4
3232

3333
- name: Setup MySQL 8.0

src/Phinx/Db/Adapter/MysqlAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ public function connect(): void
147147
// https://php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants
148148
foreach ($options as $key => $option) {
149149
if (strpos($key, 'mysql_attr_') === 0) {
150-
$pdoConstant = '\PDO::' . strtoupper($key);
150+
if (PHP_VERSION_ID < 80400) {
151+
$pdoConstant = '\PDO::' . strtoupper($key);
152+
} else {
153+
$pdoConstant = '\PDO\Mysql::' . strtoupper(substr($key, 6));
154+
}
151155
if (!defined($pdoConstant)) {
152156
throw new UnexpectedValueException('Invalid PDO attribute: ' . $key . ' (' . $pdoConstant . ')');
153157
}

tests/Phinx/Db/Adapter/MysqlAdapterTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,19 +2731,19 @@ public function testCreateTableWithPrecisionCurrentTimestamp()
27312731
public function pdoAttributeProvider()
27322732
{
27332733
return [
2734-
['mysql_attr_invalid'],
2735-
['attr_invalid'],
2734+
['mysql_attr_invalid', PHP_VERSION_ID < 80400 ? '\PDO::MYSQL_ATTR_INVALID' : '\PDO\Mysql::ATTR_INVALID'],
2735+
['attr_invalid', '\PDO::ATTR_INVALID'],
27362736
];
27372737
}
27382738

27392739
/**
27402740
* @dataProvider pdoAttributeProvider
27412741
*/
2742-
public function testInvalidPdoAttribute($attribute)
2742+
public function testInvalidPdoAttribute($attribute, $constant)
27432743
{
27442744
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG + [$attribute => true]);
27452745
$this->expectException(UnexpectedValueException::class);
2746-
$this->expectExceptionMessage('Invalid PDO attribute: ' . $attribute . ' (\PDO::' . strtoupper($attribute) . ')');
2746+
$this->expectExceptionMessage('Invalid PDO attribute: ' . $attribute . ' (' . $constant . ')');
27472747
$adapter->connect();
27482748
}
27492749

@@ -2797,4 +2797,18 @@ public function testPdoNotPersistentConnection()
27972797
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG);
27982798
$this->assertFalse($adapter->getConnection()->getAttribute(PDO::ATTR_PERSISTENT));
27992799
}
2800+
2801+
public function testMysqlPdoMultiStatementsEnabled()
2802+
{
2803+
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG + ['mysql_attr_multi_statements' => true]);
2804+
$result = $adapter->fetchAll('SELECT 1 a; SELECT 2 b;');
2805+
$this->assertSame([['a' => 1, 0 => 1]], $result);
2806+
}
2807+
2808+
public function testMysqlPdoMultiStatementsDisabled()
2809+
{
2810+
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG + ['mysql_attr_multi_statements' => false]);
2811+
$this->expectException(PDOException::class);
2812+
$adapter->fetchAll('SELECT 1 a; SELECT 2 b;');
2813+
}
28002814
}

0 commit comments

Comments
 (0)