legacy

Testing code that generates warnings

Our code base has a lot of code that looks like this: try { $this->doScaryThing(); } catch(Exception $e) { trigger_error("Downgraded: " . get_class($e) . ":" . $e->getMessage(), E_USER_WARNING); } Or sometimes trigger_error is used as a way to log other thigns. This makes it rather difficult to test. Thankfully PHPUnit 8.4 has the expectWarning method, that allows us to check this: public function testItTriggersWarning(): void { $object = new Danger(); $this->expectWarning(); $object->doWarningThing(); } This does mean that the execution stops after the warning is triggered, so we can’t assert anything after that.