10 * A facade for managing the test environment.
17 private static $singletons = [];
22 public static $statics = [];
25 * Run code in a pre-boot fashion.
27 * @param callable $callback
29 * Pass through the result of the callback.
31 public static function asPreInstall($callback) {
32 $conn = \Civi\Test
::pdo();
34 $oldEscaper = \CRM_Core_I18n
::$SQL_ESCAPER;
35 \Civi
::$statics['testPreInstall'] = (\Civi
::$statics['testPreInstall'] ??
0) +
1;
37 \CRM_Core_I18n
::$SQL_ESCAPER = function ($text) use ($conn) {
38 return substr($conn->quote($text), 1, -1);
42 \CRM_Core_I18n
::$SQL_ESCAPER = $oldEscaper;
43 \Civi
::$statics['testPreInstall']--;
44 if (\Civi
::$statics['testPreInstall'] <= 0) {
45 unset(\Civi
::$statics['testPreInstall']);
51 * Get the data source used for testing.
53 * @param string|NULL $part
54 * One of NULL, 'hostspec', 'port', 'username', 'password', 'database'.
55 * @return string|array|NULL
56 * If $part is omitted, return full DSN array.
57 * If $part is a string, return that part of the DSN.
59 public static function dsn($part = NULL) {
60 if (!isset(self
::$singletons['dsn'])) {
61 require_once "DB.php";
62 self
::$singletons['dsn'] = \DB
::parseDSN(CIVICRM_DSN
);
66 return self
::$singletons['dsn'];
69 if (isset(self
::$singletons['dsn'][$part])) {
70 return self
::$singletons['dsn'][$part];
77 * Get a connection to the test database.
81 public static function pdo() {
82 if (!isset(self
::$singletons['pdo'])) {
83 $dsninfo = self
::dsn();
84 $host = $dsninfo['hostspec'];
85 $port = @$dsninfo['port'];
87 self
::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ?
";port=$port" : ""),
88 $dsninfo['username'], $dsninfo['password'],
89 [PDO
::MYSQL_ATTR_USE_BUFFERED_QUERY
=> TRUE]
92 catch (PDOException
$e) {
93 echo "Can't connect to MySQL server:" . PHP_EOL
. $e->getMessage() . PHP_EOL
;
97 return self
::$singletons['pdo'];
101 * Create a builder for the headless environment.
104 * \Civi\Test::headless()->apply();
105 * \Civi\Test::headless()->sqlFile('ex.sql')->apply();
108 * @return \Civi\Test\CiviEnvBuilder
110 public static function headless() {
111 $civiRoot = dirname(__DIR__
);
112 $builder = new \Civi\Test\
CiviEnvBuilder('CiviEnvBuilder');
114 ->callback(function ($ctx) {
115 if (CIVICRM_UF
!== 'UnitTests') {
116 throw new \
RuntimeException("\\Civi\\Test::headless() requires CIVICRM_UF=UnitTests");
118 $dbName = \Civi\Test
::dsn('database');
119 echo "Installing {$dbName} schema\n";
120 \Civi\Test
::schema()->dropAll();
123 ->sql("DELETE FROM civicrm_extension")
124 ->callback(function ($ctx) {
125 \Civi\Test
::data()->populate();
131 * Create a builder for end-to-end testing on the live environment.
134 * \Civi\Test::e2e()->apply();
135 * \Civi\Test::e2e()->install('foo.bar')->apply();
138 * @return \Civi\Test\CiviEnvBuilder
140 public static function e2e() {
141 $builder = new \Civi\Test\
CiviEnvBuilder('CiviEnvBuilder');
143 ->callback(function ($ctx) {
144 if (CIVICRM_UF
=== 'UnitTests') {
145 throw new \
RuntimeException("\\Civi\\Test::e2e() requires a real CMS. Found CIVICRM_UF=UnitTests.");
152 * @return \Civi\Test\Schema
154 public static function schema() {
155 if (!isset(self
::$singletons['schema'])) {
156 self
::$singletons['schema'] = new \Civi\Test\
Schema();
158 return self
::$singletons['schema'];
162 * @return \CRM_Core_CodeGen_Main
164 public static function codeGen() {
165 if (!isset(self
::$singletons['codeGen'])) {
166 $civiRoot = str_replace(DIRECTORY_SEPARATOR
, '/', dirname(__DIR__
));
167 $codeGen = new \
CRM_Core_CodeGen_Main("$civiRoot/CRM/Core/DAO", "$civiRoot/sql", $civiRoot, "$civiRoot/templates", NULL, "UnitTests", NULL, "$civiRoot/xml/schema/Schema.xml", NULL);
169 self
::$singletons['codeGen'] = $codeGen;
171 return self
::$singletons['codeGen'];
175 * @return \Civi\Test\Data
177 public static function data() {
178 if (!isset(self
::$singletons['data'])) {
179 self
::$singletons['data'] = new \Civi\Test\
Data('CiviTesterData');
181 return self
::$singletons['data'];
185 * Prepare and execute a batch of SQL statements.
187 * @param string $query
190 public static function execute($query) {
191 $pdo = \Civi\Test
::pdo();
193 $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
194 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
196 $queries = preg_split('/;\s*$/m', $string);
197 foreach ($queries as $query) {
198 $query = trim($query);
199 if (!empty($query)) {
200 $result = $pdo->query($query);
201 if ($pdo->errorCode() == 0) {
206 var_dump($pdo->errorInfo());
207 // die( "Cannot execute $query: " . $pdo->errorInfo() );