phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Multiple calls to expectExceptionMessage

Open Seldaek opened this issue 1 year ago • 2 comments

I have a long exception message which has a lot of garbage in, but a few elements I want to check the presence of.

So I figured I'd call self::expectExceptionMessage() several times to assert these elements, however it seems that only the last call takes effect. That makes sense as that method just sets the expectedExceptionMessage property, but I thought it might be nice if it would collect a list of assertions to make instead of just overwrite.

Anyway as a workaround to make two assertions I now have expectExceptionMessage + expectExceptionMessageMatches, as both are preserved and checked.

Seldaek avatar Oct 31 '24 09:10 Seldaek

with the current means you could do something like

public function testMultipleMessages(): void {
	try {
	  $foo->doFoo();
      $this->fail('expected exception not thrown');
	} catch (MyException $e) {
	  $this->assertContains('string1', $e->getMessage());
	  $this->assertContains('another string', $e->getMessage());
	  $this->assertContains('a 3rd string', $e->getMessage());
	}
}

staabm avatar Oct 31 '24 15:10 staabm

Right that'd have been my next option if I had to but I figured it could be nice if PHPUnit accepted multiple, or if not maybe it should throw if something is set already because it's kinda swallowing assertions silently right now.

Seldaek avatar Oct 31 '24 19:10 Seldaek