用管道测试嘲弄

Test mockery with a pipeline

我该如何断言:

$this->assertEquals('incoming', $payload['routerAction']);

跳过管道。我正在使用 phpleague/pipeline

代码:

class IncomingPipeline
{
    public function __invoke(array $payload)
    {
        $router = $payload['router'];
        $payload['routerAction'] = 'incoming';

        return (new Pipeline())
            ->pipe(new DispatchIncomingEventStage())
            ->pipe(BaseRouter::route($router))
            ->process($payload);
    }
}

我基本上想跳过流水线或对它们设置 andReturn($payload)。

您可以使管道成为 IncomingPipeline 的依赖项并将其传递给构造函数。这样,您就可以在测试中注入没有任何阶段的管道。

如果您想保持原样,可以使用 overload 前缀 (docs):

final class IncomingPipelineTest extends TestCase
{
    public function test()
    {
        $pipeline = Mockery::mock('overload:' . Pipeline::class);
        $pipeline->allows('pipe')->andReturnSelf();
        $pipeline->allows('process')->andReturnArg(0);

        $payload = (new IncomingPipeline())(...);

        self::assertEquals('incoming', $payload['routerAction']);
    }
}