librenms/tests/bootstrap.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/**
* bootstrap.php
*
* Initialize the Autoloader and includes for phpunit to be able to run tests
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
2021-09-10 18:09:53 +00:00
*
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
use LibreNMS\Config;
use LibreNMS\Util\Snmpsim;
$install_dir = realpath(__DIR__ . '/..');
$init_modules = ['web', 'discovery', 'polling', 'nodb'];
require $install_dir . '/includes/init.php';
chdir($install_dir);
ini_set('display_errors', '1');
//error_reporting(E_ALL & ~E_WARNING);
$snmpsim = new Snmpsim('127.1.6.2', 1162, null);
if (getenv('SNMPSIM')) {
Laravel 9.x Shift (#14504) * Move `resources/lang` folder * Shift registered middleware * Remove `fruitcake/laravel-cors` dependency * Streamline `$commands` property * Upgrade to Flysystem 3.0 * Shift core files * Convert `optional()` to nullsafe operator * Remove unnecessary `$model` property * Convert route options to fluent methods Laravel 8 adopts the tuple syntax for controller actions. Since the old options array is incompatible with this syntax, Shift converted them to use modern, fluent methods. * Convert deprecated `$dates` property to `$casts` * Shift config files * Default config files In an effort to make upgrading the constantly changing config files easier, Shift defaulted them and merged your true customizations - where ENV variables may not be used. * Bump Laravel dependencies * Use `<env>` tags for configuration `<env>` tags have a lower precedence than system environment variables making it easier to overwrite PHPUnit configuration values in additional environments, such a CI. Review this blog post for more details on configuration precedence when testing Laravel: https://jasonmccreary.me/articles/laravel-testing-configuration-precedence/ * Fix error provider * Match new symfony syntax * Match upstream syntax * Fix route syntax * generate composer.lock * Sync back configs * routes * composer * Fix more flare * fix cors * sync lang * Apply fixes from StyleCI (#14517) Co-authored-by: StyleCI Bot <bot@styleci.io> * bump larastan * update packages * wip * Temporarily lower phpstan level * Update phpstan.neon * wip * wip * wip * Apply fixes from StyleCI (#14592) Co-authored-by: StyleCI Bot <bot@styleci.io> * test * Update CiHelper.php * Update test.yml * Update CiHelper.php * Update CiHelper.php * Apply fixes from StyleCI (#14616) Co-authored-by: StyleCI Bot <bot@styleci.io> * test? * fix phpstan problems * dont run snmpsim on github ci * Fix whitespace * More whitespace * More whitespace ??? * I think the space broke it * fix the reset of the whitespace * hard code auth guard --------- Co-authored-by: Shift <shift@laravelshift.com> Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Tony Murray <murraytony@gmail.com>
2023-04-17 11:51:35 +00:00
if (! getenv('GITHUB_ACTIONS')) {
$snmpsim->fork(6);
}
// make PHP hold on a reference to $snmpsim so it doesn't get destructed
register_shutdown_function(function (Snmpsim $ss) {
$ss->stop();
}, $snmpsim);
}
if (getenv('DBTEST')) {
global $migrate_result, $migrate_output;
// create testing table if needed
$db_config = \config('database.connections.testing');
$connection = new PDO("mysql:host={$db_config['host']};port={$db_config['port']}", $db_config['username'], $db_config['password']);
$result = $connection->query("CREATE DATABASE IF NOT EXISTS {$db_config['database']} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
if ($connection->errorCode() == '42000') {
echo implode(' ', $connection->errorInfo()) . PHP_EOL;
echo "Either create database {$db_config['database']} or populate DB_TEST_USERNAME and DB_TEST_PASSWORD in your .env with credentials that can" . PHP_EOL;
exit(1);
}
unset($connection); // close connection
// sqlite db file
// $dbFile = fopen(storage_path('testing.sqlite'), 'a+');
// ftruncate($dbFile, 0);
// fclose($dbFile);
// try to avoid erasing people's primary databases
if ($db_config['database'] !== \config('database.connections.mysql.database', 'librenms')) {
if (! getenv('SKIP_DB_REFRESH')) {
echo 'Refreshing database...';
$migrate_result = Artisan::call('migrate:fresh', ['--seed' => true, '--env' => 'testing', '--database' => 'testing']);
$migrate_output = Artisan::output();
echo "done\n";
}
} else {
echo "Info: Refusing to reset main database: {$db_config['database']}. Running migrations.\n";
$migrate_result = Artisan::call('migrate', ['--seed' => true, '--env' => 'testing', '--database' => 'testing']);
$migrate_output = Artisan::output();
}
unset($db_config);
}
Config::reload(); // reload the config including database config
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
app()->terminate(); // destroy the bootstrap Laravel application