diff --git a/CHANGELOG.md b/CHANGELOG.md index 176d9bea..ce72b705 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Enhancements: * [ORM] Added support for count query with `DISTINCT` in PostgreSQL dialect. * [Utilities] `Strings::EMPTY_STRING` is deprecated in favour of `Strings::EMPTY`. * [Core] `Bootstrap::withErrorHandler(ErroraHandler $errorHandler)` set custom error handler which is registered on `Bootstrap::runApplication()`. +* [Tests] Added new assertion `messageContains` to `CatchException`. Release 2.0.0 -------- diff --git a/src/Ouzo/Goodies/Tests/CatchExceptionAssert.php b/src/Ouzo/Goodies/Tests/CatchExceptionAssert.php index 62230926..c705b44e 100644 --- a/src/Ouzo/Goodies/Tests/CatchExceptionAssert.php +++ b/src/Ouzo/Goodies/Tests/CatchExceptionAssert.php @@ -50,6 +50,13 @@ public function hasCode(int $code): CatchExceptionAssert return $this; } + public function messageContains(string $messagePart): CatchExceptionAssert + { + $this->validateExceptionThrown(); + AssertAdapter::assertContains($messagePart, $this->exception->getMessage()); + return $this; + } + private function validateExceptionThrown(): void { if (!$this->exception) { diff --git a/test/src/Ouzo/Goodies/Tests/CatchExceptionTest.php b/test/src/Ouzo/Goodies/Tests/CatchExceptionTest.php index 16b82a3b..13e2d815 100644 --- a/test/src/Ouzo/Goodies/Tests/CatchExceptionTest.php +++ b/test/src/Ouzo/Goodies/Tests/CatchExceptionTest.php @@ -49,7 +49,7 @@ public function shouldNotCatchException() } #[Test] - public function shouldCheckIsMessageContains() + public function shouldCheckMessageIsEqualTo() { //given $object = new MyClass(); @@ -61,6 +61,19 @@ public function shouldCheckIsMessageContains() CatchException::assertThat()->hasMessage('Fatal error'); } + #[Test] + public function shouldCheckMessageContains() + { + //given + $object = new MyClass(); + + //when + CatchException::when($object)->someMethodThatThrowsException(); + + //then + CatchException::assertThat()->messageContains('tal err'); + } + #[Test] public function getShouldReturnException() {