From 426a2854318e15991c5408acf36efb2e7aa37ebe Mon Sep 17 00:00:00 2001 From: Manish Verma Date: Mon, 17 Sep 2018 12:15:29 +0530 Subject: [PATCH] Travis DB setup - Added testing setup command for configuring testing db and environment - Updated travis.yml to use MySQL service Apply fixes from StyleCI added user to access db in travis.yml updates updates Apply fixes from StyleCI --- .travis.yml | 5 + app/Console/Commands/SetupTestEnv.php | 198 ++++++++++++++++++++++++++ app/Console/Kernel.php | 1 + app/Http/helpers.php | 20 +++ 4 files changed, 224 insertions(+) create mode 100644 app/Console/Commands/SetupTestEnv.php diff --git a/.travis.yml b/.travis.yml index a7e84bf83..ea00759e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,10 @@ # Required to run your project under the correct environment. language: php +#Use Mysql service for DB to run testcases +services: + - mysql + # Versions of PHP you want your project run with. php: - 7.1 @@ -10,6 +14,7 @@ php: before_script: - composer self-update - composer install --prefer-source --no-interaction --dev + - php artisan testing-setup --username="root" # Commands you want to run that will verify your build. script: phpunit diff --git a/app/Console/Commands/SetupTestEnv.php b/app/Console/Commands/SetupTestEnv.php new file mode 100644 index 000000000..86e4e93fb --- /dev/null +++ b/app/Console/Commands/SetupTestEnv.php @@ -0,0 +1,198 @@ +option('username') ? $this->option('username') : env('DB_USERNAME'); + $dbPassword = $this->option('password') ? $this->option('password') : (env('DB_PASSWORD')) ? env('DB_PASSWORD') : ''; + $this->setupConfig($dbUsername, $dbPassword); + + echo "\nCreating database...\n"; + $dbName = 'testing_db'; + createDB($dbName); + echo "\nDatabase Created Successfully!\n"; + + //setting up new database name + Config::set('database.connections.mysql.database', $dbName); + + //opening a database connection + DB::purge('mysql'); + + $this->migrate(); + + $this->seed(); + + //closing the database connection + DB::disconnect('mysql'); + + $this->createEnv($dbUsername, $dbPassword); + + $this->updateAppUrl(); + + echo "\nTesting Database setup Successfully\n"; + } + + /** + * Sets up DB config for testing. + * + * @param string $dbUsername mysql username + * @param string $dbPassword mysql password + * + * @return null + */ + private function setupConfig($dbUsername, $dbPassword) + { + Config::set('app.env', 'development'); + Config::set('database.connections.mysql.port', ''); + Config::set('database.connections.mysql.database', null); + Config::set('database.connections.mysql.username', $dbUsername); + Config::set('database.connections.mysql.password', $dbPassword); + Config::set('database.connections.mysql.engine', 'Innodb'); + Config::set('database.install', 0); + } + + /** + * migrates DB. + * + * @return null + */ + private function migrate() + { + try { + echo "\nMigrating...\n"; + Artisan::call('migrate', ['--force'=>true]); + + echo Artisan::output(); + + //migrating plugins + $this->migratePlugins(); + + echo "\nMigrated Successfully!\n"; + } catch (\Exception $e) { + echo "\n".$e->getMessage()."\n"; + } + } + + /** + * Will run plugin migrations. + * + * @return null + */ + public function migratePlugins() + { + $pluginBasePath = app_path().DIRECTORY_SEPARATOR.'Plugins'; + + // check all the folders inside plugin folder and run all the migration if exists + $plugins = scandir($pluginBasePath); + + foreach ($plugins as $plugin) { + $migrationPath = $pluginBasePath.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations'; + $migrationRelativePath = "app/Plugins/$plugin/database/migrations"; + + if (file_exists($migrationPath)) { + echo "\nMigrating $plugin tables\n"; + Artisan::call('migrate', ['--path'=>$migrationRelativePath, '--force'=>true]); + echo Artisan::output(); + } + } + } + + /** + * seeds DB. + * + * @return null + */ + private function seed() + { + try { + echo "\nSeeding...\n"; + Artisan::call('db:seed', ['--force'=>true]); + echo Artisan::output(); + echo "\nSeeded Successfully!\n"; + } catch (\Exception $e) { + echo "\n".$e."\n"; + } + } + + /** + * updates app url in the DB (by default it is localhost). + * + * @return bool true on success else false + */ + private function updateAppUrl() + { + return System::first()->update(['url'=>'http://localhost:8000']); + } + + /** + * Creates an env file if not exists already. + * + * @param string $dbUsername + * @param string $dbPassword + * + * @return null + */ + private function createEnv(string $dbUsername, string $dbPassword) + { + $env['DB_USERNAME'] = $dbUsername; + $env['DB_PASSWORD'] = $dbPassword; + $env['APP_ENV'] = 'development'; + + $config = ''; + + foreach ($env as $key => $val) { + $config .= "{$key}={$val}\n"; + } + + $envLocation = base_path().DIRECTORY_SEPARATOR.'.env'; + + if (is_file($envLocation)) { + echo "\nEnvironment file already exists. It is assumed that username and password in the file is correct\n"; + + return; + } + + // Write environment file + $fp = fopen(base_path().DIRECTORY_SEPARATOR.'.env', 'w'); + fwrite($fp, $config); + fclose($fp); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index fd3938f32..f3519dc75 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -22,6 +22,7 @@ class Kernel extends ConsoleKernel \App\Console\Commands\DropTables::class, \App\Console\Commands\Install::class, \App\Console\Commands\InstallDB::class, + \App\Console\Commands\SetupTestEnv::class, ]; /** diff --git a/app/Http/helpers.php b/app/Http/helpers.php index 2524aa9fa..589040d74 100644 --- a/app/Http/helpers.php +++ b/app/Http/helpers.php @@ -270,3 +270,23 @@ function exceptionResponse(\Exception $exception) 'exception' => $exception->getMessage(), ], 500); } + +/** + * Creates an empty DB with given name. + * + * @param string $dbName name of the DB + * + * @return null + */ +function createDB(string $dbName) +{ + \DB::purge('mysql'); + // removing old db + \DB::connection('mysql')->getPdo()->exec("DROP DATABASE IF EXISTS `{$dbName}`"); + + // Creating testing_db + \DB::connection('mysql')->getPdo()->exec("CREATE DATABASE `{$dbName}`"); + //disconnecting it will remove database config from the memory so that new database name can be + // populated + \DB::disconnect('mysql'); +}