wikimedia/testing-access-wrapper
Small PHP utility from Wikimedia that wraps objects to access otherwise non-public (protected/private) methods and properties in tests. Helps write focused unit tests without changing production visibility, acting as a thin “testing access wrapper.”
Installation Add the package via Composer:
composer require wikimedia/testing-access-wrapper
No additional configuration is required for basic usage.
First Use Case: Testing Private/Protected Methods
The package provides a TestingAccessTrait to bypass visibility restrictions in unit tests.
use Wikimedia\TestingAccessWrapper\TestingAccessTrait;
class MyTest extends \PHPUnit\Framework\TestCase
{
use TestingAccessTrait;
public function testPrivateMethod()
{
$object = new MyClass();
$result = $this->callPrivateMethod($object, 'privateMethod', [$arg1, $arg2]);
$this->assertEquals(expected, $result);
}
}
Key Classes/Methods to Explore
TestingAccessTrait – Core trait for accessing private/protected methods.callPrivateMethod() – Invokes a private method with arguments.getPrivateProperty() – Accesses private/protected properties.setPrivateProperty() – Modifies private/protected properties.Testing Legacy Code
Use the trait to test methods marked as private or protected without refactoring:
$this->callPrivateMethod($service, 'internalValidate', [$input]);
Mocking Dependencies Override private properties to simulate dependency injection:
$this->setPrivateProperty($repository, 'connection', $mockConnection);
Integration with Laravel
Combine with Laravel’s testing helpers (e.g., make:mock):
$user = $this->callPrivateMethod($authManager, 'findUserByToken', [$token]);
Dynamic Testing Use reflection-like access in tests without exposing internals:
$value = $this->getPrivateProperty($model, 'hiddenField');
$this->callPrivateMethod(\Illuminate\Support\Facades\Route::class, 'getRoutes', []);
$this->setPrivateProperty(app(), 'resolved', ['MyService' => $mock]);
$this->callPrivateMethod($listener, 'handle', [$event]);
Reflection Overhead
ReflectionClass, which can slow down tests.Breaking Encapsulation
Static Method Limitations
callPrivateMethod.callStaticMethod() (if available) or refactor to instance methods.Laravel-Specific Quirks
Illuminate\Foundation\Application) may throw errors when accessing internals.ReflectionException.private/protected (not public).var_dump(): Debug property/method signatures:
var_dump($this->getPrivateProperty($object, 'property'));
Custom Accessors Extend the trait to add domain-specific helpers:
trait MyTestingAccessTrait {
use TestingAccessTrait;
protected function callAdminMethod($object, $method, $args) {
return $this->callPrivateMethod($object, $method, $args);
}
}
Integration with Pest If using Pest, alias the trait for cleaner syntax:
uses(TestingAccessTrait::class)->in('Tests');
Logging Access Override methods to log accesses (useful for auditing):
protected function callPrivateMethod($object, $method, $args) {
\Log::debug("Accessing private method: {$method}");
return parent::callPrivateMethod($object, $method, $args);
}
How can I help you explore Laravel packages today?