[9.x] Add dynamic trashed factory state#42414
Conversation
|
Gorgeous stuff 👌 I would just say set the deleted at default to be now() as subDay might cause confusion. |
|
Not sure about the failing tests. All the database test cases passed for me locally (PHP 8.1). |
trashed factory statetrashed factory state
|
@lukeraymonddowning, the only reason for a "past" date would be to avoid any foot-gun date comparisons in tests since they run so quickly. For example: $model = Model::factory()->trashed()->create();
$this->assertTrue($model->deleted_at->isPast())Open to changing it. But that was my reasoning. |
|
@jasonmccreary good point 👍 |
|
I'm getting one test failure and one error locally on PHP 8.1. I do not see any failures or errors on current |
|
@taylorotwell, unfortunately, I don't get these failures locally on PHP 8 or PHP 8.1. |
|
@jasonmccreary retriggered the tests for ya. Might be a hiccup. |
|
@driesvints, actually, seems like I do get the failure when I run the entire test suite. Strange, as when it's run independently it passes. Maybe some kind of test pollution. I'll take a closer look. |
29c7d36 to
2b2f1c8
Compare
|
These same tests fail even when I remove the new code. I rebased, just in case there was an error on I'm at a bit of a loss. I'll look more closely this evening. |
f273f54 to
2b2f1c8
Compare
|
I think the $timeOnline = $trashed->created_at->diff($trashed->deleted_at);It could be fixed by only using // in Factory class
public function trashed()
{
return $this->state([
'deleted_at' => now()
]);
} |
|
@DarkGhostHunter, I'm open to changing it. Keep in mind, this does support passing in the |
|
@taylorotwell + @driesvints, it was test pollution. Everything is passing now, so I'm marking this for review. Let me know what you think about the default timestamp (now versus yesterday). Glad to make any final changes. |
|
Does it handles intervals? For example, I need to compute messages that have been "online" for more than a minute. This way I can warn the user that deleting it may not remove it from the internerks. |
This adds dynamic support of a
trashedfactory state for models using theSoftDeletestrait. It has an optional argument of the date value. If not passed, the default is yesterday.Before
If this is merged, you would no longer need to define such a state in factories for
SoftDeletesmodels.This was implemented within
__call. As such, this should be fully backward compatible if atrashedmethod already exists. A test case also demonstrates this.