lundi 28 juin 2021

PHPUnit tests actually runs the code instead of just testing

I am not really sure if I set it up correctly or not as it is my first time working with PHPUnit tests.

I want to test one particular console command which sends emails to users.

Here's my unit test.

     /**
     * @test
     */
    public function test_expiring_assessment_command_success()
    {
        $user = $this->getOrCreateTestUser();
        $assessment = $this->getOrCreateTestOnlineVisit($user, 'cancelled');

        Log::debug($assessment); //this log logs the factory created assessment
        Log::debug(env('APP_ENV')); //testing

        $this->artisan('process:send-visit-emails');
    }

This is the code in my SendVistEmails.php

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $from = Carbon::now()->subDays(7)->startOfDay();
        $to = $from->copy()->endOfDay();

        $assessments = Assessment::query()
            ->whereIn('status', ['pending_pause', 'pending'])
            ->whereNotNull('response_required_from')
            ->whereBetween('response_required_from', [$from, $to])
            ->select('details', 'status')
            ->get();

        foreach ($assessments as $a){
            Log::debug($a); //this logs assessments present in DB instead of the one created in my test.
        }

        $assessments->each->expire();
    }

Now, when I run phpunit tests it just interacts with code and sends emails to all the users present in DB.

I want to run command just for testing and in my SendVistEmails.php code, when I log $a, it should have only the assessment my unit test created and not the ones from DB.

I spent two days on this reading so much documentation but couldn't figure out What am I missing.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire