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