Skip to content

Commit 66397d4

Browse files
authored
Merge pull request #1554 from codeigniter4/serviceinstances
Serviceinstances
2 parents ef8619d + ccba30b commit 66397d4

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

system/Database/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function connect($group = null, bool $getShared = true)
7777
if (is_array($group))
7878
{
7979
$config = $group;
80-
$group = 'custom';
80+
$group = 'custom-' . md5(json_encode($config));
8181
}
8282

8383
$config = $config ?? new \Config\Database();
@@ -87,7 +87,7 @@ public static function connect($group = null, bool $getShared = true)
8787
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
8888
}
8989

90-
if (is_string($group) && ! isset($config->$group) && $group !== 'custom')
90+
if (is_string($group) && ! isset($config->$group) && strpos($group, 'custom-') !== 0)
9191
{
9292
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
9393
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace CodeIgniter\Database\Live;
2+
3+
;
4+
5+
use CodeIgniter\Test\CIDatabaseTestCase;
6+
use Config\Database;
7+
8+
class AliasTest extends CIDatabaseTestCase
9+
{
10+
protected $group1;
11+
12+
protected $group2;
13+
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$config = config('database');
19+
20+
$this->group1 = $config->default;
21+
$this->group2 = $config->default;
22+
23+
$this->group1['strictOn'] = false;
24+
$this->group2['strictOn'] = true;
25+
}
26+
27+
public function testConnectWithMultipleCustomGroups()
28+
{
29+
// We should have our test database connection already.
30+
$instances = $this->getPrivateProperty(Database::class, 'instances');
31+
$this->assertEquals(1, count($instances));
32+
33+
$db1 = Database::connect($this->group1);
34+
$db2 = Database::connect($this->group2);
35+
36+
$this->assertNotSame($db1, $db2);
37+
38+
$instances = $this->getPrivateProperty(Database::class, 'instances');
39+
$this->assertEquals(3, count($instances));
40+
}
41+
42+
//--------------------------------------------------------------------
43+
44+
}

0 commit comments

Comments
 (0)