Laravel 5 : Use different database for testing and local

后端 未结 6 1460
栀梦
栀梦 2020-12-24 12:21

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

相关标签:
6条回答
  • 2020-12-24 12:23

    If your testing database uses the same configuration but only its name is different, it would be enough to only change the selected database name by adding

    <env name="DB_DATABASE" value="testing_db_name"/>
    

    to the file phpunit.xml in the <php> node

    0 讨论(0)
  • 2020-12-24 12:32

    Create a testing database configuration in Laravel

    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.

    Edit PHPUnit configuration

    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.

    0 讨论(0)
  • 2020-12-24 12:36

    For Laravel 5.5, the proper way to do this is create a testing environment file called .env.testing. Here, you can define your testing environment, including the database you want to use for testing...

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=test DB
    DB_USERNAME=test DB user
    DB_PASSWORD=test DB password
    

    Then run this command...

    php artisan config:cache --env=testing
    

    This will configure the cache to the .env.testing file parameters.

    Here is a link to the documentation.

    0 讨论(0)
  • 2020-12-24 12:36

    For those, who want to use sqlite for testing

    Create a test environment file and test.sqlite file

    > cp .env .env.testing
    > touch test.sqlite
    

    In .env.testing file

    DB_CONNECTION=sqlite
    DB_DATABASE=./test.sqlite
    

    Followed by

    php artisan config:cache --env=testing
    

    Now again migrate your databases

    php artisan migrate
    

    if you need then seed it

    php artisan db:seed
    

    And whenever you want to use local env, run

    php artisan config:cache --env=local
    

    Please note, env name can be found in .env file as APP_ENV=local

    0 讨论(0)
  • 2020-12-24 12:39

    if you need a specific database for one of your unit tests, add this code at the start of your unit test

        $connections                                    = config('database.connections');
        $connections['single_test_database_connection'] = [
            'driver'      => 'mysql',
            'host'        => env('DB_HOST', '127.0.0.1'),
            'port'        => env('DB_PORT', '3306'),
            'database'    => 'single_test_database',
            'username'    => env('DB_USERNAME', 'forge'),
            'password'    => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset'     => 'utf8mb4',
            'collation'   => 'utf8mb4_unicode_ci',
            'prefix'      => '',
            'strict'      => true,
            'engine'      => null,
        ];
        config()->set('database.connections', $connections);
        config()->set('database.default', 'single_test_database_connection');
    
    0 讨论(0)
  • 2020-12-24 12:44

    You can use a different .env file for each type of test. You can modify your tests/functional.suite.yml and tests/unit.suite.yml something like this:

    class_name: FunctionalTester
    modules:
        enabled: [Laravel5, FunctionalHelper, Asserts]
        config:
            Laravel5:
                environment_file: .env.testing
    

    And this:

    class_name: UnitTester
    modules:
        enabled: [Asserts, UnitHelper, Laravel5]
        config:
            Laravel5:
                environment_file: .env.unittesting
    

    Or you can simply modify your phpunit.xml and add environment vars like @Tijmen did above.

    0 讨论(0)
提交回复
热议问题