mirror of
https://github.com/danog/Valinor.git
synced 2024-12-15 10:27:02 +01:00
a805ba0442
A new class `NodeMessage` is used to wrap messages added to a node during the mapping. This class will allow further features by giving access to useful data related to the bound node. BREAKING CHANGE: as of now every message is wrapped into a `NodeMessage` it is therefore not possible to check whether the message is an instance of `Throwable` — a new method `$message->isError()` is now to be used for such cases.
80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree\Message;
|
|
|
|
use CuyZ\Valinor\Mapper\Tree\Message\NodeMessage;
|
|
use CuyZ\Valinor\Tests\Fake\Definition\FakeAttributes;
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\FakeNode;
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage;
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeMessage;
|
|
use CuyZ\Valinor\Tests\Fake\Type\FakeType;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class NodeMessageTest extends TestCase
|
|
{
|
|
public function test_node_properties_can_be_accessed(): void
|
|
{
|
|
$originalMessage = new FakeMessage();
|
|
$type = FakeType::permissive();
|
|
$attributes = new FakeAttributes();
|
|
|
|
$node = FakeNode::branch([[
|
|
'name' => 'foo',
|
|
'type' => $type,
|
|
'value' => 'some value',
|
|
'attributes' => $attributes,
|
|
'message' => $originalMessage
|
|
]])->children()['foo'];
|
|
|
|
$message = new NodeMessage($node, $originalMessage);
|
|
|
|
self::assertSame('foo', $message->name());
|
|
self::assertSame('foo', $message->path());
|
|
self::assertSame('some value', $message->value());
|
|
self::assertSame($type, $message->type());
|
|
self::assertSame($attributes, $message->attributes());
|
|
self::assertSame($originalMessage, $message->originalMessage());
|
|
}
|
|
|
|
public function test_value_from_invalid_node_returns_null(): void
|
|
{
|
|
$originalMessage = new FakeErrorMessage();
|
|
$node = FakeNode::leaf(FakeType::permissive(), 'foo')->withMessage($originalMessage);
|
|
|
|
$message = new NodeMessage($node, $originalMessage);
|
|
|
|
self::assertNull($message->value());
|
|
}
|
|
|
|
public function test_format_message_replaces_placeholders_with_default_values(): void
|
|
{
|
|
$originalMessage = new FakeMessage();
|
|
$type = FakeType::permissive();
|
|
|
|
$node = FakeNode::branch([[
|
|
'name' => 'foo',
|
|
'type' => $type,
|
|
'value' => 'some value',
|
|
'message' => $originalMessage
|
|
]])->children()['foo'];
|
|
|
|
$message = new NodeMessage($node, $originalMessage);
|
|
$text = $message->format('%1$s / %2$s / %3$s / %4$s / %5$s');
|
|
|
|
self::assertSame("some_code / some message / $type / foo / foo", $text);
|
|
}
|
|
|
|
public function test_format_message_replaces_placeholders_with_given_values(): void
|
|
{
|
|
$originalMessage = new FakeMessage();
|
|
$node = FakeNode::any();
|
|
|
|
$message = new NodeMessage($node, $originalMessage);
|
|
$text = $message->format('%1$s / %2$s', 'foo', 'bar');
|
|
|
|
self::assertSame('foo / bar', $text);
|
|
}
|
|
}
|