Valinor/tests/Unit/Mapper/Tree/NodeTest.php
Romain Canon d3b1dcb64e feat!: refactor tree node API
The class `\CuyZ\Valinor\Mapper\Tree\Node` has been refactored to remove
access to unwanted methods that were not supposed to be part of the
public API. Below are a list of all changes:

- New methods `$node->sourceFilled()` and `$node->sourceValue()` allow
  accessing the source value.

- The method `$node->value()` has been renamed to `$node->mappedValue()`
  and will throw an exception if the node is not value.

- The method `$node->type()` now returns a string.

- The methods `$message->name()`, `$message->path()`, `$message->type()`
  and `$message->value()` have been deprecated in favor of the new
  method `$message->node()`.

- The message parameter `{original_value}` has been deprecated in favor
  of `{source_value}`.
2022-07-10 19:28:36 +02:00

103 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree;
use CuyZ\Valinor\Mapper\Tree\Exception\InvalidNodeHasNoMappedValue;
use CuyZ\Valinor\Mapper\Tree\Exception\SourceValueWasNotFilled;
use CuyZ\Valinor\Mapper\Tree\Node;
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\FakeNode;
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage;
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeMessage;
use PHPUnit\Framework\TestCase;
final class NodeTest extends TestCase
{
public function test_properties_can_be_accessed(): void
{
$message = new FakeMessage('some message');
$child = FakeNode::any();
$node = new Node(
true,
'nodeName',
'some.node.path',
'string',
true,
'some source value',
'some value',
[$message],
[$child]
);
self::assertSame(true, $node->isRoot());
self::assertSame('nodeName', $node->name());
self::assertSame('some.node.path', $node->path());
self::assertSame('string', $node->type());
self::assertSame('some source value', $node->sourceValue());
self::assertSame('some value', $node->value());
self::assertSame('some value', $node->mappedValue());
self::assertSame(true, $node->isValid());
self::assertSame('some message', (string)$node->messages()[0]);
self::assertSame([$child], $node->children());
}
public function test_error_message_makes_node_not_valid(): void
{
$message = new FakeErrorMessage();
$node = new Node(
true,
'nodeName',
'some.node.path',
'string',
true,
'some source value',
'some value',
[$message],
[]
);
self::assertSame(false, $node->isValid());
}
public function test_get_source_value_not_filled_throws_exception(): void
{
$this->expectException(SourceValueWasNotFilled::class);
$this->expectExceptionCode(1657466107);
$this->expectExceptionMessage('Source was not filled at path `some.node.path`; use method `$node->sourceFilled()`.');
(new Node(
true,
'nodeName',
'some.node.path',
'string',
false,
null,
'some value',
[],
[]
))->sourceValue();
}
public function test_get_mapped_value_from_invalid_node_throws_exception(): void
{
$this->expectException(InvalidNodeHasNoMappedValue::class);
$this->expectExceptionCode(1657466305);
$this->expectExceptionMessage('Cannot get mapped value for invalid node at path `some.node.path`; use method `$node->isValid()`.');
(new Node(
true,
'nodeName',
'some.node.path',
'string',
true,
'some source value',
null,
[new FakeErrorMessage()],
[]
))->mappedValue();
}
}