From 0e02dc2a46f32e8e9e001b6c38603cb683513f9f Mon Sep 17 00:00:00 2001 From: CyberVitexus Date: Wed, 8 Nov 2023 13:44:50 +0100 Subject: [PATCH] Init now can only return false when failed --- src/Ease/Shared.php | 10 +++++++--- tests/src/Ease/SharedTest.php | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Ease/Shared.php b/src/Ease/Shared.php index f188d1a..f79cf91 100644 --- a/src/Ease/Shared.php +++ b/src/Ease/Shared.php @@ -61,8 +61,11 @@ class Shared extends Atom * * @param array $configKeys * @param string $envFile + * @param boolean $exit do exit(1) when unsuccessful ? + * + * @return boolean Configuration success */ - public static function init($configKeys = [], $envFile = '') + public static function init($configKeys = [], $envFile = '', $exit = true) { if (empty($envFile) === false) { if (file_exists($envFile)) { @@ -72,7 +75,7 @@ public static function init($configKeys = [], $envFile = '') } } $configured = true; - if (array_key_exists('DB_CONNECTION', $configKeys) && preg_match('/^sqlite/', self::cfg('DB_CONNECTION', ''))) { + if ((array_search('DB_CONNECTION', $configKeys) !== false) && preg_match('/^sqlite/', self::cfg('DB_CONNECTION', ''))) { unset($configKeys['DB_PASSWORD']); unset($configKeys['DB_USERNAME']); unset($configKeys['DB_HOST']); @@ -84,9 +87,10 @@ public static function init($configKeys = [], $envFile = '') $configured = false; } } - if ($configured === false) { + if ($exit && ($configured === false)) { exit(1); } + return $configured; } /** diff --git a/tests/src/Ease/SharedTest.php b/tests/src/Ease/SharedTest.php index c309860..b589edf 100644 --- a/tests/src/Ease/SharedTest.php +++ b/tests/src/Ease/SharedTest.php @@ -126,6 +126,15 @@ public function testLoadConfig() $this->object->loadConfig('tests/Bootstrap.php', true); } + /** + * @covers Ease\Shared::init + */ + public function testInit() + { + putenv('DB_CONNECTION=sqlite3'); + $this->assertTrue(\Ease\Shared::init(['DB_CONNECTION'], null, false)); + } + /** * @covers Ease\Shared::saveStatusMessages */