mirror of
https://github.com/danog/Valinor.git
synced 2024-12-04 02:27:54 +01:00
ad1207153e
The `\CuyZ\Valinor\Mapper\Tree\Message\Message` interface is no longer a `Stringable`, however it defines a new method `body` that must return the body of the message, which can contain placeholders that will be replaced by parameters. These parameters can now be defined by implementing the interface `\CuyZ\Valinor\Mapper\Tree\Message\HasParameters`. This leads to the deprecation of the no longer needed interface `\CuyZ\Valinor\Mapper\Tree\Message\TranslatableMessage` which had a confusing name. ```php final class SomeException extends DomainException implements ErrorMessage, HasParameters, HasCode { private string $someParameter; public function __construct(string $someParameter) { parent::__construct(); $this->someParameter = $someParameter; } public function body() : string { return 'Some message / {some_parameter} / {source_value}'; } public function parameters(): array { return [ 'some_parameter' => $this->someParameter, ]; } public function code() : string { // A unique code that can help to identify the error return 'some_unique_code'; } } ```
33 lines
884 B
PHP
33 lines
884 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree\Message;
|
|
|
|
use CuyZ\Valinor\Mapper\Tree\Message\ThrowableMessage;
|
|
use PHPUnit\Framework\TestCase;
|
|
use RuntimeException;
|
|
|
|
final class ThrowableMessageTest extends TestCase
|
|
{
|
|
public function test_properties_can_be_accessed(): void
|
|
{
|
|
$message = 'some message';
|
|
$code = 'some code';
|
|
|
|
$codedError = ThrowableMessage::new($message, $code);
|
|
|
|
self::assertSame($message, $codedError->body());
|
|
self::assertSame($code, $codedError->code());
|
|
}
|
|
|
|
public function test_from_throwable_returns_error_message(): void
|
|
{
|
|
$original = new RuntimeException('some message', 1337);
|
|
$message = ThrowableMessage::from($original);
|
|
|
|
self::assertSame('some message', $message->getMessage());
|
|
self::assertSame('1337', $message->code());
|
|
}
|
|
}
|