logo

A subtle gotcha when faking UUIDs in tests

It works until it doesn't

Joel Clermont
Joel Clermont
2026-03-30

In a previous tip, I showed how to use Str::createUuidsUsing to control UUID generation in your tests. The example I shared returned a plain string from the callback:

Str::createUuidsUsing(fn() => '00000000-1111-2222-3333-000000000000');

While recording a video demo of this tip, I noticed something a bit sloppy about that approach. Str::uuid() normally returns a UuidInterface object, not a plain string. The only reason the plain string in my callback works is that the code I was testing just happened to only ever use the UUID as a string, relying on the normal string coercion behavior.

But if any code tried to call a method on the result, like ->toString(), it would have blown up.

PHP didn't yell at me because Laravel only specifies the UuidInterface as the return type in a docblock, not in actual code.

The better approach is to actually build a proper UUID:

Str::createUuidsUsing(fn() => Uuid::fromString('00000000-1111-2222-3333-000000000000'));

This returns the right kind of object, so it behaves exactly like a real UUID in every context. It costs you nothing and removes the risk of breakage if the code under test changes or if Laravel ever adds a native return type.

Here to help,

Joel

P.S. Sometimes a second set of eyes catches the subtle things that tests miss. Schedule a code review.

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.