ルートのユニットテスト、テスターの修正

諏訪子 <suwako@076.moe>、2025-12-29 19:55
コミット 3dc00297701a1d701b41f3d03a518633bace0bfb ・親 d37c49a ・ファイルを見る

3個のファイル、+74 -56

@@ -2,6 +2,12 @@
22 namespace Site\Lib;
33
44 /**
5+ * アサーション失敗用のカスタム例外
6+ */
7+class AssertionFailedException extends \Exception {
8+}
9+
10+/**
511 * 軽量なユニットテストフレームワーク
612 */
713 class Tester {
@@ -61,8 +67,7 @@
6167 }
6268
6369 // サポートされていない場合は色を無効にする
64- if (PHP_SAPI !== 'cli' || strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
65- && !getenv('ANSICON')) {
70+ if (PHP_SAPI !== 'cli' || strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' && !getenv('ANSICON')) {
6671 $this->colorOutput = false;
6772 }
6873 }
@@ -486,19 +491,8 @@
486491 $this->output($this->colorize('bold', "テスト結果の概要:"));
487492 $this->output(" テスト総数: {$this->testCount}");
488493 $this->output(" ".$this->colorize('green', "合格: {$this->passCount}"));
489-
490- if ($this->failCount > 0) {
491- $this->output(" ".$this->colorize('red', "失敗: {$this->failCount}"));
492- } else {
493- $this->output(" 失敗: 0");
494- }
495-
496- if ($this->errorCount > 0) {
497- $this->output(" ".$this->colorize('yellow', "エラー: {$this->errorCount}"));
498- } else {
499- $this->output(" エラー: 0");
500- }
501-
494+ $this->output(" ".$this->colorize('red', "失敗: {$this->failCount}"));
495+ $this->output(" ".$this->colorize('yellow', "エラー: {$this->errorCount}"));
502496 $this->output('');
503497
504498 // 失敗を書き出す
@@ -620,10 +614,4 @@
620614 'trace' => $trace,
621615 ];
622616 }
623-}
624-
625-/**
626- * アサーション失敗用のカスタム例外
627- */
628-class AssertionFailedException extends \Exception {
629617 }
@@ -13,55 +13,47 @@
1313 ]);
1414
1515 $test->describe('パケットのデバッグ', function($test): void {
16- try {
17- $db = new Mysql();
16+ $db = new Mysql();
1817
19- $db->setDebug(true);
20- $db->connect();
18+ $db->setDebug(true);
19+ $db->connect();
2120
22- $result = $db->query('SELECT * FROM user WHERE id = 1');
21+ $result = $db->query('SELECT * FROM user WHERE id = 1');
2322
24- foreach ($result['rows'] as $row) {
25- echo "ユーザー名: ".$row['nickname']."\n";
26- }
27-
28- $db->savePacketLogToFile('mysql_log.txt');
29- $db->close();
30- } catch (\Exception $e) {
31- echo 'エラー: '.$e->getMessage()."\n";
23+ foreach ($result['rows'] as $row) {
24+ echo "ユーザー名: ".$row['nickname']."\n";
3225 }
26+
27+ $db->savePacketLogToFile('mysql_log.txt');
28+ $db->close();
3329 });
3430
3531 $test->describe('プリペアドステートメント', function($test): void {
36- try {
37- $db = new Mysql();
38- $db->connect();
32+ $db = new Mysql();
33+ $db->connect();
3934
40- // データの入り
41- $stmt = $db->prepare('INSERT INTO users (name, age) VALUES (?, ?)');
42- $test->assertTrue($stmt);
35+ // データの入り
36+ $stmt = $db->prepare('INSERT INTO users (name, age) VALUES (?, ?)');
37+ $test->assertTrue($stmt);
4338
44- $db->execute($stmt, ['山田太郎', 25]);
45- // TODO: assert
39+ $db->execute($stmt, ['山田太郎', 25]);
40+ // TODO: assert
4641
47- $close = $db->demolish($stmt);
48- $this->assertTrue($close);
42+ $close = $db->demolish($stmt);
43+ $this->assertTrue($close);
4944
50- // データの受け取り
51- $stmt = $db->prepare('SELECT * FROM users WHERE age > ?');
52- $test->assertTrue($stmt);
45+ // データの受け取り
46+ $stmt = $db->prepare('SELECT * FROM users WHERE age > ?');
47+ $test->assertTrue($stmt);
5348
54- $res = $db->execute($stmt, [20]);
55- // TODO: assert
56- print_r($res);
49+ $res = $db->execute($stmt, [20]);
50+ // TODO: assert
51+ print_r($res);
5752
58- $close = $db->demolish($stmt);
59- $this->assertTrue($close);
53+ $close = $db->demolish($stmt);
54+ $this->assertTrue($close);
6055
61- $db->close();
62- } catch (\Exception $e) {
63- echo 'エラー: '.$e->getMessage()."\n";
64- }
56+ $db->close();
6557 });
6658
6759 $test->printSummary();
@@ -0,0 +1,38 @@
1+<?php
2+namespace Site\Test;
3+
4+require_once __DIR__.'/../../../autoload.php';
5+
6+use Site\Lib\Tester;
7+use Site\Lib\Route;
8+
9+$test = new Tester([
10+ 'colorOutput' => true,
11+ 'verboseOutput' => true
12+]);
13+
14+$test->describe('ルート', function($test): void {
15+ $test->it('ルートに追加出来るはず', function($test): void {
16+ class Webpage {
17+ public function about(array $params): void {}
18+ }
19+
20+ $routes = [
21+ Route::add('GET', 'about', Webpage::class.'@about'),
22+ ];
23+
24+ $expect = [
25+ [
26+ 'method' => 'GET',
27+ 'path' => 'about',
28+ 'class' => 'Site\Test\Webpage@about',
29+ 'params' => [],
30+ ]
31+ ];
32+
33+ $test->assertNotNull($routes);
34+ $test->assertEquals($expect, $routes);
35+ });
36+});
37+
38+$test->printSummary();