duanbimo7212 2015-06-30 07:35
浏览 80
已采纳

将SQLite设置为Laravel 5.1中的单元测试数据库

I'm trying to set-up unit testing in Laravel 5.1. Following the documentation I see this:

Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box

and

When running tests, Laravel will automatically set the configuration environment to testing. Laravel automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.

which is awesome. But... how do I tell Laravel to use SQLite database connection when tests are ran?

Only thing there is in config/database.php is this commented code:

/* We might use this connection for unit tests
'sqlite' => [
    'driver'   => 'sqlite',
    'database' => storage_path().'/database.sqlite',
    'prefix'   => '',
],
*/

We might use this :)

Is there a way to set this globally for all test? Do I need to set the connecton in every test case?

Any help appreciated.

  • 写回答

2条回答 默认 最新

  • dsfe167834 2015-06-30 10:57
    关注

    Actually it's pretty simple.

    Create a testing database on your storage/ folder, with the name database.sqlite or if you want another name or another location you have to change the configs on the config/database.php file, these are the default configs:

    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path('database.sqlite'),
        'prefix'   => '',
    ],
    

    You have two options, you either edit your .env or you just specify the name of the database where you want to run your migrations:

    php artisan migrate --database=sqlite
    

    If you prefer editing your .env file, then we have to add a new variable:

    DB_CONNECTION=sqlite
    

    That's because Laravel defaults to MySQL when this variable is absent from .env file:

    //config/database.php file
    'default' => env('DB_CONNECTION', 'mysql'),
    

    Now that our app is pointing into our sqlite database we can now run our migrations and seeding. After that if you just want to keep running MySQL remove DB_CONNECTION=sqlite from the .env file.

    Now that you have your migrations on your testing database the last step is just to specify, your SQLite as your default database for tests.

    On your root folder you have a phpunit.xml file, open it and a new variable under <php> node:

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

    Now your application is using MySQL and phpunit is using SQLite.

    Remember that if you change the .env to change it back to your default database;

    PS

    Be careful when running your migrations, if you specified a connection on the migration itself, it will try to run it on that connection.

    <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class AdminUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::connection('manage')->create('admin_users', function (Blueprint $t) {
                $t->increments('id');
                $t->string('name');
                $t->softDeletes();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::connection('manage')->dropIfExists('admin_users');
        }
    }
    

    This migration will always run on the connection manage no matter what you specify on the .env file or in the migration command

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵