nyholm/nsa
Testing helper to access and manipulate private/protected properties and methods in PHP. Set/get instance or static properties and invoke hidden methods to simplify tests and improve DX. Install via Composer: nyholm/nsa.
NSA::getConstant() and static property access for abstract classes address Laravel-specific testing gaps, such as verifying static properties in base models or traits (e.g., Illuminate\Database\Eloquent\Model or custom abstract services).require-dev in composer.json to prevent production inclusion. Laravel’s dependency management supports this cleanly.UserService::validatePrivateLogic()).BaseModel::incrementing).NSA::setProperty($user, 'api_token', 'test-token')).tinker, debugbar, or Log::dump()).Illuminate classes) due to reflection limitations or sealed methods in PHP 8+.NSA::getClosure() results for repeated calls).--profile flag or Blackfire).webmozart/assert (v1.x) is lightweight but could introduce breaking changes in future major versions (e.g., v2.0+ in NSA v1.4.0). Lock to ^1.0 in composer.json to avoid surprises.// NSA: Testing private method due to [reason])./tests/Internal)?TESTING.md section to document NSA usage guidelines?allowMockingPrivateMethods as an alternative?refreshDatabase(), create()).create(), factory(), or assertDatabaseHas().composer.json under require-dev.composer validate check to prevent NSA from leaking into production.@method annotations to test classes).composer.json:
composer require --dev nyholm/nsa
composer.json to restrict NSA to dev:
"config": {
"preferred-install": "dist",
"allow-plugins": {
"composer/installers": true
}
},
"require-dev": {
"nyholm/nsa": "^1.3"
}
composer update and validate no production dependencies are affected.ReflectionClass) with NSA in existing tests:
// Before
$reflection = new ReflectionClass($user);
$method = $reflection->getMethod('privateMethod');
$method->setAccessible(true);
$result = $method->invoke($user, $arg);
// After
$result = NSA::invokeMethod($user, 'privateMethod', $arg);
NSA::setProperty($user, 'api_token', 'test-token');
TESTING.md section with NSA usage guidelines.How can I help you explore Laravel packages today?