I'm having a strange problem with my Laravel feature tests.
I have a createTeam
method that creates a factory record and persists it in memory-database.
public function createTeam(): static
{
$this->team = Team::factory()->create([
'name' => $this->user->name . ' Team',
]);
$this->team->assignMember($this->user, TeamRoleTypes::OWNER);
return $this;
}
Then I go on and try to test an action.
public function testUserCanDeleteItsOwnTeam()
{
$this->createTeam();
$this
->useToken()
->deleteJson(route('team.delete', ['team' => $this->team->id]), [
'name' => 'Second team',
])
->assertOk();
}
However, the response says "No query results for model [App\Models\Team] e99a7514-58e2-4d29-91f2-f0c3a034a419"
. When I check the same model for existence in the same test by using something like Team::find("e99a7514-58e2-4d29-91f2-f0c3a034a419")
and it says it's there!
Does anyone have any idea why such thing happens?
CodePudding user response:
Turns out, the problem lied with the factory and the mystical behavior of Laravel/PHPUnit/Memory DB or whatever... What happened was caused by the autogenerated factory class trying to fill the timestamp fields manually as can be seen below:
class TeamFactory extends Factory
{
protected $model = Team::class;
public function definition(): array
{
return [
'id' => Str::uuid(),
'name' => $this->faker->name(),
'deleted_at' => Carbon::now(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
];
}
}
The last three definitions were causing the cancer and now everything is working as expected after deletion of these little buggers.