Summer Sale! All accounts are 50% off this week.

Lars-Janssen's avatar

phpunit use right database

hey,

I'm testing an api function with the following code:

      factory(Company::class,1)
            ->create(['CompanyId' => 1]);
        factory(School::class,1)
            ->create(['SchoolId' => 1, 'CompanyId' => 1]);
        factory(Mentor::class,1)
            ->create(['MentorId' => 1, 'SchoolId' => 1, 'CompanyId' => 1]);
        factory(Department::class,1)
            ->create(['DepartmentId' => 1,'CompanyId' => 1]);
        factory(Intern::class,1)
            ->create(['InternId' => 1, 'MentorId' => 1]);
        factory(Employee::class,1)
            ->create(['EmployeeId' => 1, 'RoleId' => 1, 'DepartmentId' => 1, 'InternId' => 1, ]);

        $client = new Client();
        $res = $client->request('POST', $this->url.'/api/v1/school/1', [
            'form_params' => [
                'currentUserId' => 1
            ]
        ]);
        $this->assertEquals(200, $res->getStatusCode());

How can I make sure that it makes a post request to my testdatabase. I already changed this: in my phpunit.xml but it makes a post request to the wrong database.

How could I accomplish that?

0 likes
11 replies
skliche's avatar

@lars64 What exactly did you change? Did you add a new database connection to config/database.php and specify that in your phpunit.xml? E.g. in config/database.php

'sqlite_testing' => [
            'driver'   => 'sqlite',
            'database' => database_path('testing.sqlite'),
            'prefix'   => '',
        ],

and in phpunit.xml

<env name="DB_CONNECTION" value="sqlite_testing"/>

Then just migrate your database ...

php artisan migrate --database=sqlite_testing

... and phpunit uses that database.

1 like
Lars-Janssen's avatar

@skliche Yes I've that. But when I want to test my api routes I've to change

  'default' => env('DB_CONNECTION', 'mysql'),

to


    'default' => env('DB_CONNECTION', 'mysql_testing'),
Mirdrack's avatar

@lara_elicit s you need to change the environment on the phpunit.xml

This is my file, you can check it

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite"/>
    </php>
</phpunit>
Lars-Janssen's avatar

@Mirdrack I've changed DB_CONNECTION in phpunit.xml this is my phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="mysql_testing"/>
    </php>
</phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="mysql_testing"/>
    </php>
</phpunit>

And my connection for testing:

  'mysql_testing' => [
            'driver'    => 'mysql',
            'host'      => env('TEST_DB_HOST', 'localhost'),
            'database'  => env('TEST_DB_DATABASE', 'forge'),
            'username'  => env('TEST_DB_USERNAME', 'forge'),
            'password'  => env('TEST_DB_PASSWORD', ''),
            'port'      => env('DB_PORT','33060'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
skliche's avatar

@lars64 Does your environment configuration for TEST_DB_DATABASE contain a different database name than your other database configuration?

skliche's avatar

@lars64 Ok, sounds like your configuration is ok but is not actually used. Are you using Homestead?

Try the following in your test file:

public function setUp() {
    parent::setUp();
    dd(DB::connection()->getConfig(null));
}

If the above outputs the correct database settings: Please try it with a sqlite database like I described it in my first reply. I have just used those steps on a different project that uses MySQL as the production database and there it works just fine, the tests use the sqlite database.

durino13's avatar

Hi. Did you manage to resolve this problem? I'm also experiencing this issue. I have the correct connection set in phpunit.xml, but still, my tests are using the production database.

Thx.

ruslansteiger's avatar
php artisan optimize:clear

You need to clear the configuration cache.

I was running in the same problem and did not notice that my configuration was still cached. Maybe an old post but I did stumble on it on google. Have a great day mates ✌️

5 likes

Please or to participate in this conversation.