マークダウンのユニットテスト

諏訪子 <suwako@076.moe>、2025-12-29 21:42
コミット 66e1b39050a8924bdb80a31e977080050a4276be ・親 3dc0029 ・ファイルを見る

1個のファイル、+234 -0

@@ -0,0 +1,234 @@
1+<?php
2+namespace Site\Test;
3+
4+require_once __DIR__.'/../../../autoload.php';
5+
6+use Site\Lib\Tester;
7+use Site\Lib\Markdown;
8+
9+$test = new Tester([
10+ 'colorOutput' => true,
11+ 'verboseOutput' => true
12+]);
13+
14+function mkfile(string $body): void {
15+ $post = fopen(ROOT."/blog/dietrannynigger.md", "w");
16+ fwrite($post, "title: 【】\n");
17+ fwrite($post, "uuid: ".uuid()."\n");
18+ fwrite($post, "author: 諏訪子\n");
19+ fwrite($post, "date: ".date("Y-m-d H:i:s")."\n");
20+ fwrite($post, "thumbnail: \n");
21+ fwrite($post, "thumborient: center\n");
22+ fwrite($post, "category: \n");
23+ fwrite($post, "----\n");
24+ fwrite($post, $body);
25+ fclose($post);
26+}
27+
28+function rmfile(): void {
29+ unlink(ROOT."/blog/dietrannynigger.md");
30+}
31+
32+class Constants {
33+ public const START = "<p> ";
34+ public const END = " </p>";
35+}
36+
37+$test->describe('マークダウン', function($test): void {
38+
39+ $test->it('メタデータをパーシング出来るはず', function($test): void {
40+ $md = new Markdown('feature-test', '/blog/');
41+ $test->assertNotNull($md);
42+
43+ $meta = $md->getMetadata();
44+ $test->assertNotNull($meta);
45+
46+ $expect = new \stdClass;
47+ $expect->title = '【マークダウン】関数';
48+ $expect->uuid = 'a8c04518-4181-4ec6-9ef0-3f88f23b84b6';
49+ $expect->author = '諏訪子';
50+ $expect->date = '2025-11-07 20:33:42';
51+ $expect->thumbnail = 'o_53803618dc1691.28179609-orig.jpg';
52+ $expect->thumborient = 'center';
53+ $expect->category = ['test', 'おちんちん', 'でっかいおっぱい'];
54+ $expect->css = ['code', 'blockquote', 'table', 'blink'];
55+
56+ $test->assertEquals($expect, $meta);
57+ });
58+
59+ $test->it('横線はHTMLに交換出来るはず', function($test): void {
60+ mkfile('----');
61+
62+ $md = new Markdown('dietrannynigger', '/blog/');
63+ $test->assertNotNull($md);
64+
65+ $body = $md->parse();
66+ $test->assertNotNull($body);
67+
68+ $expect = "<hr />";
69+ $actual = str_replace("\n", '', trim($body));
70+
71+ $test->assertEquals($expect, $actual);
72+ });
73+
74+ $test->it('ヘッダーはHTMLに交換出来るはず', function($test): void {
75+ mkfile('# ケロケロ');
76+
77+ $md = new Markdown('dietrannynigger', '/blog/');
78+ $test->assertNotNull($md);
79+
80+ $body = $md->parse();
81+ $test->assertNotNull($body);
82+
83+ $expect = "<h1>ケロケロ</h1>";
84+ $actual = trim($body);
85+
86+ $test->assertEquals($expect, $actual);
87+
88+ // ---------------
89+
90+ mkfile('## ケロケロ');
91+
92+ $md = new Markdown('dietrannynigger', '/blog/');
93+ $test->assertNotNull($md);
94+
95+ $body = $md->parse();
96+ $test->assertNotNull($body);
97+
98+ $expect = "<h2>ケロケロ</h2>";
99+ $actual = trim($body);
100+
101+ $test->assertEquals($expect, $actual);
102+
103+ // ---------------
104+
105+ mkfile('### ケロケロ');
106+
107+ $md = new Markdown('dietrannynigger', '/blog/');
108+ $test->assertNotNull($md);
109+
110+ $body = $md->parse();
111+ $test->assertNotNull($body);
112+
113+ $expect = "<h3>ケロケロ</h3>";
114+ $actual = trim($body);
115+
116+ $test->assertEquals($expect, $actual);
117+
118+ // ---------------
119+
120+ mkfile('#### ケロケロ');
121+
122+ $md = new Markdown('dietrannynigger', '/blog/');
123+ $test->assertNotNull($md);
124+
125+ $body = $md->parse();
126+ $test->assertNotNull($body);
127+
128+ $expect = "<h4>ケロケロ</h4>";
129+ $actual = trim($body);
130+
131+ $test->assertEquals($expect, $actual);
132+
133+ // ---------------
134+
135+ mkfile('##### ケロケロ');
136+
137+ $md = new Markdown('dietrannynigger', '/blog/');
138+ $test->assertNotNull($md);
139+
140+ $body = $md->parse();
141+ $test->assertNotNull($body);
142+
143+ $expect = "<h5>ケロケロ</h5>";
144+ $actual = trim($body);
145+
146+ $test->assertEquals($expect, $actual);
147+
148+ // ---------------
149+
150+ mkfile('###### ケロケロ');
151+
152+ $md = new Markdown('dietrannynigger', '/blog/');
153+ $test->assertNotNull($md);
154+
155+ $body = $md->parse();
156+ $test->assertNotNull($body);
157+
158+ $expect = "<h6>ケロケロ</h6>";
159+ $actual = trim($body);
160+
161+ $test->assertEquals($expect, $actual);
162+ });
163+
164+ $test->skip('コメントはHTMLに交換出来るはず', '作成中・・・');
165+
166+ $test->it('画像、音楽、動画はHTMLに交換出来るはず', function($test): void {
167+ mkfile("![](https://ass.technicalsuwako.moe/banner.png)\n
168+![ケロケロ](https://ass.technicalsuwako.moe/banner.png)\n
169+![ケロケロ#max-width: 200px;](https://ass.technicalsuwako.moe/banner.png)\n
170+$[audio/ogg](https://ass.technicalsuwako.moe/砕月着信音.ogg)\n
171+#[video/mp4](https://ass.technicalsuwako.moe/フリーズ.mp4)");
172+
173+ $md = new Markdown('dietrannynigger', '/blog/');
174+ $test->assertNotNull($md);
175+
176+ $body = $md->parse();
177+ $test->assertNotNull($body);
178+
179+ $expect = '<p><img style="width: 100%; " src="https://ass.technicalsuwako.moe/banner.png" alt="" /> </p> '
180+ . '<p><img style="width: 100%; " src="https://ass.technicalsuwako.moe/banner.png" alt="ケロケロ" /> </p> '
181+ . '<p><img style="width: 100%; max-width: 200px;" src="https://ass.technicalsuwako.moe/banner.png" alt="ケロケロ" /> </p> '
182+ . '<p><audio controls><source src="https://ass.technicalsuwako.moe/砕月着信音.ogg" type="audio/ogg" /></audio> </p> '
183+ . '<p><video style="max-width: 100%;" controls><source src="https://ass.technicalsuwako.moe/フリーズ.mp4" type="video/mp4" /></video> </p>';
184+ $actual = str_replace("\n", '', str_replace(' ', '', trim($body)));
185+
186+ $test->assertEquals($expect, $actual);
187+ });
188+
189+ $test->it('基本的なマークダウンはHTMLに交換出来るはず', function($test): void {
190+ mkfile("**太文字**\n
191+*イタリック*\n
192+_下線_\n
193+~取消線~\n
194+!:(5)明滅文字:!\n
195+^(72)フォントサイズ^\n
196+%(ff00ff)フォントカラー%");
197+
198+ $md = new Markdown('dietrannynigger', '/blog/');
199+ $test->assertNotNull($md);
200+
201+ $body = $md->parse();
202+ $test->assertNotNull($body);
203+
204+ $expect = '<p> <strong>太文字</strong> </p> '
205+ . '<p> <em>イタリック</em> </p> '
206+ . '<p> <u>下線</u> </p> '
207+ . '<p> <s>取消線</s> </p> '
208+ . '<p> <span class="blink" style="animation-duration: 5s;">明滅文字</span> </p> '
209+ . '<p> <span style="font-size: 72px;">フォントサイズ</span> </p> '
210+ . '<p> <span style="color: #ff00ff;">フォントカラー</span> </p>';
211+ $actual = str_replace("\n", '', trim($body));
212+
213+ $test->assertEquals($expect, $actual);
214+ });
215+
216+ $test->it('複数マークダウンはHTMLに交換出来るはず', function($test): void {
217+ mkfile("[^(500)<God>(すわこ)](https://techsuwako.jp/)^");
218+
219+ $md = new Markdown('dietrannynigger', '/blog/');
220+ $test->assertNotNull($md);
221+
222+ $body = $md->parse();
223+ $test->assertNotNull($body);
224+
225+ $expect = Constants::START.'<a href="https://techsuwako.jp/"><span style="font-size: 500px;"><ruby>God<rp>(</rp><rt>すわこ</rt><rp>)</rp></ruby></a></span>'.Constants::END;
226+ $actual = str_replace("\n", '', trim($body));
227+
228+ $test->assertEquals($expect, $actual);
229+ });
230+
231+ rmfile();
232+});
233+
234+$test->printSummary();