-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBlockJsonTest.php
More file actions
118 lines (88 loc) · 2.92 KB
/
BlockJsonTest.php
File metadata and controls
118 lines (88 loc) · 2.92 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
use HiFolks\DataType\Block;
use PHPUnit\Framework\TestCase;
final class BlockJsonTest extends TestCase
{
private array $fruitsArray = [
"avocado" => [
"name" => "Avocado",
"fruit" => "🥑",
"wikipedia" => "https://en.wikipedia.org/wiki/Avocado",
"color" => "green",
"rating" => 8,
],
"apple" => [
"name" => "Apple",
"fruit" => "🍎",
"wikipedia" => "https://en.wikipedia.org/wiki/Apple",
"color" => "red",
"rating" => 7,
],
"banana" => [
"name" => "Banana",
"fruit" => "🍌",
"wikipedia" => "https://en.wikipedia.org/wiki/Banana",
"color" => "yellow",
"rating" => 8.5,
],
"cherry" => [
"name" => "Cherry",
"fruit" => "🍒",
"wikipedia" => "https://en.wikipedia.org/wiki/Cherry",
"color" => "red",
"rating" => 9,
],
];
public function testToJson(): void
{
$data = Block::make($this->fruitsArray);
$this->assertIsString($data->toJson());
$this->assertSame(773, strlen($data->toJson()));
$string = $data->toJson();
$data1 = Block::fromJsonString($string);
$this->assertSame($data->get("0.fruit"), $data1->get("0.fruit"));
}
public function testToJsonWithDifferentIterateBlock(): void
{
$data1 = Block::make($this->fruitsArray, true);
$string1 = $data1->toJson();
$data2 = Block::make($this->fruitsArray, false);
$string2 = $data2->toJson();
$this->assertIsString($string1);
$this->assertIsString($string2);
$this->assertSame(773, strlen($string1));
$this->assertSame(773, strlen($string2));
$this->assertSame($string1, $string2);
}
public function testSaveToJson(): void
{
$data = Block::make($this->fruitsArray);
$data->saveToJson("fruits.json");
$this->assertFileExists("fruits.json");
unlink("fruits.json");
}
public function testSaveToJsonWithOverwrite(): void
{
$data = Block::make($this->fruitsArray);
$data->saveToJson("fruits.json");
$this->assertFileExists("fruits.json");
$result = $data->saveToJson("fruits.json", true);
$this->assertTrue($result);
unlink("fruits.json");
}
public function testSaveToJsonWithExistingFile(): void
{
$data = Block::make($this->fruitsArray);
$data->saveToJson("fruits.json");
$this->assertFileExists("fruits.json");
$result = $data->saveToJson("fruits.json");
$this->assertFalse($result);
unlink("fruits.json");
}
public function testEmptyJson(): void
{
$data = Block::fromJsonFile("file-not-exists");
$this->assertSame(0, $data->count());
}
}