Merge pull request #24109 from yashodha/reports_improvements
[civicrm-core.git] / Civi / Test / CiviTestListener.php
index af5c2cc2680a1bf8ff2bfc410cfb1d322cc9b789..6e3b4cdc08c9d5561d64384eef4f9f40de528516 100644 (file)
@@ -51,18 +51,13 @@ else {
     public function startTest(\PHPUnit\Framework\Test $test) {
       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();
@@ -70,9 +65,24 @@ else {
       else {
         $this->tx = NULL;
       }
+
+      if ($this->isCiviTest($test) || $test instanceof \CiviUnitTestCase) {
+        \Civi\Test::eventChecker()->start($test);
+      }
     }
 
     public function endTest(\PHPUnit\Framework\Test $test, $time) {
+      $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;
@@ -82,9 +92,14 @@ else {
       }
       \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;
+      }
     }
 
     /**
@@ -112,25 +127,6 @@ else {
       }
     }
 
-    /**
-     * @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
@@ -139,25 +135,6 @@ else {
       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.