From 423c922925adfe09a5b7d09490dd5b9e317d8308 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 28 May 2021 02:42:41 -0700 Subject: [PATCH] EventChecker - Add base-class and loader --- Civi/Test.php | 10 ++++ Civi/Test/EventCheck.php | 81 ++++++++++++++++++++++++++++ Civi/Test/EventChecker.php | 106 +++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 Civi/Test/EventCheck.php create mode 100644 Civi/Test/EventChecker.php diff --git a/Civi/Test.php b/Civi/Test.php index 1509481623..a0d6769747 100644 --- a/Civi/Test.php +++ b/Civi/Test.php @@ -212,6 +212,16 @@ class Test { return $result['data']; } + /** + * @return \Civi\Test\EventChecker + */ + public static function eventChecker() { + if (!isset(self::$singletons['eventChecker'])) { + self::$singletons['eventChecker'] = new \Civi\Test\EventChecker(); + } + return self::$singletons['eventChecker']; + } + /** * Prepare and execute a batch of SQL statements. * diff --git a/Civi/Test/EventCheck.php b/Civi/Test/EventCheck.php new file mode 100644 index 0000000000..6254201ed2 --- /dev/null +++ b/Civi/Test/EventCheck.php @@ -0,0 +1,81 @@ +test; + } + + /** + * @param \PHPUnit\Framework\Test|NULL $test + */ + public function setTest($test): void { + $this->test = $test; + } + + /** + * Assert that a variable has a given type. + * + * @param string|string[] $types + * List of types, per `gettype()` or `get_class()` + * Ex: 'int|string|NULL' + * Ex: [`array`, `NULL`, `CRM_Core_DAO`] + * @param mixed $value + * The variable to check + * @param string|NULL $msg + * @see \CRM_Utils_Type::validatePhpType + */ + public function assertType($types, $value, ?string $msg = NULL) { + if (!\CRM_Utils_Type::validatePhpType($value, $types, FALSE)) { + $defactoType = is_object($value) ? get_class($value) : gettype($value); + $types = is_array($types) ? implode('|', $types) : $types; + $this->fail(sprintf("Expected one of (%s) but found %s\n%s", $types, $defactoType, $msg)); + } + } + + public function setUp() { + } + + public function tearDown() { + } + +} diff --git a/Civi/Test/EventChecker.php b/Civi/Test/EventChecker.php new file mode 100644 index 0000000000..1dc1e27ed0 --- /dev/null +++ b/Civi/Test/EventChecker.php @@ -0,0 +1,106 @@ +activeChecks === NULL) { + $this->activeChecks = []; + foreach ($this->findAll() as $template) { + /** @var EventCheck $template */ + if ($template->isSupported($test)) { + $checker = clone $template; + $checker->setTest($test); + $this->activeChecks[] = $checker; + $checker->setUp(); + } + } + } + return $this; + } + + /** + * @return $this + */ + public function addListeners() { + $d = \Civi::dispatcher(); + foreach ($this->activeChecks ?: [] as $checker) { + /** @var EventCheck $checker */ + $d->addListenerMap($checker, EventScanner::findListeners($checker)); + // For the moment, KISS. But we may want a counter at some point - to ensure things actually run. + //foreach (EventScanner::findListeners($checker) as $event => $listeners) { + // foreach ($listeners as $listener) { + // $d->addListener($event, + // function($args...) use ($listener) { + // $count++; + // $m = $listener[1]; + // $checker->$m(...$args); + // }, + // $listener[1] ?? 0 + // ); + // } + //} + } + return $this; + } + + /** + * @return $this + */ + public function stop() { + // NOTE: In test environment, dispatcher will be removed regardless. + foreach ($this->activeChecks ?? [] as $checker) { + /** @var \Civi\Test\EventCheck $checker */ + Invasive::call([$checker, 'tearDown']); + $checker->setTest(NULL); + } + $this->activeChecks = NULL; + return $this; + } + + /** + * @return EventCheck[] + */ + protected function findAll() { + if ($this->allChecks === NULL) { + $all = []; + $testDir = \Civi::paths()->getPath('[civicrm.root]/tests/events'); + $files = \CRM_Utils_File::findFiles($testDir, '*.evch.php', TRUE); + sort($files); + foreach ($files as $file) { + $all[$file] = require $testDir . '/' . $file; + } + $this->allChecks = $all; + } + + return $this->allChecks; + } + +} -- 2.25.1