diff --git a/app/helpers.php b/app/helpers.php index eb44268c..38329fc4 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -65,6 +65,20 @@ if (! function_exists('json')) { } } +if (! function_exists('add_filter')) { + function add_filter($hook, $callback, $priority = 20, $arguments = 1): void + { + app('eventy')->addFilter($hook, $callback, $priority, $arguments); + } +} + +if (! function_exists('apply_filters')) { + function apply_filters() + { + return call_user_func_array([app('eventy'), 'filter'], func_get_args()); + } +} + if (! function_exists('bs_footer_extra')) { function bs_footer_extra(): string { diff --git a/tests/ServicesTest/FilterTest.php b/tests/ServicesTest/FilterTest.php new file mode 100644 index 00000000..4b04fe7e --- /dev/null +++ b/tests/ServicesTest/FilterTest.php @@ -0,0 +1,38 @@ +mock('eventy', function ($mock) { + $mock->shouldReceive('addFilter') + ->withArgs(function ($hook, $callback) { + $this->assertEquals('my.hook', $hook); + $this->assertEquals('Filtered text', $callback('text')); + return true; + }) + ->once(); + }); + add_filter('my.hook', function ($value) { + return "Filtered $value"; + }); + } + + public function testApplyFilters() + { + $this->mock('eventy', function ($mock) { + $mock->shouldReceive('filter')->withArgs(['my.hook', 'value'])->once(); + }); + apply_filters('my.hook', 'value'); + } + + public function testIntegration() + { + add_filter('hook.test', function ($value) { + return $value.'ed'; + }); + $this->assertEquals('tested', apply_filters('hook.test', 'test')); + } +}