php-mock/php-mock-phpunit

Data are leaking between tests

kiler129 opened this issue · 2 comments

This may be my obvious error, but since there's no real documentation I cannot verify it.
While testing rather complicated (and to be honest poorly written ;)) I came across one problem - all tests are green if run one by one, but they generate weird errors while all tests are run at once. I even tried using runInSeparateProcess annotation for all tests but it get even worse ;)

Example: https://github.com/kiler129/TorrentGhost/blob/fetchJobTests/tests/Http/FetchJobTest.php
Travis: https://travis-ci.org/kiler129/TorrentGhost/builds/89312464
screenshot 2015-11-04 22 10 35

Thank you very much for your bug report. I very much assume that you are hitting PHP's bug #68541. I recently discovered that HHVM is not affected by this bug and so does your HHVM CI environment not fail. In PHP a workaround for this issue is Mock::define().

You could confirm that by adding something like this to your test:

/**
 * @beforeClass
 */
public static function definePHPMocks()
{
    $curl_getinfo = new \phpmock\Mock(self::SUT_NAMESPACE, "curl_getinfo", function () {});
    $curl_getinfo->define();

    $curl_init = new \phpmock\Mock(self::SUT_NAMESPACE, "curl_init", function () {});
    $curl_init->define();

    // add further functions if it still fails.
}

Unfortunately I didn't incorporate that directly into php-mock-phpunit. For now you have to directly use the above suggested work around. Or it might even help if you would reorder your tests. I.e. put testCurlIsInitializedWithUriSpecifiedInsideRequest() as your first test. Yeah, it's a hack, but again it's because of a bug.

I'll think about integrating this more elegantly.

If you would upgrade to php-mock-phpunit-1.1.1 you could now use PHPMock::defineFunctionMock():

/**
 * @beforeClass
 */
public static function definePHPMocks()
{
    self::defineFunctionMock(self::SUT_NAMESPACE, "curl_getinfo");
    self::defineFunctionMock(self::SUT_NAMESPACE, "curl_init");

    // add further functions if it still fails.
}