Beispiel #1
0
 /**
  * Database factory.
  *
  * It returns the same instance for the same config.
  *
  * @see \Kicaj\Tools\Itf\DbConnect
  *
  * @param array $dbConfig The database configuration.
  *
  * @throws SchemaException
  *
  * @return Db
  */
 public static function factory(array $dbConfig)
 {
     $key = md5(json_encode($dbConfig));
     if (isset(self::$instances[$key])) {
         return self::$instances[$key];
     }
     switch (DbConnect::getDriver($dbConfig[Schema::CONFIG_KEY_CONNECTION])) {
         case DbConnector::DB_DRIVER_MYSQL:
             $driver = new MySQL();
             break;
         default:
             throw new SchemaException('Unknown database driver name: ' . DbConnect::getDriver($dbConfig[Schema::CONFIG_KEY_CONNECTION]));
     }
     $driver->dbSetup($dbConfig[Schema::CONFIG_KEY_CONNECTION])->dbConnect();
     self::$instances[$key] = new self($driver);
     return self::$instances[$key];
 }
Beispiel #2
0
 /**
  * @dataProvider connectionProvider
  *
  * @covers ::dbSetup
  * @covers ::dbConnect
  *
  * @param string $host
  * @param string $username
  * @param string $password
  * @param string $database
  * @param string $port
  * @param string $errorMsg
  *
  * @throws SchemaException
  */
 public function test_connection($host, $username, $password, $database, $port, $errorMsg)
 {
     // Given
     $this->driver = null;
     $thrown = false;
     // Database config
     $dbConfig = [DbConnector::DB_CFG_HOST => $host, DbConnector::DB_CFG_USERNAME => $username, DbConnector::DB_CFG_PASSWORD => $password, DbConnector::DB_CFG_DATABASE => $database, DbConnector::DB_CFG_PORT => $port];
     // When
     $myMySQL = new MySQL();
     // Then
     try {
         $myMySQL->dbSetup($dbConfig)->dbConnect();
         // Call method that is actually doing something with database.
         $myMySQL->dbGetTableNames();
     } catch (SchemaException $e) {
         $thrown = true;
         $this->assertFalse('' == $errorMsg, 'Did not expect to see error: ' . $e->getMessage());
     } finally {
         $this->assertFalse('' !== $errorMsg && false === $thrown, 'Expected to see error: ' . $errorMsg);
     }
 }