6 * Class CiviTestListener
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.
13 * @see EndToEndInterface
14 * @see HeadlessInterface
17 class CiviTestListenerPHPUnit7
implements \PHPUnit\Framework\TestListener
{
19 use \PHPUnit\Framework\TestListenerDefaultImplementation
;
23 * Ex: $cache['Some_Test_Class']['civicrm_foobar'] = 'hook_civicrm_foobar';
24 * Array(string $testClass => Array(string $hookName => string $methodName)).
29 * @var \CRM_Core_Transaction|null
33 public function startTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
{
34 $byInterface = $this->indexTestsByInterface($suite->tests());
35 $this->validateGroups($byInterface);
36 $this->autoboot($byInterface);
39 public function endTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
{
43 public function startTest(\PHPUnit\Framework\Test
$test): void
{
44 if ($this->isCiviTest($test)) {
45 error_reporting(E_ALL
);
46 $GLOBALS['CIVICRM_TEST_CASE'] = $test;
49 if ($test instanceof HeadlessInterface
) {
50 $this->bootHeadless($test);
53 if ($test instanceof TransactionalInterface
) {
54 $this->tx
= new \
CRM_Core_Transaction(TRUE);
55 $this->tx
->rollback();
61 if ($this->isCiviTest($test) ||
$test instanceof \CiviUnitTestCase
) {
62 \Civi\Test
::eventChecker()->start($test);
66 public function endTest(\PHPUnit\Framework\Test
$test, float $time): void
{
69 if ($this->isCiviTest($test) ||
$test instanceof \CiviUnitTestCase
) {
71 \Civi\Test
::eventChecker()->stop($test);
73 catch (\Exception
$e) {
78 if ($test instanceof TransactionalInterface
) {
79 $this->tx
->rollback()->commit();
82 if ($test instanceof HookInterface
) {
83 \CRM_Utils_Hook
::singleton()->reset();
85 \CRM_Utils_Time
::resetTime();
86 if ($this->isCiviTest($test)) {
87 unset($GLOBALS['CIVICRM_TEST_CASE']);
88 error_reporting(E_ALL
& ~E_NOTICE
);
89 $this->errorScope
= NULL;
98 * @param HeadlessInterface|\PHPUnit\Framework\Test $test
100 protected function bootHeadless($test) {
101 if (CIVICRM_UF
!== 'UnitTests') {
102 throw new \
RuntimeException('HeadlessInterface requires CIVICRM_UF=UnitTests');
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();
110 \CRM_Utils_System
::flushCache();
112 \CRM_Core_Session
::singleton()->set('userID', NULL);
114 $config = \CRM_Core_Config
::singleton(TRUE, TRUE);
116 if (property_exists($config->userPermissionClass
, 'permissions')) {
117 $config->userPermissionClass
->permissions
= NULL;
122 * @param \PHPUnit\Framework\Test $test
125 protected function isCiviTest(\PHPUnit\Framework\Test
$test) {
126 return $test instanceof HookInterface ||
$test instanceof HeadlessInterface
;
130 * The first time we come across HeadlessInterface or EndToEndInterface, we'll
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.
137 * @param array $byInterface
138 * List of test classes, keyed by major interface (HeadlessInterface vs EndToEndInterface).
140 protected function autoboot($byInterface) {
141 if (defined('CIVICRM_UF')) {
142 // OK, nothing we can do. System has booted already.
144 elseif (!empty($byInterface['HeadlessInterface'])) {
145 putenv('CIVICRM_UF=UnitTests');
147 eval($this->cv('php:boot --level=full', 'phpcode'));
150 elseif (!empty($byInterface['EndToEndInterface'])) {
151 putenv('CIVICRM_UF=');
153 eval($this->cv('php:boot --level=full', 'phpcode'));
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";
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");
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");
172 * Call the "cv" command.
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.
180 * The rest of the command to send.
181 * @param string $decode
182 * Ex: 'json' or 'phpcode'.
184 * Response output (if the command executed normally).
185 * @throws \RuntimeException
186 * If the command terminates abnormally.
188 protected function cv($cmd, $decode = 'json') {
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");
196 $result = stream_get_contents($pipes[1]);
198 if (proc_close($process) !== 0) {
199 throw new \
RuntimeException("Command failed ($cmd):\n$result");
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");
213 return json_decode($result, 1);
216 throw new \
RuntimeException("Bad decoder format ($decode)");
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;
231 if ($test instanceof EndToEndInterface
) {
232 $byInterface['EndToEndInterface'][get_class($test)] = 1;
239 * Ensure that any tests have sensible groups, e.g.
241 * `HeadlessInterface` ==> `group headless`
242 * `EndToEndInterface` ==> `group e2e`
244 * @param array $byInterface
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";
253 if (strpos($docComment, "@group e2e\n") !== FALSE) {
254 echo "WARNING: Class $className implements HeadlessInterface. It should not declare \"@group e2e\".\n";
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";
263 if (strpos($docComment, "@group headless\n") !== FALSE) {
264 echo "WARNING: Class $className implements EndToEndInterface. It should not declare \"@group headless\".\n";