Skip to content

Commit

Permalink
ext/pdo_mysql: Improve tests cleanup (php#11879)
Browse files Browse the repository at this point in the history
This allows the tests to be run in parallel.
  • Loading branch information
alexandre-daubois authored Nov 16, 2023
1 parent 44467eb commit 6f95273
Show file tree
Hide file tree
Showing 61 changed files with 1,831 additions and 870 deletions.
18 changes: 11 additions & 7 deletions ext/pdo_mysql/tests/bug44327.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ $db = MySQLPDOTest::factory();

print "----------------------------------\n";

@$db->exec("DROP TABLE test");
$db->exec("CREATE TABLE test (id INT)");
$db->exec("INSERT INTO test(id) VALUES (1)");
$stmt = $db->prepare("SELECT id FROM test");
$db->exec("CREATE TABLE test_44327 (id INT)");
$db->exec("INSERT INTO test_44327(id) VALUES (1)");
$stmt = $db->prepare("SELECT id FROM test_44327");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_LAZY);
var_dump($row);
var_dump($row->queryString);
@$db->exec("DROP TABLE test");

print "----------------------------------\n";

Expand All @@ -41,6 +39,12 @@ $db = MySQLPDOTest::factory();
$row = $stmt->fetch();
var_dump($row->queryString);

?>
--CLEAN--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$db = MySQLPDOTest::factory();
$db->exec("DROP TABLE test_44327");
?>
--EXPECTF--
object(PDORow)#%d (2) {
Expand All @@ -55,11 +59,11 @@ string(17) "SELECT 1 AS "one""
----------------------------------
object(PDORow)#5 (2) {
["queryString"]=>
string(19) "SELECT id FROM test"
string(25) "SELECT id FROM test_44327"
["id"]=>
string(1) "1"
}
string(19) "SELECT id FROM test"
string(25) "SELECT id FROM test_44327"
----------------------------------

Warning: Attempt to read property "queryString" on false in %s on line %d
Expand Down
10 changes: 4 additions & 6 deletions ext/pdo_mysql/tests/bug46292.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ MySQLPDOTest::skip();
$pdoDb->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdoDb->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);

$pdoDb->query('DROP TABLE IF EXISTS testz');
$pdoDb->query('CREATE TABLE test_46292 (name VARCHAR(20) NOT NULL, value INT)');

$pdoDb->query('CREATE TABLE testz (name VARCHAR(20) NOT NULL, value INT)');
$pdoDb->query("INSERT INTO test_46292 VALUES ('myclass', 1), ('myclass2', 2), ('myclass', NULL), ('myclass3', NULL)");

$pdoDb->query("INSERT INTO testz VALUES ('myclass', 1), ('myclass2', 2), ('myclass', NULL), ('myclass3', NULL)");

$stmt = $pdoDb->prepare("SELECT * FROM testz");
$stmt = $pdoDb->prepare("SELECT * FROM test_46292");

var_dump($stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE | PDO::FETCH_GROUP));
$stmt->execute();
Expand All @@ -46,7 +44,7 @@ MySQLPDOTest::skip();
<?php
require __DIR__ . '/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS testz');
$db->exec('DROP TABLE IF EXISTS test_46292');
?>
--EXPECTF--
bool(true)
Expand Down
9 changes: 4 additions & 5 deletions ext/pdo_mysql/tests/bug66528.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ $dbh = MySQLPDOTest::factory();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

$dbh->exec('DROP TABLE IF EXISTS test');
$dbh->exec('CREATE TABLE test (a int) engine=innodb');
$dbh->exec('CREATE TABLE test_66528 (a int) engine=innodb');
$dbh->beginTransaction();
$dbh->exec('INSERT INTO test (a) VALUES (1), (2)');
$stmt = $dbh->query('SELECT * FROM test');
$dbh->exec('INSERT INTO test_66528 (a) VALUES (1), (2)');
$stmt = $dbh->query('SELECT * FROM test_66528');

try {
$dbh->commit();
Expand All @@ -44,7 +43,7 @@ try {
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
MySQLPDOTest::dropTestTable(NULL, 'test_66528');
?>
--EXPECT--
SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
Expand Down
7 changes: 3 additions & 4 deletions ext/pdo_mysql/tests/bug70862.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ MySQLPDOTest::skip();
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$db = MySQLPDOTest::factory();

$db->exec('DROP TABLE IF EXISTS test');
$db->exec(sprintf('CREATE TABLE test(id INT, label BLOB)'));
$db->exec(sprintf('CREATE TABLE test_70862(id INT, label BLOB)'));

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
Expand All @@ -29,7 +28,7 @@ MySQLPDOTest::skip();

$f = fopen("hello://there", "r");

$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (1, :para)');
$stmt = $db->prepare('INSERT INTO test_70862(id, label) VALUES (1, :para)');
$stmt->bindParam(":para", $f, PDO::PARAM_LOB);
$stmt->execute();

Expand All @@ -41,7 +40,7 @@ MySQLPDOTest::skip();
<?php
require __DIR__ . '/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test');
$db->exec('DROP TABLE IF EXISTS test_70862');
?>
--EXPECT--
string(0) ""
Expand Down
14 changes: 5 additions & 9 deletions ext/pdo_mysql/tests/bug75177.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ if (!MySQLPDOTest::isPDOMySQLnd()) die('skip only for mysqlnd');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$pdo = MySQLPDOTest::factory();

$tbl = "test";
$pdo->query("DROP TABLE IF EXISTS $tbl");
$pdo->query("CREATE TABLE $tbl (`bit` bit(8)) ENGINE=InnoDB");
$pdo->query("INSERT INTO $tbl (`bit`) VALUES (1)");
$pdo->query("INSERT INTO $tbl (`bit`) VALUES (0b011)");
$pdo->query("INSERT INTO $tbl (`bit`) VALUES (0b01100)");
$pdo->query("CREATE TABLE test_75177 (`bit` bit(8)) ENGINE=InnoDB");
$pdo->query("INSERT INTO test_75177 (`bit`) VALUES (1), (0b011), (0b01100)");

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$ret = $pdo->query("SELECT * FROM $tbl")->fetchAll();
$ret = $pdo->query("SELECT * FROM test_75177")->fetchAll();
foreach ($ret as $i) {
var_dump($i["bit"]);
}

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$ret = $pdo->query("SELECT * FROM $tbl")->fetchAll();
$ret = $pdo->query("SELECT * FROM test_75177")->fetchAll();
foreach ($ret as $i) {
var_dump($i["bit"]);
}
Expand All @@ -36,7 +32,7 @@ foreach ($ret as $i) {
--CLEAN--
<?php
require dirname(__FILE__) . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
MySQLPDOTest::dropTestTable(NULL, 'test_75177');
?>
--EXPECT--
int(1)
Expand Down
14 changes: 7 additions & 7 deletions ext/pdo_mysql/tests/bug79375.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ function createDB(): PDO {

$db = createDB();
$db2 = createDB();
$db->query('DROP TABLE IF EXISTS test');
$db->query('CREATE TABLE test (first int) ENGINE = InnoDB');
$db->query('INSERT INTO test VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');
$db->query('CREATE TABLE test_79375 (first int) ENGINE = InnoDB');
$db->query('INSERT INTO test_79375 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');

function testNormalQuery(PDO $db, string $name) {
$db->exec("SET innodb_lock_wait_timeout = 1");
$db->exec("START TRANSACTION");
$query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
$query = "SELECT first FROM test_79375 WHERE first = 1 FOR UPDATE";
echo "Running query on $name\n";
try {
$stmt = $db->query($query);
Expand All @@ -42,7 +41,7 @@ function testNormalQuery(PDO $db, string $name) {
function testPrepareExecute(PDO $db, string $name) {
$db->exec("SET innodb_lock_wait_timeout = 1");
$db->exec("START TRANSACTION");
$query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
$query = "SELECT first FROM test_79375 WHERE first = 1 FOR UPDATE";
echo "Running query on $name\n";
$stmt = $db->prepare($query);
try {
Expand All @@ -57,7 +56,7 @@ function testUnbuffered(PDO $db, string $name) {
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$db->exec("SET innodb_lock_wait_timeout = 1");
$db->exec("START TRANSACTION");
$query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
$query = "SELECT first FROM test_79375 WHERE first = 1 FOR UPDATE";
echo "Running query on $name\n";
$stmt = $db->prepare($query);
$stmt->execute();
Expand Down Expand Up @@ -96,7 +95,8 @@ echo "\n";
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test_79375');
?>
--EXPECT--
Running query on first connection
Expand Down
48 changes: 22 additions & 26 deletions ext/pdo_mysql/tests/bug80458.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,38 @@ $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);

$db->query('DROP TABLE IF EXISTS test');
$db->query('CREATE TABLE test (first int) ENGINE = InnoDB');
$res = $db->query('INSERT INTO test(first) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16)');
$db->query('CREATE TABLE test_80458 (first int) ENGINE = InnoDB');
$res = $db->query('INSERT INTO test_80458(first) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16)');
var_dump($res->fetchAll());

$stmt = $db->prepare('DELETE FROM test WHERE first=1');
$stmt = $db->prepare('DELETE FROM test_80458 WHERE first=1');
$stmt->execute();
var_dump($stmt->fetchAll());

$res = $db->query('DELETE FROM test WHERE first=2');
$res = $db->query('DELETE FROM test_80458 WHERE first=2');
var_dump($res->fetchAll());

$stmt2 = $db->prepare('DELETE FROM test WHERE first=3');
$stmt2 = $db->prepare('DELETE FROM test_80458 WHERE first=3');
$stmt2->execute();
foreach($stmt2 as $row){
// expect nothing
}

$stmt3 = $db->prepare('DELETE FROM test WHERE first=4');
$stmt3 = $db->prepare('DELETE FROM test_80458 WHERE first=4');
$stmt3->execute();
var_dump($stmt3->fetch(PDO::FETCH_ASSOC));

$stmt = $db->prepare('SELECT first FROM test WHERE first=5');
$stmt = $db->prepare('SELECT first FROM test_80458 WHERE first=5');
$stmt->execute();
var_dump($stmt->fetchAll());

$db->exec('DROP PROCEDURE IF EXISTS nores');
$db->exec('CREATE PROCEDURE nores() BEGIN DELETE FROM test WHERE first=6; END;');
$db->exec('CREATE PROCEDURE nores() BEGIN DELETE FROM test_80458 WHERE first=6; END;');
$stmt4 = $db->prepare('CALL nores()');
$stmt4->execute();
var_dump($stmt4->fetchAll());
$db->exec('DROP PROCEDURE IF EXISTS nores');

$db->exec('DROP PROCEDURE IF EXISTS ret');
$db->exec('CREATE PROCEDURE ret() BEGIN SELECT first FROM test WHERE first=7; END;');
$db->exec('CREATE PROCEDURE ret() BEGIN SELECT first FROM test_80458 WHERE first=7; END;');
$stmt5 = $db->prepare('CALL ret()');
$stmt5->execute();
var_dump($stmt5->fetchAll());
Expand All @@ -63,36 +60,32 @@ $db->exec('DROP PROCEDURE IF EXISTS ret');
print("Emulated prepares\n");
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

$stmt = $db->prepare('DELETE FROM test WHERE first=8');
$stmt = $db->prepare('DELETE FROM test_80458 WHERE first=8');
$stmt->execute();
var_dump($stmt->fetchAll());

$res = $db->query('DELETE FROM test WHERE first=9');
$res = $db->query('DELETE FROM test_80458 WHERE first=9');
var_dump($res->fetchAll());

$stmt2 = $db->prepare('DELETE FROM test WHERE first=10');
$stmt2 = $db->prepare('DELETE FROM test_80458 WHERE first=10');
$stmt2->execute();
foreach($stmt2 as $row){
// expect nothing
}

$stmt3 = $db->prepare('DELETE FROM test WHERE first=11');
$stmt3 = $db->prepare('DELETE FROM test_80458 WHERE first=11');
$stmt3->execute();
var_dump($stmt3->fetch(PDO::FETCH_ASSOC));

$stmt = $db->prepare('SELECT first FROM test WHERE first=12');
$stmt = $db->prepare('SELECT first FROM test_80458 WHERE first=12');
$stmt->execute();
var_dump($stmt->fetchAll());

$db->exec('DROP PROCEDURE IF EXISTS nores');
$db->exec('CREATE PROCEDURE nores() BEGIN DELETE FROM test WHERE first=13; END;');
$db->exec('CREATE PROCEDURE nores() BEGIN DELETE FROM test_80458 WHERE first=13; END;');
$stmt4 = $db->prepare('CALL nores()');
$stmt4->execute();
var_dump($stmt4->fetchAll());
$db->exec('DROP PROCEDURE IF EXISTS nores');

$db->exec('DROP PROCEDURE IF EXISTS ret');
$db->exec('CREATE PROCEDURE ret() BEGIN SELECT first FROM test WHERE first=14; END;');
$db->exec('CREATE PROCEDURE ret() BEGIN SELECT first FROM test_80458 WHERE first=14; END;');
$stmt5 = $db->prepare('CALL ret()');
$stmt5->execute();
var_dump($stmt5->fetchAll());
Expand All @@ -103,19 +96,22 @@ $db->exec('DROP PROCEDURE IF EXISTS ret');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

$stmt = $db->prepare('DELETE FROM test WHERE first=15');
$stmt = $db->prepare('DELETE FROM test_80458 WHERE first=15');
$stmt->execute();
var_dump($stmt->fetchAll());

$stmt = $db->prepare('SELECT first FROM test WHERE first=16');
$stmt = $db->prepare('SELECT first FROM test_80458 WHERE first=16');
$stmt->execute();
var_dump($stmt->fetchAll());

?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
$db = MySQLPDOTest::factory();
MySQLPDOTest::dropTestTable($db, 'test_80458');
$db->exec('DROP PROCEDURE IF EXISTS nores');
$db->exec('DROP PROCEDURE IF EXISTS ret');
?>
--EXPECT--
array(0) {
Expand Down
15 changes: 10 additions & 5 deletions ext/pdo_mysql/tests/bug80808.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ MySQLPDOTest::skip();
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$pdo = MySQLPDOTest::factory();

$pdo->exec('DROP TABLE IF EXISTS test');
$pdo->exec('CREATE TABLE test (postcode INT(4) UNSIGNED ZEROFILL)');
$pdo->exec('INSERT INTO test (postcode) VALUES (\'0800\')');
$pdo->exec('CREATE TABLE test_80808 (postcode INT(4) UNSIGNED ZEROFILL)');
$pdo->exec('INSERT INTO test_80808 (postcode) VALUES (\'0800\')');

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
var_dump($pdo->query('SELECT * FROM test')->fetchColumn(0));
var_dump($pdo->query('SELECT * FROM test_80808')->fetchColumn(0));
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
var_dump($pdo->query('SELECT * FROM test')->fetchColumn(0));
var_dump($pdo->query('SELECT * FROM test_80808')->fetchColumn(0));

?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test_80808');
?>
--EXPECT--
string(4) "0800"
Expand Down
8 changes: 4 additions & 4 deletions ext/pdo_mysql/tests/bug80908.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ function createDB(): PDO {
}

$db = createDB();
$db->exec('DROP TABLE IF EXISTS test');
$db->exec('CREATE TABLE test (`id` bigint(20) unsigned AUTO_INCREMENT, `name` varchar(5), primary key (`id`)) ENGINE = InnoDB AUTO_INCREMENT=10376293541461622799');
$db->exec('CREATE TABLE test_80908 (`id` bigint(20) unsigned AUTO_INCREMENT, `name` varchar(5), primary key (`id`)) ENGINE = InnoDB AUTO_INCREMENT=10376293541461622799');

function testLastInsertId(PDO $db) {
echo "Running test lastInsertId\n";
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
try {
$db->exec("insert into test (`name`) values ('bar')");
$db->exec("insert into test_80908 (`name`) values ('bar')");
$id = $db->lastInsertId();
echo "Last insert id is " . $id . "\n";
} catch (PDOException $e) {
Expand All @@ -43,7 +42,8 @@ echo "\n";
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test_80908');
?>
--EXPECT--
Running test lastInsertId
Expand Down
Loading

0 comments on commit 6f95273

Please sign in to comment.