EventChecker - Activate during unit-tests
authorTim Otten <totten@civicrm.org>
Fri, 28 May 2021 09:44:48 +0000 (02:44 -0700)
committerTim Otten <totten@civicrm.org>
Sat, 25 Sep 2021 10:12:23 +0000 (03:12 -0700)
CRM/Utils/System/UnitTests.php
Civi/Test/CiviTestListener.php
Civi/Test/CiviTestListenerPHPUnit7.php
Civi/Test/Legacy/CiviTestListener.php

index 6ac827fa919a022e6f4bccfdd3f3df363d01218c..547b93f98dbf0446030a139e9416dd3376c9d9ff 100644 (file)
@@ -34,6 +34,7 @@ class CRM_Utils_System_UnitTests extends CRM_Utils_System_Base {
       $listenerMap = \Civi\Core\Event\EventScanner::findListeners($test);
       \Civi::dispatcher()->addListenerMap($test, $listenerMap);
     }
+    \Civi\Test::eventChecker()->addListeners();
   }
 
   /**
index 8f4c21d7a4de45833a2487ced64163f893361aec..6e3b4cdc08c9d5561d64384eef4f9f40de528516 100644 (file)
@@ -65,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;
@@ -81,6 +96,10 @@ else {
         error_reporting(E_ALL & ~E_NOTICE);
         $this->errorScope = NULL;
       }
+
+      if ($exception) {
+        throw $exception;
+      }
     }
 
     /**
index 42161cb12b0d90b607bb99d19b5106a75e58d49e..97c122081a8310dcb994613c03fbde7bf1917e53 100644 (file)
@@ -57,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;
@@ -73,6 +88,10 @@ class CiviTestListenerPHPUnit7 implements \PHPUnit\Framework\TestListener {
       error_reporting(E_ALL & ~E_NOTICE);
       $this->errorScope = NULL;
     }
+
+    if ($exception) {
+      throw $exception;
+    }
   }
 
   /**
index 4a71a74504df902c38bd42c4482556690ad4f23d..93fb4ac8fb830a28d480d66171bffad9e37f8576 100644 (file)
@@ -55,9 +55,24 @@ class CiviTestListener extends \PHPUnit_Framework_BaseTestListener {
     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 \Civi\Test\TransactionalInterface) {
       $this->tx->rollback()->commit();
       $this->tx = NULL;
@@ -71,6 +86,10 @@ class CiviTestListener extends \PHPUnit_Framework_BaseTestListener {
       error_reporting(E_ALL & ~E_NOTICE);
       $this->errorScope = NULL;
     }
+
+    if ($exception) {
+      throw $exception;
+    }
   }
 
   /**