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