ユニットテストの修正
守矢諏訪子 <suwako@076.moe>、2026-11-11 10:07
コミット 785d2625a6649992afde7f0e5d868d670299542d
・親 a68c177
・ファイルを見る
6個のファイル、+39 -7
変更
README-JP.md
| @@ -236,4 +236,7 @@ | ||
| 236 | 236 | 端末を失くした場合は、管理者がそのユーザーの設定画面(`/profile/ユーザー名/2fa`)で自分のパスワードを入力すると無効に出来ます。 |
| 237 | 237 | |
| 238 | 238 | ## 必要な PHP モジュール |
| 239 | -* php_gmp | |
| 239 | +* php_fileinfo(OpenBSDでデフォルトで有効している) | |
| 240 | +* php_gmp | |
| 241 | +* php_mbstring(OpenBSDでデフォルトで有効している) | |
| 242 | +* php_openssl(OpenBSDでデフォルトで有効している) |
| @@ -588,6 +588,15 @@ | ||
| 588 | 588 | } |
| 589 | 589 | |
| 590 | 590 | /** |
| 591 | + * 失敗もエラーも無かったか(tester.phpがファイル毎の結果に使う) | |
| 592 | + * | |
| 593 | + * @return bool | |
| 594 | + */ | |
| 595 | + public function passed(): bool { | |
| 596 | + return $this->failCount === 0 && $this->errorCount === 0; | |
| 597 | + } | |
| 598 | + | |
| 599 | + /** | |
| 591 | 600 | * 有効な場合はテキストに色を適用する |
| 592 | 601 | * |
| 593 | 602 | * @param string $color 色名 |
| @@ -81,6 +81,9 @@ | ||
| 81 | 81 | $domain = substr($finger['links'][0]['href'] ?? '', 0, -strlen('/ap/actor')); |
| 82 | 82 | $feature = 'a8c04518-4181-4ec6-9ef0-3f88f23b84b6'; // /blog/feature-test |
| 83 | 83 | $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)が無い'; | |
| 84 | 87 | $remoteActor = 'https://1.1.1.1/users/alice'; |
| 85 | 88 | |
| 86 | 89 | $test->it('WebFingerを受け取れるはず', function($test) use ($finger, $domain): void { |
| @@ -104,7 +107,7 @@ | ||
| 104 | 107 | $test->assertEquals("{$domain}/ap/actor#main-key", $json['publicKey']['id']); |
| 105 | 108 | }); |
| 106 | 109 | |
| 107 | - $test->it('公開投稿のアクティビティを受け取れるはず', function($test) use ($make, $posts, $feature, $domain): void { | |
| 110 | + if ($sample) $test->it('公開投稿のアクティビティを受け取れるはず', function($test) use ($make, $posts, $feature, $domain): void { | |
| 108 | 111 | $json = json_decode($make($posts)->getActivity($feature), true); |
| 109 | 112 | $test->assertEquals('Create', $json['type']); |
| 110 | 113 | $test->assertEquals("{$domain}/ap/activities/create/{$feature}", $json['id']); |
| @@ -123,6 +126,7 @@ | ||
| 123 | 126 | $test->assertEquals('image/jpeg', $json['object']['attachment'][0]['mediaType']); |
| 124 | 127 | } |
| 125 | 128 | }); |
| 129 | + else $test->skip('公開投稿のアクティビティを受け取れるはず', $noSample); | |
| 126 | 130 | |
| 127 | 131 | $test->it('会員限定・存在しない投稿は返さないはず', function($test) use ($make, $posts, $member): void { |
| 128 | 132 | $ap = $make($posts); |
| @@ -132,12 +136,13 @@ | ||
| 132 | 136 | $test->assertEquals('', $ap->getObject($member)); |
| 133 | 137 | }); |
| 134 | 138 | |
| 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 { | |
| 136 | 140 | $json = json_decode($make($posts)->getObject($feature), true); |
| 137 | 141 | $test->assertEquals('Note', $json['type']); |
| 138 | 142 | $test->assertEquals("{$domain}/ap/objects/{$feature}", $json['id']); |
| 139 | 143 | $test->assertTrue(isset($json['@context'])); |
| 140 | 144 | }); |
| 145 | + else $test->skip('オブジェクト(Note)を受け取れるはず', $noSample); | |
| 141 | 146 | |
| 142 | 147 | $test->it('アウトボックスは公開投稿だけを新しい順に返すはず', function($test) use ($make, $posts, $member): void { |
| 143 | 148 | $json = json_decode($make($posts)->getOutbox(), true); |
| @@ -244,10 +244,13 @@ | ||
| 244 | 244 | }); |
| 245 | 245 | |
| 246 | 246 | $test->describe('マルチユーザーのFediverse(2つのサーバー)', function($test) use ($use, $mkuser, $seed, $pump, $tmp, $alice, $bob, $carol): void { |
| 247 | + $openssl = extension_loaded('openssl'); | |
| 247 | 248 | if (!ACTIVITYPUB_MULTIUSER) { |
| 248 | 249 | $test->skipAll('マルチユーザーモードが無効(FEDIINFOのisSingleUserがtrue、又はAUTH_ENABLEDが無効)'); |
| 250 | + } else if (!$openssl) { | |
| 251 | + $test->skipAll('openssl拡張が無効(php.iniの「extension=openssl」)'); | |
| 249 | 252 | } |
| 250 | - if (ACTIVITYPUB_MULTIUSER) { | |
| 253 | + if (ACTIVITYPUB_MULTIUSER && $openssl) { | |
| 251 | 254 | $mkuser('A', 'alice', 1); |
| 252 | 255 | $mkuser('A', 'carol', 2); |
| 253 | 256 | $mkuser('A', 'dave', 3, 0); // BANされた |
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | function rmfile(): void { |
| 65 | - unlink(ROOT."/blog/dietrannynigger.md"); | |
| 65 | + if (is_file(ROOT."/blog/dietrannynigger.md")) unlink(ROOT."/blog/dietrannynigger.md"); | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | class Constants { |
| @@ -71,6 +71,12 @@ | ||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $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 | + | |
| 74 | 80 | $test->it('メタデータをパーシング出来るはず', function($test): void { |
| 75 | 81 | $md = new Markdown('feature-test', '/blog/'); |
| 76 | 82 | $test->assertNotNull($md); |
変更
tester.php
| @@ -69,6 +69,7 @@ | ||
| 69 | 69 | |
| 70 | 70 | try { |
| 71 | 71 | ob_start(); |
| 72 | + unset($test); | |
| 72 | 73 | require $testFile; |
| 73 | 74 | $output = ob_get_clean(); |
| 74 | 75 | |
| @@ -76,7 +77,11 @@ | ||
| 76 | 77 | echo $output; |
| 77 | 78 | echo "\n"; |
| 78 | 79 | |
| 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 | + } | |
| 80 | 85 | } catch (\Throwable $e) { |
| 81 | 86 | $output = ob_get_clean(); |
| 82 | 87 | if (!empty($output)) { |
| @@ -102,6 +107,7 @@ | ||
| 102 | 107 | if (!empty($failedFiles)) { |
| 103 | 108 | echo "\n失敗したテストファイル:\n"; |
| 104 | 109 | 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"; | |
| 106 | 111 | } |
| 112 | + exit(1); | |
| 107 | 113 | } |