forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONFormatterTest.php
More file actions
65 lines (52 loc) · 1.85 KB
/
JSONFormatterTest.php
File metadata and controls
65 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Format;
use CodeIgniter\Format\Exceptions\FormatException;
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class JSONFormatterTest extends CIUnitTestCase
{
private JSONFormatter $jsonFormatter;
protected function setUp(): void
{
parent::setUp();
$this->jsonFormatter = new JSONFormatter();
}
/**
* @param array<string, string> $data
*/
#[DataProvider('provideFormattingToJson')]
public function testFormattingToJson(array $data, string $expected): void
{
$this->assertSame($expected, $this->jsonFormatter->format($data));
}
/**
* @return iterable<string, array{0: array<string, string>, 1: string}>
*/
public static function provideFormattingToJson(): iterable
{
yield 'empty array' => [[], '[]'];
yield 'simple array' => [['foo' => 'bar'], "{\n \"foo\": \"bar\"\n}"];
yield 'unicode array' => [['foo' => 'База данни грешка'], "{\n \"foo\": \"База данни грешка\"\n}"];
yield 'url array' => [['foo' => 'https://www.example.com/foo/bar'], "{\n \"foo\": \"https://www.example.com/foo/bar\"\n}"];
}
public function testJSONFormatterThrowsError(): void
{
$this->expectException(FormatException::class);
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
$this->assertSame('Boom', $this->jsonFormatter->format(["\xB1\x31"]));
}
}