-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #485 from patchlevel/improve-projection-traces
improve projection errors and traces
- Loading branch information
Showing
4 changed files
with
118 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection; | ||
|
||
use Patchlevel\EventSourcing\Aggregate\CustomId; | ||
use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
use function count; | ||
use function fclose; | ||
use function fopen; | ||
|
||
final class ErrorContextTest extends TestCase | ||
{ | ||
public function testErrorContext(): void | ||
{ | ||
$resource = fopen('php://memory', 'r'); | ||
$result = ErrorContext::fromThrowable( | ||
$this->createException( | ||
'test', | ||
new CustomId('test'), | ||
$resource, | ||
['test' => [1, 2, 3]], | ||
static fn () => null, | ||
), | ||
); | ||
fclose($resource); | ||
|
||
$this->assertCount(1, $result); | ||
$error = $result[0]; | ||
|
||
$this->assertSame(RuntimeException::class, $error['class']); | ||
$this->assertSame('test', $error['message']); | ||
$this->assertSame(0, $error['code']); | ||
$this->assertSame(__FILE__, $error['file']); | ||
$this->assertGreaterThan(0, count($error['trace'])); | ||
$this->assertArrayHasKey(0, $error['trace']); | ||
|
||
$firstTrace = $error['trace'][0]; | ||
|
||
$this->assertArrayHasKey('file', $firstTrace); | ||
$this->assertSame(__FILE__, $firstTrace['file'] ?? null); | ||
$this->assertArrayHasKey('line', $firstTrace); | ||
$this->assertSame('createException', $firstTrace['function'] ?? null); | ||
$this->assertArrayHasKey('class', $firstTrace); | ||
$this->assertSame(self::class, $firstTrace['class'] ?? null); | ||
$this->assertArrayHasKey('type', $firstTrace); | ||
$this->assertSame('->', $firstTrace['type'] ?? null); | ||
$this->assertArrayHasKey('args', $firstTrace); | ||
$this->assertSame([ | ||
'test', | ||
'object(Patchlevel\EventSourcing\Aggregate\CustomId)', | ||
'resource(stream)', | ||
['test' => [1, 2, 3]], | ||
'object(Closure)', | ||
], $firstTrace['args'] ?? null); | ||
} | ||
|
||
/** | ||
* @param resource $resource | ||
* @param array<array-key, mixed> $array | ||
*/ | ||
private function createException( | ||
string $message, | ||
CustomId $id, | ||
$resource, | ||
array $array, | ||
callable $callable, | ||
): RuntimeException { | ||
return new RuntimeException($message); | ||
} | ||
} |