mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-01-24 14:04:07 +08:00
Add helper functions for Filter API
This commit is contained in:
parent
b0dbba1475
commit
a14ff87d0d
@ -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
|
||||
{
|
||||
|
38
tests/ServicesTest/FilterTest.php
Normal file
38
tests/ServicesTest/FilterTest.php
Normal 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'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user