测试phpunit函数的测试用例


Test cases to test a function phpunit

我是php单元测试的新手,下面函数的有效测试用例是什么。

 protected function validateParams($graph, $start, $destination)
{
    if (!is_object($graph)) {
        throw new 'InvalidArgumentException('Graph param should be an object !');
    }
    if (empty($start)) {
        throw new 'InvalidArgumentException('Start param is empty !');
    }
    if (empty($destination)) {
        throw new 'InvalidArgumentException('Graph param is empty !');
    }
    return true;
}

首先,测试在将正确的参数传递给方法时,它会返回true

public function testParamsValidation();

然后检查当任何参数为空时是否抛出了InvalidArgumentException。请注意,您应该有3个测试,每个参数一个。在每个测试中,您只通过一个空参数。您可能希望使用不同的参数值(如null、false、scalar等)多次执行这些测试。请使用dataProviders。

public function testInvalidArgumentExceptionIsThrownWhenGraphIsNotAnObject(;
public function testInvalidArgumentExceptionIsThrownWhenStartIsEmpty();
public function testInvalidArgumentExceptionIsThrownWhenDestinationIsEmpty();

附带说明:您可能希望在方法定义中显式显示所需的对象类。$graph对象应该是某个类还是实现某个接口?