diff options
author | Mario <mario@mariovavti.com> | 2024-07-06 11:05:22 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-07-06 11:05:22 +0000 |
commit | 45275910e606a02b12393714ea3b0409da440d61 (patch) | |
tree | 10b2d173d58cb930f8df28fe75af73dd4974c08c /tests/unit/CallHooksTest.php | |
parent | 0c1d0f7498661fb34dcca6f3c6566e757af310a7 (diff) | |
parent | c04e781926a78e514cdf211fa24930a331149072 (diff) | |
download | volse-hubzilla-45275910e606a02b12393714ea3b0409da440d61.tar.gz volse-hubzilla-45275910e606a02b12393714ea3b0409da440d61.tar.bz2 volse-hubzilla-45275910e606a02b12393714ea3b0409da440d61.zip |
Merge branch '9.2RC'master
Diffstat (limited to 'tests/unit/CallHooksTest.php')
-rw-r--r-- | tests/unit/CallHooksTest.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/unit/CallHooksTest.php b/tests/unit/CallHooksTest.php new file mode 100644 index 000000000..0170f31d0 --- /dev/null +++ b/tests/unit/CallHooksTest.php @@ -0,0 +1,65 @@ +<?php +/** + * Unit tests for the `call_hooks` function, located in include/plugin.php. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit; + +use PHPUnit\Framework\Attributes\BackupStaticProperties; +use App; + +#[BackupStaticProperties(App::class)] +class CallHooksTest extends UnitTestCase { + + /** + * Test using a freestanding function as callback. + * + * @SuppressWarnings(PHPMD.EvalExpression) + */ + public function test_freestanding_function_as_string(): void { + eval('function hook_test_function(array &$args): void { $args["called"] = true; }'); + insert_hook('test_hook', 'hook_test_function'); + $this->assertHookInvoked(); + } + + public function test_static_class_function_as_string(): void { + insert_hook('test_hook', 'Zotlabs\Tests\Unit\CallHooksTest::static_test_hook'); + $this->assertHookInvoked(); + } + + public function test_static_class_function_as_array(): void { + insert_hook('test_hook', ['Zotlabs\Tests\Unit\CallHooksTest', 'static_test_hook']); + $this->assertHookInvoked(); + } + + public function test_static_class_function_as_serialized_array(): void { + insert_hook('test_hook', serialize(['Zotlabs\Tests\Unit\CallHooksTest', 'static_test_hook'])); + $this->assertHookInvoked(); + } + + public function test_instance_function_as_array(): void { + insert_hook('test_hook', [$this, 'instance_test_hook']); + $this->assertHookInvoked(); + } + + + public function assertHookInvoked(): void { + $test_hook_args = ['called' => false]; + call_hooks('test_hook', $test_hook_args); + + $this->assertTrue($test_hook_args['called']); + } + + public function instance_test_hook(array &$args): void { + $args['called'] = true; + } + public static function static_test_hook(array &$args): void { + $args['called'] = true; + } +} + |