Testing Guide

Comprehensive testing setup including PHPUnit tests, PHPStan static analysis, and CI/CD integration for robust development.

Home / Documentation / Testing Guide

Testing Overview

Scriptlog uses two complementary testing approaches:

Tool Purpose Coverage
PHPUnit Unit and integration testing Functional correctness
PHPStan Static code analysis Type safety, code quality

Test Suite Metrics

Metric Value
Total Tests 868
Assertions ~1000+
PHPUnit Version 9.6.34
Target Coverage 40%
Completed Tests 407+

Test Categories

Category Description
Unit Tests Utility function tests, class existence tests
Integration Tests Database CRUD operations using blogware_test database

Test Coverage Plan

The test coverage plan is organized into phases:

Phase Status

Phase Priority Status Tests
Phase 1: DAO Integration HIGH Complete 92
Phase 2: Service Layer HIGH Complete 148
Phase 3: Core Classes MEDIUM Pending 65
Phase 4: Controllers MEDIUM Pending 34
Phase 5: Utilities LOW Complete 68

PHPUnit Testing

Running Tests

BASH PHPUnit Commands
# Run all tests
lib/vendor/bin/phpunit

# Run with coverage (requires Xdebug)
lib/vendor/bin/phpunit --coverage-html coverage

# Run specific test file
lib/vendor/bin/phpunit tests/unit/ImageDisplayTest.php

# Run tests matching pattern
lib/vendor/bin/phpunit --filter "EmailValidation"

Test Database Setup

Tests use a separate database (blogware_test) to avoid affecting production data.

BASH commands
# Create test database
php tests/setup_test_db.php

# Or manually
mysql -u root -p -e "CREATE DATABASE blogware_test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"

PHPStan Static Analysis

PHPStan is a static analysis tool that finds bugs in your code without running it.

Configuration Files

File Purpose
phpstan.neon Main configuration
phpstan.baseline.neon Baseline of known issues to ignore

PHPStan Configuration

NEON phpstan.neon
includes:
    - phpstan.baseline.neon

parameters:
    phpVersion: 70400
    paths:
        - lib/
        - index.php
    excludePaths:
        - lib/vendor/
        - lib/core/HTMLPurifier/
    level: 0

Running PHPStan

BASH PHPStan Commands
# Run static analysis
lib/vendor/bin/phpstan analyse

# Run with specific config
lib/vendor/bin/phpstan analyse --configuration=phpstan.neon

# Run with memory limit (recommended)
lib/vendor/bin/phpstan analyse --memory-limit=1G

# Generate/update baseline
lib/vendor/bin/phpstan analyse --generate-baseline=phpstan.baseline.neon

# Increase analysis level for stricter checks
lib/vendor/bin/phpstan analyse -l 5

Key Settings

  • phpVersion: Set to 70400 for PHP 7.4 compatibility
  • level: Currently at level 0 (most lenient). Increase gradually for stricter checks
  • excludePaths: Excludes vendor and third-party code

Writing Tests

PHPUnit Test Structure

PHP ExampleTest.php
<?php
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    public function testSomething(): void
    {
        $this->assertTrue(true);
        $this->assertEquals(1, 1);
        $this->assertIsString('test');
    }
    
    public function testWithFunction(): void
    {
        if (function_exists('some_function')) {
            $result = some_function('input');
            $this->assertIsString($result);
        }
    }
}

Best Practices

Single Responsibility

Test one thing per method. Each test should verify a single, specific behavior.

Descriptive Names

Method names should clearly describe what is being tested. Use test[Behavior] pattern.

Arrange-Act-Assert

Structure tests with clear setup (Arrange), action (Act), and verification (Assert) phases.

Mock External Dependencies

Use mocks for database, filesystem, and external services to isolate unit tests.

Recently Added Tests

Latest test additions to the project (April 2026)

April 2026

Medoo & Membership Tests

MedooinFunctionsTest.php 26 tests

Unit tests for is_medoo_database(), is_db_database(), db_build_where()

MedooinIntegrationTest.php 8+ tests

Integration tests for database selection and operations

MembershipFunctionsTest.php 26 tests

Tests for is_registration_unable(), membership_default_role()

MembershipIntegrationTest.php 8 tests

Integration tests for membership settings

April 2026

PostDao Security Tests

PostDaoSecurityTest.php 6 tests

Security-focused tests for the PostDao class

SQL injection prevention verification
Security filters validation
ORDER BY column whitelist testing

CI/CD Integration

GitHub Actions Workflow

YAML .github/workflows/test.yml
name: Test

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v3
       
      - name: Install dependencies
        run: composer install --no-interaction --no-dev
       
      - name: Run PHPUnit
        run: lib/vendor/bin/phpunit
       
      - name: Run PHPStan
        run: lib/vendor/bin/phpstan analyse --memory-limit=1G

Pre-commit Hook

Add to .git/hooks/pre-commit:

BASH .git/hooks/pre-commit
#!/bin/bash
lib/vendor/bin/phpstan analyse --memory-limit=1G
lib/vendor/bin/phpunit

Troubleshooting

PHPUnit Issues

Issue Solution
Tests fail with "Database not found" Run php tests/setup_test_db.php
Xdebug required for coverage Install Xdebug or skip coverage

PHPStan Issues

Issue Solution
Memory limit exceeded Run with --memory-limit=1G
Too many errors Use baseline or increase level gradually
False positives Add to ignoreErrors in phpstan.neon
Missing bleedingEdge.neon Remove from includes in phpstan.neon

Additional Resources

Official documentation and guides for testing tools.