[9.x] Allow WithFaker to be used when app is not bound #46529
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
I wanted to use the WithFaker trait outside of the base TestCase.
makeFaker()
will throw an exception becauseconfig()
is not available untilIlluminate\Foundation\helpers.php
has been requiredSolution
Rely on
\Faker\Factory::DEFAULT_LOCALE
and if the app is bound, then use the app's config.Other Thoughts
Unit tests, by nature, shouldn't require setting up the entire application. The Laravel application pushes us towards using PHPUnit's TestCase class, which doesn't set up anything. Makes sense... except... For our application, we are running into the problem where we want a few broader categories for tests.
Ideally, we would like to have Unit tests (for testing that doesn't rely on Laravel at all), Feature tests (which are still testing small pieces of the application, but would require Events, Notifications, database seeding, etc), and then finally Integration tests (which test multiple features spanning many units of work). Trying to make a test case that doesn't extend from the Foundation TestCase is difficult.
It might be nice to move
Illuminate\Foundation\Testing\TestCase@setUpTraits()
to a Trait so that it's easier to compose a base*TestCase
class. Otherwise, we have to copy-paste code from the Foundation TestCase which is better to avoid.I think this level of granularity could be helpful. Happy to implement this if it's desirable.