mixin/polyfill.php - Import. Update comments.
[civicrm-core.git] / Civi / Test / CiviTestListenerPHPUnit7.php
index 1efbacce135d8a06ebdac5c4ef20ed9f61aa62ee..97c122081a8310dcb994613c03fbde7bf1917e53 100644 (file)
@@ -43,18 +43,13 @@ class CiviTestListenerPHPUnit7 implements \PHPUnit\Framework\TestListener {
   public function startTest(\PHPUnit\Framework\Test $test): void {
     if ($this->isCiviTest($test)) {
       error_reporting(E_ALL);
+      $GLOBALS['CIVICRM_TEST_CASE'] = $test;
     }
 
     if ($test instanceof HeadlessInterface) {
       $this->bootHeadless($test);
     }
 
-    if ($test instanceof HookInterface) {
-      // Note: bootHeadless() indirectly resets any hooks, which means that hook_civicrm_config
-      // is unsubscribable. However, after bootHeadless(), we're free to subscribe to hooks again.
-      $this->registerHooks($test);
-    }
-
     if ($test instanceof TransactionalInterface) {
       $this->tx = new \CRM_Core_Transaction(TRUE);
       $this->tx->rollback();
@@ -62,9 +57,24 @@ class CiviTestListenerPHPUnit7 implements \PHPUnit\Framework\TestListener {
     else {
       $this->tx = NULL;
     }
+
+    if ($this->isCiviTest($test) || $test instanceof \CiviUnitTestCase) {
+      \Civi\Test::eventChecker()->start($test);
+    }
   }
 
   public function endTest(\PHPUnit\Framework\Test $test, float $time): void {
+    $exception = NULL;
+
+    if ($this->isCiviTest($test) || $test instanceof \CiviUnitTestCase) {
+      try {
+        \Civi\Test::eventChecker()->stop($test);
+      }
+      catch (\Exception $e) {
+        $exception = $e;
+      }
+    }
+
     if ($test instanceof TransactionalInterface) {
       $this->tx->rollback()->commit();
       $this->tx = NULL;
@@ -74,9 +84,14 @@ class CiviTestListenerPHPUnit7 implements \PHPUnit\Framework\TestListener {
     }
     \CRM_Utils_Time::resetTime();
     if ($this->isCiviTest($test)) {
+      unset($GLOBALS['CIVICRM_TEST_CASE']);
       error_reporting(E_ALL & ~E_NOTICE);
       $this->errorScope = NULL;
     }
+
+    if ($exception) {
+      throw $exception;
+    }
   }
 
   /**
@@ -103,25 +118,6 @@ class CiviTestListenerPHPUnit7 implements \PHPUnit\Framework\TestListener {
     }
   }
 
-  /**
-   * @param \Civi\Test\HookInterface $test
-   * @return array
-   *   Array(string $hookName => string $methodName)).
-   */
-  protected function findTestHooks(HookInterface $test) {
-    $class = get_class($test);
-    if (!isset($this->cache[$class])) {
-      $funcs = [];
-      foreach (get_class_methods($class) as $func) {
-        if (preg_match('/^hook_/', $func)) {
-          $funcs[substr($func, 5)] = $func;
-        }
-      }
-      $this->cache[$class] = $funcs;
-    }
-    return $this->cache[$class];
-  }
-
   /**
    * @param \PHPUnit\Framework\Test $test
    * @return bool
@@ -130,25 +126,6 @@ class CiviTestListenerPHPUnit7 implements \PHPUnit\Framework\TestListener {
     return $test instanceof HookInterface || $test instanceof HeadlessInterface;
   }
 
-  /**
-   * Find any hook functions in $test and register them.
-   *
-   * @param \Civi\Test\HookInterface $test
-   */
-  protected function registerHooks(HookInterface $test) {
-    if (CIVICRM_UF !== 'UnitTests') {
-      // This is not ideal -- it's just a side-effect of how hooks and E2E tests work.
-      // We can temporarily subscribe to hooks in-process, but for other processes, it gets messy.
-      throw new \RuntimeException('CiviHookTestInterface requires CIVICRM_UF=UnitTests');
-    }
-    \CRM_Utils_Hook::singleton()->reset();
-    /** @var \CRM_Utils_Hook_UnitTests $hooks */
-    $hooks = \CRM_Utils_Hook::singleton();
-    foreach ($this->findTestHooks($test) as $hook => $func) {
-      $hooks->setHook($hook, [$test, $func]);
-    }
-  }
-
   /**
    * The first time we come across HeadlessInterface or EndToEndInterface, we'll
    * try to autoboot.