X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FTest.php;h=50d6ebf31c4a39ee1d97fa1071b29516757eec2d;hb=4327f20a9a6e7c2b12eac4b1a845f85569618724;hp=8e44fcd5094fdd6bf3346cd22660c677dc003caa;hpb=5c22567ff39bf585b2da7920620666f4aef2cc92;p=civicrm-core.git diff --git a/Civi/Test.php b/Civi/Test.php index 8e44fcd509..50d6ebf31c 100644 --- a/Civi/Test.php +++ b/Civi/Test.php @@ -16,6 +16,37 @@ class Test { */ 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. * @@ -45,7 +76,7 @@ class Test { /** * Get a connection to the test database. * - * @return PDO + * @return \PDO */ public static function pdo() { if (!isset(self::$singletons['pdo'])) { @@ -88,7 +119,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(); @@ -127,6 +158,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