mirror of
https://github.com/danog/Valinor.git
synced 2024-12-03 10:07:48 +01:00
a97b406154
Will recursively flatten messages of a node and all its children. This helper can for instance be used when errors occurred during a mapping to flatten all caught errors into a basic array of string that can then easily be used to inform the user of what is wrong. ``` try { // … } catch(MappingError $error) { $messages = (new MessagesFlattener($error->node()))->errors(); foreach ($messages as $message) { echo $message; } } ```
32 lines
1.0 KiB
PHP
32 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree\Message;
|
|
|
|
use CuyZ\Valinor\Mapper\Tree\Message\MessagesFlattener;
|
|
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 PHPUnit\Framework\TestCase;
|
|
|
|
final class MessagesFlattenerTest extends TestCase
|
|
{
|
|
public function test_messages_are_filtered_and_can_be_iterated_through(): void
|
|
{
|
|
$messageA = new FakeMessage();
|
|
$errorA = new FakeErrorMessage('some error message A');
|
|
$errorB = new FakeErrorMessage('some error message B');
|
|
|
|
$node = FakeNode::branch([
|
|
'foo' => ['message' => $messageA],
|
|
'bar' => ['message' => $errorA],
|
|
])->withMessage($errorB);
|
|
|
|
$messages = [...(new MessagesFlattener($node))->errors()];
|
|
|
|
self::assertSame('some error message B', (string)$messages[0]);
|
|
self::assertSame('some error message A', (string)$messages[1]);
|
|
}
|
|
}
|