jakubjv's avatar

Is possible in Laravel, test multiple databses through phpunit.xml?

Hello everyone, I am struggling with testing in Laravel. This is my phpunit.xml

<?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>
        <testsuite name="Modules">
            <directory suffix="Test.php">./Modules/*/Tests/Feature</directory>
            <directory suffix="Test.php">./Modules/*/Tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="pgsql"/>
        <server name="DB_DATABASE" value="media_testing"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

I have a local database 'media' which is connected to the testing database 'media_testing,' where, during the test execution, re-migration is actually taking place, which is fine. Then I have a database 'orders_import' that is another database connected for testing purposes to the 'orders_import_testing' database. However, when running the test, this error occurs

www@3ae82b37db09:/var/www$ php artisan test --filter StoreItemProductTest

   FAIL  Modules\Store\Tests\Feature\StoreItemProductTest
  ⨯ store item creates store item product

  ---

  • Modules\Store\Tests\Feature\StoreItemProductTest > store item creates store item product
   Illuminate\Database\QueryException 

  SQLSTATE[42P07]: Duplicate table: 7 ERROR:  relation "customer" already exists (SQL: create table "customer" ("id_customer" bigserial primary key not null, "name" varchar(50) null))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
    708▕         // If an exception occurs when attempting to run a query, we'll format the error
    709▕         // message to include the bindings with SQL, which will make this exception a
    710▕         // lot more helpful to the developer instead of just the database's errors.
    711▕         catch (Exception $e) {
  ➜ 712▕             throw new QueryException(
    713▕                 $query, $this->prepareBindings($bindings), $e
    714▕             );
    715▕         }
    716▕     }

      +11 vendor frames 
  12  Modules/Import/Database/Migrations/2023_10_23_102504_create_customer_table.php:19
      Illuminate\Database\Schema\Builder::create("customer", Object(Closure))


  Tests:  1 failed
  Time:   6.36s

I will be glad for every advice. Thank you.

0 likes
1 reply
Tray2's avatar

It's never a good idea to use multiple databases like that, it only makes you life more complex.

Please or to participate in this conversation.