-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
CodeTest.php
149 lines (129 loc) · 4.96 KB
/
CodeTest.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Notion\Test\Unit\Blocks;
use Notion\Blocks\BlockFactory;
use Notion\Blocks\Code;
use Notion\Common\Date;
use Notion\Common\RichText;
use PHPUnit\Framework\TestCase;
class CodeTest extends TestCase
{
public function test_create_empty_code_block(): void
{
$code = Code::create();
$this->assertEmpty($code->text());
$this->assertEquals(Code::LANG_PLAIN_TEXT, $code->language());
}
public function test_create_from_array(): void
{
$array = [
"object" => "block",
"id" => "04a13895-f072-4814-8af7-cd11af127040",
"created_time" => "2021-10-18T17:09:00.000Z",
"last_edited_time" => "2021-10-18T17:09:00.000Z",
"archived" => false,
"has_children" => false,
"type" => "code",
"code" => [
"text" => [
[
"plain_text" => "<?php\necho 'Hello World!';",
"href" => null,
"type" => "text",
"text" => [
"content" => "<?php\necho 'Hello World!';",
"link" => null,
],
"annotations" => [
"bold" => false,
"italic" => false,
"strikethrough" => false,
"underline" => false,
"code" => false,
"color" => "default",
],
],
],
"language" => "php",
],
];
$code = Code::fromArray($array);
$this->assertCount(1, $code->text());
$this->assertEquals("<?php\necho 'Hello World!';", $code->toString());
$this->assertEquals($code, BlockFactory::fromArray($array));
}
public function test_error_on_wrong_type(): void
{
$array = [
"object" => "block",
"id" => "04a13895-f072-4814-8af7-cd11af127040",
"created_time" => "2021-10-18T17:09:00.000Z",
"last_edited_time" => "2021-10-18T17:09:00.000Z",
"archived" => false,
"has_children" => false,
"type" => "wrong-type",
"code" => [
"language" => "php",
"text" => [],
],
];
$this->expectException(\Exception::class);
Code::fromArray($array);
}
public function test_transform_in_array(): void
{
$h = Code::create("<?php\necho 'Hello World!';", "php");
$expected = [
"object" => "block",
"created_time" => $h->block()->createdTime()->format(Date::FORMAT),
"last_edited_time" => $h->block()->lastEditedType()->format(Date::FORMAT),
"archived" => false,
"has_children" => false,
"type" => "code",
"code" => [
"language" => "php",
"text" => [[
"plain_text" => "<?php\necho 'Hello World!';",
"href" => null,
"type" => "text",
"text" => [
"content" => "<?php\necho 'Hello World!';",
"link" => null,
],
"annotations" => [
"bold" => false,
"italic" => false,
"strikethrough" => false,
"underline" => false,
"code" => false,
"color" => "default",
],
]],
],
];
$this->assertEquals($expected, $h->toArray());
}
public function test_replace_text(): void
{
$oldHeading = Code::create("This is an old code");
$newHeading = $oldHeading->withText(
RichText::createText("This is a "),
RichText::createText("new code"),
);
$this->assertEquals("This is an old code", $oldHeading->toString());
$this->assertEquals("This is a new code", $newHeading->toString());
}
public function test_append_text(): void
{
$oldHeading = Code::create("A code");
$newHeading = $oldHeading->appendText(
RichText::createText(" can be extended.")
);
$this->assertEquals("A code", $oldHeading->toString());
$this->assertEquals("A code can be extended.", $newHeading->toString());
}
public function test_change_language(): void
{
$code = Code::create("Simple code")->withLanguage("php");
$this->assertEquals("php", $code->language());
}
}