Skip to content

Commit

Permalink
Added a removeConnection() method in the connection manager (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecourtial authored Mar 9, 2022
1 parent 9853333 commit 0d0c238
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### Next version

**New features**
* Added a _removeConnection()_ method in the connection manager class.

## 2.1.1

**Bugfixes**
Expand Down
11 changes: 11 additions & 0 deletions src/Database/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions tests/Database/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 0d0c238

Please sign in to comment.