From bfa62a3f0c5a420e9a89cd16f8170e5e7221ab62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 3 May 2020 17:27:37 +0200 Subject: [PATCH] Write test for extra config, fix bug with extra config --- src/DatabaseConfig.php | 11 +++++--- tests/DatabaseManagerTest.php | 5 ---- tests/TenantClassTest.php | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/DatabaseConfig.php b/src/DatabaseConfig.php index 00dc9714..c2e8a875 100644 --- a/src/DatabaseConfig.php +++ b/src/DatabaseConfig.php @@ -125,13 +125,18 @@ class DatabaseConfig }); // Remove DB name because we set that separately - if (isset($dbConfig['_tenancy_db_name'])) { - unset($dbConfig['_tenancy_db_name']); + if (($pos = array_search('_tenancy_db_name', $dbConfig)) !== false) { + unset($dbConfig[$pos]); + } + + // Remove DB connection because that's not used inside the array + if (($pos = array_search('_tenancy_db_connection', $dbConfig)) !== false) { + unset($dbConfig[$pos]); } return array_reduce($dbConfig, function ($config, $key) { return array_merge($config, [ - Str::substr($key, 0, strlen('_tenancy_db_')) => $this->tenant[$key], + Str::substr($key, strlen('_tenancy_db_')) => $this->tenant[$key], ]); }, []); } diff --git a/tests/DatabaseManagerTest.php b/tests/DatabaseManagerTest.php index 0d18ca76..1bb29663 100644 --- a/tests/DatabaseManagerTest.php +++ b/tests/DatabaseManagerTest.php @@ -70,9 +70,4 @@ class DatabaseManagerTest extends TestCase tenancy()->all()->each->delete(); } - - /** @test */ - public function extra_config_is_merged_into_the_connection_config_array() - { - } } diff --git a/tests/TenantClassTest.php b/tests/TenantClassTest.php index 744f76c0..be85b99f 100644 --- a/tests/TenantClassTest.php +++ b/tests/TenantClassTest.php @@ -174,4 +174,54 @@ class TenantClassTest extends TestCase return $tenant->id; })); } + + /** @test */ + public function extra_config_is_merged_into_the_connection_config_array() + { + $tenant = Tenant::new()->withData([ + '_tenancy_db_link' => 'foo', + '_tenancy_db_name' => 'dbname', + '_tenancy_db_username' => 'usernamefoo', + '_tenancy_db_password' => 'passwordfoo', + '_tenancy_db_connection' => 'mysql', + ]); + + config(['database.connections.mysql' => [ + "driver" => "mysql", + "url" => null, + "host" => "mysql", + "port" => "3306", + "database" => "main", + "username" => "root", + "password" => "password", + "unix_socket" => "", + "charset" => "utf8mb4", + "collation" => "utf8mb4_unicode_ci", + "prefix" => "", + "prefix_indexes" => true, + "strict" => true, + "engine" => null, + "options" => [], + ]]); + + $this->assertEquals([ + 'database' => 'dbname', + 'username' => 'usernamefoo', + 'password' => 'passwordfoo', + 'link' => 'foo', + + "driver" => "mysql", + "url" => null, + "host" => "mysql", + "port" => "3306", + "unix_socket" => "", + "charset" => "utf8mb4", + "collation" => "utf8mb4_unicode_ci", + "prefix" => "", + "prefix_indexes" => true, + "strict" => true, + "engine" => null, + "options" => [], + ], $tenant->database()->connection()); + } }