0

I have laravel project, which I'm using two database: relational PostgresSQL and MongoDB.

I have to write feature tests to database but. I don't know how to create connection with mongo from laravel testing.

My phpunit.xml fragment file

<php>
        <env name="MONGO_DB_CONNECTION" value="mongodb"/>
        <env name="MONGO_DB_DSN" value="mongodb://localhost:27017"/>
        <env name="MONGO_DB_DATABASE" value="test"/>
        <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>

config/database.php

'mongodb' => [
        'driver' => 'mongodb',
        'dsn' => env('MONGO_DB_DSN'),
        'database' => env('MONGO_DB_DATABASE', 'homestead'),
    ],

When I run test I got message:

MongoDB\Driver\Exception\ConnectionTimeoutException : No suitable servers found (serverSelectionTryOnce set): [connection refused calling hello on 'localhost:27017']

Can I use MongoDB on memory like env name 'DB_DATABASE' variable

4
  • connection refused calling hello on 'localhost:27017' - did you ensure that there is a server available at that DSN? Commented Dec 7, 2022 at 10:48
  • I can configure it by docker, but I want to use it independent from docker. Like as in memory Commented Dec 7, 2022 at 10:51
  • As far as I know, in-memory is sqlite only Commented Dec 7, 2022 at 11:08
  • Looks like you can do MongoDB in memory: dev.to/remrkabledev/testing-with-mongodb-memory-server-4ja2 Commented Jan 18, 2023 at 13:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.