PHPUnit测试错误参数类型引发的错误

Spe*_*ark 2 php phpunit

我刚刚开始使用PHPUnit并且可以使用所有的assert*方法,但是无法弄清楚当向方法提供错误的参数时如何测试错误抛出 - 比如暗示array如下:

public function(array $list) { }
Run Code Online (Sandbox Code Playgroud)

然后用nullas参数进行测试.

有人可以提供一个如何测试这种错误的例子吗?

我在stackoverflow上查了不少帖子,但找不到这个特定问题的答案.

编辑

好的 - 只是为了让您了解我正在测试的内容 - 这里是ArrayHelper :: removeIfValueIsEmpty()方法:

public static function removeIfValueIsEmpty(array $array) {

    if (empty($array)) {

        return array();

    }

    return array_filter($array, function($value) {

        return !Helper::isEmpty($value);

    });

}
Run Code Online (Sandbox Code Playgroud)

现在测试:

class ArrayHelperTest extends PHPUnit_Framework_TestCase {


    public function testRemoveIfValueIsEmpty() {

        $this->assertEmpty(
            \Cmd\Helper\ArrayHelper::removeIfValueIsEmpty(null),
            '\Cmd\Helper\ArrayHelper::removeIfValueIsEmpty method failed (checking with null as argument)'
        );

    }

}
Run Code Online (Sandbox Code Playgroud)

这会引发错误:

PHPUnit_Framework_Error : Argument 1 passed to Cmd\Helper\ArrayHelper::removeIfValueIsEmpty() must be of the type array, null given
Run Code Online (Sandbox Code Playgroud)

Nak*_*lda 6

http://phpunit.de/manual/4.1/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.errors

/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage  Argument 1 passed to Cmd\Helper\ArrayHelper::removeIfValueIsEmpty() must be of the type array, null given
*/
public function testRemoveIfValueIsEmpty() {

    \Cmd\Helper\ArrayHelper::removeIfValueIsEmpty(null);

}
Run Code Online (Sandbox Code Playgroud)