Commit | Line | Data |
---|---|---|
728bbd5b TO |
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 | */ | |
c64f69d9 | 17 | private static $singletons = []; |
728bbd5b | 18 | |
9d114ab6 TO |
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 | ||
728bbd5b TO |
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"; | |
58d1e21e SL |
62 | $dsn = \CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN); |
63 | self::$singletons['dsn'] = \DB::parseDSN($dsn); | |
728bbd5b TO |
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 | * | |
34f3bbd9 | 80 | * @return \PDO |
728bbd5b TO |
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'], | |
c64f69d9 | 90 | [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE] |
728bbd5b TO |
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 | /** | |
33545abf | 102 | * Create a builder for the headless environment. |
728bbd5b | 103 | * |
0b882a86 | 104 | * ``` |
728bbd5b TO |
105 | * \Civi\Test::headless()->apply(); |
106 | * \Civi\Test::headless()->sqlFile('ex.sql')->apply(); | |
0b882a86 CW |
107 | * ``` |
108 | * | |
109 | * @return \Civi\Test\CiviEnvBuilder | |
728bbd5b TO |
110 | */ |
111 | public static function headless() { | |
112 | $civiRoot = dirname(__DIR__); | |
33545abf | 113 | $builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder'); |
728bbd5b TO |
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(); | |
33545abf | 122 | }, 'headless-drop') |
c0d84418 | 123 | ->coreSchema() |
728bbd5b TO |
124 | ->sql("DELETE FROM civicrm_extension") |
125 | ->callback(function ($ctx) { | |
126 | \Civi\Test::data()->populate(); | |
127 | }, 'populate'); | |
128 | return $builder; | |
129 | } | |
130 | ||
33545abf TO |
131 | /** |
132 | * Create a builder for end-to-end testing on the live environment. | |
133 | * | |
0b882a86 | 134 | * ``` |
33545abf TO |
135 | * \Civi\Test::e2e()->apply(); |
136 | * \Civi\Test::e2e()->install('foo.bar')->apply(); | |
0b882a86 CW |
137 | * ``` |
138 | * | |
139 | * @return \Civi\Test\CiviEnvBuilder | |
33545abf TO |
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 | ||
728bbd5b TO |
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 | ||
c0d84418 TO |
162 | /** |
163 | * @return \CRM_Core_CodeGen_Main | |
164 | */ | |
165 | public static function codeGen() { | |
166 | if (!isset(self::$singletons['codeGen'])) { | |
bc76e5f7 | 167 | $civiRoot = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__DIR__)); |
c0d84418 TO |
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 | ||
728bbd5b TO |
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 | } |