Hello,
I begin my first tests and I wonder if I do that correctly. To be more precise, I never did tests, so I discover both tests and tests with Livewire / PHP Unit / Features.
I have read the documentation and some MOOC about the subject, but I'm not sure about what I have done.
Here is an example of what I have done for testing C(R)UD.
In my code, you can see that I create a new group (test for creating), then I update the group that has just been created in the previous test (test for updating) and finally I delete the updated group (test for deleting).
/** @test */
public function admin_can_create()
{
$user = User::where('admin', true)->first();
$this->actingAs($user);
Livewire::test(AdminGroupForm::class)
->set('updateMode', false)
->set('group.name', 'Nouveau groupe')
->set('group.description', 'Ceci est un nouveau groupe')
->call('save');
$result = Group::
where('name', 'Nouveau groupe')
->where('description', 'Ceci est un nouveau groupe')
->exists();
$this->assertTrue($result);
}
/** @test */
public function admin_can_update()
{
$user = User::where('admin', true)->first();
$this->actingAs($user);
$group = Group::
where('name', 'Nouveau groupe')
->where('description', 'Ceci est un nouveau groupe')
->first();
Livewire::test(AdminGroupForm::class)
->set('updateMode', true)
->call('initForm', $group->id)
->set('group.name', 'Nouveau groupe corrigé')
->set('group.description', 'Ceci est une nouvelle description corrigée')
->call('save');
$result = Group::
where('name', 'Nouveau groupe corrigé')
->where('description', 'Ceci est une nouvelle description corrigée')
->exists();
$this->assertTrue($result);
}
/** @test */
public function admin_can_delete()
{
$user = User::where('admin', true)->first();
$this->actingAs($user);
$group = Group::
where('name', 'Nouveau groupe corrigé')
->where('description', 'Ceci est une nouvelle description corrigée')
->first();
Livewire::test(AdminGroups::class)
->call('delete', $group->id);
$result = Group::
where('name', 'Nouveau groupe corrigé')
->where('description', 'Ceci est une nouvelle description corrigée')
->doesntExist();
$this->assertTrue($result);
}
Perhaps it would be better, for my update and delete test to create a group (inside the test method) and work with this created group ? Because if the create test does not work, this means that automatically my update and delete tests won't work too.
And what about using factories to generate the contents for testing ? And where use them ? Inside the test methods ? Manually before the test methods ?
I think I'm a little lost.
Could you help me ? ;)
Thanks a lot.
Vincent