-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[5.4] Factory: refresh model(s) from database when created. #19621
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
Conversation
Add full tests for Model::fresh() and Collection::fresh() methods.
Seems like that is the nature of 'create' in general. You get back what you've provided. Otherwise you're making another trip to the dB when many times all you want to do is create something and move on. |
Hum... I understand your pov, and it can be ok for the I had some problem with assertion where I was comparing collections from factories, and collections from builder, and the difference was all the fields not set by the factories, but by the database (null and default). It's something we don't think about, and it can help a lot. It cost nothing to fresh it, but it can bring back a lot imo. If you want we can just add a new method which create and fresh..? |
Maybe you can just do |
Yeah, it's what I had to do on every factory in my tests. Here, a real example about that: public function testToVue()
{
$section = factory(Section::class)->create()->fresh();
$paramsShown = collect()->times(2, function ($time) use ($section) {
return factory(Param::class)->create([
'section_id' => $section->id,
'type' => $time,
'order' => $time,
])->fresh();
});
factory(Param::class)->create([
'section_id' => $section->id,
'type' => Param::TYPE_HIDDEN,
])->fresh();
$this->assertEquals(collect([
'title' => $section->title,
'column' => $section->column,
'params' => $paramsShown->map->toVue(),
]), $section->toVue());
} If I forget the |
Can't you solve this by simply extending/completing your factories? |
@tillkruss Yeah as I said I totally can manage that on my own projects, but I think it interesting enough to share it, and make it as a default. In my opinion a factory is the easy and quick way to have a full working model. Not just a part of it. It's why I'm proposing to do it directly. |
I think for now I'll just let people define default values in their factories rather than hit the DB each time again. |
@mathieutu - for what its worth, I |
Ok no problem, thank you all for your comments! |
Hi,
Here we are for the third PR of the day!
When we create some models from FactoryBuilder, it saves it in database, but the model is not refreshed from it.
It can create some problem, because the object you have is not the exact reflect of the true model stored in database. I think specially at all the default values in database.
Example:
Before the PR:
After the PR :
I unfortunately didn't test it because I didn't find any test about FactoryBuilder in the framework.. 😰
I don't have the time right now to write them all, maybe another day..!
This PR is made above the previous one (#19616) because it uses the
fresh()
method on Eloquent Collection. I split them in two PR because they are really two distinct functionalities. Only the last commit is for this one, so it's better to merge the first one (#19616) before this one.See you for the next one guys!
(yeah it's almost ready in local, maybe tomorrow...)
Matt'