johnkary/phpunit-speedtrap

Exception in setUpBeforeClass

Thomblin opened this issue · 5 comments

With my current configuration, SpeedTrapListener fails with an unhelpful Exception, if setUpBeforeClass throws an Exception.

SpeedTrapListener::endTest is called with $test as instance of PHPUnit_Framework_TestSuite,
which causes an E_RECOVERABLE_ERROR because SpeedTrapListener::getSlowThreshold expects PHPUnit_Framework_TestCase to be passed.

current result:

PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'There should be an error triggered while test run
E_RECOVERABLE_ERROR: Argument 1 passed to JohnKary\PHPUnit\Listener\SpeedTrapListener::getSlowThreshold() must be an instance of PHPUnit_Framework_TestCase, instance of PHPUnit_Framework_TestSuite given, called in /vagrant/vendor/johnkary/phpunit-speedtrap/src/JohnKary/PHPUnit/Listener/SpeedTrapListener.php on line 128 and defined
         at 0  /vagrant/Tests/phpunit/bootstrap.php (line 64) -> getDebugAsString()
         at 1  /vagrant/vendor/johnkary/phpunit-speedtrap/src/JohnKary/PHPUnit/Listener/SpeedTrapListener.php (line 309) -> user_error_handler()
         at 2  /vagrant/vendor/johnkary/phpunit-speedtrap/src/JohnKary/PHPUnit/Listener/SpeedTrapListener.php (line 128) -> getSlowThreshold()
         at 3  /vagrant/vendor/phpunit/phpunit/src/Framework/TestResult.php (line 344) -> endTest()
         at 4  /vagrant/vendor/phpunit/phpunit/src/Framework/TestSuite.php (line 695) -> endTest()
         at 5  /vagrant/vendor/phpunit/phpunit/src/TextUI/Te in /vagrant/Tests/phpunit/bootstrap.php on line 71

expected result:

There was 1 error:

1) TestsPhpunitSpeedtrapTest
exception 'Exception' with message 'I am broken' in /vagrant/Tests/phpunit/speedtrap_test.php:11

Suggestion to fix this:

add if ( $test instanceof \PHPUnit_Framework_TestCase ) { to SpeedTrapListener::endTest

TestCase to reproduce the problem:

class TestsPhpunitSpeedtrapTest extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        throw new Exception("I am broken");
    }

    public function test()
    {
        $this->assertTrue(true);
    }
}
PHPUnit 4.4.5 by Sebastian Bergmann.
PHP 5.4.36-0+deb7u3 (cli) (built: Jan  9 2015 08:07:06) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with XCache v2.0.0, Copyright (c) 2005-2012, by mOo
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

$test in the SpeedTrapListener ::endTest method is typecasted to PHPUnit_Framework_Test, so having it look for it being an instance of PHPUnit_Framework_TestCase won't work.

Honestly, the problem here is that your setUpBeforeClass just shouldn't be throwing exceptions. Fix that and everything else works.

Well,

in the described case, PHPUnit_Framework_TestSuite is send to SpeedTrapListener::endTest (no idea why), which implements PHPUnit_Framework_Test but not PHPUnit_Framework_TestCase, which is expected in getSlowThreshold()

I'm having the same problem with a skipped test, this is indeed a real issue.

class SlowApiTest extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        if (!getenv('SKIP_CONDITION')) {
            self::markTestSkipped('foo');
        }
    }

    // ...
}

I've pushed up a fix into #13 for this.

Thank you so much for reporting the issue and the detailed way to recreate it! This issue has been fixed in 160af2e.