Most of my classes include loading env variables for configuration. When I was coding I thought vlucas/phpdotenv ignored the .env file on testing environments since there is already a conf file like phpunit.xml where you define them.
Is it possible to make dotenv ignore .env when on testing environment?
Testing will use your .env file, but you can override any of its settings in phpunit.xml.
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_DRIVER" value="sqlite"/>
<env name="CACHE_HOST" value="localhost"/>
<env name="SESSION_HOST" value="localhost"/>
</php>
Related
I am trying to run laravel feature tests in parallel mode as it is in document. My phpunit.xml is configed as below :
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
</php>
and I am running the tests with this command :
php artisan test --parallel
The result is all tests fail with this error message :
PDOException: SQLSTATE[HY000] [1049] Unknown database 'mydbname_test_11'
should I create this database before starting test or laravel creates that automatically ?
Yes, the database must exist before you run your test. another thing to note is that parallel unlike PHP artisan test, will not migrate your database for you. So what I found works best is to migrate my database before running the test in parallel.
When running tests in parallel mode with Laravel, it is necessary to create a separate database for each parallel test process. Laravel will not create these databases automatically, so it is up to you to ensure that they exist before running your tests. You can either create the databases manually, or you can use a tool like PHPUnit's #database annotations to automatically create and tear down the necessary databases before and after each test run.
In your phpunit.xml file, you can specify the DB_DATABASE and DB_USERNAME environment variables to control which database Laravel should use for testing. For example:
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="mydbname_test_%s"/>
<env name="DB_USERNAME" value="myusername"/>
</php>
The %s in the DB_DATABASE value will be replaced with the current test process number, so each test process will use a different database. You will need to make sure that a database with the appropriate name exists for each test process before running your tests.
You can then use the #database annotation in your test classes to control the creation and destruction of these databases.
I have app which uses laravel 5.0 and phpunit 4.8.
I have test, which looks like this:
$this->call('POST', 'api/get_user_by_id', [
'token' => 'token',
'user_id' => 3,
]);
It connects to may main database and get data from it. But I want test connect to other, test database and work with it. How can I do it?
You can configure your test environment in your phpunit.xml file:
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_HOST" value="localhost"/>
<env name="DB_PORT" value="3306"/>
<env name="DB_USERNAME" value="username"/>
<env name="DB_PASSWORD" value="password"/>
</php>
I'm having this weird problem that doesn't always happen and sometimes php artisan config:cache fixes it.
Sometimes when I run phpunit test, it loads the local environment configuration and messes up the database. Changing values in phpunit.xml doesn't seem to have any effect at all.
Any idea why this happening and how to fix this?
I've tried overriding the createsApplication method and manually set the environment variables, clearing cache, caching config, PHPStorm test suit with custom configuration.
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
Im trying to test mail sending in laravel 5.1 application.
I need run tests on prod server.
I need MAIL_DRIVER=log at app environment for running test.
And MAIL_DRIVER=smtp for production in my .env file.
I edited phpunit.xml at application root folder and added line:
<env name="MAIL_DRIVER" value="log"/>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="log"/>
</php>
The problem is when I running test it still use MAIL_DRIVER=smtp
Any idea what is wrong?
Try to redefine mail transport type directly. Changing environment constants will do nothing (or share the code where MAIL_DRIVER is used).
I mm trying to set 3 mode in laravel 5 Example : mode local , staging , production
I am setting environment 3 mode in .env but I try to separate to
.local.env , .staging.env and production.env someone here have any idea to do like this ?
Now i'm try in .env -> APP_ENV to 3 mode right now :)
Thank you
Other answers/comments are wrong.
You only store one .env per environment. That is:
Your local machine will have a .env with your local config
The staging maching will have a .env with your staging config,
and
Your production maching will have a .env with your production config
So it is always one .env file per machine. Laravel will load that config from that file.
note that .env file is in .gitignore, .env.example is not
When testing on local machine using PHPUnit you can add env variables in phpunit.xml
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_DEBUG" value="true"/>
<env name="APP_KEY" value="some crazy value"/>
<env name="DB_DRIVER" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
</php>