ルートのユニットテスト、テスターの修正
諏訪子 <suwako@076.moe>、2025-12-29 19:55
コミット 3dc00297701a1d701b41f3d03a518633bace0bfb
・親 d37c49a
・ファイルを見る
3個のファイル、+74 -56
| @@ -2,6 +2,12 @@ | ||
| 2 | 2 | namespace Site\Lib; |
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | + * アサーション失敗用のカスタム例外 | |
| 6 | + */ | |
| 7 | +class AssertionFailedException extends \Exception { | |
| 8 | +} | |
| 9 | + | |
| 10 | +/** | |
| 5 | 11 | * 軽量なユニットテストフレームワーク |
| 6 | 12 | */ |
| 7 | 13 | class Tester { |
| @@ -61,8 +67,7 @@ | ||
| 61 | 67 | } |
| 62 | 68 | |
| 63 | 69 | // サポートされていない場合は色を無効にする |
| 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')) { | |
| 66 | 71 | $this->colorOutput = false; |
| 67 | 72 | } |
| 68 | 73 | } |
| @@ -486,19 +491,8 @@ | ||
| 486 | 491 | $this->output($this->colorize('bold', "テスト結果の概要:")); |
| 487 | 492 | $this->output(" テスト総数: {$this->testCount}"); |
| 488 | 493 | $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}")); | |
| 502 | 496 | $this->output(''); |
| 503 | 497 | |
| 504 | 498 | // 失敗を書き出す |
| @@ -620,10 +614,4 @@ | ||
| 620 | 614 | 'trace' => $trace, |
| 621 | 615 | ]; |
| 622 | 616 | } |
| 623 | -} | |
| 624 | - | |
| 625 | -/** | |
| 626 | - * アサーション失敗用のカスタム例外 | |
| 627 | - */ | |
| 628 | -class AssertionFailedException extends \Exception { | |
| 629 | 617 | } |
| @@ -13,55 +13,47 @@ | ||
| 13 | 13 | ]); |
| 14 | 14 | |
| 15 | 15 | $test->describe('パケットのデバッグ', function($test): void { |
| 16 | - try { | |
| 17 | - $db = new Mysql(); | |
| 16 | + $db = new Mysql(); | |
| 18 | 17 | |
| 19 | - $db->setDebug(true); | |
| 20 | - $db->connect(); | |
| 18 | + $db->setDebug(true); | |
| 19 | + $db->connect(); | |
| 21 | 20 | |
| 22 | - $result = $db->query('SELECT * FROM user WHERE id = 1'); | |
| 21 | + $result = $db->query('SELECT * FROM user WHERE id = 1'); | |
| 23 | 22 | |
| 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"; | |
| 32 | 25 | } |
| 26 | + | |
| 27 | + $db->savePacketLogToFile('mysql_log.txt'); | |
| 28 | + $db->close(); | |
| 33 | 29 | }); |
| 34 | 30 | |
| 35 | 31 | $test->describe('プリペアドステートメント', function($test): void { |
| 36 | - try { | |
| 37 | - $db = new Mysql(); | |
| 38 | - $db->connect(); | |
| 32 | + $db = new Mysql(); | |
| 33 | + $db->connect(); | |
| 39 | 34 | |
| 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); | |
| 43 | 38 | |
| 44 | - $db->execute($stmt, ['山田太郎', 25]); | |
| 45 | - // TODO: assert | |
| 39 | + $db->execute($stmt, ['山田太郎', 25]); | |
| 40 | + // TODO: assert | |
| 46 | 41 | |
| 47 | - $close = $db->demolish($stmt); | |
| 48 | - $this->assertTrue($close); | |
| 42 | + $close = $db->demolish($stmt); | |
| 43 | + $this->assertTrue($close); | |
| 49 | 44 | |
| 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); | |
| 53 | 48 | |
| 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); | |
| 57 | 52 | |
| 58 | - $close = $db->demolish($stmt); | |
| 59 | - $this->assertTrue($close); | |
| 53 | + $close = $db->demolish($stmt); | |
| 54 | + $this->assertTrue($close); | |
| 60 | 55 | |
| 61 | - $db->close(); | |
| 62 | - } catch (\Exception $e) { | |
| 63 | - echo 'エラー: '.$e->getMessage()."\n"; | |
| 64 | - } | |
| 56 | + $db->close(); | |
| 65 | 57 | }); |
| 66 | 58 | |
| 67 | 59 | $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(); |