Files
xamxam/docs/TEST_CENTRALIZATION.md
Théophile Gervreau-Mercier d2b3c6ca67 Major refactor
- update the structure to have monolithic setup
- updated deployments
- added live-reloading for devops
2026-02-05 20:16:19 +01:00

7.1 KiB

Test Centralization Summary

All tests have been centralized into the tests/ directory following standard testing conventions.

📁 New Test Structure

tests/
├── run-tests.php           # Main test runner
├── README.md               # Test documentation
├── Unit/                   # Unit tests
│   ├── DatabaseTest.php    # Database connection & queries
│   └── RateLimitTest.php   # Rate limiting functionality
├── Integration/            # Integration tests
│   └── SearchTest.php      # Search functionality
└── Security/               # Security tests
    └── SecurityTest.php    # SQL injection & XSS protection

What Was Done

1. Created Test Directory Structure

  • tests/Unit/ - Tests for individual components
  • tests/Integration/ - Tests for feature workflows
  • tests/Security/ - Tests for security vulnerabilities

2. Moved & Created Tests

Before:

  • test_db.php (root) - Basic database test
  • run-tests.php (root) - Old test runner

After:

  • tests/Unit/DatabaseTest.php - Comprehensive database testing
  • tests/Unit/RateLimitTest.php - Rate limit testing
  • tests/Integration/SearchTest.php - Search functionality testing
  • tests/Security/SecurityTest.php - Security testing
  • tests/run-tests.php - New unified test runner
  • tests/README.md - Complete test documentation

3. Updated Justfile

New Commands:

just test              # Run all tests
just test-unit         # Run unit tests only
just test-integration  # Run integration tests only
just test-security     # Run security tests only
just syntax            # Check PHP syntax

Removed:

  • Old scattered test commands
  • Duplicate test logic

4. Removed Old Files

  • Deleted test_db.php from root
  • Deleted run-tests.php from root

🚀 Running Tests

just test

Output:

╔════════════════════════════════════════════╗
║         Post-ERG Test Suite                ║
╚════════════════════════════════════════════╝

┌─────────────────────────────────────────┐
│ Database (Unit)                          │
└─────────────────────────────────────────┘
✓ PASS: Database connection successful
✓ PASS: Found 16 published theses
...
✅ TEST PASSED

...

╔════════════════════════════════════════════╗
║              Test Summary                  ║
╠════════════════════════════════════════════╣
║ Total:   4                                 ║
║ Passed:  4 ✅                              ║
║ Failed:  0                                  ║
╚════════════════════════════════════════════╝

✅ All tests passed!

Run Specific Test Suites

# Unit tests only
just test-unit

# Integration tests only
just test-integration

# Security tests only
just test-security

# Syntax check only
just syntax

Run Individual Tests

# Database test
php tests/Unit/DatabaseTest.php

# Search test
php tests/Integration/SearchTest.php

# Security test
php tests/Security/SecurityTest.php

# Rate limit test
php tests/Unit/RateLimitTest.php

Test Coverage

Unit Tests (2)

DatabaseTest.php - 4 assertions

  • Database connection
  • Count published theses
  • Get published theses
  • Get single thesis by ID

RateLimitTest.php - 5 assertions

  • RateLimit initialization
  • check() method returns boolean
  • sendHeaders() executes
  • getResetTime() returns valid value
  • cleanup() executes

Integration Tests (1)

SearchTest.php - 3 assertions

  • Empty search query handling
  • Search for specific terms
  • Special characters in search

Security Tests (1)

SecurityTest.php - 3 test groups

  • SQL injection protection (4 injection attempts blocked)
  • Invalid ID rejection (4 invalid IDs rejected)
  • XSS protection verification

Total: 4 test files, 15 assertions


📝 Test Results

All tests passing:

✅ Database (Unit)         - PASSED
✅ Rate Limit (Unit)       - PASSED
✅ Search (Integration)    - PASSED
✅ Security                - PASSED

Total:   4
Passed:  4 ✅
Failed:  0

🎯 Benefits

Before Centralization

  • Tests scattered in root directory
  • No clear organization
  • Hard to run specific test types
  • No test documentation
  • Inconsistent test format

After Centralization

  • All tests in tests/ directory
  • Clear organization (Unit/Integration/Security)
  • Easy to run any combination
  • Comprehensive test documentation
  • Consistent test format and output
  • Single test runner
  • Beautiful formatted output

📚 Writing New Tests

1. Choose Test Type

  • Unit Testtests/Unit/ - Tests single functions/classes
  • Integration Testtests/Integration/ - Tests feature workflows
  • Security Testtests/Security/ - Tests security measures

2. Use Template

<?php
/**
 * Test Name
 * Description
 */

require_once __DIR__ . '/../../lib/YourClass.php';

echo "Test Name\n";
echo "=========\n\n";

try {
    echo "Test 1: Description\n";
    // ... test code ...
    echo "✓ PASS: Test passed\n\n";

    echo "✅ All tests passed!\n";
    return true;

} catch (Exception $e) {
    echo "❌ FAIL: " . $e->getMessage() . "\n";
    return false;
}

3. Add to Test Runner

Edit tests/run-tests.php and add your test to the $testFiles array:

['name' => 'Your Test Name', 'path' => __DIR__ . '/Unit/YourTest.php'],

4. Run It

just test

🔄 CI/CD Integration (Future)

Tests are ready for CI/CD integration:

# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.4'
      - name: Run tests
        run: php tests/run-tests.php


Quick Reference

Command Description
just test Run all tests
just test-unit Unit tests only
just test-integration Integration tests only
just test-security Security tests only
just syntax Check PHP syntax
php tests/run-tests.php Run test runner directly

All tests centralized and passing!