Skip to content

Commit

Permalink
Added new assertion messageContains to CatchException. (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbankowski committed Nov 8, 2023
1 parent e417070 commit be9a039
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
7 changes: 7 additions & 0 deletions src/Ouzo/Goodies/Tests/CatchExceptionAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion test/src/Ouzo/Goodies/Tests/CatchExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function shouldNotCatchException()
}

#[Test]
public function shouldCheckIsMessageContains()
public function shouldCheckMessageIsEqualTo()
{
//given
$object = new MyClass();
Expand All @@ -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()
{
Expand Down

0 comments on commit be9a039

Please sign in to comment.