How does one change database for development and testing on local system without editing the .env
file each time?
I have found it quite inconvenient to practice TDD because of this.
Is it possible for a Laravel application to differentiate between normal development and testing so that it can choose the appropriate database?
Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy) PostgreSQL 10.0+ (Version Policy)
Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server.
In . env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database. php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.
Edit the config\database.php
file and add a testing
- array into the connections
array:
'connections' => [ 'testing' => [ 'driver' => env('DB_TEST_DRIVER'), // more details on your testing database ] ]
Then add the necessary variables to your .env
-file.
Open your phpunit.xml
-file and add the following within your <php>
-tag:
<env name="DB_CONNECTION" value="testing"/>
Now PHPUnit will run with the tests on the database you defined in the testing
- array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With