Valinor/tests/Unit/Utility/ValueDumperTest.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

62 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Unit\Utility;
use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum;
use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum;
use CuyZ\Valinor\Tests\Fixture\Enum\PureEnum;
use CuyZ\Valinor\Utility\ValueDumper;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use stdClass;
final class ValueDumperTest extends TestCase
{
/**
* @dataProvider dump_value_returns_correct_signature_data_provider
*
* @param mixed $value
*/
public function test_dump_value_returns_correct_signature($value, string $expected): void
{
self::assertSame($expected, ValueDumper::dump($value));
}
public function dump_value_returns_correct_signature_data_provider(): array
{
return [
'null' => [null, 'null'],
'boolean true' => [true, 'true'],
'boolean false' => [false, 'false'],
'integer' => [42, '42'],
'float' => [1337.404, '1337.404'],
'string with single quote' => ['foo', "'foo'"],
'string with double quote' => ["foo'bar", '"foo\'bar"'],
'string with both quotes' => ['"foo\'bar"', '\'"foo\\\'bar"\''],
'string with exact max length' => ['Lorem ipsum dolor sit amet, consectetur adipiscing', "'Lorem ipsum dolor sit amet, consectetur adipiscing'"],
'string cropped' => ['Lorem ipsum dolor sit amet, consectetur adipiscing elit.', "'Lorem ipsum dolor sit amet, consectetur adipiscing…'"],
'string cropped only after threshold' => ['Lorem12345 ipsumdolorsitamet,consecteturadipiscingelit.Curabitur', "'Lorem12345 ipsumdolorsitamet,consecteturadipiscinge…'"],
'string without space cropped' => ['Loremipsumdolorsitamet,consecteturadipiscingelit.Curabitur',"'Loremipsumdolorsitamet,consecteturadipiscingelit.Cu…'"],
'date' => [new DateTimeImmutable('@1648733888'), '2022/03/31 13:38:08'],
'object' => [new stdClass(), 'object(stdClass)'],
'array' => [['foo' => 'bar', 'baz'], "array{foo: 'bar', 0: 'baz'}"],
'array with in-depth entries' => [['foo' => ['bar' => 'baz']], "array{foo: array{…}}"],
'array with too much entries' => [[0, 1, 2, 3, 4, 5, 6, 7], "array{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, …}"],
];
}
/**
* @PHP8.1 move to data provider
*
* @requires PHP >= 8.1
*/
public function test_dump_enum_value_returns_correct_signature(): void
{
self::assertSame('FOO', ValueDumper::dump(PureEnum::FOO));
self::assertSame('foo', ValueDumper::dump(BackedStringEnum::FOO));
self::assertSame('42', ValueDumper::dump(BackedIntegerEnum::FOO));
}
}