Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用PHPUnit模拟对象的部分数组_Php_Phpunit - Fatal编程技术网

需要使用PHPUnit模拟对象的部分数组

需要使用PHPUnit模拟对象的部分数组,php,phpunit,Php,Phpunit,在PHPUnit mockwith()子句中测试多个数组键的最佳方法是什么 例如,要测试方法调用的第二个参数是否是包含'foo'键的数组,请执行以下操作: $this->stubDispatcher->expects($this->once()) ->method('send') ->with('className', $this->arrayHasKey('foo')); 我想做的是类似于$this->arrayHasKey

在PHPUnit mock
with()
子句中测试多个数组键的最佳方法是什么

例如,要测试方法调用的第二个参数是否是包含
'foo'
键的数组,请执行以下操作:

$this->stubDispatcher->expects($this->once())
        ->method('send')
        ->with('className', $this->arrayHasKey('foo'));

我想做的是类似于
$this->arrayHasKey('foo','bar')
的事情,而不是实际匹配数组的确切内容。

您可以使用回调来进行多个断言

$this->stubDispatcher->expects($this->once())
     ->method('send')
     ->will($this->returnCallback(function($class, $array) {
             self::assertEquals('className', $class);
             self::assertArrayHasKey('foo', $array);
             self::assertArrayHasKey('bar', $array);
     }));
编辑:如果要对某些参数进行基本断言,对其他参数进行更复杂的断言,可以使用()添加


要非常清楚,您不应该将
$this->returnCallback()
传递给
with()
。同样适用于
returnValue()
throweexception()
。这些都是指示mock在调用该方法时要做什么的指令
with()
用于告诉模拟应该接受哪些参数。

您可以将断言直接传递给
->with()
,但它们的名称不同

有关列表,请查看源代码:
https://github.com/sebastianbergmann/phpunit/blob/3.5/PHPUnit/Framework/Assert.php#L2097
和以下内容。所有不以“assert”开头并返回新PHPUnit\u框架的内容*

虽然我认为@David的答案是有效的,但我认为这种方法使用
logicalAnd()
更干净/更容易阅读

$mock->expects($this->once())->method("myMethod")->with(
    $this->logicalAnd(
        $this->arrayHasKey("foo"),
        $this->arrayHasKey("bar")
    )
);
工作示例
您读过本手册的这一章吗?你可以扩展PHPUunit来创建你自己的约束。是的,我看到了。我只是希望不用自己滚。谢谢,这很方便。不幸的是,它似乎不适用于()。您确定吗?我以前没有将它们组合在一起,但我希望它们能一起工作。但是,上面的内容是为了用()替换现有的
,我知道,对吧?我在phpunit 3.5.13上,我得到了如下预期错误:
断言失败,等于phpunit\u Framework\u MockObject\u Stub\u ReturnCallback Object(etc)
啊,现在我看到问题了。再次阅读我的代码。我正在使用
will()
设置
ReturnCallback
——而不是
with()
。对,我注意到了这一点。抱歉,如果评论中不清楚。
$mock->expects($this->once())->method("myMethod")->with(
    $this->logicalAnd(
        $this->arrayHasKey("foo"),
        $this->arrayHasKey("bar")
    )
);
<?php


class MyTest extends PHPUnit_Framework_TestCase {

    public function testWorks() {
        $mock = $this->getMock("stdClass", array("myMethod"));
        $mock->expects($this->once())->method("myMethod")->with(
            $this->logicalAnd(
                $this->arrayHasKey("foo"),
                $this->arrayHasKey("bar")
            )
        );
        $array = array("foo" => 1, "bar" => 2);
        $mock->myMethod($array);
    }

    public function testFails() {
        $mock = $this->getMock("stdClass", array("myMethod"));
        $mock->expects($this->once())->method("myMethod")->with(
            $this->logicalAnd(
                $this->arrayHasKey("foo"),
                $this->arrayHasKey("bar")
            )
        );
        $array = array("foo" => 1);
        $mock->myMethod($array);
    }

}
phpunit assertArrayKey.php 
PHPUnit 3.5.13 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 6.50Mb

There was 1 failure:

1) MyTest::testFails
Expectation failed for method name is equal to <string:myMethod> when invoked 1 time(s)
Parameter 0 for invocation stdClass::myMethod(array( <string:foo> => <integer:1> )) does not match expected value.
Failed asserting that an array has the key <string:bar>.

/home/edo/test/assertArrayKey.php:27