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

[9.x] Allow WithFaker to be used when app is not bound #46529

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Mar 20, 2023

Problem

I wanted to use the WithFaker trait outside of the base TestCase. makeFaker() will throw an exception because config() is not available until Illuminate\Foundation\helpers.php has been required

Solution

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.

@cosmastech cosmastech changed the title Allow WithFaker to be used when app is not bound [9.x] Allow WithFaker to be used when app is not bound Mar 20, 2023
@taylorotwell taylorotwell merged commit 8dee8c0 into laravel:9.x Mar 21, 2023
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