如何测试laravel管道



我正在使用管道来过滤消息。

$value = app(Pipeline::class)
->send($value)
->through([
HtmlAttributeFilter::class,
ProfanityFilter::class,
RemoveTags::class,
])
->thenReturn();

我想测试这个代码

<?php
namespace AppFilters;
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);

我认为给它一个自定义闭包是正确的做法,例如:

public function testHtmlAttributeFilterDoesSomething() {
$next = function ($result) {
$this->assertEquals('expected value', $result);

};
app()->make(HtmlAttributeFilter::class)->handle('given value', $next);
} 

我认为只要每个组成部分都经过测试,就不需要测试整个管道,因为Laravel包括测试管道逻辑是否按预期工作的测试

最新更新