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