kahlan\matcher\ToThrow::match PHP Method

match() public static method

The value passed to $expected is either an exception or the expected exception's message.
public static match ( Closure $actual, mixed $expected = null, integer $code ) : boolean
$actual Closure The closure to run.
$expected mixed A string indicating what the error text is expected to be or a exception instance.
$code integer The expected `Exception` code if `$expected` is a string.
return boolean
    public static function match($actual, $expected = null, $code = 0)
    {
        $exception = static::expected($expected, $code);
        $a = static::actual($actual);
        static::_buildDescription($a, $exception);
        return static::_matchException($a, $exception);
    }

Usage Example

Example #1
0
            $actualException = new Exception();
            $expectedException = new Exception();
            $actual = function () use($actualException) {
                throw $actualException;
            };
            ToThrow::match($actual, $expectedException, 0);
            $actual = ToThrow::description();
            expect($actual['description'])->toBe('throw a compatible exception.');
            expect($actual['data']['actual'])->toBe($actualException);
            expect($actual['data']['expected'])->toBe($expectedException);
        });
        it("returns the description message when actual doesn't throw any exception", function () {
            $exception = new Exception();
            ToThrow::match(function () {
            }, $exception, 0);
            $actual = ToThrow::description();
            expect($actual['description'])->toBe('throw a compatible exception.');
            expect($actual['data']['actual'])->toBe(null);
            expect($actual['data']['expected'])->toBe($exception);
        });
        it("returns the description message when the expected value is a string", function () {
            ToThrow::match(function () {
            }, 'Expected exception message', 0);
            $actual = ToThrow::description();
            expect($actual['description'])->toBe('throw a compatible exception.');
            expect($actual['data']['actual'])->toBe(null);
            expect($actual['data']['expected'])->toBeAnInstanceOf('Kahlan\\Matcher\\AnyException');
            expect($actual['data']['expected']->getMessage())->toBe('Expected exception message');
        });
    });
});