diff --git a/changelog.MD b/changelog.MD index 3a82787d..dcf5ce39 100644 --- a/changelog.MD +++ b/changelog.MD @@ -1,5 +1,10 @@ # Changelog +### Next version + +**New features** +* Added a _removeConnection()_ method in the connection manager class. + ## 2.1.1 **Bugfixes** diff --git a/src/Database/Manager.php b/src/Database/Manager.php index 5357d265..a78ebdf1 100644 --- a/src/Database/Manager.php +++ b/src/Database/Manager.php @@ -44,6 +44,17 @@ public function addConnection(array $config, string $name = 'default'): void $this->config[$name] = $config; } + public function removeConnection(string $name = 'default'): void + { + if (true === array_key_exists($name, $this->config)) { + unset($this->config[$name]); + } + + if (true === array_key_exists($name, $this->connections)) { + unset($this->connections[$name]); + } + } + public function getConfig(): array { return $this->config; diff --git a/tests/Database/ManagerTest.php b/tests/Database/ManagerTest.php index ed8c2c6b..12cf98be 100644 --- a/tests/Database/ManagerTest.php +++ b/tests/Database/ManagerTest.php @@ -96,4 +96,32 @@ public function invalidConnectionThrowsException(): void $manager->pdo('invalid'); } + + /** @test */ + public function testRemoveConnection(): void + { + $factory = $this->createMock('Wizaplace\Etl\Database\ConnectionFactory'); + $factory->expects(static::once())->method('make')->with(['options2'])->willReturn($this->createMock('PDO')); + + $manager = new Manager($factory); + $manager->addConnection(['options']); + $manager->addConnection(['options2'], 'theOtherConnection'); + + static::assertEquals( + ['default' => ['options'], 'theOtherConnection' => ['options2']], + $manager->getConfig() + ); + + $manager->removeConnection('default'); + static::assertEquals( + ['theOtherConnection' => ['options2']], + $manager->getConfig() + ); + static::assertInstanceOf(\PDO::class, $manager->pdo('theOtherConnection')); + + $manager->removeConnection('theOtherConnection'); + static::expectException(\InvalidArgumentException::class); + static::expectExceptionMessage('Database [theOtherConnection] not configured.'); + $manager->pdo('theOtherConnection'); + } }