Back to index

php5  5.3.10
mysql_pdo_test.inc
Go to the documentation of this file.
00001 <?php
00002 require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.inc');
00003 require_once(dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc');
00004 
00005 class MySQLPDOTest extends PDOTest {
00006 
00007        static function factory($classname = 'PDO', $drop_test_tables = false, $myattr = null, $mydsn = null) {
00008 
00009               $dsn   = self::getDSN($mydsn);
00010               $user  = PDO_MYSQL_TEST_USER;
00011               $pass  = PDO_MYSQL_TEST_PASS;
00012               $attr  = getenv('PDOTEST_ATTR');
00013 
00014               if (is_string($attr) && strlen($attr)) {
00015                      $attr = unserialize($attr);
00016               } else {
00017                      $attr = null;
00018               }
00019               if ($user === false)
00020                      $user = NULL;
00021               if ($pass === false)
00022                      $pass = NULL;
00023 
00024               $db = new $classname($dsn, $user, $pass, $attr);
00025               if (!$db) {
00026                      die("Could not create PDO object (DSN=$dsn, user=$user)\n");
00027               }
00028 
00029               $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
00030               $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
00031 
00032               return $db;
00033        }
00034 
00035        static function createTestTable($db, $engine = null) {
00036               if (!$engine)
00037                      $engine = PDO_MYSQL_TEST_ENGINE;
00038 
00039               $db->exec('DROP TABLE IF EXISTS test');
00040               $db->exec('CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=' . $engine);
00041               $db->exec("INSERT INTO test(id, label) VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')");
00042        }
00043 
00044        static function getTableEngine() {
00045               return PDO_MYSQL_TEST_ENGINE;
00046        }
00047 
00048 
00049        static function getDSN($new_options = null, $addition = '') {
00050               if (!$new_options)
00051                      return PDO_MYSQL_TEST_DSN . $addition;
00052 
00053               $old_options = array();
00054               $dsn = substr(PDO_MYSQL_TEST_DSN,
00055                      strpos(PDO_MYSQL_TEST_DSN, ':') + 1,
00056                      strlen(PDO_MYSQL_TEST_DSN));
00057 
00058               // no real parser - any excotic setting can fool us
00059               $parts = explode(';', $dsn);
00060               foreach ($parts as $k => $v) {
00061                      $tmp = explode('=', $v);
00062                      if (count($tmp) == 2)
00063                             $old_options[$tmp[0]] = $tmp[1];
00064               }
00065 
00066               $options = $old_options;
00067               foreach ($new_options as $k => $v)
00068                      $options[$k] = $v;
00069 
00070               $dsn = 'mysql:';
00071               foreach ($options as $k => $v)
00072                      $dsn .= sprintf('%s=%s;', $k, $v);
00073 
00074               if ($addition)
00075                      $dsn .= $addition;
00076               else
00077                      $dsn = substr($dsn, 0, strlen($dsn) -1);
00078 
00079               return $dsn;
00080        }
00081 
00082        static function getClientVersion($db) {
00083               return self::extractVersion($db->getAttribute(PDO::ATTR_CLIENT_VERSION));
00084        }
00085 
00086        static function getServerVersion($db) {
00087               return self::extractVersion($db->getAttribute(PDO::ATTR_SERVER_VERSION));
00088        }
00089 
00090        static function extractVersion($version_string) {
00091               /*
00092               TODO:
00093               We're a bit in trouble: PDO_MYSQL returns version strings.
00094               That's wrong according to the manual. According to the manual
00095               integers should be returned. However, this code needs to work
00096               with stinky PDO_MYSQL and hopefully better PDO_MYSQLND.
00097               */
00098 
00099               // already an int value?
00100               if (is_int($version_string))
00101                      return $version_string;
00102 
00103               // string but int value?
00104               $tmp = (int)$version_string;
00105               if (((string)$tmp) === $version_string)
00106                      return $tmp;
00107 
00108               // stinky string which we need to parse
00109               $parts = explode('.', $version_string);
00110               if (count($parts) != 3)
00111                      return -1;
00112 
00113               $version = (int)$parts[0] * 10000;
00114               $version+= (int)$parts[1] * 100;
00115               $version+= (int)$parts[2];
00116 
00117               return $version;
00118        }
00119 
00120        static function getTempDir() {
00121 
00122               if (!function_exists('sys_get_temp_dir')) {
00123 
00124                      if (!empty($_ENV['TMP']))
00125                             return realpath( $_ENV['TMP'] );
00126                      if (!empty($_ENV['TMPDIR']))
00127                             return realpath( $_ENV['TMPDIR'] );
00128                      if (!empty($_ENV['TEMP']))
00129                             return realpath( $_ENV['TEMP'] );
00130 
00131                      $temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
00132                      if ($temp_file) {
00133                             $temp_dir = realpath(dirname($temp_file));
00134                             unlink($temp_file);
00135                             return $temp_dir;
00136                      }
00137                      return FALSE;
00138               } else {
00139                      return sys_get_temp_dir();
00140               }
00141 
00142        }
00143 
00144        static function detect_transactional_mysql_engine($db) {              
00145               foreach ($db->query("show variables like 'have%'") as $row) {
00146                      if (!empty($row) && $row[1] == 'YES' && ($row[0] == 'have_innodb' || $row[0] == 'have_bdb')) {
00147                             return str_replace("have_", "", $row[0]);
00148                      }
00149               }
00150               /* MySQL 5.6.1+ */
00151               foreach ($db->query("SHOW ENGINES") as $row) {
00152                      if (isset($row['engine']) && isset($row['support'])) {
00153                              if ('InnoDB' == $row['engine'] && ('YES' == $row['support'] || 'DEFAULT' == $row['support']))
00154                                    return 'innodb';
00155                      }
00156               }
00157               return false;
00158        }
00159 
00160        static function isPDOMySQLnd() {
00161                      ob_start();
00162                      phpinfo();
00163                      $tmp = ob_get_contents();
00164                      ob_end_clean();
00165                      $tmp = stristr($tmp, "PDO Driver for MySQL => enabled");
00166                      return (bool)preg_match('/Client API version.*mysqlnd/', $tmp);
00167        }
00168 
00169        static function dropTestTable($db = NULL) {
00170               if (is_null($db))
00171                      $db = self::factory();
00172 
00173               $db->exec('DROP TABLE IF EXISTS test');
00174        }
00175 
00176 }
00177 ?>