Laravel 5 app always using 'testing' environment configuration

前端 未结 3 1656
南方客
南方客 2021-02-04 08:43

I have a Laravel 5 app with two environments and two configurations: testing (for PHPUnit configuration, in-memory db) and local (my development configuration).

Even whe

相关标签:
3条回答
  • 2021-02-04 09:21

    1) open the config file database.php and add the following inside the 'connections' => [

    'sqlite_testing' => [
        'driver'   => 'sqlite',
        'database' => ':memory:',
        'prefix'   => '',
    ],
    

    this in memory sqlite will be used for your testing

    2) open the .env file and make sure it has the following:

    DB_CONNECTION=mysql
    

    3) open phpunit.xml and add the following inside the <php> tag:

    <env name="DB_CONNECTION" value="sqlite_testing"/>
    

    here’s where you can add all the testing related env variables

    4) in your tests were you use a database connection use the Database Migration Trait:

    use DatabaseMigrations;
    

    for more details about the DatabaseMigrations refer to the documentation https://laravel.com/docs/5.1/testing#resetting-the-database-after-each-test

    0 讨论(0)
  • 2021-02-04 09:29

    Laravel 5 doesn't cascade config files correctly anymore so your testing config file is overriding anything you have in your local config file.

    Now you aren't supposed to have any subfolders for each environment, but rather set configuration settings inside the .env file in the root folder.

    This file isn't checked in to the repo to ensure that nothing sensitive is checked into the repo. You should have a separate .env file for each environment your application is living.

    TESTING

    For php unit (functional) you can set env variables in the phpunit.xml file e.g..

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
    </php>
    

    For behat (acceptance) testing the Laracasts Laravel Behat extension allows you to create a .env.behat file to change the environment variables.

    For phpspec (unit) testing well the environment shouldn't matter as your testing individual methods in isolation and mock everything else.

    For selenium (integration / system / e2e) testing the environment variables should come from the .env file on the server wherever you are doing this testing.

    0 讨论(0)
  • 2021-02-04 09:33

    Additional points I've just had to use to get tests running on sqlite, while the main app normally defaults to mysql:

    Add something like this to phpunit.xml, as explained by craig.michael.morris:

    <env name="DB_DRIVER" value="sqlite"/>
    

    And this change to config/database.php

    'default' => env('DB_DRIVER', 'mysql'),
    

    And in the 'sqlite' section of config/database.php

    'database' => ':memory:',
    
    0 讨论(0)
提交回复
热议问题