jaracas's avatar

Tests keep getting run on my dev db rather than test db

I don't know what's changed, but for some reason sometimes when I run"php artisan test" on my local machine, rather than doing what it used to do (Run against the test database), it runs against my development database. I also get the following error:

Illuminate\Contracts\Container\BindingResolutionException

Target class [env] does not exist.

at C:\project\path\vendor\laravel\framework\src\Illuminate\Container\Container.php:891

This is starting to get annoying since it wipes my several gb local database, instead of using the settings in .env.testing or phpunit.xml

How can I keep this from happening again?

0 likes
10 replies
Tray2's avatar

I'm guessing you don't use the correct parameter when running the tests.

The .env.testing Environment File In addition, you may create a .env.testing file in the root of your project. This file will be used instead of the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.

https://laravel.com/docs/10.x/testing#the-env-testing-environment-file

jaracas's avatar

@Tray2 Do I have to use --env=testing every time to use the .env.testing? Because I already had a .env.testing, and I never had to use --env=testing a few weeks ago.

Tray2's avatar

@jaracas According to the docs yes, or you can make an alias for it, or delete your test env file and update your phpunit.xml instead to use the proper database.

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="BCRYPT_ROUNDS" value="4"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="DB_CONNECTION" value="mysql"/>
    <env name="DB_DATABASE" value="mediabase_v2_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>
gych's avatar

@jaracas No if your configuration is good, you don't have to use this each time.

Can you share your phpunit config?

jaracas's avatar

@gych Sure

<?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>
        <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="test"/>
        <env name="DB_USERNAME" value="test" />
        <env name="DB_PASSWORD" value="" />
        <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>
</phpunit>
gych's avatar

@jaracas If you have the right db credentials in your .env.testing file you don't need to specify those in the phpunit config. You can remove these from the config.

  <php>
    <env name="APP_ENV" value="testing"/>
    <env name="BCRYPT_ROUNDS" value="4"/>
    <env name="CACHE_DRIVER" value="array"/>
    <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>
arthvrian's avatar

Did you check your phpunit.xml? set APP_ENV to testing

<env name="APP_ENV" value="testing"/>

maybe a config:plublish overwrote it

or try setting your database configuration inside the phpunit.xml file (to check if it is not reading the .env.testing)

martinbean's avatar

@jaracas Please tell me you’re not running php artisan config:cache or php artisan optimize locally…

jaracas's avatar

@martinbean I am, I wasn't aware I wasn't supposed to 😅.

I'm guessing that explains the issue? It was caching the .env file and using that instead of .env.testing because a cache existed for config?

martinbean's avatar

@jaracas Yes. If you cache your configuration then Laravel’s going to stop reading environment variables and just read from the cached config instead, which will contain your site’s database credentials.

Run php artisan config:clear and avoid running in locally in the future. You should only be caching things like configuration as part of your deployment process to your production server.

Please or to participate in this conversation.