TL;DR: if you call Str::createUuidsUsing(…)
in a test method, don’t forget to call Str::createUuidsNormally()
later in that same method (or the tearDown
method), or the rest of your test suite will continue to use that same UUID.
Laravel’s string helper provides a nice interface for generating uuids, as well as a nice way to fake the UUIDs during a test:
While this is very useful, I expected that it would reset between tests, similar to Queue::fake()
, Http::fake()
, etc.
However, because of how the Str
helper generates UUIDs, whatever you provide will be used for the rest of the test run. If you have other tests or app code that expects a unique UUID each time Str::uuid()
is used, you may get unexpected results.
There are a couple of options to work around this:
- After running the code that needs a UUID, call
Str::createUuidsNormally()
to reset theStr
helper. - If you don’t actually need the value of the UUID for testing, you can wrap your code in the
freezeUuids()
method instead. Once your code in the callback finishes running, the framework will callcreateUuidsNormally()
to reset everything for you: