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)
10 elseif (version_compare(\PHPUnit\Runner\Version
::id(), '7.0.0', '>=')) {
11 class_alias('Civi\Test\CiviTestListenerPHPUnit7', 'Civi\Test\CiviTestListener');
16 * Class CiviTestListener
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.
23 * @see EndToEndInterface
24 * @see HeadlessInterface
27 class CiviTestListener
extends \PHPUnit\Framework\BaseTestListener
{
29 * @var \CRM_Core_TemporaryErrorScope
35 * Ex: $cache['Some_Test_Class']['civicrm_foobar'] = 'hook_civicrm_foobar';
36 * Array(string $testClass => Array(string $hookName => string $methodName)).
41 * @var \CRM_Core_Transaction|null
45 public function startTestSuite(\PHPUnit\Framework\TestSuite
$suite) {
46 $byInterface = $this->indexTestsByInterface($suite->tests());
47 $this->validateGroups($byInterface);
48 $this->autoboot($byInterface);
51 public function endTestSuite(\PHPUnit\Framework\TestSuite
$suite) {
55 public function startTest(\PHPUnit\Framework\Test
$test) {
56 if ($this->isCiviTest($test)) {
57 error_reporting(E_ALL
);
58 $this->errorScope
= \CRM_Core_TemporaryErrorScope
::useException();
61 if ($test instanceof HeadlessInterface
) {
62 $this->bootHeadless($test);
65 if ($test instanceof HookInterface
) {
66 // Note: bootHeadless() indirectly resets any hooks, which means that hook_civicrm_config
67 // is unsubscribable. However, after bootHeadless(), we're free to subscribe to hooks again.
68 $this->registerHooks($test);
71 if ($test instanceof TransactionalInterface
) {
72 $this->tx
= new \
CRM_Core_Transaction(TRUE);
73 $this->tx
->rollback();
80 public function endTest(\PHPUnit\Framework\Test
$test, $time) {
81 if ($test instanceof TransactionalInterface
) {
82 $this->tx
->rollback()->commit();
85 if ($test instanceof HookInterface
) {
86 \CRM_Utils_Hook
::singleton()->reset();
88 if ($this->isCiviTest($test)) {
89 error_reporting(E_ALL
& ~E_NOTICE
);
90 $this->errorScope
= NULL;
95 * @param HeadlessInterface|\PHPUnit\Framework\Test $test
97 protected function bootHeadless($test) {
98 if (CIVICRM_UF
!== 'UnitTests') {
99 throw new \
RuntimeException('HeadlessInterface requires CIVICRM_UF=UnitTests');
102 // Hrm, this seems wrong. Shouldn't we be resetting the entire session?
103 $session = \CRM_Core_Session
::singleton();
104 $session->set('userID', NULL);
106 $test->setUpHeadless();
108 \CRM_Utils_System
::flushCache();
110 \CRM_Core_Session
::singleton()->set('userID', NULL);
112 $config = \CRM_Core_Config
::singleton(TRUE, TRUE);
114 if (property_exists($config->userPermissionClass
, 'permissions')) {
115 $config->userPermissionClass
->permissions
= NULL;
120 * @param \Civi\Test\HookInterface $test
122 * Array(string $hookName => string $methodName)).
124 protected function findTestHooks(HookInterface
$test) {
125 $class = get_class($test);
126 if (!isset($this->cache
[$class])) {
128 foreach (get_class_methods($class) as $func) {
129 if (preg_match('/^hook_/', $func)) {
130 $funcs[substr($func, 5)] = $func;
133 $this->cache
[$class] = $funcs;
135 return $this->cache
[$class];
139 * @param \PHPUnit\Framework\Test $test
142 protected function isCiviTest(\PHPUnit\Framework\Test
$test) {
143 return $test instanceof HookInterface ||
$test instanceof HeadlessInterface
;
147 * Find any hook functions in $test and register them.
149 * @param \Civi\Test\HookInterface $test
151 protected function registerHooks(HookInterface
$test) {
152 if (CIVICRM_UF
!== 'UnitTests') {
153 // This is not ideal -- it's just a side-effect of how hooks and E2E tests work.
154 // We can temporarily subscribe to hooks in-process, but for other processes, it gets messy.
155 throw new \
RuntimeException('CiviHookTestInterface requires CIVICRM_UF=UnitTests');
157 \CRM_Utils_Hook
::singleton()->reset();
158 /** @var \CRM_Utils_Hook_UnitTests $hooks */
159 $hooks = \CRM_Utils_Hook
::singleton();
160 foreach ($this->findTestHooks($test) as $hook => $func) {
161 $hooks->setHook($hook, [$test, $func]);
166 * The first time we come across HeadlessInterface or EndToEndInterface, we'll
169 * Once the system is booted, there's nothing we can do -- we're stuck with that
170 * environment. (Thank you, prolific define()s!) If there's a conflict between a
171 * test-class and the active boot-level, then we'll have to bail.
173 * @param array $byInterface
174 * List of test classes, keyed by major interface (HeadlessInterface vs EndToEndInterface).
176 protected function autoboot($byInterface) {
177 if (defined('CIVICRM_UF')) {
178 // OK, nothing we can do. System has booted already.
180 elseif (!empty($byInterface['HeadlessInterface'])) {
181 putenv('CIVICRM_UF=UnitTests');
183 eval($this->cv('php:boot --level=full', 'phpcode'));
186 elseif (!empty($byInterface['EndToEndInterface'])) {
187 putenv('CIVICRM_UF=');
189 eval($this->cv('php:boot --level=full', 'phpcode'));
193 $blurb = "Tip: Run the headless tests and end-to-end tests separately, e.g.\n"
194 . " $ phpunit5 --group headless\n"
195 . " $ phpunit5 --group e2e \n";
197 if (!empty($byInterface['HeadlessInterface']) && CIVICRM_UF
!== 'UnitTests') {
198 $testNames = implode(', ', array_keys($byInterface['HeadlessInterface']));
199 throw new \
RuntimeException("Suite includes headless tests ($testNames) which require CIVICRM_UF=UnitTests.\n\n$blurb");
201 if (!empty($byInterface['EndToEndInterface']) && CIVICRM_UF
=== 'UnitTests') {
202 $testNames = implode(', ', array_keys($byInterface['EndToEndInterface']));
203 throw new \
RuntimeException("Suite includes end-to-end tests ($testNames) which do not support CIVICRM_UF=UnitTests.\n\n$blurb");
208 * Call the "cv" command.
210 * This duplicates the standalone `cv()` wrapper that is recommended in bootstrap.php.
211 * This duplication is necessary because `cv()` is optional, and downstream implementers
212 * may alter, rename, or omit the wrapper, and (by virtue of its role in bootstrap) there
213 * it is impossible to define it centrally.
216 * The rest of the command to send.
217 * @param string $decode
218 * Ex: 'json' or 'phpcode'.
220 * Response output (if the command executed normally).
221 * @throws \RuntimeException
222 * If the command terminates abnormally.
224 protected function cv($cmd, $decode = 'json') {
226 $descriptorSpec = [0 => ["pipe", "r"], 1 => ["pipe", "w"], 2 => STDERR
];
227 $oldOutput = getenv('CV_OUTPUT');
228 putenv("CV_OUTPUT=json");
229 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__
);
230 putenv("CV_OUTPUT=$oldOutput");
232 $result = stream_get_contents($pipes[1]);
234 if (proc_close($process) !== 0) {
235 throw new \
RuntimeException("Command failed ($cmd):\n$result");
242 // If the last output is /*PHPCODE*/, then we managed to complete execution.
243 if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" ||
substr(trim($result), -10) !== "/*ENDPHP*/") {
244 throw new \
RuntimeException("Command failed ($cmd):\n$result");
249 return json_decode($result, 1);
252 throw new \
RuntimeException("Bad decoder format ($decode)");
260 protected function indexTestsByInterface($tests) {
261 $byInterface = ['HeadlessInterface' => [], 'EndToEndInterface' => []];
262 foreach ($tests as $test) {
263 /** @var \PHPUnit\Framework\Test $test */
264 if ($test instanceof HeadlessInterface
) {
265 $byInterface['HeadlessInterface'][get_class($test)] = 1;
267 if ($test instanceof EndToEndInterface
) {
268 $byInterface['EndToEndInterface'][get_class($test)] = 1;
275 * Ensure that any tests have sensible groups, e.g.
277 * `HeadlessInterface` ==> `group headless`
278 * `EndToEndInterface` ==> `group e2e`
280 * @param array $byInterface
282 protected function validateGroups($byInterface) {
283 foreach ($byInterface['HeadlessInterface'] as $className => $nonce) {
284 $clazz = new \
ReflectionClass($className);
285 $docComment = str_replace("\r\n", "\n", $clazz->getDocComment());
286 if (strpos($docComment, "@group headless\n") === FALSE) {
287 echo "WARNING: Class $className implements HeadlessInterface. It should declare \"@group headless\".\n";
289 if (strpos($docComment, "@group e2e\n") !== FALSE) {
290 echo "WARNING: Class $className implements HeadlessInterface. It should not declare \"@group e2e\".\n";
293 foreach ($byInterface['EndToEndInterface'] as $className => $nonce) {
294 $clazz = new \
ReflectionClass($className);
295 $docComment = str_replace("\r\n", "\n", $clazz->getDocComment());
296 if (strpos($docComment, "@group e2e\n") === FALSE) {
297 echo "WARNING: Class $className implements EndToEndInterface. It should declare \"@group e2e\".\n";
299 if (strpos($docComment, "@group headless\n") !== FALSE) {
300 echo "WARNING: Class $className implements EndToEndInterface. It should not declare \"@group headless\".\n";