Rretzko's avatar
Level 15

Testing truncates MySql database

Hi - I'm early in my learning TDD. I've set up my testing environment to use the in-memory sqlite database, but when I run my tests, the actual MySql database gets wiped out as well. Any thoughts on what I can look for that is triggering this? Here's the phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

Here's an example test:

<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class ProfilePageTest extends TestCase
{
    public function test_page_will_not_open_for_guest()
    {
        $response = $this->get('profile/3');

        $response->assertRedirect('login');
    }

    public function test_page_will_open_for_auth_user()
    {
        $this->withoutExceptionHandling();

		$this-refreshDatabase();

        $user = User::factory()->create();

        auth()->loginUsingId($user->id);

        $response = $this->get(route('profile',['user' => $user]));

        $response->assertViewIs('users.profile');
    }
}

I have the RefreshDatabase in the TestCase model

<?php

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use RefreshDatabase;
}

I didn't have the DB_CONNECTION and DB_DATABASE uncommented on my first few tests, so I understood why I was losing my data then, but I can't figure it out now. Thanks for your help!

0 likes
6 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

It could be cached config. Try php artisan config:clear

Only cache in production

Rretzko's avatar
Level 15

@sinnbeck Winner! I've been working with Laravel for over a year now, so I should know this, but how do I NOT cache on my local environment?

Sinnbeck's avatar

@Rretzko just never run any of the caching commands

Examples

php artisan config:cache
php artisan optimize
Rretzko's avatar
Level 15

Ah, very good, thanks. After a bit of research, I also set up store on config/cache.php

'none' => [
            'driver' => 'null',
        ],

and updated my .env to include

CACHE_DRIVER=none
CACHE_EXPIRE=-1
Sinnbeck's avatar

@Rretzko not quite the same thing. The caches I am talking about will still take effect. They just write one files to the bootstrap directly. What you are changing is how caching in your code works which is different

Please or to participate in this conversation.