feat: eloquent factories (primarily for tests) #3982
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.
Please ignore the failing PHPStan and Frontend tests, those have been fixed in other PRs that are still open
Changes proposed in this pull request:
This PR introduces some eloquent factories for some models. The primary reason for introducing this is the following use case within tests:
We have a very large number of tests, in most tests we declare the data we need in the database like so:
We set `comment_count' to 1 almost always rather than not, so that we are certain the discussions are visible. The problem is that when you introduce a new column that requires you to similarly always give it a value, you have to go through all the data in all the tests and make changes accordingly.. Which is very inconvenient.
By introducing factories, each model has a default template of fake data that can be edited, and the data specified in the tests is passed to the model factory to generate the final data.
So with this PR, it becomes as follows:
To implement a factory class, the model must use the
HasFactory
trait, and the factory class which extendsIlluminate\Database\Eloquent\Factories\Factory
can exist in the same namespace as the model class. Otherwise the model may specify the class of the factory through the staticnewFactory
method.More on factories here: https://laravel.com/docs/10.x/eloquent-factories.
Additionally, factories are useful for other features the ecosystem may build (an example can involve a previewing feature of sorts).
Reviewers should focus on:
Screenshot
QA
Necessity
Confirmed
composer test
).Required changes: