TestCase is not extended directly, the trait-based approach (StrictMocking) allows seamless integration without modifying Laravel’s core test classes. This avoids vendor lock-in risks.TestCase extension provides a zero-configuration opt-in.createMock() returning null for unstubbed methods) will require refactoring. The pardoned config mitigates this but adds maintenance overhead.method_exists() checks) as violations. The pardoned config helps, but teams must curate exceptions carefully.MockApplicationServices or MockFacade may need updates if they rely on unstubbed defaults.partialMock()) might conflict with strict mocking rules.createMockWithPartial()) or third-party packages that might conflict with strict mocking?phpunit.xml configuration.laravel/pint).HttpTests, DatabaseMigrations) but requires explicit opt-in per test class.createStrictMock().phpstan.neon (no Laravel-specific conflicts). Ensure the rules.neon file is included in the project’s static analysis pipeline.// .phpstorm.meta.php
{
"rules": {
"vendors/lendable/phpunit-extensions/phpstan/rules.neon": {
"level": "warning"
}
}
}
TestCase or individual test files, but Pest’s Mock facade may need updates to support createStrictMock().composer require --dev phpstan/phpstan lendable/phpunit-extensions
vendor/bin/phpstan analyse --level=max --memory-limit=1G
phpunit.php and composer.json to meet PHPUnit/PHP version requirements:
"require-dev": {
"phpunit/phpunit": "^12.0",
"php": "^8.3"
}
Lendable\PHPUnitExtensions\TestCase or using the StrictMocking trait.createMock() calls to createStrictMock() in existing tests.pardoned config for tests that cannot be immediately fixed:
lendable_phpunit:
enforceStrictMocking:
pardoned:
- App\Tests\Feature\LegacyTest
pardoned exclusions incrementally).MockApplicationServices), ensure all resolved methods are stubbed explicitly.MockFacade usage to align with strict mocking (e.g., stub shouldReceive() calls).partialMock()) for conflicts. Example fix:
// Before (loose)
$mock = $this->partialMock(MyService::class, ['method1']);
// After (strict)
$mock = $this->createStrictMock(MyService::class);
$mock->method('method1')->willReturn(...);
Mockery, Brain\Monkey) for compatibility. If they rely on unstubbed defaults, consider wrapping their mocks with createStrictMock().| Phase | Action | Dependencies | Tools/Commands |
|---|---|---|---|
| Analysis | Audit test suite for unstubbed mocks and PHPStan violations. | PHPStan configured. | vendor/bin/phpstan analyse |
| Infrastructure | Upgrade PHPUnit and PHP versions. | CI/CD pipeline access. | composer update phpunit/phpunit |
| Configuration | Add PHPStan rules and pardoned exclusions to phpstan.neon. |
PHPStan installed. | Edit phpstan.neon |
| Pilot | Extend LendableTestCase in a new test file or use the StrictMocking trait. |
Composer dev dependency. | composer require --dev lendable/phpunit-extensions |
| Refactor | Migrate existing tests to strict mocks (trait or class extension). | Pilot phase success. | IDE refactoring tools, sed/awk |
| Enforce | Remove pardoned exclusions in CI and fail builds on new violations. |
All critical tests passing. | CI pipeline update |
| Optimize | Review false positives and adjust PHPStan rules or test stubs. | Enforcement phase complete. | vendor/bin/phpstan analyse --level=max |
How can I help you explore Laravel packages today?