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

dvmian's avatar

MongoDB authentication failed during testing

Hello everyone, I use the jenssegers/mongodb package and get the above error when I run any tests where I use Models. What's more interesting, this error occur only during tests, when I use models in my app, everything is okay.

Details: My mongodb driver config:

   'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', 27017),
            'database' => env('DB_DATABASE', 'homestead'),
            'username' => env('DB_USERNAME', 'homestead'),
            'password' => env('DB_PASSWORD', 'secret'),
            'options' => [
                'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
            ],
        ],

Database enviroment's variables in the .env.testing file:

DB_CONNECTION=mongodb
DB_HOST=mongo-db-test
DB_PORT=27018
DB_DATABASE=testing_db
DB_USERNAME=testing_user
DB_PASSWORD=Pa$$word12345%

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>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

And databases in the docker-compose.yaml

  mongo:
    image: mongo
    container_name: mongo
    restart: unless-stopped
    tty: true
    ports:
      - 27017:27017
    volumes:
      - mongo_data:/var/lib/mongo/data
    environment:
      - MONGO_INITDB_DATABASE=${DB_DATABASE}
      - MONGO_INITDB_ROOT_USERNAME=${DB_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD}
    networks:
      - app-network

  mongo-db-test:
    image: mongo
    container_name: mongo-db-test
    restart: unless-stopped
    tty: true
    command: mongod --port 27018
    ports:
      - 27018:27018
    env_file:
      - .env.testing
    environment:
      - MONGO_INITDB_DATABASE= ${DB_DATABASE}
      - MONGO_INITDB_ROOT_USERNAME= ${DB_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD= ${DB_PASSWORD}
    networks:
      - app-network
0 likes
0 replies

Please or to participate in this conversation.