From 2eace61247a497924be90c3d4176d528e655611a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 28 May 2021 02:44:48 -0700 Subject: [PATCH] EventChecker - Activate during unit-tests --- CRM/Utils/System/UnitTests.php | 1 + Civi/Test/CiviTestListener.php | 19 +++++++++++++++++++ Civi/Test/CiviTestListenerPHPUnit7.php | 19 +++++++++++++++++++ Civi/Test/Legacy/CiviTestListener.php | 19 +++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/CRM/Utils/System/UnitTests.php b/CRM/Utils/System/UnitTests.php index 6ac827fa91..547b93f98d 100644 --- a/CRM/Utils/System/UnitTests.php +++ b/CRM/Utils/System/UnitTests.php @@ -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(); } /** diff --git a/Civi/Test/CiviTestListener.php b/Civi/Test/CiviTestListener.php index 8f4c21d7a4..6e3b4cdc08 100644 --- a/Civi/Test/CiviTestListener.php +++ b/Civi/Test/CiviTestListener.php @@ -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; + } } /** diff --git a/Civi/Test/CiviTestListenerPHPUnit7.php b/Civi/Test/CiviTestListenerPHPUnit7.php index 42161cb12b..97c122081a 100644 --- a/Civi/Test/CiviTestListenerPHPUnit7.php +++ b/Civi/Test/CiviTestListenerPHPUnit7.php @@ -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; + } } /** diff --git a/Civi/Test/Legacy/CiviTestListener.php b/Civi/Test/Legacy/CiviTestListener.php index 4a71a74504..93fb4ac8fb 100644 --- a/Civi/Test/Legacy/CiviTestListener.php +++ b/Civi/Test/Legacy/CiviTestListener.php @@ -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; + } } /** -- 2.25.1