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