forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.php
257 lines (234 loc) · 6.92 KB
/
Test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace Civi;
use PDO;
use PDOException;
/**
* Class Test
*
* A facade for managing the test environment.
*/
class Test {
/**
* @var 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\Test::$statics['testPreInstall'] = (\Civi\Test::$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\Test::$statics['testPreInstall']--;
if (\Civi\Test::$statics['testPreInstall'] <= 0) {
unset(\Civi\Test::$statics['testPreInstall']);
}
}
}
/**
* Get the data source used for testing.
*
* @param string|null $part
* One of NULL, 'hostspec', 'port', 'username', 'password', 'database'.
* @return string|array|NULL
* If $part is omitted, return full DSN array.
* If $part is a string, return that part of the DSN.
*/
public static function dsn($part = NULL) {
if (!isset(self::$singletons['dsn'])) {
require_once "DB.php";
$dsn = \CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN);
self::$singletons['dsn'] = \DB::parseDSN($dsn);
}
if ($part === NULL) {
return self::$singletons['dsn'];
}
if (isset(self::$singletons['dsn'][$part])) {
return self::$singletons['dsn'][$part];
}
return NULL;
}
/**
* Get a connection to the test database.
*
* @return \PDO
*/
public static function pdo() {
if (!isset(self::$singletons['pdo'])) {
$dsninfo = self::dsn();
$host = $dsninfo['hostspec'];
$port = @$dsninfo['port'];
try {
self::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""),
$dsninfo['username'], $dsninfo['password'],
[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE]
);
}
catch (PDOException $e) {
echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL;
exit(1);
}
}
return self::$singletons['pdo'];
}
/**
* Create a builder for the headless environment.
*
* ```
* \Civi\Test::headless()->apply();
* \Civi\Test::headless()->sqlFile('ex.sql')->apply();
* ```
*
* @return \Civi\Test\CiviEnvBuilder
*/
public static function headless() {
$civiRoot = dirname(__DIR__);
$builder = new \Civi\Test\CiviEnvBuilder();
$builder
->callback(function ($ctx) {
if (CIVICRM_UF !== 'UnitTests') {
throw new \RuntimeException("\\Civi\\Test::headless() requires CIVICRM_UF=UnitTests");
}
$dbName = \Civi\Test::dsn('database');
fprintf(STDERR, "Installing {$dbName} schema\n");
\Civi\Test::schema()->dropAll();
}, 'headless-drop')
->coreSchema()
->sql("DELETE FROM civicrm_extension")
->callback(function ($ctx) {
\Civi\Test::data()->populate();
}, 'populate');
$builder->install(['org.civicrm.search_kit', 'org.civicrm.afform', 'authx']);
return $builder;
}
/**
* Create a builder for end-to-end testing on the live environment.
*
* ```
* \Civi\Test::e2e()->apply();
* \Civi\Test::e2e()->install('foo.bar')->apply();
* ```
*
* @return \Civi\Test\CiviEnvBuilder
*/
public static function e2e() {
$builder = new \Civi\Test\CiviEnvBuilder();
$builder
->callback(function ($ctx) {
if (CIVICRM_UF === 'UnitTests') {
throw new \RuntimeException("\\Civi\\Test::e2e() requires a real CMS. Found CIVICRM_UF=UnitTests.");
}
}, 'e2e-check');
return $builder;
}
/**
* @return \Civi\Test\Schema
*/
public static function schema() {
if (!isset(self::$singletons['schema'])) {
self::$singletons['schema'] = new \Civi\Test\Schema();
}
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->setVerbose(FALSE);
$codeGen->init();
self::$singletons['codeGen'] = $codeGen;
}
return self::$singletons['codeGen'];
}
/**
* @return \Civi\Test\Data
*/
public static function data() {
if (!isset(self::$singletons['data'])) {
self::$singletons['data'] = new \Civi\Test\Data('CiviTesterData');
}
return self::$singletons['data'];
}
/**
* @return \Civi\Test\ExampleDataLoader
*/
public static function examples(): \Civi\Test\ExampleDataLoader {
if (!isset(self::$singletons['examples'])) {
self::$singletons['examples'] = new \Civi\Test\ExampleDataLoader();
}
return self::$singletons['examples'];
}
/**
* Lookup the content of an example data-set.
*
* This helper is for the common case of looking up the data for a specific example.
* If you need more detailed information (eg the list of examples or other metadata),
* then use `\Civi\Test::examples(): ExampleDataLoader`. It provides more methods.
*
* @param string $name
* Symbolic name of the data-set.
* @return array
* The example data.
*/
public static function example(string $name): array {
$result = static::examples()->getFull($name);
if (!isset($result['data'])) {
throw new \CRM_Core_Exception("Failed to load example data-set: $name");
}
return $result['data'];
}
/**
* @return \Civi\Test\EventChecker
*/
public static function eventChecker() {
if (!isset(self::$singletons['eventChecker'])) {
self::$singletons['eventChecker'] = new \Civi\Test\EventChecker();
}
return self::$singletons['eventChecker'];
}
/**
* Prepare and execute a batch of SQL statements.
*
* @param string $query
* @return bool
*/
public static function execute($query) {
$pdo = \Civi\Test::pdo();
$string = preg_replace("/^#[^\n]*$/m", "\n", $query);
$string = preg_replace("/^(--[^-]).*/m", "\n", $string);
$queries = preg_split('/;\s*$/m', $string);
foreach ($queries as $query) {
$query = trim($query);
if (!empty($query)) {
$result = $pdo->query($query);
if ($pdo->errorCode() == 0) {
continue;
}
else {
var_dump($result);
var_dump($pdo->errorInfo());
// die( "Cannot execute $query: " . $pdo->errorInfo() );
}
}
}
return TRUE;
}
}