Back to index

php5  5.3.10
pdo_test.inc
Go to the documentation of this file.
00001 <?php
00002 # PDO test framework utilities
00003 
00004 if (getenv('PDOTEST_DSN') === false) {
00005        $common = '';
00006        $append = false;
00007        foreach(file(dirname($_SERVER['PHP_SELF']).'/common.phpt') as $line) {
00008               if ($append) {
00009                      $common .= $line;
00010               } elseif (trim($line) == '--REDIRECTTEST--') {
00011                      $append = true;
00012               }
00013        }
00014        if (ini_get('magic_quotes_runtime')) $common = stripslashes($common);
00015        $conf = eval($common);
00016        foreach($conf['ENV'] as $n=>$v) putenv("$n=$v");
00017 }
00018 
00019 class PDOTest {
00020        // create an instance of the PDO driver, based on
00021        // the current environment
00022        static function factory($classname = 'PDO', $drop_test_tables = true) {
00023               $dsn = getenv('PDOTEST_DSN');
00024               $user = getenv('PDOTEST_USER');
00025               $pass = getenv('PDOTEST_PASS');
00026               $attr = getenv('PDOTEST_ATTR');
00027               if (is_string($attr) && strlen($attr)) {
00028                      $attr = unserialize($attr);
00029               } else {
00030                      $attr = null;
00031               }
00032 
00033               if ($user === false) $user = NULL;
00034               if ($pass === false) $pass = NULL;
00035 
00036               $db = new $classname($dsn, $user, $pass, $attr);
00037 
00038               if (!$db) {
00039                      die("Could not create PDO object (DSN=$dsn, user=$user)\n");
00040               }
00041 
00042               // clean up any crufty test tables we might have left behind
00043               // on a previous run
00044               static $test_tables = array(
00045                      'test',
00046                      'test2',
00047                      'classtypes'
00048                      );
00049               if ($drop_test_tables) {
00050                      foreach ($test_tables as $table) {
00051                             $db->exec("DROP TABLE $table");
00052                      }
00053               }
00054 
00055               $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
00056               $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
00057               $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
00058               return $db;
00059        }
00060 
00061        static function skip() {
00062               try {
00063                      $db = PDOTest::factory();
00064               } catch (PDOException $e) {
00065                      die("skip " . $e->getMessage());
00066               }
00067        }
00068 
00069        static function test_factory($file) {
00070               $data = file_get_contents($file);
00071               $data = preg_replace('/^.*--REDIRECTTEST--/s', '', $data);
00072               $config = eval($data);
00073               foreach ($config['ENV'] as $k => $v) {
00074                      putenv("$k=$v");
00075               }
00076               return self::factory();
00077        }
00078 }
00079 ?>