From 96e27a7df3ba8111b217d25609109a4744a8bba5 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 15 Aug 2022 16:13:52 -0700 Subject: [PATCH] mixin/scan-classes@1 - Define test case for auto-registering listeners (in classes) --- .../example/CRM/Shimmy/ShimmyHookObject.php | 23 ++++++++++++ .../example/CRM/Shimmy/ShimmyHookStatic.php | 24 ++++++++++++ .../example/tests/mixin/ScanClassesTest.php | 37 +++++++++++++++++++ tests/extensions/shimmy/shimmy.php | 24 ++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookObject.php create mode 100644 mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookStatic.php diff --git a/mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookObject.php b/mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookObject.php new file mode 100644 index 0000000000..4dfa37c26d --- /dev/null +++ b/mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookObject.php @@ -0,0 +1,23 @@ +data[] = "hello {$e->for} (shimmy.hook.object, as event)"; + } + +} diff --git a/mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookStatic.php b/mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookStatic.php new file mode 100644 index 0000000000..b5c05bcb36 --- /dev/null +++ b/mixin/scan-classes@1/example/CRM/Shimmy/ShimmyHookStatic.php @@ -0,0 +1,24 @@ +data[] = "hello {$e->for} (CRM_Shimmy_ShimmyHookStatic, as event)"; + } + +} diff --git a/mixin/scan-classes@1/example/tests/mixin/ScanClassesTest.php b/mixin/scan-classes@1/example/tests/mixin/ScanClassesTest.php index e993c84e17..c664c96d1d 100644 --- a/mixin/scan-classes@1/example/tests/mixin/ScanClassesTest.php +++ b/mixin/scan-classes@1/example/tests/mixin/ScanClassesTest.php @@ -20,22 +20,59 @@ class ScanClassesTest extends \PHPUnit\Framework\Assert { // Assert that WorkflowMessageInterface's are registered. $items = $cv->api4('WorkflowMessage', 'get', ['where' => [['name', '=', 'shimmy_message_example']]]); $this->assertEquals('CRM_Shimmy_ShimmyMessage', $items[0]['class']); + + // Assert that HookInterface's are registered. + $hookData = $this->fireHookShimmyFooBar($cv, 'world'); + sort($hookData); + $expectHookData = [ + 'hello world (CRM_Shimmy_ShimmyHookStatic, as event)', + 'hello world (CRM_Shimmy_ShimmyHookStatic, as hook)', + 'hello world (shimmy.hook.object, as event)', + 'hello world (shimmy.hook.object, as hook)', + ]; + $this->assertEquals($expectHookData, $hookData); } public function testDisabled($cv) { // Assert that WorkflowMessageInterface's are removed. $items = $cv->api4('WorkflowMessage', 'get', ['where' => [['name', '=', 'shimmy_message_example']]]); $this->assertEmpty($items); + + // Assert that HookInterface's are removed. + $hookData = $this->fireHookShimmyFooBar($cv, 'world'); + $this->assertEquals([], $hookData); } public function testUninstalled($cv) { // Assert that WorkflowMessageInterface's are removed. $items = $cv->api4('WorkflowMessage', 'get', ['where' => [['name', '=', 'shimmy_message_example']]]); $this->assertEmpty($items); + + // Assert that HookInterface's are removed. + $hookData = $this->fireHookShimmyFooBar($cv, 'world'); + $this->assertEquals([], $hookData); } protected static function getPath($suffix = ''): string { return dirname(__DIR__, 2) . $suffix; } + /** + * Fire hook_civicrm_shimmyFooBar() in the system-under-test. + * + * @param $cv + * @param string $name + * @return array + * The modified $data + */ + protected function fireHookShimmyFooBar($cv, string $name): array { + try { + putenv('SHIMMY_FOOBAR=' . $name); + return $cv->phpEval('$d=[]; Civi::dispatcher()->dispatch("hook_civicrm_shimmyFooBar", \Civi\Core\Event\GenericHookEvent::create(["data"=>&$d,"for"=>getenv("SHIMMY_FOOBAR")])); return $d;'); + } + finally { + putenv('SHIMMY_FOOBAR'); + } + } + } diff --git a/tests/extensions/shimmy/shimmy.php b/tests/extensions/shimmy/shimmy.php index f600a18ba7..b78168fd35 100644 --- a/tests/extensions/shimmy/shimmy.php +++ b/tests/extensions/shimmy/shimmy.php @@ -106,3 +106,27 @@ function shimmy_civicrm_entityTypes(&$entityTypes) { // )); // _shimmy_civix_navigationMenu($menu); //} + +/** + * Assert that there is a service with a given name+type. + * + * @param string $class + * @param string $expectServiceName + * @param string $notServiceName + * @throws \Exception + */ +function _shimmy_assert_service_object(string $class, string $expectServiceName, string $notServiceName) { + if (Civi::container()->has($expectServiceName) && Civi::container()->has($notServiceName)) { + throw new \Exception("Oops! Found both names ($expectServiceName and $notServiceName)!"); + } + elseif (!Civi::container()->has($expectServiceName) && Civi::container()->has($notServiceName)) { + throw new \Exception("Oops! Found ($notServiceName) and missing expected ($expectServiceName)!"); + } + + if (!(Civi::container()->get($expectServiceName) instanceof $class)) { + $actual = Civi::container()->get($expectServiceName); + $actualType = is_object($actual) ? get_class($actual) : gettype($actual); + throw new \Exception("Oops! The service ($expectServiceName) should be an instance the class ($class). But found ($actualType)!"); + } + +} -- 2.25.1