1

I've refactored some code and introduced deprecation warnings with trigger_error() and the E_USER_DEPRECATED constant.

Therefore I needed to modify the test that is touching that feature

public function testMethod()
{
    $expected = 'value';
    $actual   = $this->subject->callDeprecatedMethod();
    $this->assertEquals($expected, $actual)
}

So that the method call does not raise the exception (e.g. as outlined here or here):

public function testMethod()
{
    $expected = 'value';

    $save = PHPUnit_Framework_Error_Deprecated::$enabled;
    PHPUnit_Framework_Error_Deprecated::$enabled = false;
    $actual = $this->subject->callDeprecatedMethod();
    PHPUnit_Framework_Error_Deprecated::$enabled = save;

    $this->assertEquals($expected, $actual)
}

This worked fine, however I realized that when running all tests, the testrunner took much much longer at one dot suddenly and by checking against JSON and also in PHPStorm that for this just changed testMethod the timings went up from milliseconds to 0.71 seconds.

How can I prevent this?

I need my tests to run fast fast fast :)

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836

1 Answers1

1

It looks like that in case of an error, the PHPUnit error handler does keep some of the information desipite the error exceptions are disabled. As those backtraces can be larger, I assume this is iterating those plus handling the data.

So my idea was to get the error handler out of the equation - thanks to storing $actual results to variables - and re-enable PHPUnits error handler then again:

public function testMethod()
{
    $expected = 'value';

    $noop = function() {};
    $previous = set_error_handler($noop);

    $actual = $this->subject->callDeprecatedMethod();

    set_error_handler($previous);

    $this->assertEquals($expected, $actual)
}

This brings back the original timings.

Actually, a similar effect has to use the error suppression operator as I then found out. Albeit it doesn't disable the handler from PHPUnit, the fast timings are restored:

public function testMethod()
{
    $expected = 'value';

    $actual = @$this->subject->callDeprecatedMethod();

    $this->assertEquals($expected, $actual)
}

It is also more comfortable to use.

hakre
  • 193,403
  • 52
  • 435
  • 836