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