endtoend-test-psl/tests/Psl/Json/EncodeTest.php

81 lines
1.8 KiB
PHP
Raw Normal View History

2020-08-08 06:09:49 +02:00
<?php
declare(strict_types=1);
namespace Psl\Json;
use PHPUnit\Framework\TestCase;
use Psl\Json;
use Psl\Math;
use Psl\Str;
2020-08-08 06:09:49 +02:00
use const PHP_EOL;
2020-10-07 08:30:37 +02:00
final class EncodeTest extends TestCase
2020-08-08 06:09:49 +02:00
{
public function testEncode(): void
{
$actual = Json\encode(['a']);
static::assertSame('["a"]', $actual);
2020-08-08 06:09:49 +02:00
}
public function testPrettyEncode(): void
{
$actual = Json\encode([
'name' => 'azjezz/psl',
'type' => 'library',
'description' => 'PHP Standard Library.',
'keywords' => ['php', 'std', 'stdlib', 'utility', 'psl'],
'license' => 'MIT'
], true);
$json = Str\replace(<<<JSON
2020-08-08 06:09:49 +02:00
{
"name": "azjezz/psl",
"type": "library",
"description": "PHP Standard Library.",
"keywords": [
"php",
"std",
"stdlib",
"utility",
"psl"
],
"license": "MIT"
}
2020-10-07 08:30:37 +02:00
JSON, PHP_EOL, "\n");
2020-08-08 06:09:49 +02:00
static::assertSame($json, $actual);
2020-08-08 06:09:49 +02:00
}
public function testEncodeThrowsForMalformedUTF8(): void
{
2020-09-11 02:24:28 +02:00
$this->expectException(Json\Exception\EncodeException::class);
2020-08-08 06:09:49 +02:00
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded.');
Json\encode(["bad utf\xFF"]);
}
public function testEncodeThrowsWithNAN(): void
{
2020-09-11 02:24:28 +02:00
$this->expectException(Json\Exception\EncodeException::class);
2020-08-08 06:09:49 +02:00
$this->expectExceptionMessage('Inf and NaN cannot be JSON encoded.');
Json\encode(Math\NAN);
2020-08-08 06:09:49 +02:00
}
public function testEncodeThrowsWithInf(): void
{
2020-09-11 02:24:28 +02:00
$this->expectException(Json\Exception\EncodeException::class);
2020-08-08 06:09:49 +02:00
$this->expectExceptionMessage('Inf and NaN cannot be JSON encoded.');
Json\encode(Math\INFINITY);
}
public function testEncodePreserveZeroFraction(): void
{
static::assertSame('1.0', Json\encode(1.0));
2020-08-08 06:09:49 +02:00
}
}