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