SVNからのミラー

This commit is contained in:
2025-11-07 22:48:07 +09:00
commit 438c7d8aef
68 changed files with 7619 additions and 0 deletions

122
src/Site/Test/LibCsv.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
namespace Site\Test;
require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester;
use Site\Lib\Csv;
use Site\Lib\Delimiter;
$test = new Tester([
'colorOutput' => true,
'verboseOutput' => true
]);
$test->describe('Csvの基本的なパーシング', function($test): void {
$test->it('簡単なCSVファイルをパーシングするはず', function($test): void {
$str = "岩田聡,プロジューサー\n宮本茂,デザイナー\nJeffrey Epstein,幼児性愛者";
$tmpFile = tempnam(sys_get_temp_dir(), 'csv_test');
file_put_contents($tmpFile, $str);
$csv = new Csv($tmpFile);
$res = $csv->parse();
unlink($tmpFile);
$expect = [
["岩田聡", "プロジューサー"],
["宮本茂", "デザイナー"],
["Jeffrey Epstein", "幼児性愛者"]
];
$test->assertNotNull($res);
$test->assertEquals($res, $expect);
});
$test->it('異なるデリミタでCSVをパーシングするはず', function ($test): void {
// セミコロン
$semiStr = "岩田聡;プロジューサー\n宮本茂;デザイナー";
$tmpFile = tempnam(sys_get_temp_dir(), 'csv_semi_test');
file_put_contents($tmpFile, $semiStr);
$csv = new Csv($tmpFile);
$res = $csv->parse(Delimiter::SEMICOLON);
unlink($tmpFile);
$expect = [
["岩田聡", "プロジューサー"],
["宮本茂", "デザイナー"]
];
$test->assertEquals($res, $expect, "セミコロンデリミタでパーシングに失敗");
// タブ
$tabStr = "岩田聡\tプロジューサー\n宮本茂\tデザイナー";
$tmpFile = tempnam(sys_get_temp_dir(), 'csv_tab_test');
file_put_contents($tmpFile, $tabStr);
$csv = new Csv($tmpFile);
$res = $csv->parse(Delimiter::TAB);
unlink($tmpFile);
$test->assertEquals($res, $expect, "タブデリミタでパーシングに失敗");
// パイプ
$pipeStr = "岩田聡|プロジューサー\n宮本茂|デザイナー";
$tmpFile = tempnam(sys_get_temp_dir(), 'csv_pipe_test');
file_put_contents($tmpFile, $pipeStr);
$csv = new Csv($tmpFile);
$res = $csv->parse(Delimiter::PIPE);
unlink($tmpFile);
$test->assertEquals($res, $expect, "パイプデリミタでパーシングに失敗");
});
$test->it('ヘッダー付きCSVをパーシングするはず', function ($test) {
$str = "name,job title\n岩田聡,プロジューサー\n宮本茂,デザイナー";
$tmpFile = tempnam(sys_get_temp_dir(), 'csv_header_test');
file_put_contents($tmpFile, $str);
$csv = new Csv($tmpFile);
$res = $csv->parse(Delimiter::COMMA, true); // isHeader = true
unlink($tmpFile);
$expect = [
'header' => ["name", "job title"],
'body' => [
["岩田聡", "プロジューサー"],
["宮本茂", "デザイナー"],
],
];
$test->assertNotNull($res);
$test->assertEquals($res, $expect, "ヘッダーパーシングに失敗");
});
$test->it('セル内にカンマを含むCSVをパーシングするはず', function ($test): void {
$str = "\"守矢, 諏訪子\",エンジニア\n\"青, 猫ちゃん\",サーバー管理者";
$tmpFile = tempnam(sys_get_temp_dir(), 'csv_quoted_comma_test');
file_put_contents($tmpFile, $str);
$csv = new Csv($tmpFile);
$res = $csv->parse(\Site\Lib\delimiter::COMMA);
unlink($tmpFile);
$expect = [
["守矢, 諏訪子", "エンジニア"],
["青, 猫ちゃん", "サーバー管理者"]
];
$test->assertNotNull($res);
$test->assertEquals($res, $expect, "セル内にカンマを含むパーシングに失敗");
});
});

92
src/Site/Test/LibCurl.php Normal file
View File

@@ -0,0 +1,92 @@
<?php
namespace Site\Test;
require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester;
use Site\Lib\Curl;
$test = new Tester([
'colorOutput' => true,
'verboseOutput' => true
]);
$test->describe('Curlの基本的な機能性', function($test): void {
$test->it('URLで使って新しいインスタンスを作成するはず', function($test): void {
$curl = new Curl('https://076.moe');
$test->assertNotNull($curl);
});
$test->it('メソッドでURLを設定出来るはす', function($test): void {
$curl = new Curl();
$curl->setUrl('https://076.moe');
$test->assertNotNull($curl);
});
$test->it('ヘッダー文字を作成出来るはず', function($test): void {
$curl = new Curl('https://076.moe');
$curl->setHeaders([
'Accept' => 'application/json',
'User-Agent' => 'LoliTest/1.0'
]);
$reflectionClass = new \ReflectionClass($curl);
$method = $reflectionClass->getMethod('buildHeaderString');
$method->setAccessible(true);
$headerString = $method->invoke($curl);
$test->assertStringContains('Accept: application/json', $headerString);
$test->assertStringContains('User-Agent: LoliTest/1.0', $headerString);
});
$test->skip('移転を対応するはず', '作成中・・・');
$test->it('メソッドチェーニングを対応するはず', function($test): void {
$curl = new Curl();
$result = $curl->setUrl('https://076.moe')
->setMethod('GET')
->setTimeout(30);
$test->assertSame($curl, $result);
});
});
$test->describe('Curl HTTP リクエスト', function($test): void {
$networkAvailable = true;
if (!$networkAvailable) {
$test->skip('076.moeでGETリクエストの確認', 'ネットワークが無効です。');
$test->skip('postman-echo.comでPOSTリクエストの確認', 'ネットワークが無効です。');
return;
}
$test->it('076.moeでGETリクエストの確認', function($test): void {
$curl = new Curl('https://076.moe');
$result = $curl->execute();
$test->assertTrue($result);
$test->assertEquals(200, $curl->getResponseCode());
$test->assertNotNull($curl->getResponseBody());
$test->assertStringContains('<html', $curl->getResponseBody());
});
$test->it('postman-echo.comでPOSTリクエストの確認', function($test): void {
$curl = new Curl();
$curl->setUrl('https://postman-echo.com/post')
->setMethod('POST')
->setPostFields([
'name' => '山田太郎',
'email' => 't.yamada@example.com'
]);
$result = $curl->execute();
$test->assertTrue($result);
$test->assertEquals(200, $curl->getResponseCode());
$responseBody = $curl->getResponseBody();
$test->assertStringContains('山田太郎', $responseBody);
$test->assertStringContains('t.yamada@example.com', $responseBody);
});
});
$test->printSummary();

View File

@@ -0,0 +1,64 @@
<?php
namespace Site\Test;
require_once __DIR__.'/../../../autoload.php';
use Site\Lib\Tester;
use Site\Lib\Mysql;
$test = new Tester([
'colorOutput' => true,
'verboseOutput' => true
]);
$test->describe('パケットのデバッグ', function($test): void {
try {
$db = new Mysql();
$db->setDebug(true);
$db->connect();
$result = $db->query('SELECT * FROM user WHERE id = 1');
foreach ($result['rows'] as $row) {
echo "ユーザー名: ".$row['nickname']."\n";
}
$db->savePacketLogToFile('mysql_log.txt');
$db->close();
} catch (\Exception $e) {
echo 'エラー: '.$e->getMessage()."\n";
}
});
$test->describe('プリペアドステートメント', function($test): void {
try {
$db = new Mysql();
$db->connect();
// データの入り
$stmt = $db->prepare('INSERT INTO users (name, age) VALUES (?, ?)');
$test->assertTrue($stmt);
$db->execute($stmt, ['山田太郎', 25]);
// TODO: assert
$close = $db->demolish($stmt);
$this->assertTrue($close);
// データの受け取り
$stmt = $db->prepare('SELECT * FROM users WHERE age > ?');
$test->assertTrue($stmt);
$res = $db->execute($stmt, [20]);
// TODO: assert
print_r($res);
$close = $db->demolish($stmt);
$this->assertTrue($close);
$db->close();
} catch (\Exception $e) {
echo 'エラー: '.$e->getMessage()."\n";
}
});