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

This commit is contained in:
2025-12-29 19:55:38 +09:00
parent d37c49a15d
commit 3dc0029770
3 changed files with 74 additions and 56 deletions

View File

@@ -1,6 +1,12 @@
<?php <?php
namespace Site\Lib; namespace Site\Lib;
/**
* アサーション失敗用のカスタム例外
*/
class AssertionFailedException extends \Exception {
}
/** /**
* 軽量なユニットテストフレームワーク * 軽量なユニットテストフレームワーク
*/ */
@@ -61,8 +67,7 @@ class Tester {
} }
// サポートされていない場合は色を無効にする // サポートされていない場合は色を無効にする
if (PHP_SAPI !== 'cli' || strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' if (PHP_SAPI !== 'cli' || strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' && !getenv('ANSICON')) {
&& !getenv('ANSICON')) {
$this->colorOutput = false; $this->colorOutput = false;
} }
} }
@@ -486,19 +491,8 @@ class Tester {
$this->output($this->colorize('bold', "テスト結果の概要:")); $this->output($this->colorize('bold', "テスト結果の概要:"));
$this->output(" テスト総数: {$this->testCount}"); $this->output(" テスト総数: {$this->testCount}");
$this->output(" ".$this->colorize('green', "合格: {$this->passCount}")); $this->output(" ".$this->colorize('green', "合格: {$this->passCount}"));
if ($this->failCount > 0) {
$this->output(" ".$this->colorize('red', "失敗: {$this->failCount}")); $this->output(" ".$this->colorize('red', "失敗: {$this->failCount}"));
} else {
$this->output(" 失敗: 0");
}
if ($this->errorCount > 0) {
$this->output(" ".$this->colorize('yellow', "エラー: {$this->errorCount}")); $this->output(" ".$this->colorize('yellow', "エラー: {$this->errorCount}"));
} else {
$this->output(" エラー: 0");
}
$this->output(''); $this->output('');
// 失敗を書き出す // 失敗を書き出す
@@ -621,9 +615,3 @@ class Tester {
]; ];
} }
} }
/**
* アサーション失敗用のカスタム例外
*/
class AssertionFailedException extends \Exception {
}

View File

@@ -13,7 +13,6 @@ if (MYSQL_ENABLED) {
]); ]);
$test->describe('パケットのデバッグ', function($test): void { $test->describe('パケットのデバッグ', function($test): void {
try {
$db = new Mysql(); $db = new Mysql();
$db->setDebug(true); $db->setDebug(true);
@@ -27,13 +26,9 @@ if (MYSQL_ENABLED) {
$db->savePacketLogToFile('mysql_log.txt'); $db->savePacketLogToFile('mysql_log.txt');
$db->close(); $db->close();
} catch (\Exception $e) {
echo 'エラー: '.$e->getMessage()."\n";
}
}); });
$test->describe('プリペアドステートメント', function($test): void { $test->describe('プリペアドステートメント', function($test): void {
try {
$db = new Mysql(); $db = new Mysql();
$db->connect(); $db->connect();
@@ -59,9 +54,6 @@ if (MYSQL_ENABLED) {
$this->assertTrue($close); $this->assertTrue($close);
$db->close(); $db->close();
} catch (\Exception $e) {
echo 'エラー: '.$e->getMessage()."\n";
}
}); });
$test->printSummary(); $test->printSummary();

View File

@@ -0,0 +1,38 @@
<?php
namespace Site\Test;
require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester;
use Site\Lib\Route;
$test = new Tester([
'colorOutput' => true,
'verboseOutput' => true
]);
$test->describe('ルート', function($test): void {
$test->it('ルートに追加出来るはず', function($test): void {
class Webpage {
public function about(array $params): void {}
}
$routes = [
Route::add('GET', 'about', Webpage::class.'@about'),
];
$expect = [
[
'method' => 'GET',
'path' => 'about',
'class' => 'Site\Test\Webpage@about',
'params' => [],
]
];
$test->assertNotNull($routes);
$test->assertEquals($expect, $routes);
});
});
$test->printSummary();