Commit | Line | Data |
---|---|---|
09e1f1e3 TO |
1 | <?php |
2 | ||
3 | namespace Civi\Test; | |
4 | ||
5 | /** | |
6 | * Class CiviTestListener | |
7 | * @package Civi\Test | |
8 | * | |
9 | * CiviTestListener participates in test-execution, looking for test-classes | |
10 | * which have certain tags. If the tags are found, the listener will perform | |
11 | * additional setup/teardown logic. | |
12 | * | |
13 | * @see EndToEndInterface | |
14 | * @see HeadlessInterface | |
15 | * @see HookInterface | |
16 | */ | |
17 | class CiviTestListener extends \PHPUnit_Framework_BaseTestListener { | |
18 | /** | |
19 | * @var \CRM_Core_TemporaryErrorScope | |
20 | */ | |
21 | private $errorScope; | |
22 | ||
23 | /** | |
24 | * @var array | |
25 | * Ex: $cache['Some_Test_Class']['civicrm_foobar'] = 'hook_civicrm_foobar'; | |
26 | * Array(string $testClass => Array(string $hookName => string $methodName)). | |
27 | */ | |
c64f69d9 | 28 | private $cache = []; |
09e1f1e3 TO |
29 | |
30 | /** | |
31 | * @var \CRM_Core_Transaction|NULL | |
32 | */ | |
33 | private $tx; | |
34 | ||
35 | public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) { | |
36 | $byInterface = $this->indexTestsByInterface($suite->tests()); | |
37 | $this->validateGroups($byInterface); | |
38 | $this->autoboot($byInterface); | |
39 | } | |
40 | ||
41 | public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) { | |
c64f69d9 | 42 | $this->cache = []; |
09e1f1e3 TO |
43 | } |
44 | ||
45 | public function startTest(\PHPUnit_Framework_Test $test) { | |
46 | if ($this->isCiviTest($test)) { | |
47 | error_reporting(E_ALL); | |
48 | $this->errorScope = \CRM_Core_TemporaryErrorScope::useException(); | |
49 | } | |
50 | ||
51 | if ($test instanceof HeadlessInterface) { | |
52 | $this->bootHeadless($test); | |
53 | } | |
54 | ||
55 | if ($test instanceof HookInterface) { | |
56 | // Note: bootHeadless() indirectly resets any hooks, which means that hook_civicrm_config | |
57 | // is unsubscribable. However, after bootHeadless(), we're free to subscribe to hooks again. | |
58 | $this->registerHooks($test); | |
59 | } | |
60 | ||
61 | if ($test instanceof TransactionalInterface) { | |
62 | $this->tx = new \CRM_Core_Transaction(TRUE); | |
63 | $this->tx->rollback(); | |
64 | } | |
65 | else { | |
66 | $this->tx = NULL; | |
67 | } | |
68 | } | |
69 | ||
70 | public function endTest(\PHPUnit_Framework_Test $test, $time) { | |
71 | if ($test instanceof TransactionalInterface) { | |
72 | $this->tx->rollback()->commit(); | |
73 | $this->tx = NULL; | |
74 | } | |
75 | if ($test instanceof HookInterface) { | |
76 | \CRM_Utils_Hook::singleton()->reset(); | |
77 | } | |
78 | if ($this->isCiviTest($test)) { | |
79 | error_reporting(E_ALL & ~E_NOTICE); | |
80 | $this->errorScope = NULL; | |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
85 | * @param HeadlessInterface|\PHPUnit_Framework_Test $test | |
86 | */ | |
87 | protected function bootHeadless($test) { | |
88 | if (CIVICRM_UF !== 'UnitTests') { | |
415cb25d | 89 | throw new \RuntimeException('HeadlessInterface requires CIVICRM_UF=UnitTests'); |
09e1f1e3 TO |
90 | } |
91 | ||
92 | // Hrm, this seems wrong. Shouldn't we be resetting the entire session? | |
93 | $session = \CRM_Core_Session::singleton(); | |
94 | $session->set('userID', NULL); | |
95 | ||
96 | $test->setUpHeadless(); | |
97 | ||
09e1f1e3 TO |
98 | \CRM_Utils_System::flushCache(); |
99 | \Civi::reset(); | |
100 | \CRM_Core_Session::singleton()->set('userID', NULL); | |
34f3bbd9 SL |
101 | // ugh, performance |
102 | $config = \CRM_Core_Config::singleton(TRUE, TRUE); | |
09e1f1e3 TO |
103 | |
104 | if (property_exists($config->userPermissionClass, 'permissions')) { | |
105 | $config->userPermissionClass->permissions = NULL; | |
106 | } | |
107 | } | |
108 | ||
109 | /** | |
110 | * @param \Civi\Test\HookInterface $test | |
111 | * @return array | |
112 | * Array(string $hookName => string $methodName)). | |
113 | */ | |
114 | protected function findTestHooks(HookInterface $test) { | |
115 | $class = get_class($test); | |
116 | if (!isset($this->cache[$class])) { | |
c64f69d9 | 117 | $funcs = []; |
09e1f1e3 TO |
118 | foreach (get_class_methods($class) as $func) { |
119 | if (preg_match('/^hook_/', $func)) { | |
120 | $funcs[substr($func, 5)] = $func; | |
121 | } | |
122 | } | |
123 | $this->cache[$class] = $funcs; | |
124 | } | |
125 | return $this->cache[$class]; | |
126 | } | |
127 | ||
128 | /** | |
129 | * @param \PHPUnit_Framework_Test $test | |
130 | * @return bool | |
131 | */ | |
132 | protected function isCiviTest(\PHPUnit_Framework_Test $test) { | |
133 | return $test instanceof HookInterface || $test instanceof HeadlessInterface; | |
134 | } | |
135 | ||
136 | /** | |
137 | * Find any hook functions in $test and register them. | |
138 | * | |
139 | * @param \Civi\Test\HookInterface $test | |
140 | */ | |
141 | protected function registerHooks(HookInterface $test) { | |
142 | if (CIVICRM_UF !== 'UnitTests') { | |
143 | // This is not ideal -- it's just a side-effect of how hooks and E2E tests work. | |
144 | // We can temporarily subscribe to hooks in-process, but for other processes, it gets messy. | |
145 | throw new \RuntimeException('CiviHookTestInterface requires CIVICRM_UF=UnitTests'); | |
146 | } | |
147 | \CRM_Utils_Hook::singleton()->reset(); | |
148 | /** @var \CRM_Utils_Hook_UnitTests $hooks */ | |
149 | $hooks = \CRM_Utils_Hook::singleton(); | |
150 | foreach ($this->findTestHooks($test) as $hook => $func) { | |
c64f69d9 | 151 | $hooks->setHook($hook, [$test, $func]); |
09e1f1e3 TO |
152 | } |
153 | } | |
154 | ||
155 | /** | |
156 | * The first time we come across HeadlessInterface or EndToEndInterface, we'll | |
157 | * try to autoboot. | |
158 | * | |
159 | * Once the system is booted, there's nothing we can do -- we're stuck with that | |
160 | * environment. (Thank you, prolific define()s!) If there's a conflict between a | |
161 | * test-class and the active boot-level, then we'll have to bail. | |
162 | * | |
163 | * @param array $byInterface | |
164 | * List of test classes, keyed by major interface (HeadlessInterface vs EndToEndInterface). | |
165 | */ | |
166 | protected function autoboot($byInterface) { | |
167 | if (defined('CIVICRM_UF')) { | |
168 | // OK, nothing we can do. System has booted already. | |
169 | } | |
170 | elseif (!empty($byInterface['HeadlessInterface'])) { | |
171 | putenv('CIVICRM_UF=UnitTests'); | |
7aca9a14 | 172 | // phpcs:disable |
415cb25d | 173 | eval($this->cv('php:boot --level=full', 'phpcode')); |
7aca9a14 | 174 | // phpcs:enable |
09e1f1e3 TO |
175 | } |
176 | elseif (!empty($byInterface['EndToEndInterface'])) { | |
177 | putenv('CIVICRM_UF='); | |
7aca9a14 | 178 | // phpcs:disable |
415cb25d | 179 | eval($this->cv('php:boot --level=full', 'phpcode')); |
7aca9a14 | 180 | // phpcs:enable |
09e1f1e3 TO |
181 | } |
182 | ||
183 | $blurb = "Tip: Run the headless tests and end-to-end tests separately, e.g.\n" | |
08f3d81d SL |
184 | . " $ phpunit5 --group headless\n" |
185 | . " $ phpunit5 --group e2e \n"; | |
09e1f1e3 TO |
186 | |
187 | if (!empty($byInterface['HeadlessInterface']) && CIVICRM_UF !== 'UnitTests') { | |
188 | $testNames = implode(', ', array_keys($byInterface['HeadlessInterface'])); | |
189 | throw new \RuntimeException("Suite includes headless tests ($testNames) which require CIVICRM_UF=UnitTests.\n\n$blurb"); | |
190 | } | |
191 | if (!empty($byInterface['EndToEndInterface']) && CIVICRM_UF === 'UnitTests') { | |
192 | $testNames = implode(', ', array_keys($byInterface['EndToEndInterface'])); | |
193 | throw new \RuntimeException("Suite includes end-to-end tests ($testNames) which do not support CIVICRM_UF=UnitTests.\n\n$blurb"); | |
194 | } | |
195 | } | |
196 | ||
197 | /** | |
198 | * Call the "cv" command. | |
199 | * | |
200 | * This duplicates the standalone `cv()` wrapper that is recommended in bootstrap.php. | |
201 | * This duplication is necessary because `cv()` is optional, and downstream implementers | |
202 | * may alter, rename, or omit the wrapper, and (by virtue of its role in bootstrap) there | |
203 | * it is impossible to define it centrally. | |
204 | * | |
205 | * @param string $cmd | |
206 | * The rest of the command to send. | |
207 | * @param string $decode | |
208 | * Ex: 'json' or 'phpcode'. | |
209 | * @return string | |
210 | * Response output (if the command executed normally). | |
211 | * @throws \RuntimeException | |
212 | * If the command terminates abnormally. | |
213 | */ | |
214 | protected function cv($cmd, $decode = 'json') { | |
215 | $cmd = 'cv ' . $cmd; | |
c64f69d9 | 216 | $descriptorSpec = [0 => ["pipe", "r"], 1 => ["pipe", "w"], 2 => STDERR]; |
09e1f1e3 TO |
217 | $oldOutput = getenv('CV_OUTPUT'); |
218 | putenv("CV_OUTPUT=json"); | |
219 | $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); | |
220 | putenv("CV_OUTPUT=$oldOutput"); | |
221 | fclose($pipes[0]); | |
222 | $result = stream_get_contents($pipes[1]); | |
223 | fclose($pipes[1]); | |
224 | if (proc_close($process) !== 0) { | |
225 | throw new \RuntimeException("Command failed ($cmd):\n$result"); | |
226 | } | |
227 | switch ($decode) { | |
228 | case 'raw': | |
229 | return $result; | |
230 | ||
231 | case 'phpcode': | |
232 | // If the last output is /*PHPCODE*/, then we managed to complete execution. | |
233 | if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { | |
234 | throw new \RuntimeException("Command failed ($cmd):\n$result"); | |
235 | } | |
236 | return $result; | |
237 | ||
238 | case 'json': | |
239 | return json_decode($result, 1); | |
240 | ||
241 | default: | |
242 | throw new \RuntimeException("Bad decoder format ($decode)"); | |
243 | } | |
244 | } | |
245 | ||
246 | /** | |
247 | * @param $tests | |
248 | * @return array | |
249 | */ | |
250 | protected function indexTestsByInterface($tests) { | |
c64f69d9 | 251 | $byInterface = ['HeadlessInterface' => [], 'EndToEndInterface' => []]; |
09e1f1e3 TO |
252 | foreach ($tests as $test) { |
253 | /** @var \PHPUnit_Framework_Test $test */ | |
254 | if ($test instanceof HeadlessInterface) { | |
255 | $byInterface['HeadlessInterface'][get_class($test)] = 1; | |
256 | } | |
257 | if ($test instanceof EndToEndInterface) { | |
258 | $byInterface['EndToEndInterface'][get_class($test)] = 1; | |
259 | } | |
260 | } | |
261 | return $byInterface; | |
262 | } | |
263 | ||
264 | /** | |
265 | * Ensure that any tests have sensible groups, e.g. | |
266 | * | |
267 | * `HeadlessInterface` ==> `group headless` | |
268 | * `EndToEndInterface` ==> `group e2e` | |
269 | * | |
270 | * @param array $byInterface | |
271 | */ | |
272 | protected function validateGroups($byInterface) { | |
273 | foreach ($byInterface['HeadlessInterface'] as $className => $nonce) { | |
274 | $clazz = new \ReflectionClass($className); | |
275 | $docComment = str_replace("\r\n", "\n", $clazz->getDocComment()); | |
276 | if (strpos($docComment, "@group headless\n") === FALSE) { | |
277 | echo "WARNING: Class $className implements HeadlessInterface. It should declare \"@group headless\".\n"; | |
278 | } | |
279 | if (strpos($docComment, "@group e2e\n") !== FALSE) { | |
280 | echo "WARNING: Class $className implements HeadlessInterface. It should not declare \"@group e2e\".\n"; | |
281 | } | |
282 | } | |
283 | foreach ($byInterface['EndToEndInterface'] as $className => $nonce) { | |
284 | $clazz = new \ReflectionClass($className); | |
285 | $docComment = str_replace("\r\n", "\n", $clazz->getDocComment()); | |
286 | if (strpos($docComment, "@group e2e\n") === FALSE) { | |
287 | echo "WARNING: Class $className implements EndToEndInterface. It should declare \"@group e2e\".\n"; | |
288 | } | |
289 | if (strpos($docComment, "@group headless\n") !== FALSE) { | |
290 | echo "WARNING: Class $className implements EndToEndInterface. It should not declare \"@group headless\".\n"; | |
291 | } | |
292 | } | |
293 | } | |
294 | ||
295 | } |