Commit | Line | Data |
---|---|---|
8851bdf5 SL |
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 | ||
8851bdf5 SL |
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); | |
1b64d7cb | 46 | $GLOBALS['CIVICRM_TEST_CASE'] = $test; |
8851bdf5 SL |
47 | } |
48 | ||
49 | if ($test instanceof HeadlessInterface) { | |
50 | $this->bootHeadless($test); | |
51 | } | |
52 | ||
8851bdf5 SL |
53 | if ($test instanceof TransactionalInterface) { |
54 | $this->tx = new \CRM_Core_Transaction(TRUE); | |
55 | $this->tx->rollback(); | |
56 | } | |
57 | else { | |
58 | $this->tx = NULL; | |
59 | } | |
2eace612 TO |
60 | |
61 | if ($this->isCiviTest($test) || $test instanceof \CiviUnitTestCase) { | |
62 | \Civi\Test::eventChecker()->start($test); | |
63 | } | |
8851bdf5 SL |
64 | } |
65 | ||
66 | public function endTest(\PHPUnit\Framework\Test $test, float $time): void { | |
2eace612 TO |
67 | $exception = NULL; |
68 | ||
69 | if ($this->isCiviTest($test) || $test instanceof \CiviUnitTestCase) { | |
70 | try { | |
71 | \Civi\Test::eventChecker()->stop($test); | |
72 | } | |
73 | catch (\Exception $e) { | |
74 | $exception = $e; | |
75 | } | |
76 | } | |
77 | ||
8851bdf5 SL |
78 | if ($test instanceof TransactionalInterface) { |
79 | $this->tx->rollback()->commit(); | |
80 | $this->tx = NULL; | |
81 | } | |
82 | if ($test instanceof HookInterface) { | |
83 | \CRM_Utils_Hook::singleton()->reset(); | |
84 | } | |
fcd647c0 | 85 | \CRM_Utils_Time::resetTime(); |
8851bdf5 | 86 | if ($this->isCiviTest($test)) { |
1b64d7cb | 87 | unset($GLOBALS['CIVICRM_TEST_CASE']); |
8851bdf5 SL |
88 | error_reporting(E_ALL & ~E_NOTICE); |
89 | $this->errorScope = NULL; | |
90 | } | |
2eace612 TO |
91 | |
92 | if ($exception) { | |
93 | throw $exception; | |
94 | } | |
8851bdf5 SL |
95 | } |
96 | ||
97 | /** | |
98 | * @param HeadlessInterface|\PHPUnit\Framework\Test $test | |
99 | */ | |
100 | protected function bootHeadless($test) { | |
101 | if (CIVICRM_UF !== 'UnitTests') { | |
102 | throw new \RuntimeException('HeadlessInterface requires CIVICRM_UF=UnitTests'); | |
103 | } | |
104 | ||
105 | // Hrm, this seems wrong. Shouldn't we be resetting the entire session? | |
106 | $session = \CRM_Core_Session::singleton(); | |
107 | $session->set('userID', NULL); | |
108 | $test->setUpHeadless(); | |
109 | ||
110 | \CRM_Utils_System::flushCache(); | |
111 | \Civi::reset(); | |
112 | \CRM_Core_Session::singleton()->set('userID', NULL); | |
113 | // ugh, performance | |
114 | $config = \CRM_Core_Config::singleton(TRUE, TRUE); | |
115 | ||
116 | if (property_exists($config->userPermissionClass, 'permissions')) { | |
117 | $config->userPermissionClass->permissions = NULL; | |
118 | } | |
119 | } | |
120 | ||
8851bdf5 SL |
121 | /** |
122 | * @param \PHPUnit\Framework\Test $test | |
123 | * @return bool | |
124 | */ | |
125 | protected function isCiviTest(\PHPUnit\Framework\Test $test) { | |
126 | return $test instanceof HookInterface || $test instanceof HeadlessInterface; | |
127 | } | |
128 | ||
8851bdf5 SL |
129 | /** |
130 | * The first time we come across HeadlessInterface or EndToEndInterface, we'll | |
131 | * try to autoboot. | |
132 | * | |
133 | * Once the system is booted, there's nothing we can do -- we're stuck with that | |
134 | * environment. (Thank you, prolific define()s!) If there's a conflict between a | |
135 | * test-class and the active boot-level, then we'll have to bail. | |
136 | * | |
137 | * @param array $byInterface | |
138 | * List of test classes, keyed by major interface (HeadlessInterface vs EndToEndInterface). | |
139 | */ | |
140 | protected function autoboot($byInterface) { | |
141 | if (defined('CIVICRM_UF')) { | |
142 | // OK, nothing we can do. System has booted already. | |
143 | } | |
144 | elseif (!empty($byInterface['HeadlessInterface'])) { | |
145 | putenv('CIVICRM_UF=UnitTests'); | |
146 | // phpcs:disable | |
147 | eval($this->cv('php:boot --level=full', 'phpcode')); | |
148 | // phpcs:enable | |
149 | } | |
150 | elseif (!empty($byInterface['EndToEndInterface'])) { | |
151 | putenv('CIVICRM_UF='); | |
152 | // phpcs:disable | |
153 | eval($this->cv('php:boot --level=full', 'phpcode')); | |
154 | // phpcs:enable | |
155 | } | |
156 | ||
157 | $blurb = "Tip: Run the headless tests and end-to-end tests separately, e.g.\n" | |
158 | . " $ phpunit5 --group headless\n" | |
159 | . " $ phpunit5 --group e2e \n"; | |
160 | ||
161 | if (!empty($byInterface['HeadlessInterface']) && CIVICRM_UF !== 'UnitTests') { | |
162 | $testNames = implode(', ', array_keys($byInterface['HeadlessInterface'])); | |
163 | throw new \RuntimeException("Suite includes headless tests ($testNames) which require CIVICRM_UF=UnitTests.\n\n$blurb"); | |
164 | } | |
165 | if (!empty($byInterface['EndToEndInterface']) && CIVICRM_UF === 'UnitTests') { | |
166 | $testNames = implode(', ', array_keys($byInterface['EndToEndInterface'])); | |
167 | throw new \RuntimeException("Suite includes end-to-end tests ($testNames) which do not support CIVICRM_UF=UnitTests.\n\n$blurb"); | |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
172 | * Call the "cv" command. | |
173 | * | |
174 | * This duplicates the standalone `cv()` wrapper that is recommended in bootstrap.php. | |
175 | * This duplication is necessary because `cv()` is optional, and downstream implementers | |
176 | * may alter, rename, or omit the wrapper, and (by virtue of its role in bootstrap) there | |
177 | * it is impossible to define it centrally. | |
178 | * | |
179 | * @param string $cmd | |
180 | * The rest of the command to send. | |
181 | * @param string $decode | |
182 | * Ex: 'json' or 'phpcode'. | |
183 | * @return string | |
184 | * Response output (if the command executed normally). | |
185 | * @throws \RuntimeException | |
186 | * If the command terminates abnormally. | |
187 | */ | |
188 | protected function cv($cmd, $decode = 'json') { | |
189 | $cmd = 'cv ' . $cmd; | |
190 | $descriptorSpec = [0 => ["pipe", "r"], 1 => ["pipe", "w"], 2 => STDERR]; | |
191 | $oldOutput = getenv('CV_OUTPUT'); | |
192 | putenv("CV_OUTPUT=json"); | |
193 | $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); | |
194 | putenv("CV_OUTPUT=$oldOutput"); | |
195 | fclose($pipes[0]); | |
196 | $result = stream_get_contents($pipes[1]); | |
197 | fclose($pipes[1]); | |
198 | if (proc_close($process) !== 0) { | |
199 | throw new \RuntimeException("Command failed ($cmd):\n$result"); | |
200 | } | |
201 | switch ($decode) { | |
202 | case 'raw': | |
203 | return $result; | |
204 | ||
205 | case 'phpcode': | |
206 | // If the last output is /*PHPCODE*/, then we managed to complete execution. | |
207 | if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { | |
208 | throw new \RuntimeException("Command failed ($cmd):\n$result"); | |
209 | } | |
210 | return $result; | |
211 | ||
212 | case 'json': | |
213 | return json_decode($result, 1); | |
214 | ||
215 | default: | |
216 | throw new \RuntimeException("Bad decoder format ($decode)"); | |
217 | } | |
218 | } | |
219 | ||
220 | /** | |
221 | * @param $tests | |
222 | * @return array | |
223 | */ | |
224 | protected function indexTestsByInterface($tests) { | |
225 | $byInterface = ['HeadlessInterface' => [], 'EndToEndInterface' => []]; | |
226 | foreach ($tests as $test) { | |
227 | /** @var \PHPUnit\Framework\Test $test */ | |
228 | if ($test instanceof HeadlessInterface) { | |
229 | $byInterface['HeadlessInterface'][get_class($test)] = 1; | |
230 | } | |
231 | if ($test instanceof EndToEndInterface) { | |
232 | $byInterface['EndToEndInterface'][get_class($test)] = 1; | |
233 | } | |
234 | } | |
235 | return $byInterface; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Ensure that any tests have sensible groups, e.g. | |
240 | * | |
241 | * `HeadlessInterface` ==> `group headless` | |
242 | * `EndToEndInterface` ==> `group e2e` | |
243 | * | |
244 | * @param array $byInterface | |
245 | */ | |
246 | protected function validateGroups($byInterface) { | |
247 | foreach ($byInterface['HeadlessInterface'] as $className => $nonce) { | |
248 | $clazz = new \ReflectionClass($className); | |
249 | $docComment = str_replace("\r\n", "\n", $clazz->getDocComment()); | |
250 | if (strpos($docComment, "@group headless\n") === FALSE) { | |
251 | echo "WARNING: Class $className implements HeadlessInterface. It should declare \"@group headless\".\n"; | |
252 | } | |
253 | if (strpos($docComment, "@group e2e\n") !== FALSE) { | |
254 | echo "WARNING: Class $className implements HeadlessInterface. It should not declare \"@group e2e\".\n"; | |
255 | } | |
256 | } | |
257 | foreach ($byInterface['EndToEndInterface'] as $className => $nonce) { | |
258 | $clazz = new \ReflectionClass($className); | |
259 | $docComment = str_replace("\r\n", "\n", $clazz->getDocComment()); | |
260 | if (strpos($docComment, "@group e2e\n") === FALSE) { | |
261 | echo "WARNING: Class $className implements EndToEndInterface. It should declare \"@group e2e\".\n"; | |
262 | } | |
263 | if (strpos($docComment, "@group headless\n") !== FALSE) { | |
264 | echo "WARNING: Class $className implements EndToEndInterface. It should not declare \"@group headless\".\n"; | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | } |