I have a class that takes in a dependency. One of the things the class does is catch errors thrown by that dependency (does something useful to log this, and goes on to return a return the rest of the code finds useful.)
In PHPUnit, I can mock up the dependency and test as follows:
$stubqueryrunner = $this->createStub(QueryRunner::class);
$stubqueryrunner->method('getArray')->will($this->throwException(new \Error));
$inner = new myClass("mark", $stubqueryrunner);
$this->assertEquals(["what" => "", "why" => ""], $inner->getReasons());
The class I'm testing catches any error raised in its dependencies (and returns some default values) for the getReasons method.
My code passes my tests (ie - the Method I'm testing has indeed caught the error and substituted the default values.)
However, the act of raising the error writes many lines of stack trace to the output of PHPUnit.
I can't use the @ operator at the point I call it, because I'm in PHP8, where the stfu operator is deprecated for errors.
I can't use
/**
* @expectedError PHPUnit\Framework\Error
*/
... because that's not in PHPUnit 9.
So how do I actually suppress all the stack trace stuff from appearing in the PHPUnit console?
Update: Adding
$this->expectException(\Error::class);
just creates an additional assertion which then immediately fails precisely because the class I'm testing has successfully caught it.
The code I'm testing never actually produces errors. The only thing that's producing an error is the Stub I've written in the Unit test when it runs throwException
Adding error_reporting(0);
in the test doesn't help.