mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 08:09:18 +02:00
Add PHPUnit setup (Phase 0) and pure-logic tests (Phase 1): Crypto, EmailObfuscator, SystemController helpers, StudentEmail, TfeController OG tags
This commit is contained in:
195
tests/phpunit/SystemControllerHelpersTest.php
Normal file
195
tests/phpunit/SystemControllerHelpersTest.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* SystemControllerHelpersTest — Pure logic tests for the static helper methods
|
||||
* in SystemController (humanBytes, diskColor, logLineClass, nginxLineClass,
|
||||
* statusLabel, statusClass).
|
||||
*
|
||||
* These are stateless, no-IO functions.
|
||||
*/
|
||||
class SystemControllerHelpersTest extends TestCase
|
||||
{
|
||||
// ── humanBytes() ──────────────────────────────────────────────────────────
|
||||
|
||||
public function testHumanBytesZero(): void
|
||||
{
|
||||
$this->assertSame('0.0 KB', SystemController::humanBytes(0));
|
||||
}
|
||||
|
||||
public function testHumanBytesBelowOneKB(): void
|
||||
{
|
||||
$this->assertSame('1.0 KB', SystemController::humanBytes(1023));
|
||||
}
|
||||
|
||||
public function testHumanBytesOneKB(): void
|
||||
{
|
||||
$this->assertSame('1.0 KB', SystemController::humanBytes(1024));
|
||||
}
|
||||
|
||||
public function testHumanBytesOneMB(): void
|
||||
{
|
||||
// threshold is > 1048576, so exactly 1048576 is still KB
|
||||
$this->assertSame('1,024.0 KB', SystemController::humanBytes(1048576));
|
||||
}
|
||||
|
||||
public function testHumanBytesOneGB(): void
|
||||
{
|
||||
// threshold is > 1073741824, so exactly that is still MB
|
||||
$this->assertSame('1,024.0 MB', SystemController::humanBytes(1073741824));
|
||||
}
|
||||
|
||||
public function testHumanBytes1523MB(): void
|
||||
{
|
||||
$bytes = (int)(1.5 * 1048576);
|
||||
$this->assertSame('1.5 MB', SystemController::humanBytes($bytes));
|
||||
}
|
||||
|
||||
public function testHumanBytes2500GB(): void
|
||||
{
|
||||
$bytes = (int)(2.5 * 1073741824);
|
||||
$this->assertSame('2.5 GB', SystemController::humanBytes($bytes));
|
||||
}
|
||||
|
||||
// ── diskColor() ───────────────────────────────────────────────────────────
|
||||
|
||||
public function testDiskColorBelowWarning(): void
|
||||
{
|
||||
$this->assertSame('#4caf50', SystemController::diskColor(0));
|
||||
$this->assertSame('#4caf50', SystemController::diskColor(50));
|
||||
$this->assertSame('#4caf50', SystemController::diskColor(70));
|
||||
}
|
||||
|
||||
public function testDiskColorWarning(): void
|
||||
{
|
||||
$this->assertSame('#ffc107', SystemController::diskColor(71));
|
||||
$this->assertSame('#ffc107', SystemController::diskColor(85));
|
||||
}
|
||||
|
||||
public function testDiskColorCritical(): void
|
||||
{
|
||||
$this->assertSame('#e05555', SystemController::diskColor(86));
|
||||
$this->assertSame('#e05555', SystemController::diskColor(99));
|
||||
$this->assertSame('#e05555', SystemController::diskColor(100));
|
||||
}
|
||||
|
||||
// ── logLineClass() ────────────────────────────────────────────────────────
|
||||
|
||||
public function testLogLineClassCrit(): void
|
||||
{
|
||||
$this->assertSame('log-crit', SystemController::logLineClass('[crit] Fatal'));
|
||||
$this->assertSame('log-crit', SystemController::logLineClass('[emerg] Emergency'));
|
||||
$this->assertSame('log-crit', SystemController::logLineClass('[alert] Alert'));
|
||||
}
|
||||
|
||||
public function testLogLineClassError(): void
|
||||
{
|
||||
$this->assertSame('log-error', SystemController::logLineClass('[error] An error'));
|
||||
}
|
||||
|
||||
public function testLogLineClassWarn(): void
|
||||
{
|
||||
$this->assertSame('log-warn', SystemController::logLineClass('[warn] Warning'));
|
||||
}
|
||||
|
||||
public function testLogLineClassNotice(): void
|
||||
{
|
||||
$this->assertSame('log-notice', SystemController::logLineClass('[notice] Notice'));
|
||||
}
|
||||
|
||||
public function testLogLineClassHttp500(): void
|
||||
{
|
||||
// Matches " 500 " pattern in nginx-style log
|
||||
$this->assertSame('log-error', SystemController::logLineClass('GET / " 500 123 "'));
|
||||
$this->assertSame('log-error', SystemController::logLineClass('GET / " 404 123 "'));
|
||||
}
|
||||
|
||||
public function testLogLineClassHttp300(): void
|
||||
{
|
||||
$this->assertSame('log-notice', SystemController::logLineClass('GET / " 302 456 "'));
|
||||
}
|
||||
|
||||
public function testLogLineClassDefault(): void
|
||||
{
|
||||
$this->assertSame('', SystemController::logLineClass('Just a plain log message'));
|
||||
$this->assertSame('', SystemController::logLineClass('GET / " 200 123 "'));
|
||||
}
|
||||
|
||||
// ── nginxLineClass() ──────────────────────────────────────────────────────
|
||||
|
||||
public function testNginxLineClassComment(): void
|
||||
{
|
||||
$this->assertSame('nginx-comment', SystemController::nginxLineClass('# comment'));
|
||||
$this->assertSame('nginx-comment', SystemController::nginxLineClass(' # indented comment'));
|
||||
$this->assertSame('nginx-comment', SystemController::nginxLineClass(''));
|
||||
$this->assertSame('nginx-comment', SystemController::nginxLineClass(' '));
|
||||
}
|
||||
|
||||
public function testNginxLineClassBlock(): void
|
||||
{
|
||||
$this->assertSame('nginx-block', SystemController::nginxLineClass('server {'));
|
||||
$this->assertSame('nginx-block', SystemController::nginxLineClass('location / {'));
|
||||
$this->assertSame('nginx-block', SystemController::nginxLineClass(' upstream backend {'));
|
||||
$this->assertSame('nginx-block', SystemController::nginxLineClass('events {'));
|
||||
$this->assertSame('nginx-block', SystemController::nginxLineClass('http {'));
|
||||
}
|
||||
|
||||
public function testNginxLineClassDirective(): void
|
||||
{
|
||||
$this->assertSame('nginx-directive', SystemController::nginxLineClass('listen 80;'));
|
||||
$this->assertSame('nginx-directive', SystemController::nginxLineClass('root /var/www;'));
|
||||
$this->assertSame('nginx-directive', SystemController::nginxLineClass(' server_name example.com;'));
|
||||
}
|
||||
|
||||
// ── statusLabel() ─────────────────────────────────────────────────────────
|
||||
|
||||
public function testStatusLabelActive(): void
|
||||
{
|
||||
$this->assertSame('● En ligne', SystemController::statusLabel('active'));
|
||||
}
|
||||
|
||||
public function testStatusLabelInactive(): void
|
||||
{
|
||||
$this->assertSame('○ Inactif', SystemController::statusLabel('inactive'));
|
||||
}
|
||||
|
||||
public function testStatusLabelFailed(): void
|
||||
{
|
||||
$this->assertSame('✕ Erreur', SystemController::statusLabel('failed'));
|
||||
}
|
||||
|
||||
public function testStatusLabelWarn(): void
|
||||
{
|
||||
$this->assertSame('⚠ Attention', SystemController::statusLabel('warn'));
|
||||
}
|
||||
|
||||
public function testStatusLabelUnknown(): void
|
||||
{
|
||||
$this->assertSame('? Inconnu', SystemController::statusLabel('unknown'));
|
||||
$this->assertSame('? Inconnu', SystemController::statusLabel('bogus'));
|
||||
}
|
||||
|
||||
// ── statusClass() ─────────────────────────────────────────────────────────
|
||||
|
||||
public function testStatusClassOk(): void
|
||||
{
|
||||
$this->assertSame('status-ok', SystemController::statusClass('active'));
|
||||
}
|
||||
|
||||
public function testStatusClassWarn(): void
|
||||
{
|
||||
$this->assertSame('status-warn', SystemController::statusClass('inactive'));
|
||||
$this->assertSame('status-warn', SystemController::statusClass('warn'));
|
||||
}
|
||||
|
||||
public function testStatusClassError(): void
|
||||
{
|
||||
$this->assertSame('status-err', SystemController::statusClass('failed'));
|
||||
}
|
||||
|
||||
public function testStatusClassUnknown(): void
|
||||
{
|
||||
$this->assertSame('status-unknown', SystemController::statusClass('bogus'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user