2

These are my configs:

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="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
        <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>

.env.testing

APP_NAME=metrina
APP_ENV=testing
APP_KEY=base64:***************************
APP_DEBUG=true
APP_URL=http://localhost:81
# ...
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3307
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=
DB_ENGINE=InnoDB
# ...

and .env:

APP_NAME=metrina
APP_ENV=local
APP_KEY=base64:***************************
APP_DEBUG=true
APP_URL=http://localhost:81

#...

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3307
DB_DATABASE=actual
DB_USERNAME=root
DB_PASSWORD=
DB_ENGINE=InnoDB

#...

And this is my feature test:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TradeTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $databaseName = \DB::connection()->getDatabaseName();
        dd($databaseName);
    }
}

and result of $ php artisan test:

Testing started at 11:11 PM ...
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

"actual"
Process finished with exit code 1

The main problem is test not using my .env.testing, otherwise it must returns

"testing"

Instead of

"actual"

I've tried php artisan optimize already


Update

This will works only if I first manually remove the content of ./bootrap/cache directory and then run the test with following command

$ php artisan test --env=testing
Pejman
  • 2,442
  • 4
  • 34
  • 62
  • Did you try specifying the `env` via the `--env` flag? i.e.: `php artisan --env=testing test`? – Tim Lewis Dec 16 '21 at 20:07
  • Yes I have tried that already, so what is the point of what ever laravel promises anyway? https://laravel.com/docs/8.x/testing#environment says `When running tests, Laravel will automatically set the configuration environment to testing...` in the first line!! Did I do something wrong? or Laravel has bugs ? even with --env=testing. It's only working when I manually remove the `./bootstrap/cache/` files. – Pejman Dec 16 '21 at 20:15
  • https://stackoverflow.com/a/56308862/8015878 - please try this answer and see if it solves. – WebMan Dec 16 '21 at 20:17
  • I've already tried that too, my configs and settings was fine and by default it was what that answer said. :-( @WebMan – Pejman Dec 16 '21 at 20:22
  • @WebMan It works only when I manually clear the cache directory – Pejman Dec 16 '21 at 20:25
  • `php artisan config:clear` - Did you try this ? – WebMan Dec 16 '21 at 20:28
  • So you are saying that I have to run a command before test everytime? @WebMan I'm using phpstorm all the time and I am the maintainer of this project, what about other member of my group? It's not what I'm looking for man, even if it works, I'm trying to find the final solution for this :-) – Pejman Dec 16 '21 at 20:39
  • The automatic `env.testing` does work. I use it on several Laravel projects on multiple machines. It's highly unlikely that it's a bug with laravel. I would check things like file/directory permissions – Brian Thompson Dec 16 '21 at 20:44
  • I'm using windows @BrianThompson so it not like permission error but thanks man, It helps me to not give up. I'll try to find the solution and make an answer here for anyone who has this problem like me :-) – Pejman Dec 16 '21 at 20:46
  • Being on windows doesn't remove the possibility of permission issues. How are you running it in windows? WSL or some other way of running PHP? – Brian Thompson Dec 16 '21 at 20:47
  • I'm using wamp server, and the project is in `www` folder of wamp, and Im gonna check the permissions of course – Pejman Dec 16 '21 at 20:49
  • https://stackoverflow.com/questions/56605551/laravel-5-8-env-testing-file-is-not-working – miken32 Dec 16 '21 at 20:51

1 Answers1

2

Laravel does not load the .env files when you have cached your config. Laravel warns that the environment variables should only be accessed inside the config files, so once the config files are cached, there is no reason to load the .env files anymore.

In this case, Laravel's expectation is to not cache your config in your development environment. Only cache your config in your production environment.

From the documentation on config caching:

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded; therefore, the env function will only return external, system level environment variables.

patricus
  • 59,488
  • 15
  • 143
  • 145