Add helper functions for Filter API

This commit is contained in:
Pig Fang 2019-08-10 12:16:24 +08:00
parent b0dbba1475
commit a14ff87d0d
2 changed files with 52 additions and 0 deletions

View File

@ -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
{

View File

@ -0,0 +1,38 @@
<?php
namespace Tests;
class FilterTest extends TestCase
{
public function testAddFilter()
{
$this->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'));
}
}