Option 1
How to set up a database using migration and seeds, and then using database transactions? ( https://laravel.com/docs/5.1/testing#resetting-the-database-after-each-test )
I wanted to set up a test database using the wizard:
$ php artisan migrate --database=mysql_testing $ php artisan db:seed --database=mysql_testing
As you can guess, I am using mysql, but I do not understand why this should not work for sqlite. This is how I do it.
config /database.php
First, add the test database information to the config / database.php file according to the current database information.
'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], 'mysql_testing' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_TEST_DATABASE'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], ],
If you do this like that, be sure to add DB_TEST_DATABASE to the .env file:
DB_DATABASE=abc DB_TEST_DATABASE=abc_test
phpunit.xml
Any values set in the phpunit.xml file when overwriting the values specified in the .env file. Therefore, we say that phpunit uses the mysql_testing database connection instead of the mysql database connection.
<?xml version="1.0" encoding="UTF-8"?> <phpunit> ... <php> ... <env name="DB_CONNECTION" value="mysql_testing"/> </php>
Testing class
My test classes are as follows:
class MyTest extends \TestCase { use \Illuminate\Foundation\Testing\DatabaseTransactions; public function testSomething() {
Option 2
Here the database is reset before each test, so I prefer option 1. But you can make it work the way you like.
I have tried this before and it might work for you.
Tests /TestCase.php Extend the test case to load a new .env file, .env.testing
<?php class TestCase extends Illuminate\Foundation\Testing\TestCase { protected $baseUrl = 'http://localhost'; public function createApplication() { $app = require __DIR__.'/../bootstrap/app.php'; $app->loadEnvironmentFrom('.env.testing'); $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); return $app; } }
.env.testing
Create this new .env file and add the database information
APP_ENV=testing APP_DEBUG=true APP_KEY=xxx DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_DATABASE=abc_testing DB_USERNAME=xxx DB_PASSWORD=xxx
In class :
Using PDO to delete and recreate a database is easier than trying to truncate everything. Then use mastering to migrate and seed the database.
class MyTest extends TestCase { public static function setUpBeforeClass() { $config = parse_ini_file(".env.testing"); $username = $config['DB_USERNAME']; $password = $config['DB_PASSWORD']; $database = $config['DB_DATABASE']; $host = $config['DB_HOST']; // Create test database $connection = new PDO("mysql:host={$host}", $username, $password); $connection->query("DROP DATABASE IF EXISTS " . $database); $connection->query("CREATE DATABASE " . $database); } public function testHomePage() { Artisan::call('migrate'); Artisan::call('db:seed'); $this->visit('/') ->see('Home') ->see('Please sign in') ->dontSee('Logout'); }