Search code examples
phpunithhvm

How to make phpunit @expectedException work with hhvm?


I have a few PHPUnit tests in my project, and some of them use the @expectedException feature, as shown below.

/**
 * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
 **/
public function testExceptionThrownWhenErrorObjectReceived()
{
    ...
}

/**
 * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
 **/
public function testExceptionThrownWhenOAuthErrorReceived()
{
    ...
}

/**
 * @expectedException UnexpectedValueException
 **/
public function testExceptionThrownWhenAskingForResourceOwner()
{
    ...
}

I run them with the help of travis under PHP 5.6, 7.0, 7.1 and 7.2 without problem, but HHVM fails :

There were 3 errors:
1) Mrjoops\OAuth2\Client\Test\Provider\JiraTest::testExceptionThrownWhenErrorObjectReceived
Mrjoops\OAuth2\Client\Provider\Exception\JiraIdentityProviderException: Validation Failed
2) Mrjoops\OAuth2\Client\Test\Provider\JiraTest::testExceptionThrownWhenOAuthErrorReceived
Mrjoops\OAuth2\Client\Provider\Exception\JiraIdentityProviderException: error_collection
3) Mrjoops\OAuth2\Client\Test\Provider\JiraTest::testExceptionThrownWhenAskingForResourceOwner
UnexpectedValueException: Invalid response received from Authorization Server. Expected JSON.

I use the latest PHPUnit 5.7 version (for PHP 5.6 compatibility) and the latest HHVM 3.29.1.

Travis wrote on their website about this :

Please note that if you want to run PHPUnit on HHVM, you have to explicitly install version 5.7 in your .travis.yml due to a compatibility issue between HHVM and PHP7

So I suppose I'm OK.

I know there is a known issue (https://github.com/sebastianbergmann/phpunit/issues/1640) which was unresolved and closed in PHPUnit, and an inconsistency documented in HHVM (https://github.com/hhvm/user-documentation/blob/master/guides/hhvm/06-inconsistencies/03-classes-and-objects.md), but it's not clear to me if a workaround exists.

Details available here :

Thank you in advance for your help.


Solution

  • You can try to use:

    $this->expectException(UnexpectedValueException::class);
    

    but I guess that will raise the same problem. Or you try to use a workaround:

    try {
        doSomething();
    } catch (Exception $ex) {
        $this->assertInstanceOf(UnexpectedValueException::class, $ex);
    }
    $this->fail('Exception did not occur');
    

    But I would raise the question if HHVM is something you really want/have to support? Support for it was dropped by many major frameworks and applications (composer, symfony and others): https://github.com/facebook/hhvm/issues/7198