【发布时间】:2022-01-22 16:53:33
【问题描述】:
我正在使用管道来过滤消息。
$value = app(Pipeline::class)
->send($value)
->through([
HtmlAttributeFilter::class,
ProfanityFilter::class,
RemoveTags::class,
])
->thenReturn();
我想测试这段代码
<?php
namespace App\Filters;
use Closure;
class HtmlAttributeFilter implements FilterInterface
{
/**
* Handles attribute filtering removes unwanted attributes
* @param $text
* @param Closure $next
* @return mixed
*/
public function handle($text, Closure $next)
{
$text = str_replace('javascript:', '', $text);
$text = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/si", '<$1$2>', $text);
return $next($text);
}
}
我通过定义自定义闭包来测试这段代码,但我不确定我是否以正确的方式进行操作。我想模拟,但我不知道如何模拟这个对象。以前有人测试过管道吗?任何帮助都将受到高度评价。
我就是这样测试的
$callable = function (string $text) {
return $text;
};
$text = "<html lang='tr'><link href='https://www.example.com'></html>";
$expectedText = "<html><link></html>";
$obj = new HtmlAttributeFilter();
$filteredText = $obj->handle($text, $callable);
$this->assertEquals($expectedText, $filteredText);
【问题讨论】:
标签: laravel unit-testing testing