ユニットテストの修正

守矢諏訪子 <suwako@076.moe>、2026-11-11 10:07
コミット 785d2625a6649992afde7f0e5d868d670299542d ・親 a68c177 ・ファイルを見る

6個のファイル、+39 -7

変更 README-JP.md
@@ -236,4 +236,7 @@
236236 端末を失くした場合は、管理者がそのユーザーの設定画面(`/profile/ユーザー名/2fa`)で自分のパスワードを入力すると無効に出来ます。
237237
238238 ## 必要な PHP モジュール
239-* php_gmp
239+* php_fileinfo(OpenBSDでデフォルトで有効している)
240+* php_gmp
241+* php_mbstring(OpenBSDでデフォルトで有効している)
242+* php_openssl(OpenBSDでデフォルトで有効している)
@@ -588,6 +588,15 @@
588588 }
589589
590590 /**
591+ * 失敗もエラーも無かったか(tester.phpがファイル毎の結果に使う)
592+ *
593+ * @return bool
594+ */
595+ public function passed(): bool {
596+ return $this->failCount === 0 && $this->errorCount === 0;
597+ }
598+
599+ /**
591600 * 有効な場合はテキストに色を適用する
592601 *
593602 * @param string $color 色名
@@ -81,6 +81,9 @@
8181 $domain = substr($finger['links'][0]['href'] ?? '', 0, -strlen('/ap/actor'));
8282 $feature = 'a8c04518-4181-4ec6-9ef0-3f88f23b84b6'; // /blog/feature-test
8383 $member = 'd49f60c7-6d85-4263-9d3f-c20025f2fa0d'; // /blog/memberonly-post
84+ // サンプルの投稿はLittleBeastのblog/にだけある(サイトでは公開されてしまうので置かない)
85+ $sample = is_file(ROOT.'/blog/feature-test.md');
86+ $noSample = 'サンプルの投稿(blog/feature-test.md)が無い';
8487 $remoteActor = 'https://1.1.1.1/users/alice';
8588
8689 $test->it('WebFingerを受け取れるはず', function($test) use ($finger, $domain): void {
@@ -104,7 +107,7 @@
104107 $test->assertEquals("{$domain}/ap/actor#main-key", $json['publicKey']['id']);
105108 });
106109
107- $test->it('公開投稿のアクティビティを受け取れるはず', function($test) use ($make, $posts, $feature, $domain): void {
110+ if ($sample) $test->it('公開投稿のアクティビティを受け取れるはず', function($test) use ($make, $posts, $feature, $domain): void {
108111 $json = json_decode($make($posts)->getActivity($feature), true);
109112 $test->assertEquals('Create', $json['type']);
110113 $test->assertEquals("{$domain}/ap/activities/create/{$feature}", $json['id']);
@@ -123,6 +126,7 @@
123126 $test->assertEquals('image/jpeg', $json['object']['attachment'][0]['mediaType']);
124127 }
125128 });
129+ else $test->skip('公開投稿のアクティビティを受け取れるはず', $noSample);
126130
127131 $test->it('会員限定・存在しない投稿は返さないはず', function($test) use ($make, $posts, $member): void {
128132 $ap = $make($posts);
@@ -132,12 +136,13 @@
132136 $test->assertEquals('', $ap->getObject($member));
133137 });
134138
135- $test->it('オブジェクト(Note)を受け取れるはず', function($test) use ($make, $posts, $feature, $domain): void {
139+ if ($sample) $test->it('オブジェクト(Note)を受け取れるはず', function($test) use ($make, $posts, $feature, $domain): void {
136140 $json = json_decode($make($posts)->getObject($feature), true);
137141 $test->assertEquals('Note', $json['type']);
138142 $test->assertEquals("{$domain}/ap/objects/{$feature}", $json['id']);
139143 $test->assertTrue(isset($json['@context']));
140144 });
145+ else $test->skip('オブジェクト(Note)を受け取れるはず', $noSample);
141146
142147 $test->it('アウトボックスは公開投稿だけを新しい順に返すはず', function($test) use ($make, $posts, $member): void {
143148 $json = json_decode($make($posts)->getOutbox(), true);
@@ -244,10 +244,13 @@
244244 });
245245
246246 $test->describe('マルチユーザーのFediverse(2つのサーバー)', function($test) use ($use, $mkuser, $seed, $pump, $tmp, $alice, $bob, $carol): void {
247+ $openssl = extension_loaded('openssl');
247248 if (!ACTIVITYPUB_MULTIUSER) {
248249 $test->skipAll('マルチユーザーモードが無効(FEDIINFOのisSingleUserがtrue、又はAUTH_ENABLEDが無効)');
250+ } else if (!$openssl) {
251+ $test->skipAll('openssl拡張が無効(php.iniの「extension=openssl」)');
249252 }
250- if (ACTIVITYPUB_MULTIUSER) {
253+ if (ACTIVITYPUB_MULTIUSER && $openssl) {
251254 $mkuser('A', 'alice', 1);
252255 $mkuser('A', 'carol', 2);
253256 $mkuser('A', 'dave', 3, 0); // BANされた
@@ -62,7 +62,7 @@
6262 }
6363
6464 function rmfile(): void {
65- unlink(ROOT."/blog/dietrannynigger.md");
65+ if (is_file(ROOT."/blog/dietrannynigger.md")) unlink(ROOT."/blog/dietrannynigger.md");
6666 }
6767
6868 class Constants {
@@ -71,6 +71,12 @@
7171 }
7272
7373 $test->describe('マークダウン', function($test): void {
74+ // サンプルの投稿が無いと、Markdownは404にリダイレクトしてexitするので、テスト全体が止まる。
75+ // 一時的な投稿もblog/に書くので、サイト(本番)では動かさない
76+ if (!is_file(ROOT.'/blog/feature-test.md')) {
77+ $test->skipAll('サンプルの投稿(blog/feature-test.md)が無い');
78+ }
79+
7480 $test->it('メタデータをパーシング出来るはず', function($test): void {
7581 $md = new Markdown('feature-test', '/blog/');
7682 $test->assertNotNull($md);
変更 tester.php
@@ -69,6 +69,7 @@
6969
7070 try {
7171 ob_start();
72+ unset($test);
7273 require $testFile;
7374 $output = ob_get_clean();
7475
@@ -76,7 +77,11 @@
7677 echo $output;
7778 echo "\n";
7879
79- $successFiles++;
80+ if (isset($test) && $test instanceof \Std\Lib\Tester && !$test->passed()) {
81+ $failedFiles[] = ['file' => $filename, 'error' => 'テストが失敗・エラーで完了しました。', 'line' => null];
82+ } else {
83+ $successFiles++;
84+ }
8085 } catch (\Throwable $e) {
8186 $output = ob_get_clean();
8287 if (!empty($output)) {
@@ -102,6 +107,7 @@
102107 if (!empty($failedFiles)) {
103108 echo "\n失敗したテストファイル:\n";
104109 foreach ($failedFiles as $failed) {
105- echo "- {$failed['file']} (行: {$failed['line']}): {$failed['error']}\n";
110+ echo "- {$failed['file']}".($failed['line'] !== null ? " (行: {$failed['line']})" : '').": {$failed['error']}\n";
106111 }
112+ exit(1);
107113 }