如何测试 laravel 管道

How to test laravel pipeline

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

$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", '<>', $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 包括测试管道逻辑是否按预期工作的测试