I’m working on a Laravel project that uses multiple MySQL databases. The main database (core_db) contains system tables like users, migrations, etc., while express_db and regio_db contain most of the other application tables. For testing, I’m using phpunit and pest, and I want to ensure that the tests run efficiently without unnecessary migrations. Even tho all migrations are already ran, Laravel is rerunning the migrations, and then its failing, because the main dbs are already up2date but Laravel is not recognizing it.
Here are the relevant configurations and files:
<?php
namespace Tests;
use IlluminateFoundationTesting{RefreshDatabase, TestCase as BaseTestCase};
use LaravelTelescopeTelescope;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config()->set('audit.enabled', false);
Telescope::stopRecording();
}
}
phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/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>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="core_db_testing"/>
<env name="DB_USERNAME" value="dbuser"/>
<env name="DB_PASSWORD" value="test"/>
<env name="DB_CONNECTION_EXPRESS" value="mysql"/>
<env name="DB_DATABASE_EXPRESS" value="express_db_testing"/>
<env name="DB_USERNAME_EXPRESS" value="dbuser"/>
<env name="DB_PASSWORD_EXPRESS" value="test"/>
<env name="DB_CONNECTION_REGIOANALYSIS" value="mysql"/>
<env name="DB_DATABASE_REGIOANALYSIS" value="regio_db_testing"/>
<env name="DB_USERNAME_REGIOANALYSIS" value="dbuser"/>
<env name="DB_PASSWORD_REGIOANALYSIS" value="test"/>
<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>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
pest.php:
<?php
use AppModelsUser;
use LaravelTelescopeTelescope;
uses(TestsTestCase::class)
->beforeEach(function () {
Telescope::stopRecording();
})
->in('Feature');
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
.env.testing:
APP_ENV=testing
DB_CONNECTION=mysql
DB_HOST=coreapi-mysql-1
DB_PORT=3306
DB_DATABASE=core_db_testing
DB_USERNAME=dbuser
DB_PASSWORD=test
DB_CONNECTION_EXPRESS=mysql
DB_HOST_EXPRESS=database-mysql-1
DB_PORT_EXPRESS=3306
DB_DATABASE_EXPRESS=express_db_testing
DB_USERNAME_EXPRESS=dbuser
DB_PASSWORD_EXPRESS=test
DB_CONNECTION_REGIOANALYSIS=mysql
DB_HOST_REGIOANALYSIS=database-mysql-1
DB_PORT_REGIOANALYSIS=3306
DB_DATABASE_REGIOANALYSIS=regio_db_testing
DB_USERNAME_REGIOANALYSIS=dbuser
DB_PASSWORD_REGIOANALYSIS=test
I’m running the tests using the following command:
sail artisan test --profile --stop-on-failure --exclude-group=brands_api --env=testing
Even though I use the RefreshDatabase trait, migrations are still being run during tests, causing them to fail. According to the Laravel documentation, RefreshDatabase should only migrate if the schema is out of date and otherwise just wrap each test in a transaction.
Same behaviour for DatabaseTransactions or DatabaseMigrations of course.
How can I ensure that migrations are only run if necessary and that the database state is correctly maintained for tests? Additionally, how can I prevent migrations from running every time if the schema is already up to date? What am I missing? Why is it re-running the migrations every time and cause the whole test to fail, since the migrations are already done?