ssaweb/phpunit-slot

一个帮助您捕捉测试中方法参数的包。

v1.1.0 2024-03-07 16:07 UTC

This package is auto-updated.

Last update: 2024-09-07 17:08:24 UTC


README

一个允许您像使用 Mockk capture() 一样捕获方法参数的功能 - https://mockk.io/#capturing

好吧,我只是创建了这个包,使得参数断言更加易读(我的个人想法)。

当然,你可以这样说,下面这种方式已经可以工作,但我不知道在什么时候需要为每个参数添加回调,我觉得看起来并不好。

这就是我决定创建这个包,并使其更接近我在 Kotlin 中习惯的方式(使用 mockk 库)。

$mock
    ->expects($this->once())
    ->method('foo')
    ->with(self::callback(function ($value): bool {
        self::assertEquals('value', $value);
        return true;
    }))

而不是这样,我正在做的是类似这样的事情

$slot = new Slot();

$mock
    ->expects($this->once())
    ->method('foo')
    ->with($slot->capture())

//other mock declarations

//then
self::assertEquals(1, $slot->captured);

v1.1.0+ 现在支持多次方法调用。因此,如果您的函数被调用多次,捕获的值将是一个包含每次捕获值的数组。

$slot = new Slot();

$mock
    ->expects($this->any())
    ->method('foo')
    ->with($slot->capture())

//other mock declarations

//then
self::assertEquals(1, $slot->captured[0]);
self::assertEquals(2, $slot->captured[1]);