xref: /PHP-7.1/ext/pdo/tests/pdo_test.inc (revision bd394953)
1<?php
2# PDO test framework utilities
3
4if (getenv('PDOTEST_DSN') === false) {
5	$common = '';
6	$append = false;
7	foreach(file(dirname($_SERVER['PHP_SELF']).'/common.phpt') as $line) {
8		if ($append) {
9			$common .= $line;
10		} elseif (trim($line) == '--REDIRECTTEST--') {
11			$append = true;
12		}
13	}
14	$conf = eval($common);
15	foreach($conf['ENV'] as $n=>$v) putenv("$n=$v");
16}
17
18class PDOTest {
19	// create an instance of the PDO driver, based on
20	// the current environment
21	static function factory($classname = 'PDO', $drop_test_tables = true) {
22		$dsn = getenv('PDOTEST_DSN');
23		$user = getenv('PDOTEST_USER');
24		$pass = getenv('PDOTEST_PASS');
25		$attr = getenv('PDOTEST_ATTR');
26		if (is_string($attr) && strlen($attr)) {
27			$attr = unserialize($attr);
28		} else {
29			$attr = null;
30		}
31
32		if ($user === false) $user = NULL;
33		if ($pass === false) $pass = NULL;
34
35		$db = new $classname($dsn, $user, $pass, $attr);
36
37		if (!$db) {
38			die("Could not create PDO object (DSN=$dsn, user=$user)\n");
39		}
40
41		// clean up any crufty test tables we might have left behind
42		// on a previous run
43		static $test_tables = array(
44			'test',
45			'test2',
46			'classtypes'
47			);
48		if ($drop_test_tables) {
49			foreach ($test_tables as $table) {
50				$db->exec("DROP TABLE $table");
51			}
52		}
53
54		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
55		$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
56		$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
57		return $db;
58	}
59
60	static function skip() {
61		try {
62			$db = PDOTest::factory();
63		} catch (PDOException $e) {
64			die("skip " . $e->getMessage());
65		}
66	}
67
68	static function test_factory($file) {
69		$config = self::get_config($file);
70		foreach ($config['ENV'] as $k => $v) {
71			putenv("$k=$v");
72		}
73		return self::factory();
74	}
75
76	static function get_config($file) {
77		$data = file_get_contents($file);
78		$data = preg_replace('/^.*--REDIRECTTEST--/s', '', $data);
79		$config = eval($data);
80
81		return $config;
82	}
83}
84?>
85