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

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}"));
$this->output(" ".$this->colorize('red', "失敗: {$this->failCount}"));
if ($this->failCount > 0) { $this->output(" ".$this->colorize('yellow', "エラー: {$this->errorCount}"));
$this->output(" ".$this->colorize('red', "失敗: {$this->failCount}"));
} else {
$this->output(" 失敗: 0");
}
if ($this->errorCount > 0) {
$this->output(" ".$this->colorize('yellow', "エラー: {$this->errorCount}"));
} else {
$this->output(" エラー: 0");
}
$this->output(''); $this->output('');
// 失敗を書き出す // 失敗を書き出す
@@ -620,10 +614,4 @@ class Tester {
'trace' => $trace, 'trace' => $trace,
]; ];
} }
}
/**
* アサーション失敗用のカスタム例外
*/
class AssertionFailedException extends \Exception {
} }

View File

@@ -13,55 +13,47 @@ 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);
$db->connect(); $db->connect();
$result = $db->query('SELECT * FROM user WHERE id = 1'); $result = $db->query('SELECT * FROM user WHERE id = 1');
foreach ($result['rows'] as $row) { foreach ($result['rows'] as $row) {
echo "ユーザー名: ".$row['nickname']."\n"; echo "ユーザー名: ".$row['nickname']."\n";
}
$db->savePacketLogToFile('mysql_log.txt');
$db->close();
} catch (\Exception $e) {
echo 'エラー: '.$e->getMessage()."\n";
} }
$db->savePacketLogToFile('mysql_log.txt');
$db->close();
}); });
$test->describe('プリペアドステートメント', function($test): void { $test->describe('プリペアドステートメント', function($test): void {
try { $db = new Mysql();
$db = new Mysql(); $db->connect();
$db->connect();
// データの入り // データの入り
$stmt = $db->prepare('INSERT INTO users (name, age) VALUES (?, ?)'); $stmt = $db->prepare('INSERT INTO users (name, age) VALUES (?, ?)');
$test->assertTrue($stmt); $test->assertTrue($stmt);
$db->execute($stmt, ['山田太郎', 25]); $db->execute($stmt, ['山田太郎', 25]);
// TODO: assert // TODO: assert
$close = $db->demolish($stmt); $close = $db->demolish($stmt);
$this->assertTrue($close); $this->assertTrue($close);
// データの受け取り // データの受け取り
$stmt = $db->prepare('SELECT * FROM users WHERE age > ?'); $stmt = $db->prepare('SELECT * FROM users WHERE age > ?');
$test->assertTrue($stmt); $test->assertTrue($stmt);
$res = $db->execute($stmt, [20]); $res = $db->execute($stmt, [20]);
// TODO: assert // TODO: assert
print_r($res); print_r($res);
$close = $db->demolish($stmt); $close = $db->demolish($stmt);
$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();