Skip to content

Commit

Permalink
Merge 29cffc1 into 163b927
Browse files Browse the repository at this point in the history
  • Loading branch information
MortalFlesh committed Jul 28, 2021
2 parents 163b927 + 29cffc1 commit 8883c5a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/ValueObject/OnErrorCallback.php
Expand Up @@ -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
*/
Expand Down
48 changes: 48 additions & 0 deletions tests/ValueObject/OnErrorCallbackTest.php
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace Lmc\Cqrs\Types\ValueObject;

use PHPUnit\Framework\TestCase;

class OnErrorCallbackTest extends TestCase
{
/**
* @test
*/
public function shouldUseGivenCallback(): void
{
$actualError = null;

$callback = new OnErrorCallback(function (\Throwable $error) use (&$actualError): void {
$actualError = $error->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();
}
}

0 comments on commit 8883c5a

Please sign in to comment.