FUnit::assert_throws PHP Method

assert_throws() public static method

If $params is an array, it is passed as arguments to the callback. Otherwise, it is assumed no arguments need to be passed.
public static assert_throws ( callable $callback, array $params, string $exception = null, string $msg = null ) : boolean
$callback callable Callback that should throw an exception
$params array Callback that should throw an exception
$exception string The exception class that should be thrown
$msg string
return boolean
    public static function assert_throws(callable $callback, $params, $exception = null, $msg = null)
    {
        if (is_array($params)) {
            $exception = $exception ?: 'Exception';
        } else {
            $msg = $exception;
            $exception = $params;
            $params = array();
        }
        try {
            call_user_func_array($callback, $params);
            $rs = false;
        } catch (\Exception $e) {
            $rs = $e instanceof $exception;
        }
        $txt = isset($e) ? 'got ' . get_class($e) : 'no exception thrown';
        $fail_info = 'Expected exception ' . $exception . ', but ' . $txt;
        return array('result' => $rs, 'fail_info' => $fail_info);
    }

Usage Example

示例#1
0
    fu::strict_equal(true, fu::assert_not_strict_equal(1, "1")['result'], "1 and '1' are not strict equal");
    fu::strict_equal(true, fu::assert_not_strict_equal(1, true)['result'], "1 and true are not strict equal");
    fu::strict_equal(true, fu::assert_not_strict_equal(null, 0)['result'], "null and 0 are not strict equal");
    fu::strict_equal(true, fu::assert_not_strict_equal(false, null)['result'], "false and null are not strict equal");
    fu::strict_equal(true, fu::assert_not_strict_equal(array(), null)['result'], "array() and null are not strict equal");
});
fu::test('FUnit::assert_throws tests', function () {
    $callback = function () {
        throw new RuntimeException();
    };
    $rs = fu::assert_throws($callback, 'RuntimeException')['result'];
    fu::strict_equal(true, $rs, "callback threw correct exception type");
    $callback = function ($foo) {
        throw new RuntimeException($foo);
    };
    $rs = fu::assert_throws($callback, array('bar'), 'LogicException')['result'];
    fu::strict_equal(false, $rs, "callback didn't throw correct exception type");
});
fu::test('FUnit::assert_has tests', function () {
    $arr = array("foo" => true, "bar" => null, "baz" => "bingo");
    $obj = new stdClass();
    $obj->foo = true;
    $obj->bar = null;
    $obj->baz = "bingo";
    fu::strict_equal(true, fu::assert_has('foo', $arr)['result'], "\$arr has key 'foo'");
    fu::strict_equal(true, fu::assert_has('bar', $arr)['result'], "\$arr has key 'bar'");
    fu::strict_equal(true, fu::assert_has('baz', $arr)['result'], "\$arr has key 'baz'");
    fu::strict_equal(false, fu::assert_has('bingo', $arr)['result'], "\$arr does not have key 'bingo'");
    fu::strict_equal(true, fu::assert_has('foo', $obj)['result'], "\$obj has property 'foo'");
    fu::strict_equal(true, fu::assert_has('bar', $obj)['result'], "\$obj has property 'bar'");
    fu::strict_equal(true, fu::assert_has('baz', $obj)['result'], "\$obj has property 'baz'");