php - Laravel Illuminate Testing method for refreshing DB only at the start

one text

Solution:

I wasn't able to find a method for it.
But what I understood was that the idea of keeping the database, and checking it yourself was wrong.

The Laravel Tests themselves are supposed to be something that someone can run in the future with one simple command, such as php artisan test --group=my_group_name.
Therefore the correct way to solve this is to add an assert and compare the tables to your expectations.

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Server\Models\User;
use Server\Models\...; //call multiple models
use Tests\TestCase;

class TestRemoveCertainData extends TestCase
{
    use RefreshDatabase;

    public function testRemoveCertainData()
    {
        
        $users = [
            $this->createUserType1(),
            $this->createUserType2(),
            $this->createUserType3(),
            $this->createUserType4(),
        ];
        
        // deletes emails for users of type 2 and 4
        $this->artisan('delete_certain_emails')->assertSuccessful();

        // expecting the emails of user 2 and 4 to disappear
        $expected = [
            $users[0]->mail,
            null,
            $users[2]->mail,
            null,
        ];

        $result = User::pluck('mail')->toArray();

        $this->assertEquals($expected, $result);
    }
}

Source