コード例 #1
0
 public static function setUpBeforeClass()
 {
     jimport('joomla.database.database');
     jimport('joomla.database.table');
     // First let's look to see if we have a DSN defined or in the environment variables.
     if (defined('JTEST_DATABASE_MYSQLI_DSN') || getenv('JTEST_DATABASE_MYSQLI_DSN')) {
         $dsn = defined('JTEST_DATABASE_MYSQLI_DSN') ? JTEST_DATABASE_MYSQLI_DSN : getenv('JTEST_DATABASE_MYSQLI_DSN');
     } else {
         return;
     }
     // First let's trim the mysql: part off the front of the DSN if it exists.
     if (strpos($dsn, 'mysql:') === 0) {
         $dsn = substr($dsn, 6);
     }
     // Split the DSN into its parts over semicolons.
     $parts = explode(';', $dsn);
     // Parse each part and populate the options array.
     foreach ($parts as $part) {
         list($k, $v) = explode('=', $part, 2);
         switch ($k) {
             case 'host':
                 self::$options['host'] = $v;
                 break;
             case 'dbname':
                 self::$options['database'] = $v;
                 break;
             case 'user':
                 self::$options['user'] = $v;
                 break;
             case 'pass':
                 self::$options['password'] = $v;
                 break;
         }
     }
     if (!is_object(self::$dbo)) {
         try {
             if (class_exists('JDatabaseDriver')) {
                 self::$dbo = JDatabaseDriver::getInstance(self::$options);
             } else {
                 self::$dbo = JDatabase::getInstance(self::$options);
             }
         } catch (Exception $e) {
             self::$dbo = null;
         }
     }
     self::$database = JFactory::$database;
     JFactory::$database = self::$dbo;
 }