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

MethodProphecy::willThrow() doesn't accept (an extension of) Throwable #428

Closed
timoschinkel opened this issue Mar 19, 2019 · 0 comments · Fixed by #429
Closed

MethodProphecy::willThrow() doesn't accept (an extension of) Throwable #428

timoschinkel opened this issue Mar 19, 2019 · 0 comments · Fixed by #429

Comments

@timoschinkel
Copy link
Contributor

timoschinkel commented Mar 19, 2019

Working on implementing unit tests for a piece of code that uses a PSR-18 client I was unable to have a \Psr\Http\Client\ClientInterface::sendRequest() MethodProphecy to throw one of the expected exception interface. To reproduce:

<?php

class ReproductionTest extends \PHPUnit\Framework\TestCase
{
    public function testThrowable()
    {
        $client = $this->prophesize(\Psr\Http\Client\ClientInterface::class);
        $client->sendRequest(Argument::type(\Psr\Http\Message\RequestInterface::class))
            ->willThrow(\Psr\Http\Client\ClientExceptionInterface::class);
        
        // do assertions
    }
}

Since \Psr\Http\Client\ClientExceptionInterface extends \Throwable this should work. But it doesn't:

Prophecy\Exception\InvalidArgumentException : Exception / Throwable class or instance expected as argument to ThrowPromise, but got Psr\Http\Client\ClientExceptionInterface.

This scenario is reproducible on the latest stable version 1.8.0 and on the master branch.

timoschinkel added a commit to timoschinkel/prophecy that referenced this issue Mar 19, 2019
- add additional check for existence of interface in contructor
- change `is_subclass_of` to `is_a` to allow for `Throwable` and subclasses of `Throwable`
- add specs for cases of `Throwable` and extensions of `Throwable`

To maintain support for php 5.3 the new specs are only applicable for PHP 7.0 and higher and `ThrowPromise::isValidThrowable()` checks for both `Exception` and `Throwable`.

fixes phpspec#428
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

Successfully merging a pull request may close this issue.

1 participant