xref: /PHP-8.4/ext/pdo_odbc/tests/common.phpt (revision f39b5c4c)
1--TEST--
2ODBC
3--EXTENSIONS--
4pdo_odbc
5--SKIPIF--
6<?php
7if (substr(PHP_OS, 0, 3) == 'WIN' &&
8    false === getenv('PDOTEST_DSN') &&
9    false === getenv('PDO_ODBC_TEST_DSN') &&
10    !extension_loaded('com_dotnet')) {
11    die('skip - either PDOTEST_DSN or com_dotnet extension is needed to setup the connection');
12}
13?>
14--REDIRECTTEST--
15# magic auto-configuration
16
17$config = array(
18	'TESTS' => 'ext/pdo/tests',
19	'ENV' => array()
20);
21
22// try loading PDO driver using ENV vars and if none given, and on Windows, try using MS Access
23// and if not, skip the test
24//
25// try to use common PDO env vars, instead of PDO_ODBC specific
26if (false !== getenv('PDOTEST_DSN')) {
27	// user should have to set PDOTEST_DSN so that:
28	// 1. test is skipped if user doesn't want to test it, even if they have MS Access installed
29	// 2. it detects if ODBC driver is not installed - to avoid test bug
30	// 3. it detects if ODBC driver is installed - so test will be run
31	// 4. so a specific ODBC driver can be tested - if system has multiple ODBC drivers
32
33	$config['ENV']['PDOTEST_DSN'] = getenv('PDOTEST_DSN');
34	$config['ENV']['PDOTEST_USER'] = getenv('PDOTEST_USER');
35	$config['ENV']['PDOTEST_PASS'] = getenv('PDOTEST_PASS');
36	if (false !== getenv('PDOTEST_ATTR')) {
37		$config['ENV']['PDOTEST_ATTR'] = getenv('PDOTEST_ATTR');
38	}
39} else if (false !== getenv('PDO_ODBC_TEST_DSN')) {
40	// user set these from their shell instead
41	$config['ENV']['PDOTEST_DSN'] = getenv('PDO_ODBC_TEST_DSN');
42	$config['ENV']['PDOTEST_USER'] = getenv('PDO_ODBC_TEST_USER');
43	$config['ENV']['PDOTEST_PASS'] = getenv('PDO_ODBC_TEST_PASS');
44	if (false !== getenv('PDO_ODBC_TEST_ATTR')) {
45		$config['ENV']['PDOTEST_ATTR'] = getenv('PDO_ODBC_TEST_ATTR');
46	}
47} elseif (preg_match('/^WIN/i', PHP_OS)) {
48	// on Windows and user didn't set PDOTEST_DSN, try this as a fallback:
49	// check if MS Access DB is installed, and if yes, try using it. create a temporary MS access database.
50	//
51	$path = realpath(__DIR__) . '\pdo_odbc.mdb';
52	if (!file_exists($path)) {
53		try {
54			// try to create database
55			$adox = new COM('ADOX.Catalog');
56			$adox->Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' . $path);
57			$adox = null;
58
59		} catch (Exception $e) {
60		}
61	}
62	if (file_exists($path)) {
63		// database was created and written to file system
64		$config['ENV']['PDOTEST_DSN'] = "odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$path;Uid=Admin";
65	} // else: $config['ENV']['PDOTEST_DSN'] not set
66} // else: $config['ENV']['PDOTEST_DSN'] not set
67//         test will be skipped. see SKIPIF section of long_columns.phpt
68
69# other magic autodetection here, eg: for DB2 by inspecting env
70/*
71$USER = 'db2inst1';
72$PASSWD = 'ibmdb2';
73$DBNAME = 'SAMPLE';
74
75$CONNECTION = "odbc:DSN=$DBNAME;UID=$USER;PWD=$PASSWD;";
76*/
77
78
79return $config;
80