Valinor/tests/Unit/Mapper/Source/JsonSourceTest.php
Romain Canon 05cf4a4a4d feat: improve mapping error messages
Enhances most of the messages for the end users.

Two major changes can be noticed:

1. In most cases no class name will be written in the message; it
   prevents users that potentially have no access to the codebase to
   get a useless/unclear information.

2. The input values are now properly formatted; for instance a string
   value will now be written directly instead of the type `string`;
   arrays are also handled with the array shape format, for instance:
   `array{foo: 'some string'}`.
2022-05-21 16:30:24 +02:00

41 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Unit\Mapper\Source;
use CuyZ\Valinor\Mapper\Source\Exception\InvalidJson;
use CuyZ\Valinor\Mapper\Source\Exception\SourceNotIterable;
use CuyZ\Valinor\Mapper\Source\JsonSource;
use PHPUnit\Framework\TestCase;
use function iterator_to_array;
final class JsonSourceTest extends TestCase
{
public function test_valid_json_is_parsed_correctly(): void
{
$source = new JsonSource('{"foo": "bar"}');
self::assertSame(['foo' => 'bar'], iterator_to_array($source));
}
public function test_invalid_json_throws_exception(): void
{
$this->expectException(InvalidJson::class);
$this->expectExceptionCode(1566307185);
$this->expectExceptionMessage('The given value is not a valid JSON entry.');
new JsonSource('@');
}
public function test_invalid_json_type_throws_exception(): void
{
$this->expectException(SourceNotIterable::class);
$this->expectExceptionCode(1566307291);
$this->expectExceptionMessage('Invalid source true, expected an iterable.');
new JsonSource('true');
}
}