Valinor/tests/Unit/Mapper/Tree/Message/ThrowableMessageTest.php
Romain Canon ad1207153e feat!: rework messages body and parameters features
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';
    }
}
```
2022-07-25 22:05:31 +02:00

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());
}
}