Merge pull request #19691 from colemanw/afformEditLink
[civicrm-core.git] / Civi / Test.php
1 <?php
2 namespace Civi;
3
4 use PDO;
5 use PDOException;
6
7 /**
8 * Class Test
9 *
10 * A facade for managing the test environment.
11 */
12 class Test {
13
14 /**
15 * @var array
16 */
17 private static $singletons = [];
18
19 /**
20 * @var array
21 */
22 public static $statics = [];
23
24 /**
25 * Run code in a pre-boot fashion.
26 *
27 * @param callable $callback
28 * @return mixed
29 * Pass through the result of the callback.
30 */
31 public static function asPreInstall($callback) {
32 $conn = \Civi\Test::pdo();
33
34 $oldEscaper = \CRM_Core_I18n::$SQL_ESCAPER;
35 \Civi::$statics['testPreInstall'] = (\Civi::$statics['testPreInstall'] ?? 0) + 1;
36 try {
37 \CRM_Core_I18n::$SQL_ESCAPER = function ($text) use ($conn) {
38 return substr($conn->quote($text), 1, -1);
39 };
40 return $callback();
41 } finally {
42 \CRM_Core_I18n::$SQL_ESCAPER = $oldEscaper;
43 \Civi::$statics['testPreInstall']--;
44 if (\Civi::$statics['testPreInstall'] <= 0) {
45 unset(\Civi::$statics['testPreInstall']);
46 }
47 }
48 }
49
50 /**
51 * Get the data source used for testing.
52 *
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.
58 */
59 public static function dsn($part = NULL) {
60 if (!isset(self::$singletons['dsn'])) {
61 require_once "DB.php";
62 $dsn = \CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN);
63 self::$singletons['dsn'] = \DB::parseDSN($dsn);
64 }
65
66 if ($part === NULL) {
67 return self::$singletons['dsn'];
68 }
69
70 if (isset(self::$singletons['dsn'][$part])) {
71 return self::$singletons['dsn'][$part];
72 }
73
74 return NULL;
75 }
76
77 /**
78 * Get a connection to the test database.
79 *
80 * @return \PDO
81 */
82 public static function pdo() {
83 if (!isset(self::$singletons['pdo'])) {
84 $dsninfo = self::dsn();
85 $host = $dsninfo['hostspec'];
86 $port = @$dsninfo['port'];
87 try {
88 self::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""),
89 $dsninfo['username'], $dsninfo['password'],
90 [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE]
91 );
92 }
93 catch (PDOException $e) {
94 echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL;
95 exit(1);
96 }
97 }
98 return self::$singletons['pdo'];
99 }
100
101 /**
102 * Create a builder for the headless environment.
103 *
104 * ```
105 * \Civi\Test::headless()->apply();
106 * \Civi\Test::headless()->sqlFile('ex.sql')->apply();
107 * ```
108 *
109 * @return \Civi\Test\CiviEnvBuilder
110 */
111 public static function headless() {
112 $civiRoot = dirname(__DIR__);
113 $builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder');
114 $builder
115 ->callback(function ($ctx) {
116 if (CIVICRM_UF !== 'UnitTests') {
117 throw new \RuntimeException("\\Civi\\Test::headless() requires CIVICRM_UF=UnitTests");
118 }
119 $dbName = \Civi\Test::dsn('database');
120 echo "Installing {$dbName} schema\n";
121 \Civi\Test::schema()->dropAll();
122 }, 'headless-drop')
123 ->coreSchema()
124 ->sql("DELETE FROM civicrm_extension")
125 ->callback(function ($ctx) {
126 \Civi\Test::data()->populate();
127 }, 'populate');
128 return $builder;
129 }
130
131 /**
132 * Create a builder for end-to-end testing on the live environment.
133 *
134 * ```
135 * \Civi\Test::e2e()->apply();
136 * \Civi\Test::e2e()->install('foo.bar')->apply();
137 * ```
138 *
139 * @return \Civi\Test\CiviEnvBuilder
140 */
141 public static function e2e() {
142 $builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder');
143 $builder
144 ->callback(function ($ctx) {
145 if (CIVICRM_UF === 'UnitTests') {
146 throw new \RuntimeException("\\Civi\\Test::e2e() requires a real CMS. Found CIVICRM_UF=UnitTests.");
147 }
148 }, 'e2e-check');
149 return $builder;
150 }
151
152 /**
153 * @return \Civi\Test\Schema
154 */
155 public static function schema() {
156 if (!isset(self::$singletons['schema'])) {
157 self::$singletons['schema'] = new \Civi\Test\Schema();
158 }
159 return self::$singletons['schema'];
160 }
161
162 /**
163 * @return \CRM_Core_CodeGen_Main
164 */
165 public static function codeGen() {
166 if (!isset(self::$singletons['codeGen'])) {
167 $civiRoot = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__DIR__));
168 $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 $codeGen->init();
170 self::$singletons['codeGen'] = $codeGen;
171 }
172 return self::$singletons['codeGen'];
173 }
174
175 /**
176 * @return \Civi\Test\Data
177 */
178 public static function data() {
179 if (!isset(self::$singletons['data'])) {
180 self::$singletons['data'] = new \Civi\Test\Data('CiviTesterData');
181 }
182 return self::$singletons['data'];
183 }
184
185 /**
186 * Prepare and execute a batch of SQL statements.
187 *
188 * @param string $query
189 * @return bool
190 */
191 public static function execute($query) {
192 $pdo = \Civi\Test::pdo();
193
194 $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
195 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
196
197 $queries = preg_split('/;\s*$/m', $string);
198 foreach ($queries as $query) {
199 $query = trim($query);
200 if (!empty($query)) {
201 $result = $pdo->query($query);
202 if ($pdo->errorCode() == 0) {
203 continue;
204 }
205 else {
206 var_dump($result);
207 var_dump($pdo->errorInfo());
208 // die( "Cannot execute $query: " . $pdo->errorInfo() );
209 }
210 }
211 }
212 return TRUE;
213 }
214
215 }