testing php applications
Deprecation Testing
Deprecations warn that code relies on behaviour scheduled to change or disappear. Treat them as maintenance work before an upgrade becomes urgent. PHPUnit can report deprecations and can be configured to fail the suite on relevant deprecation events.
Separate Your Code From Dependencies
A deprecation in application code needs a code change. A dependency deprecation may need a package update, upstream report, temporary baseline, or planned replacement. Keep the source visible so the team can prioritise work.
Use A Ratchet
A mature project may need to document existing dependency noise while preventing new deprecations. Tighten the policy over time instead of ignoring the signal entirely.
<?php
declare(strict_types=1);
function oldReportFormat(): string
{
trigger_error('oldReportFormat() is deprecated; use reportFormat().', E_USER_DEPRECATED);
return 'legacy';
}
set_error_handler(static function (int $severity, string $message): bool {
echo $severity . ': ' . $message . PHP_EOL;
return true;
});
oldReportFormat();
restore_error_handler();
// Prints:
// 16384: oldReportFormat() is deprecated; use reportFormat().
Common Mistakes
- Suppressing every deprecation globally.
- Waiting until the runtime upgrade is blocked.
- Mixing application and dependency deprecations into one unactionable list.
- Ignoring CI exit-code configuration.
What To Practise
- Recognise
E_USER_DEPRECATED. - Configure deprecation visibility and failure policy.
- Plan removal before an upgrade deadline.
Practice
Practice: Plan A Deprecation Ratchet
Create a policy for application deprecations and third-party dependency deprecations.
Requirements
- Fail on new application deprecations.
- Track dependency deprecations separately.
- Assign remediation ownership.
- Recheck policy during upgrades.
Show solution
The categories keep the signal actionable.
- Fail CI for a new deprecation raised by application code and fix it before merge.
- Upgrade a maintained dependency or report its deprecation upstream with an assigned owner.
- Plan replacement for an abandoned dependency.
The exact PHPUnit XML flags depend on the project policy; review the current PHPUnit configuration reference.