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.
hashPassword(), calculateScore()).PrivateService::processOrder()).Model::incrementing).NSA::getProperties($user)).Adopt NSA if:
email_verified_at, api_token).Look Elsewhere if:
Mockery, PHPUnit) and can achieve the same goals without external dependencies.Alternatives to Evaluate:
ReflectionClass directly (more verbose but no external dependency).Mockery::mock() (requires exposing methods or using allowMockingPrivateMethods).setAccessible(): For testing protected/private properties (limited to properties, not methods).Artisan or Tinker: For debugging, not testing.For Executives: "NSA is a lightweight, MIT-licensed PHP package designed to enhance our test suite by enabling direct access to private and protected methods and properties in Laravel models and services. This allows us to thoroughly test internal logic without exposing sensitive implementation details publicly. By adopting NSA, we can reduce technical debt during refactoring, accelerate test development for complex scenarios, and maintain a focus on developer experience. The package is scoped strictly to development dependencies, ensuring no production risks. Early adoption could lead to a 20–30% reduction in test development time for edge cases, with minimal maintenance overhead."
Key Talking Points:
dev-dependencies and excluded from production.For Engineering Teams: "NSA allows us to write cleaner and more efficient tests for private methods and properties in Laravel, but it should be used judiciously as a tool for developer experience—not as a design pattern. Here’s how we’ll implement it responsibly:
// NSA: Testing private method due to [reason].validateInternal() method, we can test it directly with NSA::invokeMethod($validator, 'validateInternal', [$data])."*For QA/Test Teams: "NSA will streamline your workflow by:
NSA::setProperty($user, 'api_token', 'test-token')).For Architects/Tech Leads: "NSA addresses a common challenge in Laravel testing: verifying private logic without compromising encapsulation. Here’s the risk-reward balance:
dev-dependencies and block production inclusion via CI checks.How can I help you explore Laravel packages today?