xref: /php-src/ext/pdo_dblib/tests/config.inc (revision d6a0b3af)
1<?php
2
3// bug #72969 reflects a bug with FreeTDS, not with pdo_dblib
4// this function will version detect so the relevant tests can XFAILIF
5// assume this bug isn't present if not using FreeTDS
6// otherwise require FreeTDS>=1.1
7function driver_supports_batch_statements_without_select($db) {
8    $version = $db->getAttribute(PDO::DBLIB_ATTR_VERSION);
9    return !strstartswith($version, 'freetds ') || !strstartswith($version, 'freetds v1.0');
10}
11
12function strstartswith($haystack, $needle) {
13    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
14}
15
16function getCredentials() {
17    if (false !== getenv('PDO_DBLIB_TEST_DSN')) {
18        $dsn = getenv('PDO_DBLIB_TEST_DSN');
19    } else {
20        $dsn = 'dblib:host=localhost;dbname=test';
21    }
22
23    if (false !== getenv('PDO_DBLIB_TEST_USER')) {
24        $user = getenv('PDO_DBLIB_TEST_USER');
25    } else {
26        $user = 'php';
27    }
28
29    if (false !== getenv('PDO_DBLIB_TEST_PASS')) {
30        $pass = getenv('PDO_DBLIB_TEST_PASS');
31    } else {
32        $pass = 'password';
33    }
34
35    return [$dsn, $user, $pass];
36}
37
38function setAttributes(PDO $db) {
39    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
40    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
41}
42
43function getDbConnection(string $class = PDO::class, ?array $attributes = null) {
44    [$dsn, $user, $pass] = getCredentials();
45
46    try {
47        $db = new $class($dsn, $user, $pass, $attributes);
48        if ($attributes === null) {
49            setAttributes($db);
50        }
51    } catch (PDOException $e) {
52        die('skip ' . $e->getMessage());
53    }
54
55    return $db;
56}
57
58function connectToDb() {
59    [$dsn, $user, $pass] = getCredentials();
60
61    try {
62        $db = PDO::connect($dsn, $user, $pass);
63        setAttributes($db);
64    } catch (PDOException $e) {
65        die('skip ' . $e->getMessage());
66    }
67
68    return $db;
69}
70
71?>
72