logo
podcast Podcast
get help Get Unstuck

Tests can show code that will break in the future

Make future upgrades easier

Joel Clermont
Joel Clermont
2024-12-05

I absolutely love finding and fixing problems before they cause bigger issues in my project.

Recently, I saw a short post from Martin Bean that reminded me of a simple way to find breaking code before it actually breaks.

We all know the pain of trying to upgrade some package, only to find out a bunch of our code is now broken. What if instead, we could "see into the future" and fix those things one at a time before we upgrade the package and have to fix them all at once?

Package authors, and Laravel itself, will mark a feature as deprecated before removing it in a future release. We can use these deprecation notices to our advantage!

Assuming you have a good test suite, with the one-line change from Martin's post, you can find all the places you're relying on deprecated behavior in your codebase.

// in your base test case setup method
$this->withoutDeprecationHandling();

That's it! Now if you run your test suite and a deprecated feature is used, it will throw an exception and show you exactly where it's being used.

Martin mentioned this in the context of upgrading PHP versions, and it's just as useful for upgrading Laravel or any other package.

Keep your code tidy and the next major release won't take quite as long to upgrade.

One final note: this won't just catch deprecated usages in your application code, but even in package or framework code. So sometimes it might surface something that will need to be fixed upstream, but at least you'll know about it.

Here to help,

Joel

P.S. And if you don't have a good test suite, don't put it off any longer. We can help get you started.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.