diff --git a/composer.json b/composer.json index 064289c24..b70ec7d4a 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,6 @@ "require-dev": { "maglnet/composer-require-checker": "^4.2", "phpunit/phpunit": "^10.5", - "psr/http-server-middleware": "^1.0", "rector/rector": "^1.0", "roave/infection-static-analysis-plugin": "^1.34", "spatie/phpunit-watcher": "^1.23", @@ -46,7 +45,8 @@ "yiisoft/cache": "^3.0", "yiisoft/db-sqlite": "dev-master", "yiisoft/di": "^1.0", - "yiisoft/json": "^1.0" + "yiisoft/json": "^1.0", + "yiisoft/middleware-dispatcher": "^5.2" }, "autoload": { "psr-4": { diff --git a/src/ConnectionProvider.php b/src/ConnectionProvider.php index c8b2b20f6..909e67b46 100644 --- a/src/ConnectionProvider.php +++ b/src/ConnectionProvider.php @@ -42,6 +42,14 @@ public static function has(string $name = self::DEFAULT): bool return isset(self::$connections[$name]); } + /** + * Removes a connection by name. + */ + public static function remove(string $name = self::DEFAULT): void + { + unset(self::$connections[$name]); + } + /** * Sets a connection by name. */ diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 39e25f334..54126b82a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -980,5 +980,9 @@ public function testWithCustomConnection(): void $customer = $customer->withConnectionName('custom'); $this->assertSame($db, $customer->db()); + + $db->close(); + + ConnectionProvider::remove('custom'); } } diff --git a/tests/ConnectionProviderTest.php b/tests/ConnectionProviderTest.php new file mode 100644 index 000000000..e95285cd1 --- /dev/null +++ b/tests/ConnectionProviderTest.php @@ -0,0 +1,72 @@ +assertTrue(ConnectionProvider::has('default')); + $this->assertFalse(ConnectionProvider::has('db2')); + + $db = ConnectionProvider::get(); + + $this->assertTrue(ConnectionProvider::has('default')); + $this->assertSame($db, ConnectionProvider::get('default')); + + $list = ConnectionProvider::all(); + + $this->assertSame($list, ['default' => $db]); + + $db2 = $this->createConnection(); + ConnectionProvider::set($db2, 'db2'); + + $this->assertTrue(ConnectionProvider::has('db2')); + $this->assertSame($db2, ConnectionProvider::get('db2')); + + $list = ConnectionProvider::all(); + + $this->assertSame($list, ['default' => $db, 'db2' => $db2]); + + ConnectionProvider::remove('db2'); + + $this->assertFalse(ConnectionProvider::has('db2')); + + $list = ConnectionProvider::all(); + + $this->assertSame($list, ['default' => $db]); + } + + public function testConnectionProviderMiddleware(): void + { + ConnectionProvider::remove(); + + $this->assertEmpty(ConnectionProvider::all()); + $this->assertFalse(ConnectionProvider::has('default')); + + $db = $this->createConnection(); + $container = new Container(ContainerConfig::create()->withDefinitions([ConnectionInterface::class => $db])); + $request = $this->createMock(ServerRequestInterface::class); + $requestHandler = $this->createMock(RequestHandlerInterface::class); + + $dispatcher = (new MiddlewareDispatcher(new MiddlewareFactory($container))) + ->withMiddlewares([ConnectionProviderMiddleware::class]); + + $dispatcher->dispatch($request, $requestHandler); + + $this->assertTrue(ConnectionProvider::has('default')); + $this->assertSame($db, ConnectionProvider::get()); + } +} diff --git a/tests/Driver/Mssql/ConnectionProviderTest.php b/tests/Driver/Mssql/ConnectionProviderTest.php new file mode 100644 index 000000000..3664548f3 --- /dev/null +++ b/tests/Driver/Mssql/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Mysql/ConnectionProviderTest.php b/tests/Driver/Mysql/ConnectionProviderTest.php new file mode 100644 index 000000000..aaf56a3b2 --- /dev/null +++ b/tests/Driver/Mysql/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Oracle/ConnectionProviderTest.php b/tests/Driver/Oracle/ConnectionProviderTest.php new file mode 100644 index 000000000..2f3713237 --- /dev/null +++ b/tests/Driver/Oracle/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Pgsql/ConnectionProviderTest.php b/tests/Driver/Pgsql/ConnectionProviderTest.php new file mode 100644 index 000000000..84b67503c --- /dev/null +++ b/tests/Driver/Pgsql/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/Driver/Sqlite/ConnectionProviderTest.php b/tests/Driver/Sqlite/ConnectionProviderTest.php new file mode 100644 index 000000000..9803721cb --- /dev/null +++ b/tests/Driver/Sqlite/ConnectionProviderTest.php @@ -0,0 +1,16 @@ +createConnection(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 63015e001..0953f364f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -41,5 +41,7 @@ protected function tearDown(): void parent::tearDown(); $this->db()->close(); + + ConnectionProvider::remove(); } }