我正在使用管道來過濾訊息。
$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);
uj5u.com熱心網友回復:
我認為給它一個自定義閉包是正確的做法,例如:
public function testHtmlAttributeFilterDoesSomething() {
$next = function ($result) {
$this->assertEquals('expected value', $result);
};
app()->make(HtmlAttributeFilter::class)->handle('given value', $next);
}
我認為只要測驗每個組成部分就不需要測驗整個管道,因為 Laravel 包含測驗管道邏輯是否按預期作業的測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389707.html
