/**
  * @return \Magento\TestFramework\Db\Adapter\Mysql
  */
 protected function _getConnectionRead()
 {
     $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
     $deploymentConfig = $objectManager->get('Magento\\Framework\\App\\DeploymentConfig');
     $dbConfig = new DbConfig($deploymentConfig->getSegment(DbConfig::CONFIG_KEY));
     $connectionConfig = $dbConfig->getConnection('default');
     $connectionConfig['profiler'] = ['class' => 'Magento\\Framework\\Model\\Resource\\Db\\Profiler', 'enabled' => 'true'];
     return $objectManager->create('Magento\\TestFramework\\Db\\Adapter\\Mysql', ['config' => $connectionConfig]);
 }
 /**
  * Clear Magento instance: remove all tables in DB and use dump to load new ones, clear Magento cache
  *
  * @throws \Exception
  */
 public function clearInstance()
 {
     $dirList = \Magento\Mtf\ObjectManagerFactory::getObjectManager()->get('Magento\\Framework\\Filesystem\\DirectoryList');
     $deploymentConfig = new \Magento\Framework\App\DeploymentConfig(new \Magento\Framework\App\DeploymentConfig\Reader($dirList), []);
     $dbConfig = new DbConfig($deploymentConfig->getSegment(DbConfig::CONFIG_KEY));
     $dbInfo = $dbConfig->getConnection('default');
     $host = $dbInfo['host'];
     $user = $dbInfo['username'];
     $password = $dbInfo['password'];
     $database = $dbInfo['dbname'];
     $fileName = MTF_BP . '/' . $database . '.sql';
     if (!file_exists($fileName)) {
         echo 'Database dump was not found by path: ' . $fileName;
         return;
     }
     // Drop all tables in database
     $mysqli = new \mysqli($host, $user, $password, $database);
     $mysqli->query('SET foreign_key_checks = 0');
     if ($result = $mysqli->query("SHOW TABLES")) {
         while ($row = $result->fetch_row()) {
             $mysqli->query('DROP TABLE ' . $row[0]);
         }
     }
     $mysqli->query('SET foreign_key_checks = 1');
     $mysqli->close();
     // Load database dump
     exec("mysql -u{$user} -p{$password} {$database} < {$fileName}", $output, $result);
     if ($result) {
         throw new \Exception('Database dump loading has been failed: ' . $output);
     }
     // Clear cache
     exec("rm -rf {$dirList->getPath(DirectoryList::VAR_DIR)}/*", $output, $result);
     if ($result) {
         throw new \Exception('Cleaning Magento cache has been failed: ' . $output);
     }
 }
Example #3
0
 /**
  * Retrieve connection by $connectionName
  *
  * @param string $connectionName
  * @return bool|\Magento\Framework\DB\Adapter\AdapterInterface
  */
 public function getConnectionByName($connectionName)
 {
     if (isset($this->_connections[$connectionName])) {
         return $this->_connections[$connectionName];
     }
     $dbInfo = $this->deploymentConfig->getSegment(DbConfig::CONFIG_KEY);
     if (null === $dbInfo) {
         return false;
     }
     $dbConfig = new DbConfig($dbInfo);
     $connectionConfig = $dbConfig->getConnection($connectionName);
     if ($connectionConfig) {
         $connection = $this->_connectionFactory->create($connectionConfig);
     }
     if (empty($connection)) {
         return false;
     }
     $this->_connections[$connectionName] = $connection;
     return $connection;
 }
 public function testGetData()
 {
     $object = new DbConfig($this->data);
     $this->assertSame($this->data, $object->getData());
 }
Example #5
0
 /**
  * Validates that MySQL is accessible and MySQL version is supported
  *
  * @return void
  */
 private function assertDbAccessible()
 {
     $segment = $this->deploymentConfig->getSegment(DbConfig::CONFIG_KEY);
     $dbConfig = new DbConfig($segment);
     $config = $dbConfig->getConnection(\Magento\Framework\App\Resource\Config::DEFAULT_SETUP_CONNECTION);
     $this->checkDatabaseConnection($config[DbConfig::KEY_NAME], $config[DbConfig::KEY_HOST], $config[DbConfig::KEY_USER], $config[DbConfig::KEY_PASS]);
     if (isset($config[DbConfig::KEY_PREFIX])) {
         $this->checkDatabaseTablePrefix($config[DbConfig::KEY_PREFIX]);
     }
 }
Example #6
0
 /**
  * Retrieve the database adapter instance
  *
  * @return \Magento\TestFramework\Db\AbstractDb
  */
 public function getDbInstance()
 {
     if (null === $this->_db) {
         if ($this->isInstalled()) {
             $deploymentConfig = new DeploymentConfig(new \Magento\Framework\App\DeploymentConfig\Reader($this->dirList), []);
             $dbConfig = new DbConfig($deploymentConfig->getSegment(DbConfig::CONFIG_KEY));
             $dbInfo = $dbConfig->getConnection('default');
             $host = $dbInfo['host'];
             $user = $dbInfo['username'];
             $password = $dbInfo['password'];
             $dbName = $dbInfo['dbname'];
         } else {
             $installConfig = $this->getInstallConfig();
             $host = $installConfig['db_host'];
             $user = $installConfig['db_user'];
             $password = $installConfig['db_pass'];
             $dbName = $installConfig['db_name'];
         }
         $this->_db = new Db\Mysql($host, $user, $password, $dbName, $this->getTempDir(), $this->_shell);
     }
     return $this->_db;
 }