Skip to content

Commit a831cd0

Browse files
committed
Optimized pdo_odbc tests
1 parent 490b808 commit a831cd0

11 files changed

+185
-83
lines changed

ext/pdo_odbc/tests/basic_connection.phpt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ Basic test for connection. (When not using a DSN alias)
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
require 'ext/pdo/tests/pdo_test.inc';
8-
PDOTest::skip();
9-
$dsn = getenv('PDO_ODBC_TEST_DSN');
10-
if (!$dsn || strpos($dsn, '=') === false) {
11-
die('skip');
12-
}
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skipNoDirect();
9+
ODBCPDOTest::skipToofewCredentials();
10+
ODBCPDOTest::skip();
1311
?>
1412
--XLEAK--
1513
A bug in msodbcsql causes a memory leak when reconnecting after closing. See GH-12306
1614
--FILE--
1715
<?php
18-
$dsnWithCredentials = getenv('PDO_ODBC_TEST_DSN');
19-
$user = getenv('PDO_ODBC_TEST_USER');
20-
$password = getenv('PDO_ODBC_TEST_PASS');
16+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
17+
$dsnWithCredentials = PDO_ODBC_TEST_DSN;
18+
$user = PDO_ODBC_TEST_USER;
19+
$password = PDO_ODBC_TEST_PASS;
2120

2221
$dsn = str_replace(";uid={$user};pwd={$password}", '', $dsnWithCredentials);
2322

ext/pdo_odbc/tests/bug44643.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Bug #44643 (bound parameters ignore explicit type definitions)
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
require 'ext/pdo/tests/pdo_test.inc';
8-
PDOTest::skip();
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skip();
99
?>
1010
--FILE--
1111
<?php
12-
require 'ext/pdo/tests/pdo_test.inc';
13-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
12+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
13+
$db = ODBCPDOTest::factory();
1414
$sql = "SELECT * FROM (SELECT 'test' = :id1) a WHERE a.test = :id2";
1515
$stmt = $db->prepare($sql);
1616
$id1 = 1;

ext/pdo_odbc/tests/bug67465.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Bug #67465 (NULL Pointer dereference in odbc_handle_preparer)
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
require 'ext/pdo/tests/pdo_test.inc';
8-
PDOTest::skip();
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skip();
99
?>
1010
--FILE--
1111
<?php
12-
require 'ext/pdo/tests/pdo_test.inc';
13-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
12+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
13+
$db = ODBCPDOTest::factory();
1414
$db->prepare("SELECT 1", [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
1515
echo "done\n";
1616
?>

ext/pdo_odbc/tests/bug80783.phpt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ Bug #80783 (PDO ODBC truncates BLOB records at every 256th byte)
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
require 'ext/pdo/tests/pdo_test.inc';
8-
PDOTest::skip();
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skip();
99
?>
1010
--FILE--
1111
<?php
12-
require 'ext/pdo/tests/pdo_test.inc';
13-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
14-
$db->exec("CREATE TABLE bug80783 (name IMAGE)");
12+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
13+
$db = ODBCPDOTest::factory();
14+
15+
$table_name = 'bug80783_pdo_odbc';
16+
17+
$db->exec("CREATE TABLE {$table_name} (name IMAGE)");
1518

1619
$string = str_repeat("0123456789", 50);
17-
$db->exec("INSERT INTO bug80783 VALUES('$string')");
20+
$db->exec("INSERT INTO {$table_name} VALUES('$string')");
1821

19-
$stmt = $db->prepare("SELECT name FROM bug80783");
22+
$stmt = $db->prepare("SELECT name FROM {$table_name}");
2023
$stmt->bindColumn(1, $data, PDO::PARAM_LOB);
2124
$stmt->execute();
2225
$stmt->fetch(PDO::FETCH_BOUND);
@@ -25,9 +28,9 @@ var_dump($data === bin2hex($string));
2528
?>
2629
--CLEAN--
2730
<?php
28-
require 'ext/pdo/tests/pdo_test.inc';
29-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
30-
$db->exec("DROP TABLE IF EXISTS bug80783");
31+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
32+
$db = ODBCPDOTest::factory();
33+
$db->exec("DROP TABLE IF EXISTS bug80783_pdo_odbc");
3134
?>
3235
--EXPECT--
3336
bool(true)

ext/pdo_odbc/tests/bug80783a.phpt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ Bug #80783 (PDO ODBC truncates BLOB records at every 256th byte)
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
8-
require 'ext/pdo/tests/pdo_test.inc';
9-
PDOTest::skip();
10-
11-
if (PDO_ODBC_TYPE === "unixODBC") {
12-
die("skip Fails with unixODBC");
13-
}
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skipWithUnixODBC();
9+
ODBCPDOTest::skip();
1410
?>
1511
--FILE--
1612
<?php
17-
require 'ext/pdo/tests/pdo_test.inc';
18-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
19-
$db->exec("CREATE TABLE bug80783a (name NVARCHAR(MAX))");
13+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
14+
$db = ODBCPDOTest::factory();
15+
16+
$table_name = 'bug80783a_pdo_odbc';
17+
18+
$db->exec("CREATE TABLE {$table_name} (name NVARCHAR(MAX))");
2019

2120
$string = str_repeat("0123456789", 50);
22-
$db->exec("INSERT INTO bug80783a VALUES('$string')");
21+
$db->exec("INSERT INTO {$table_name} VALUES('$string')");
2322

24-
$stmt = $db->prepare("SELECT name FROM bug80783a");
23+
$stmt = $db->prepare("SELECT name FROM {$table_name}");
2524
$stmt->setAttribute(PDO::ODBC_ATTR_ASSUME_UTF8, true);
2625
$stmt->bindColumn(1, $data, PDO::PARAM_STR);
2726
$stmt->execute();
@@ -31,9 +30,9 @@ var_dump($data === $string);
3130
?>
3231
--CLEAN--
3332
<?php
34-
require 'ext/pdo/tests/pdo_test.inc';
35-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
36-
$db->exec("DROP TABLE IF EXISTS bug80783a");
33+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
34+
$db = ODBCPDOTest::factory();
35+
$db->exec("DROP TABLE IF EXISTS bug80783a_pdo_odbc");
3736
?>
3837
--EXPECT--
3938
bool(true)

ext/pdo_odbc/tests/get_attribute_server.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ PDO ODBC getAttribute SERVER_INFO and SERVER_VERSION
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
require 'ext/pdo/tests/pdo_test.inc';
8-
PDOTest::skip();
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skip();
99
?>
1010
--FILE--
1111
<?php
12-
require 'ext/pdo/tests/pdo_test.inc';
13-
$db = PDOTest::test_factory('ext/pdo_odbc/tests/common.phpt');
12+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
13+
$db = ODBCPDOTest::factory();
1414
// Obviously, we can't assume what driver is being used, so just check strings
1515
// Example driver output (MariaDB ODBC):
1616
// PDO::ATTR_SERVER_INFO: MariaDB

ext/pdo_odbc/tests/gh9372.phpt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ Bug GH-9372 (HY010 when binding overlong parameter)
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
require 'ext/pdo/tests/pdo_test.inc';
8-
PDOTest::skip();
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skip();
99
?>
1010
--FILE--
1111
<?php
1212
// Executing the statements fails with some drivers, but not others.
1313
// The test is written in a way to always pass, unless the execution
1414
// fails with a different code than 22001 (String data, right truncation).
1515

16-
require 'ext/pdo/tests/pdo_test.inc';
17-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
16+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
17+
$db = ODBCPDOTest::factory();
1818
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1919

20-
$db->exec("CREATE TABLE gh9372 (col VARCHAR(10))");
21-
$db->exec("INSERT INTO gh9372 VALUES ('something')");
20+
$table_name = 'gh9372_pdo_odbc';
2221

23-
$stmt = $db->prepare("SELECT * FROM gh9372 WHERE col = ?");
22+
$db->exec("CREATE TABLE {$table_name} (col VARCHAR(10))");
23+
$db->exec("INSERT INTO {$table_name} VALUES ('something')");
24+
25+
$stmt = $db->prepare("SELECT * FROM {$table_name} WHERE col = ?");
2426
$stmt->bindValue(1, 'something else');
2527
try {
2628
$stmt->execute();
@@ -30,7 +32,7 @@ try {
3032
}
3133
}
3234

33-
$stmt = $db->prepare("SELECT * FROM gh9372 WHERE col = ?");
35+
$stmt = $db->prepare("SELECT * FROM {$table_name} WHERE col = ?");
3436
$stream = fopen("php://memory", "w+");
3537
fwrite($stream, 'something else');
3638
rewind($stream);
@@ -45,8 +47,8 @@ try {
4547
?>
4648
--CLEAN--
4749
<?php
48-
require 'ext/pdo/tests/pdo_test.inc';
49-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
50-
$db->exec("DROP TABLE gh9372");
50+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
51+
$db = ODBCPDOTest::factory();
52+
$db->exec("DROP TABLE IF EXISTS gh9372_pdo_odbc");
5153
?>
5254
--EXPECT--

ext/pdo_odbc/tests/inc/config.inc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
$env = [
3+
'PDO_ODBC_TEST_DSN' => false !== getenv('PDO_ODBC_TEST_DSN') ? getenv('PDO_ODBC_TEST_DSN') : '',
4+
'PDO_ODBC_TEST_USER' => false !== getenv('PDO_ODBC_TEST_USER') ? getenv('PDO_ODBC_TEST_USER') : null,
5+
'PDO_ODBC_TEST_PASS' => false !== getenv('PDO_ODBC_TEST_PASS') ? getenv('PDO_ODBC_TEST_PASS') : null,
6+
'PDO_ODBC_TEST_ATTR' => false !== getenv('PDO_ODBC_TEST_ATTR') ? getenv('PDO_ODBC_TEST_ATTR') : null,
7+
];
8+
9+
if (!$env['PDO_ODBC_TEST_DSN'] && preg_match('/^WIN/i', PHP_OS) && extension_loaded('com_dotnet')) {
10+
// on Windows and user didn't set PDOTEST_DSN, try this as a fallback:
11+
// check if MS Access DB is installed, and if yes, try using it. create a temporary MS access database.
12+
13+
$path = realpath(__DIR__) . '\..\pdo_odbc.mdb';
14+
if (!file_exists($path)) {
15+
try {
16+
// try to create database
17+
$adox = new COM('ADOX.Catalog');
18+
$adox->Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' . $path);
19+
$adox = null;
20+
21+
} catch (Exception $e) {
22+
// do nothing
23+
}
24+
}
25+
if (file_exists($path)) {
26+
// database was created and written to file system
27+
$env['PDO_ODBC_TEST_DSN'] = 'odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$path;Uid=Admin';
28+
$config['ENV']['PDOTEST_DSN'] = $env['PDO_ODBC_TEST_DSN'];
29+
}
30+
}
31+
?>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
require_once dirname(__FILE__) . '/config.inc';
3+
require_once dirname(__FILE__) . '/../../../../ext/pdo/tests/pdo_test.inc';
4+
5+
foreach ($env as $k => $v) {
6+
define($k, $v);
7+
}
8+
9+
class ODBCPDOTest extends PDOTest {
10+
11+
static function factory($classname = 'PDO') {
12+
$dsn = PDO_ODBC_TEST_DSN;
13+
$user = PDO_ODBC_TEST_USER;
14+
$pass = PDO_ODBC_TEST_PASS;
15+
$attr = PDO_ODBC_TEST_ATTR;
16+
17+
$attr = is_string($attr) && strlen($attr) ? unserialize($attr) : null;
18+
19+
$db = new $classname($dsn, $user, $pass, $attr);
20+
if (!$db) {
21+
die("Could not create PDO object (DSN=$dsn, user=$user)\n");
22+
}
23+
24+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
25+
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
26+
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
27+
28+
return $db;
29+
}
30+
31+
static function skip() {
32+
if (substr(PHP_OS, 0, 3) == 'WIN' &&
33+
false === getenv('PDOTEST_DSN') &&
34+
false === getenv('PDO_ODBC_TEST_DSN') &&
35+
!extension_loaded('com_dotnet')
36+
) {
37+
die('skip - either PDOTEST_DSN or com_dotnet extension is needed to setup the connection');
38+
}
39+
40+
try {
41+
$db = self::factory();
42+
} catch (PDOException $e) {
43+
die('skip could not connect');
44+
}
45+
}
46+
47+
static function skipWithUnixODBC() {
48+
if (PDO_ODBC_TYPE === "unixODBC") {
49+
die("skip Fails with unixODBC");
50+
}
51+
}
52+
53+
static function skipNoDirect() {
54+
$dsn = PDO_ODBC_TEST_DSN;
55+
if (!$dsn || strpos($dsn, '=') === false) {
56+
die('skip this test is for direct connections only.');
57+
}
58+
}
59+
60+
static function skipToofewCredentials() {
61+
if (PDO_ODBC_TEST_DSN === null || PDO_ODBC_TEST_USER === null || PDO_ODBC_TEST_PASS === null) {
62+
die('skip too few credentials.');
63+
}
64+
}
65+
}
66+
?>

ext/pdo_odbc/tests/long_columns.phpt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ PDO ODBC "long" columns
44
pdo_odbc
55
--SKIPIF--
66
<?php
7-
// make sure there is an ODBC driver and a DSN, or the test will fail
8-
include 'ext/pdo/tests/pdo_test.inc';
9-
$config = PDOTest::get_config('ext/pdo_odbc/tests/common.phpt');
10-
if (!isset($config['ENV']['PDOTEST_DSN']) || $config['ENV']['PDOTEST_DSN']===false) print 'skip';
7+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
8+
ODBCPDOTest::skip();
119
?>
1210
--FILE--
1311
<?php
@@ -42,13 +40,15 @@ if (!isset($config['ENV']['PDOTEST_DSN']) || $config['ENV']['PDOTEST_DSN']===fal
4240
// configure --disable-all --enable-cli --enable-pdo --with-pdo-odbc=unixODBC,/usr,/usr --with-unixODBC=/usr --enable-debug
4341
//
4442

45-
require 'ext/pdo/tests/pdo_test.inc';
46-
$db = PDOTest::test_factory('ext/pdo_odbc/tests/common.phpt');
43+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
44+
$db = ODBCPDOTest::factory();
4745
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
4846

49-
if (false === $db->exec('CREATE TABLE test_long_columns (id INT NOT NULL PRIMARY KEY, data CLOB)')) {
50-
if (false === $db->exec('CREATE TABLE test_long_columns (id INT NOT NULL PRIMARY KEY, data longtext)')) {
51-
if (false === $db->exec('CREATE TABLE test_long_columns (id INT NOT NULL PRIMARY KEY, data varchar(4000))')) {
47+
$table_name = 'test_long_columns_pdo_odbc';
48+
49+
if (false === $db->exec("CREATE TABLE {$table_name} (id INT NOT NULL PRIMARY KEY, data CLOB)")) {
50+
if (false === $db->exec("CREATE TABLE {$table_name} (id INT NOT NULL PRIMARY KEY, data longtext)")) {
51+
if (false === $db->exec("CREATE TABLE {$table_name} (id INT NOT NULL PRIMARY KEY, data varchar(4000))")) {
5252
die("BORK: don't know how to create a long column here:\n" . implode(", ", $db->errorInfo()));
5353
}
5454
}
@@ -74,11 +74,11 @@ function alpha_repeat($len) {
7474
// this test does - nice to be able to test using MS SQL server
7575
foreach ($sizes as $num) {
7676
$text = alpha_repeat($num);
77-
$db->exec("INSERT INTO test_long_columns VALUES($num, '$text')");
77+
$db->exec("INSERT INTO {$table_name} VALUES($num, '$text')");
7878
}
7979

8080
// verify data
81-
foreach ($db->query('SELECT id, data from test_long_columns ORDER BY LEN(data) ASC') as $row) {
81+
foreach ($db->query("SELECT id, data FROM {$table_name} ORDER BY LEN(data) ASC") as $row) {
8282
$expect = alpha_repeat($row[0]);
8383
if (strcmp($expect, $row[1])) {
8484
echo "Failed on size $row[id]:\n";
@@ -94,9 +94,9 @@ echo "Finished\n";
9494
?>
9595
--CLEAN--
9696
<?php
97-
require 'ext/pdo/tests/pdo_test.inc';
98-
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
99-
$db->exec("DROP TABLE IF EXISTS test_long_columns");
97+
require_once dirname(__FILE__) . '/inc/odbc_pdo_test.inc';
98+
$db = ODBCPDOTest::factory();
99+
$db->exec("DROP TABLE IF EXISTS test_long_columns_pdo_odbc");
100100
?>
101101
--EXPECT--
102102
Passed on size 32

0 commit comments

Comments
 (0)