Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling throw() with both type and message only checks the message? #20

Closed
ezzatron opened this issue May 27, 2016 · 0 comments
Closed
Milestone

Comments

@ezzatron
Copy link
Contributor

When you specify the exception message in a throw() assertion, the type check is currently bypassed.

Code to reproduce:

<?php

describe('Bug with throw()', function () {
    it('should assert type', function () {
        $actual = function () {
            $throws = function () { throw new Exception(); };

            expect($throws)->to->throw('RuntimeException');
        };

        expect($actual)->to->throw('Peridot\Leo\Responder\Exception\AssertionException');
    });

    it('should assert type even when message is specified', function () {
        $actual = function () {
            $throws = function () { throw new Exception('foobar'); };

            expect($throws)->to->throw('RuntimeException', 'foobar');
        };

        expect($actual)->to->throw('Peridot\Leo\Responder\Exception\AssertionException');
    });
});

The first test passes because Exception is not a RuntimeException. The second test fails because the message argument bypasses the type check.

This section of the code seems to be the offender, in ExceptionMatcher::doMatch():

            $message = $e->getMessage();
            if ($this->expectedMessage) {
                $this->setMessage($message);
                return $this->expectedMessage == $message;
            }
            if (!$e instanceof $this->expected) {
                return false;
            }

I tried fixing it myself but couldn't figure out how to get it to produce the correct error message in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant