shariff's avatar
Level 51

PEST using real database instead of test database

Hello developers,

I'm using herd in windows. I have set up 2 database one is real and one is for testing. whenever I'm running php artisan test it is affecting my real database. I tried all the solutions but nothing is working. I'm not able to find out the issue. whenever I'm trying to check the current environment it is coming as local instead of testing.

.env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=***
DB_PASSWORD=***

phpunit.xml file

TestCase.php file

protected function setUp(): void
    {
        parent::setUp();
        $this->actingAs(User::factory()->create());
        dump(app()->environment());
    }

I have cleared cache run commands composer dump-autoload nothing is working.

Thanks

0 likes
10 replies
Nakov's avatar

what happens if you run php artisan test --env=testing so explicitly loading the environment?

And btw, make sure you upgrade the phpunit version as well.

shariff's avatar
Level 51

@Nakov Let me try with this php artisan test --env=testing. I recently installed a new laravel project. Again I need to upgrade phpunit version?

shariff's avatar
Level 51

@Nakov No luck. I have written this test as well. But it is failing. It is coming as local

it('uses test database for test cases', function () {
    dump(app()->environment());
    $this->assertEquals('testing', app()->environment());
    $this->assertEquals('test_db', config('database.connections.mysql.database'));
});
Nakov's avatar

@shariff what does recently means? What does it show if you run composer show phpunit/phpunit ?

and did you do any changes to the Pest.php file?

shariff's avatar
Level 51

@Nakov composer show phpunit/phpunit display phpunit version versions : * 11.4.3

In Pest.php I just uncommented this line ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)

Pest.php file

TestCase.php file

<?php

namespace Tests;

use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->actingAs(User::factory()->create());
        dump(app()->environment());
    }
}

Nakov's avatar
Nakov
Best Answer
Level 73

@shariff really weird, I am trying everything locally to reproduce it, but it simply works.

I tried to see if there are any open issues for Herd, Windows version, and it shows this one: https://github.com/beyondcode/herd-community/issues/601 which is partially related to what is happening to you I guess.

Can you try restarting Herd and see if it will load the changes in your phpunit.xml file, if you updated it after Herd was running.

I am on a Mac, and out of ideas on what can be the issue. Sorry.

shariff's avatar
Level 51

@Nakov I restarted herd. Same issue.

I am on a Mac, and out of ideas on what can be the issue. Sorry.

No worries at all. Thank you so much for your help! I really appreciate your assistance.

jesseyvanofferen's avatar

I think you've cached your config. You should be able to run php artisan config:Clear and that should fix it.

mrkalmdn's avatar

Hey, @shariff.

You need to create a .env.testing file and set the APP_ENV to testing.

Please or to participate in this conversation.