Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Resolve Faker\Generator out of the container if it is bound #30992

Merged
merged 1 commit into from Jan 1, 2020

Conversation

@ejunker
Copy link
Contributor

ejunker commented Jan 1, 2020

If Faker\Generator is bound in the container then use that instance instead of creating a default version. This allows you to bind your own custom Faker\Generator and add custom faker providers which will be available when using WithFaker

For example, you can create a FakerServiceProvider that can bind it like this:

        $this->app->singleton(Generator::class, function () {
            $faker = Factory::create();
            $faker->addProvider(new CompanyNameGenerator\FakerProvider($faker));

            return $faker;
        });

The code is adding the custom CompanyNameGenerator faker provider. Since it is bound in the container then it will be used and the custom company name provider will be available to be used. Without this change then the CompanyNameGenerator provider would not be available and would have to be manually added everytime you wanted to use it.

If Faker\Generator is bound in the container then use that instance instead of creating a default version. This allows you to bind your own custom Faker\Generator and add custom faker providers which will be available when using WithFaker
@GrahamCampbell GrahamCampbell changed the title Resolve Faker\Generator out of the container if it is bound [6.x] Resolve Faker\Generator out of the container if it is bound Jan 1, 2020
@GrahamCampbell

This comment has been minimized.

Copy link
Member

GrahamCampbell commented Jan 1, 2020

I don't quite follow why you'd need this. Why not just make your own trait?

@taylorotwell taylorotwell merged commit 50e43a5 into laravel:6.x Jan 1, 2020
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@ejunker ejunker deleted the ejunker:patch-1 branch Jan 6, 2020
@telkins

This comment has been minimized.

Copy link
Contributor

telkins commented Jan 10, 2020

Not sure what's going on here, but this seems to have broken several dozen unit tests in one of my projects.

Some of my tests use data providers and some of those use faker to generate some test data. $this->faker is not available, so I use $this->makeFaker() to get one to use to generate the test data.

This now fails. I've just started to look at it, but I wanted to raise the flag in case someone can save me time or is also experiencing the same problem. It may be that I need to make changes in my code...or it may be that this PR has caused an issue.

@driesvints

This comment has been minimized.

Copy link
Member

driesvints commented Jan 10, 2020

@telkins it's already been reported and fixed. Will be in the next release on Tuesday: #31083

@telkins

This comment has been minimized.

Copy link
Contributor

telkins commented Jan 10, 2020

Thx @driesvints . I just came back here to link the fix...looks like you've already done so...and I can now see that it was already linked. Could have saved myself a bit of time...! ;-)

I think I'll take a break now.... :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.