Skip to content

[6.x] Fix Faker Unique caching issue#32703

Merged
taylorotwell merged 2 commits into
laravel:6.xfrom
deleugpn:patch-1
May 6, 2020
Merged

[6.x] Fix Faker Unique caching issue#32703
taylorotwell merged 2 commits into
laravel:6.xfrom
deleugpn:patch-1

Conversation

@deleugpn

@deleugpn deleugpn commented May 6, 2020

Copy link
Copy Markdown
Contributor

Fixes #32700

I tested this change in my own codebase and it worked. The idea is somewhat simple: Assuming Faker is a singleton, the Container factory is executed only once per test. That means within a test, Faker will do it's job of ensuring uniqueness whenever asked.
As soon as the test finishes and the container is flushed, Laravel will start a container from scratch, which will run the container factory once again and it will clear the unique cache from faker by calling $faker->unique(true) (as suggested #32700 (comment)).

The reason for this description is to pay extra attention in $app->singleton(). This is not something easy to catch on a unit test, after all we're testing if unique works across multiple tests. If somebody ever makes Faker a non-singleton, that would cause the factory to be executed on every instantiation of the class and would again break the unique().

@deleugpn deleugpn changed the title Fix Faker Unique caching issue [6.x] Fix Faker Unique caching issue May 6, 2020
@taylorotwell
taylorotwell merged commit 5624bfc into laravel:6.x May 6, 2020
@deleugpn
deleugpn deleted the patch-1 branch May 6, 2020 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants