简体   繁体   English

如何获取PHPUnit中确定的被调用方法的数量

[英]How to get number of a determined called to method in PHPUnit

Currently I'm using strings to specifies where my test fails, like this: 目前,我正在使用字符串指定测试失败的地方,如下所示:

In the first call to 'XY' method, the first parameter: 在第一次调用“ XY”方法时,第一个参数:

so, want to get the number of call with phpunit. 因此,要获取使用phpunit的调用次数。

In short, I want a cardinal number instead of first, second, third ... , but given for phpunit (better) 简而言之,我想要一个基数,而不是first, second, third ... ,但是给了phpunit(更好)

public function testExample()
{
    $test = $this;

    $this->myClass
        ->expects($this->exactly(2))
        ->method('methodOne')
        ->withConsecutive(
            [
                $this->callback(function ($arg) use ($test) {
                    $part = 'In the first call to methodOne method, the first parameter: ';

                    $test->assertThat(
                        $arg,
                        $this->logicalAnd($this->equalTo('example1')),
                        $part . 'is not equal to "example1" '
                    );

                    return true;
                }),
            ],
            [
                $this->callback(function ($arg) use ($test) {
                    $part = 'In the first call to methodOne method, the first parameter: ';

                    $test->assertThat(
                        $arg,
                        $this->logicalAnd($this->equalTo('example2')),
                        $part . 'is not equal to "example2"'
                    );

                    return true;
                }),
            ]
        )
        ->will($this->returnSelf());
}

Using prophecy: 使用预言:

 class A {
    function abc($a, $b) {
        return ...;
    }
 }

    $a = $this->prophesize (A::class);
    $a->abc (1,2)->willReturn ("something");
    $A = $a->reveal ();

    $A->abc (1, 2);
    $A->abc (1, 2);
    $A->abc (1, 2);

This gives you the number of calls: 这样可以给您打电话:

    $calls = $a->findProphecyMethodCalls ("abc", new ArgumentsWildcard([new AnyValuesToken]));
    var_dump (count($calls));

You can loop through all the calls to see what their arguments were: 您可以遍历所有调用以查看其参数是什么:

    foreach ($calls as $call)
    {
        var_dump ($call->getArguments());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM