Home > Software design >  Why I got Unknown option "--env" error making tests?
Why I got Unknown option "--env" error making tests?

Time:08-26

Making tests ("phpunit/phpunit": "^9.5.10") for laravel ("laravel/framework": "^9.2") site I want to use .env.testing file

But I got error running command :

$ vendor/bin/phpunit tests/Feature/PagesCrudTest.php     --env=testing

Unknown option "--env"

Checking help I did not find any "env" issue mentioned:

$ vendor/bin/phpunit tests/Feature/PagesCrudTest.php  --help
...

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"
         printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinterForPhpUnit9"
>
    <testsuites>
        <testsuite name="Pages CRUD tests">
            <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="TELESCOPE_ENABLED" value="true"/>

        <!--         <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>
</phpunit>

In which way can I use .env.testing in my tests ?

Updated Info

  1. I have in phpunit.xml line:

    env name="APP_ENV" value="testing"

I suppose this file has valid structure/data

  1. Running command

    vendor/bin/phpunit tests/Feature/PagesCrudTest.php

I make checks of current database name using \DB::connection()->getDatabaseName() in the beginning of the tests and I see that is is from .env file.

  1. So I try with option

    tests --env=testing

I tried to fill option

--env=testing

manually to avoid misspeling when copypasting from browser - but I still got

Unknown option "--env"

error...

Thanks in advance!

CodePudding user response:

There's no need for --env=testing. The phpunit.xml already sets the APP_ENV to testing with <env name="APP_ENV" value="testing"/>

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.

So it will be used by PHPUnit tests and you can use it with other artisan commands as well with --env=testing

CodePudding user response:

You get the error because you pass --env=testing which is an unknown PHPUnit option. If you want to set the environment for testing, you have several options depending on your application. Remember, the application environment doesn't really have anything to do with PHPUnit.

If your application requires an environment variable, e.g. APP_ENV, then you might be able to set it on the command line at runtime:

$ APP_ENV=testing vendor/bin/phpunit tests/Feature/PagesCrudTest.php

If you don't want to have to set it every time you run a test then you might be able to set in the PHPUnit configuration file:

    <php>
        <server name="APP_ENV" value="testing" />
    </php>

See the PHPUnit configuration documentation for more details.

On the other hand, if your application uses PHP dotenv to handle environment configuration then you have even more options, for example in a testing specific dotenv file:

.env.testing

APP_ENV=testing

It all depends on your application. You indicate that you're using Laravel. Read Laravel Environment Configuration and Laravel Testing: Getting Started.

  • Related