Last active
September 9, 2020 14:06
-
-
Save neoascetic/6504409 to your computer and use it in GitHub Desktop.
[Laravel] Bootstrap file for (re)creating database before running tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Bootstrap file for (re)creating database before running tests | |
* | |
* You only need to put this file in "bootstrap" directory of the project | |
* and change "bootstrap" phpunit parameter within "phpunit.xml" | |
* from "bootstrap/autoload.php" to "bootstap/testing.php" | |
*/ | |
$testEnvironment = 'testing'; | |
$config = require("app/config/{$testEnvironment}/database.php"); | |
extract($config['connections'][$config['default']]); | |
$connection = new PDO("{$driver}:host={$host}", $username, $password); | |
$connection->query("DROP DATABASE IF EXISTS ".$database); | |
$connection->query("CREATE DATABASE ".$database); | |
require_once('app/libraries/helpers.php'); | |
// run migrations for packages | |
foreach(glob('vendor/*/*', GLOB_ONLYDIR) as $package) { | |
$packageName = substr($package, 7); // drop "vendor" prefix | |
passthru("./artisan migrate --package={$packageName} --env={$testEnvironment}"); | |
} | |
passthru('./artisan migrate --env='.$testEnvironment); | |
require('autoload.php'); // run laravel's original bootstap file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more simpler using this piece of code in setUp method
source laravel/framework#19412 (comment)
You can't use RefreshDatabase trait in first initial test. Because this trait (and some others) should use database that could be not exists