From f3e16511c09b8bbcef5c658b0edb00ae1d808a69 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 28 Jan 2016 21:41:37 -0800 Subject: [PATCH] CiviTest - Eliminate global mysql variables. Simplify bootstrap.php. --- tests/phpunit/CiviTest/CiviUnitTestCase.php | 26 +++++++-------------- tests/phpunit/CiviTest/bootstrap.php | 9 ------- tests/phpunit/Utils.php | 13 +++++++---- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 2e7fbf1f09..e3369d995b 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -159,19 +159,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // we need full error reporting error_reporting(E_ALL & ~E_NOTICE); - if (!empty($GLOBALS['mysql_db'])) { - self::$_dbName = $GLOBALS['mysql_db']; - } - else { - self::$_dbName = 'civicrm_tests_dev'; - } + self::$_dbName = self::getDBName(); // create test database - self::$utils = new Utils($GLOBALS['mysql_host'], - $GLOBALS['mysql_port'], - $GLOBALS['mysql_user'], - $GLOBALS['mysql_pass'] - ); + self::$utils = new Utils(CIVICRM_DSN); // also load the class loader require_once 'CRM/Core/ClassLoader.php'; @@ -210,7 +201,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @return string */ public static function getDBName() { - $dbName = !empty($GLOBALS['mysql_db']) ? $GLOBALS['mysql_db'] : 'civicrm_tests_dev'; + static $dbName = NULL; + if ($dbName === NULL ) { + require_once "DB.php"; + $dsninfo = DB::parseDSN(CIVICRM_DSN); + $dbName = $dsninfo['database']; + } return $dbName; } @@ -527,11 +523,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * FIXME: Maybe a better way to do it */ public function foreignKeyChecksOff() { - self::$utils = new Utils($GLOBALS['mysql_host'], - $GLOBALS['mysql_port'], - $GLOBALS['mysql_user'], - $GLOBALS['mysql_pass'] - ); + self::$utils = new Utils(CIVICRM_DSN); $dbName = self::getDBName(); $query = "USE {$dbName};" . "SET foreign_key_checks = 1"; if (self::$utils->do_query($query) === FALSE) { diff --git a/tests/phpunit/CiviTest/bootstrap.php b/tests/phpunit/CiviTest/bootstrap.php index 2dd6a2c442..84b52d7163 100644 --- a/tests/phpunit/CiviTest/bootstrap.php +++ b/tests/phpunit/CiviTest/bootstrap.php @@ -18,15 +18,6 @@ ini_set('memory_limit', '2G'); define('CIVICRM_TEST', 1); eval(cv('php:boot --level=settings', 'phpcode')); -// This is exists to support CiviUnitTestCase::populateDB(). That doesn't make it a good idea. -require_once "DB.php"; -$dsninfo = DB::parseDSN(CIVICRM_DSN); -$GLOBALS['mysql_host'] = $dsninfo['hostspec']; -$GLOBALS['mysql_port'] = @$dsninfo['port']; -$GLOBALS['mysql_user'] = $dsninfo['username']; -$GLOBALS['mysql_pass'] = $dsninfo['password']; -$GLOBALS['mysql_db'] = $dsninfo['database']; - // ------------------------------------------------------------------------------ /** diff --git a/tests/phpunit/Utils.php b/tests/phpunit/Utils.php index 1e31961069..30e1def8a8 100644 --- a/tests/phpunit/Utils.php +++ b/tests/phpunit/Utils.php @@ -44,12 +44,15 @@ class Utils { /** * Construct an object for this database. - * @param $host - * @param $port - * @param $user - * @param $pass */ - public function __construct($host, $port, $user, $pass) { + public function __construct($dsn) { + require_once "DB.php"; + $dsninfo = DB::parseDSN($dsn); + $host = $dsninfo['hostspec']; + $port = @$dsninfo['port']; + $user = $dsninfo['username']; + $pass = $dsninfo['password']; + try { $this->pdo = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""), $user, $pass, -- 2.25.1