php-forge / support
Support utilities for enhanced testing capabilities.
Installs: 44 739
Dependents: 46
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- infection/infection: ^0.27|^0.31
- maglnet/composer-require-checker: ^4.7
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0.3
- phpunit/phpunit: ^10.5
- rector/rector: ^2.1
- symplify/easy-coding-standard: ^12.5
This package is auto-updated.
Last update: 2025-08-18 23:05:57 UTC
README
Support utilities for enhanced testing capabilities.
Features
โ Advanced Reflection Utilities
- Access and modify private and protected properties via reflection.
- Invoke inaccessible methods to expand testing coverage.
โ Cross-Platform String Assertions
- Avoid false positives and negatives caused by Windows vs. Unix line ending differences.
- Normalize line endings for consistent string comparisons across platforms.
โ File System Test Management
- Recursively clean files and directories for isolated test environments.
- Safe removal that preserves Git-tracking files (for example, '.gitignore', '.gitkeep').
Quick start
System requirements
Installation
Method 1: Using Composer (recommended)
Install the extension.
composer require --dev --prefer-dist php-forge/support:^0.2
Method 2: Manual installation
Add to your composer.json
.
{ "require-dev": { "php-forge/support": "^0.2" } }
Then run.
composer update
Basic Usage
Accessing private properties
<?php declare(strict_types=1); use PHPForge\Support\TestSupport; use PHPUnit\Framework\TestCase; final class AccessPrivatePropertyTest extends TestCase { use TestSupport; public function testInaccessibleProperty(): void { $object = new class () { private string $secretValue = 'hidden'; }; $value = self::inaccessibleProperty($object, 'secretValue'); self::assertSame('hidden', $value, "Should access the private property and return its value."); } }
Invoking protected methods
<?php declare(strict_types=1); use PHPForge\Support\TestSupport; use PHPUnit\Framework\TestCase; final class InvokeProtectedMethodTest extends TestCase { use TestSupport; public function testInvokeMethod(): void { $object = new class () { protected function calculate(int $a, int $b): int { return $a + $b; } }; $result = self::invokeMethod($object, 'calculate', [5, 3]); self::assertSame(8, $result, "Should invoke the protected method and return the correct sum."); } }
Normalize line endings
<?php declare(strict_types=1); use PHPForge\Support\TestSupport; use PHPUnit\Framework\TestCase; final class NormalizeLineEndingsTest extends TestCase { use TestSupport; public function testNormalizedComparison(): void { self::assertSame( self::normalizeLineEndings("Foo\r\nBar"), self::normalizeLineEndings("Foo\nBar"), "Should match regardless of line ending style", ); } }
Remove files from directory
<?php declare(strict_types=1); use PHPForge\Support\TestSupport; use PHPUnit\Framework\TestCase; final class RemoveFilesFromDirectoryTest extends TestCase { use TestSupport; public function testCleanup(): void { $testDir = dirname(__DIR__) . '/runtime'; // clean up test artifacts (preserves '.gitignore' and '.gitkeep') self::removeFilesFromDirectory($testDir); self::assertTrue(true, "Should remove all files in the test directory while preserving Git-tracked files."); } }
Set inaccessible property
<?php declare(strict_types=1); use PHPForge\Support\TestSupport; use PHPUnit\Framework\TestCase; final class SetInaccessiblePropertyTest extends TestCase { use TestSupport; public function testSetProperty(): void { $object = new class () { private string $config = 'default'; }; // set private property for testing scenarios self::setInaccessibleProperty($object, 'config', 'test-mode'); $newValue = self::inaccessibleProperty($object, 'config'); self::assertSame('test-mode', $newValue, "Should set the inaccessible property to 'test-mode'."); } }
Documentation
For comprehensive testing guidance, see:
- ๐งช Testing Guide