psalm/plugin-phpunit
Psalm plugin that teaches Psalm about PHPUnit tests. Adds better type inference and assertions for PHPUnit APIs so your test suite is analyzed more accurately. Requires Psalm v4+. Install via Composer and enable with psalm-plugin.
Enhanced Test Reliability for Laravel Applications
Roadmap Priorities
#[Test] attributes, data provider mismatches) to catch issues early, reducing false positives in test reports.Key Use Cases
JsonResponse types) to prevent runtime serialization errors.Collection when a query returns Builder) before tests execute.bind(MyService::class) resolves to the right interface).#[Test], #[DataProvider], #[Before]) aligns with Laravel’s adoption of newer testing patterns.Adopt When:
#[Test], #[DataProvider]) and need static validation to catch misconfigurations early.Look Elsewhere If:
For Executives:
"This plugin dramatically reduces test-related bugs in production by catching type mismatches before they cause failures. For example, it’ll flag if a test incorrectly asserts null for a method returning string|array, saving hours in debugging and reducing incident response time. Low-cost, high-impact: Integrates seamlessly with existing workflows, requires minimal setup, and aligns with Laravel’s focus on reliability. ROI: Fewer production incidents, faster releases, and happier developers—all while maintaining compatibility with your current stack."
For Engineering Teams: *"Psalm’s PHPUnit plugin adds static type checking to your tests, similar to how your IDE highlights syntax errors—but for test logic. Key benefits for Laravel:
assertEquals() with mismatched types, invalid data providers).#[Test], #[DataProvider], and #[Before] attributes for Laravel’s latest testing stack.
Tradeoff: Requires fixing Psalm warnings (but reduces runtime surprises). How to start:composer.json: composer require --dev psalm/plugin-phpunit.vendor/bin/psalm-plugin enable psalm/plugin-phpunit.psalm.xml to include test files: <file-list><dir>tests/</dir></file-list>../vendor/bin/psalm --init to generate config.
Pro Tip: Use --no-cache in CI to catch new issues immediately. Example: If a test asserts assertSame(1, $user->id), Psalm will verify $user->id is actually an int—saving you from runtime TypeError exceptions."*For QA/DevOps: *"This reduces CI/CD noise by catching test-related type errors before execution. For example:
@dataProvider methods return iterable types and match test expectations.assertSame(), assertEquals(), and other assertions align with actual return types (e.g., no null vs. int mismatches).#[Test], #[Before], or #[After] attributes before tests run.
Result: Fewer flaky tests, cleaner CI logs, and fewer false positives in test reports. Recommendation: Start by enabling this in a non-critical branch to measure the impact on test failures."*How can I help you explore Laravel packages today?