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