From 29cffc15f0f83763e77c06668987317a39e51c7a Mon Sep 17 00:00:00 2001 From: Petr Chromec Date: Sat, 24 Jul 2021 18:47:39 +0200 Subject: [PATCH] Add OnErrorCallback::ignoreError named constructor to allow easily ignore errors --- CHANGELOG.md | 1 + src/ValueObject/OnErrorCallback.php | 5 +++ tests/ValueObject/OnErrorCallbackTest.php | 48 +++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tests/ValueObject/OnErrorCallbackTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fbd587..f0d4052 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ## Unreleased - Copy `$response` in `ProfilerItem` not to store a reference to the original object +- Add `OnErrorCallback::ignoreError` named constructor ## 1.0.0 - 2021-05-12 - Initial implementation diff --git a/src/ValueObject/OnErrorCallback.php b/src/ValueObject/OnErrorCallback.php index 155eae0..7f2874f 100644 --- a/src/ValueObject/OnErrorCallback.php +++ b/src/ValueObject/OnErrorCallback.php @@ -17,6 +17,11 @@ public static function throwOnError(): self }); } + public static function ignoreError(): self + { + return new self(function (\Throwable $error): void {}); + } + /** * @phpstan-param callable(\Throwable): void $callback */ diff --git a/tests/ValueObject/OnErrorCallbackTest.php b/tests/ValueObject/OnErrorCallbackTest.php new file mode 100644 index 0000000..ff3d2a7 --- /dev/null +++ b/tests/ValueObject/OnErrorCallbackTest.php @@ -0,0 +1,48 @@ +getMessage(); + }); + + $callback(new \Exception('Actual error')); + + $this->assertSame('Actual error', $actualError); + } + + /** + * @test + */ + public function shouldThrowGivenError(): void + { + $callback = OnErrorCallback::throwOnError(); + + $this->expectExceptionMessage('Should be thrown.'); + + $callback(new \Exception('Should be thrown.')); + } + + /** + * @test + */ + public function shouldIgnoreError(): void + { + $callback = OnErrorCallback::ignoreError(); + + $callback(new \Exception('Should not be thrown.')); + + $this->expectNotToPerformAssertions(); + } +}