0

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.

  • Create a handler that only logs. Then test that code. However as this is debug logging, the logging itself is the "test" already for the production system. You normally should not need to test that (apart maybe from one test that shows you the logging works the way you think, e.g. that one test where you only test for your logging expectation in a scenario it is isolated). – hakre Jun 29 '21 at 07:04

1 Answers1

1

Since you use the database in Assessment::query(), then it calls the configured database.

There are many ways to deal with it, two of them:

  1. Use another configured database for testing; see this answer of "Laravel: Use different database for testing and local"

  2. Mock the Assessment facade to return test data; see the laravel documentation

hakre
  • 193,403
  • 52
  • 435
  • 836
GrenierJ
  • 1,145
  • 8
  • 18