/** * Saves a connection * @param string $name Name of the connection * @param string $dsn DSN string for the connection * @param string $oldName Old name if we are doing a rename action * @return null * @throws Exception when an error occurs */ public function saveConnection($name, $dsn, $oldName = null) { $zibo = Zibo::getInstance(); $updateDefault = false; $baseKey = DatabaseManager::CONFIG_CONNECTION . Config::TOKEN_SEPARATOR; if (!empty($oldName) && $name != $oldName) { $defaultConnection = $this->getDefaultConnection(); if ($defaultConnection->getName() == $oldName) { $updateDefault = true; } $zibo->setConfigValue($baseKey . $oldName, null); } $dsn = new Dsn($dsn); try { $this->manager->registerConnection($name, $dsn); $zibo->setConfigValue($baseKey . $name, $dsn->__toString()); if ($updateDefault) { $this->setDefaultConnection($name); } } catch (Exception $exception) { $error = new ValidationError(self::TRANSLATION_ERROR, '%error%', array('error' => $exception->getMessage())); $validationException = new ValidationException(); $validationException->addErrors('dsn', array($error)); throw $validationException; } }
public function testGetConnectionWithoutConnectionName() { $manager = new DatabaseManager($this->zibo); $manager->registerDriver('protocol', self::DRIVER_MOCK); $connectionName = 'test'; $dsn = new Dsn('protocol://server/database'); $manager->registerConnection($connectionName, $dsn); $connection = $manager->getConnection(); $this->assertTrue($connection instanceof Driver, 'connection is not a Driver'); $this->assertTrue($connection->isConnected(), 'connection is not connected'); }