⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

php-forge/support

Repository files navigation

PHP Forge

Support


PHPUnit Mutation Testing PHPStan

Support utilities for PHPUnit-focused development
Reflection helpers, line ending normalization, and filesystem cleanup for deterministic tests.

Features

Feature Overview

Installation

composer require php-forge/support:^0.2 --dev

Quick start

This package provides helper classes for PHPUnit tests.

It supports reflection-based access to non-public members, deterministic string comparisons across platforms, and filesystem cleanup for isolated test environments.

Accessing private properties

<?php

declare(strict_types=1);

use PHPForge\Support\ReflectionHelper;
use PHPUnit\Framework\TestCase;

final class AccessPrivatePropertyTest extends TestCase
{

    public function testInaccessibleProperty(): void
    {
        $object = new class () {
            private string $secretValue = 'hidden';
        };

        $value = ReflectionHelper::inaccessibleProperty($object, 'secretValue');

        self::assertSame('hidden', $value);
    }
}

Invoking protected methods

<?php

declare(strict_types=1);

use PHPForge\Support\ReflectionHelper;
use PHPUnit\Framework\TestCase;

final class InvokeProtectedMethodTest extends TestCase
{

    public function testInvokeMethod(): void
    {
        $object = new class () {
            protected function calculate(int $a, int $b): int
            {
                return $a + $b;
            }
        };

        $result = ReflectionHelper::invokeMethod($object, 'calculate', [5, 3]);

        self::assertSame(8, $result);
    }
}

Normalize line endings

<?php

declare(strict_types=1);

use PHPForge\Support\LineEndingNormalizer;
use PHPUnit\Framework\TestCase;

final class NormalizeLineEndingsTest extends TestCase
{

    public function testNormalizedComparison(): void
    {
        self::assertSame(
            LineEndingNormalizer::normalize("Foo\r\nBar"),
            LineEndingNormalizer::normalize("Foo\nBar"),
        );
    }
}

Remove files from directory

<?php

declare(strict_types=1);

use PHPForge\Support\DirectoryCleaner;
use PHPUnit\Framework\TestCase;

final class RemoveFilesFromDirectoryTest extends TestCase
{

    public function testCleanup(): void
    {
        $testDir = sys_get_temp_dir() . '/php-forge-support-' . bin2hex(random_bytes(8));
        mkdir($testDir);

        try {
            file_put_contents($testDir . '/artifact.txt', 'test');
            file_put_contents($testDir . '/.gitignore', "*\n");

            DirectoryCleaner::clean($testDir);

            self::assertFileDoesNotExist($testDir . '/artifact.txt');
            self::assertFileExists($testDir . '/.gitignore');
        } finally {
            @unlink($testDir . '/.gitignore');
            @rmdir($testDir);
        }
    }
}

Set inaccessible property

<?php

declare(strict_types=1);

use PHPForge\Support\ReflectionHelper;
use PHPUnit\Framework\TestCase;

final class SetInaccessiblePropertyTest extends TestCase
{

    public function testSetProperty(): void
    {
        $object = new class () {
            private string $config = 'default';
        };

        ReflectionHelper::setInaccessibleProperty($object, 'config', 'test-mode');

        $newValue = ReflectionHelper::inaccessibleProperty($object, 'config');

        self::assertSame('test-mode', $newValue);
    }
}

Enum data provider

Use PHPForge\Support\EnumDataProvider to build deterministic datasets from UnitEnum::cases() and normalized enum values.

Attribute fragment output (HTML)
<?php

declare(strict_types=1);

namespace App\Tests;

use PHPForge\Support\EnumDataProvider;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use UnitEnum;

enum Size
{
    case Small;
    case Large;
}

final class EnumDataProviderHtmlTest extends TestCase
{
    public static function provideEnumAttributes(): array
    {
        return EnumDataProvider::attributeCases(Size::class, 'data-size', true);
    }

    #[DataProvider('provideEnumAttributes')]
    public function testBuildsAttributeFragment(UnitEnum $case, array $args, string $expected, string $message): void
    {
        $normalized = $case->name;
        $attributeFragment = " data-size=\"{$normalized}\"";

        self::assertSame($expected, $attributeFragment, $message);
    }
}
Enum instance output (non-HTML)
<?php

declare(strict_types=1);

namespace App\Tests;

use PHPForge\Support\EnumDataProvider;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use UnitEnum;

enum State
{
    case Active;
    case Disabled;
}

final class EnumDataProviderEnumTest extends TestCase
{
    public static function provideEnumInstances(): array
    {
        return EnumDataProvider::attributeCases(State::class, 'state', false);
    }

    #[DataProvider('provideEnumInstances')]
    public function testReturnsEnumInstance(UnitEnum $case, array $args, UnitEnum $expected, string $message): void
    {
        self::assertSame($case, $expected, $message);
    }
}
Tag cases
<?php

declare(strict_types=1);

namespace App\Tests;

use PHPForge\Support\EnumDataProvider;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

enum Heading
{
    case H1;
    case H2;
}

final class EnumDataProviderTagCasesTest extends TestCase
{
    public static function provideTags(): array
    {
        return EnumDataProvider::tagCases(Heading::class, 'heading');
    }

    #[DataProvider('provideTags')]
    public function testProvidesNormalizedTag(Heading $case, string $normalized): void
    {
        self::assertSame($case->name, $normalized);
    }
}

Documentation

For detailed configuration options and advanced usage.

Package information

PHP Latest Stable Version Total Downloads

Quality code

Codecov PHPStan Level Max Super-Linter StyleCI

Our social networks

Follow on X

License

License

About

Support utilities for enhanced testing capabilities.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages