X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FTest.php;h=cf9c841ab7981ccd8fdab426209df134aaf77d4e;hb=eb7549fd9cdb7b08d9a4c300c201ebef2557c9e7;hp=e70f0f40262f558e912b472ddbc5a1673370fe6c;hpb=2078a04eb10e9a0d655ea6dfa6c8d02615f0aefe;p=civicrm-core.git diff --git a/Civi/Test.php b/Civi/Test.php index e70f0f4026..cf9c841ab7 100644 --- a/Civi/Test.php +++ b/Civi/Test.php @@ -14,7 +14,38 @@ class Test { /** * @var array */ - private static $singletons = array(); + private static $singletons = []; + + /** + * @var array + */ + public static $statics = []; + + /** + * Run code in a pre-boot fashion. + * + * @param callable $callback + * @return mixed + * Pass through the result of the callback. + */ + public static function asPreInstall($callback) { + $conn = \Civi\Test::pdo(); + + $oldEscaper = \CRM_Core_I18n::$SQL_ESCAPER; + \Civi::$statics['testPreInstall'] = (\Civi::$statics['testPreInstall'] ?? 0) + 1; + try { + \CRM_Core_I18n::$SQL_ESCAPER = function ($text) use ($conn) { + return substr($conn->quote($text), 1, -1); + }; + return $callback(); + } finally { + \CRM_Core_I18n::$SQL_ESCAPER = $oldEscaper; + \Civi::$statics['testPreInstall']--; + if (\Civi::$statics['testPreInstall'] <= 0) { + unset(\Civi::$statics['testPreInstall']); + } + } + } /** * Get the data source used for testing. @@ -28,7 +59,8 @@ class Test { public static function dsn($part = NULL) { if (!isset(self::$singletons['dsn'])) { require_once "DB.php"; - self::$singletons['dsn'] = \DB::parseDSN(CIVICRM_DSN); + $dsn = \CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN); + self::$singletons['dsn'] = \DB::parseDSN($dsn); } if ($part === NULL) { @@ -45,7 +77,7 @@ class Test { /** * Get a connection to the test database. * - * @return PDO + * @return \PDO */ public static function pdo() { if (!isset(self::$singletons['pdo'])) { @@ -55,7 +87,7 @@ class Test { try { self::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""), $dsninfo['username'], $dsninfo['password'], - array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE) + [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE] ); } catch (PDOException $e) { @@ -69,12 +101,12 @@ class Test { /** * Create a builder for the headless environment. * - * @return \Civi\Test\CiviEnvBuilder - * - * @code + * ``` * \Civi\Test::headless()->apply(); * \Civi\Test::headless()->sqlFile('ex.sql')->apply(); - * @endCode + * ``` + * + * @return \Civi\Test\CiviEnvBuilder */ public static function headless() { $civiRoot = dirname(__DIR__); @@ -88,7 +120,7 @@ class Test { echo "Installing {$dbName} schema\n"; \Civi\Test::schema()->dropAll(); }, 'headless-drop') - ->sqlFile($civiRoot . "/sql/civicrm.mysql") + ->coreSchema() ->sql("DELETE FROM civicrm_extension") ->callback(function ($ctx) { \Civi\Test::data()->populate(); @@ -99,12 +131,12 @@ class Test { /** * Create a builder for end-to-end testing on the live environment. * - * @return \Civi\Test\CiviEnvBuilder - * - * @code + * ``` * \Civi\Test::e2e()->apply(); * \Civi\Test::e2e()->install('foo.bar')->apply(); - * @endCode + * ``` + * + * @return \Civi\Test\CiviEnvBuilder */ public static function e2e() { $builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder'); @@ -127,6 +159,18 @@ class Test { return self::$singletons['schema']; } + /** + * @return \CRM_Core_CodeGen_Main + */ + public static function codeGen() { + if (!isset(self::$singletons['codeGen'])) { + $civiRoot = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__DIR__)); + $codeGen = new \CRM_Core_CodeGen_Main("$civiRoot/CRM/Core/DAO", "$civiRoot/sql", $civiRoot, "$civiRoot/templates", NULL, "UnitTests", NULL, "$civiRoot/xml/schema/Schema.xml", NULL); + $codeGen->init(); + self::$singletons['codeGen'] = $codeGen; + } + return self::$singletons['codeGen']; + } /** * @return \Civi\Test\Data