PHPUnit>如何测试是否将正确的类型传递给方法?

时间:2014-08-13 17:28:00

标签: php phpunit

我有一个方法,我设置了要传递的第一个参数的类型(数组):

public function create(array $values, $options=array())
{
    // set datetime columns
    if(! isset($values['date_created']) or empty($values['date_created'])) {
        $values['date_created'] = date("Y-m-d H:i:s");
    }
    if(! isset($values['date_updated']) or empty($values['date_updated'])) {
        $values['date_updated'] = date("Y-m-d H:i:s");
    }

    return parent::create($values);
}

如何使用PHPUnit对此进行测试,它会不断发出致命错误,我猜这是预期的,但它不允许我的脚本运行:

1) UserTableTest::testCreateMethodWithInvalidArguments
Failed asserting that exception of type "PHPUnit_Framework_Error" matches expected exception "InvalidArgumentException". Message was: "Argument 1 passed to app\models\UserTable::create() must be of the type array, string given, called in /var/www/phpdev/tests/models/UserTableMockTest.php on line 82 and defined" at
#0 /var/www/phpdev/app/models/UserTable.php(16): PHPUnit_Util_ErrorHandler::handleError(4096, 'Argument 1 pass...', '/var/www/phpdev...', 16, Array)
#1 /var/www/phpdev/tests/models/UserTableMockTest.php(82): app\models\UserTable->create('invalid argumen...')
#2 [internal function]: UserTableTest->testCreateMethodWithInvalidArguments()
#3 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(962): ReflectionMethod->invokeArgs(Object(UserTableTest), Array)
#4 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(826): PHPUnit_Framework_TestCase->runTest()
#5 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestResult.php(686): PHPUnit_Framework_TestCase->runBare()
#6 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(760): PHPUnit_Framework_TestResult->run(Object(UserTableTest))
#7 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestSuite.php(699): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#8 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(426): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#9 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/Command.php(179): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#10 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/Command.php(132): PHPUnit_TextUI_Command->run(Array, true)
#11 /var/www/phpdev/vendor/phpunit/phpunit/phpunit(55): PHPUnit_TextUI_Command::main()
#12 {main}.

FAILURES!                            
Tests: 1, Assertions: 1, Failures: 1.

我是否应该删除类型提示并使用is_array($ values)来判断是否传递了正确的类型然后抛出异常?在PHPUnit中,我期待抛出异常。顺便说一句,下面是我的测试:

/**
* @expectedException InvalidArgumentException
*/
public function testCreateMethodWithInvalidArguments()
{
    // pass invalid argument, should be an array of values
    $result = $this->userTable->create('invalid argument type');
}

1 个答案:

答案 0 :(得分:0)

PHPUnit只需要知道你期待和异常,这样它就可以正确处理它并报告它。

 public function testExceptionHasRightMessage()
{
    $this->setExpectedException(
      'InvalidArgumentException', 'Right Message'
    );
    throw new InvalidArgumentException('Some Message', 10);
}

这是从PHP Unit Documentation - Writing Unit Tests

的示例2.11中提取的