Advertisement
Guest User

Untitled

a guest
Jan 29th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.78 KB | None | 0 0
  1. /--------------------Connection.php----------------------------/
  2. <?php
  3.  
  4. class FailedConnection extends Exception{}
  5.  
  6. class ConnectionManager {
  7.     //FUNCTION: Gets List of Database Links for given database set----------------//
  8.     static public function GetDBSetLinks($CurrentDBSet){
  9.         $DatabaseLinkList = array();
  10.  
  11.         foreach ($CurrentDBSet as $DatabaseInfoString){
  12.             //Split the Database info string by commas. The input string found in the
  13.             //configuration file is a single string containing all of the parameters
  14.             //needed to connect to the SQL database. The statement below takes this string
  15.             //and converts it into three separate variables, so that they can be passed into mysqli_connect()
  16.             $SplitDBInfo = preg_split('/,/', $DatabaseInfoString);
  17.  
  18.             //Initialize a variable that serves as an iterator. This variable also holds
  19.             //the number of arguments that were found in the config fie.
  20.             $NumArgs = 0;
  21.  
  22.             //Initialize other variables necessary to connect to the current database.
  23.             $DatabaseID = "NULL";
  24.             $HostName = "NULL";
  25.             $dbUsername = "NULL";
  26.             $dbPassword = "NULL";
  27.             $DatabaseName = "NULL"; //NOTE: Database name in the config file should exactly match the name of the database on the server.
  28.             $PortNumber = "NULL";
  29.             $Socket = "NULL";
  30.  
  31.             //Cycle through the individual params now stored in SplitDBInfo, (1.) count
  32.             //them, then (2.) assign them to the appropriate variable to pass into mysqli_connect()
  33.             foreach ($SplitDBInfo as $Parameter){
  34.                 $NumArgs ++; //(1.)              
  35.                 switch ($NumArgs) { //(2.)
  36.                     case 1:
  37.                         $DatabaseID = $Parameter;
  38.                         break;
  39.                     case 2:
  40.                         $HostName = $Parameter;
  41.                         break;
  42.                     case 3:
  43.                         $dbUsername = $Parameter;
  44.                         break;
  45.                     case 4:
  46.                         $dbPassword = $Parameter;
  47.                         break;
  48.                     case 5:
  49.                         $DatabaseName = $Parameter;
  50.                         break;
  51.                     case 6:
  52.                         $PortNumber = $Parameter;
  53.                         break;
  54.                     case 7:
  55.                         $Socket = $Parameter;
  56.                         break;
  57.                     default:
  58.                         break;
  59.                 }
  60.                 //print $Parameter . "<br>"; //DEBUG
  61.             }
  62.             print '<br>' ."NumArgs: " . ($NumArgs) . '<br>'; //DEBUG ONLY
  63.             print "Number of Function Arguments: " . ($NumArgs -1) . '<br>'; //DEBUG ONLY;
  64.             echo "Connecting to Database '" . $DatabaseID . "' at hostname " . $HostName ."." . '<br>';
  65.  
  66.             $link = self::ConnectWithMysqli($NumArgs, $HostName, $dbUsername, $dbPassword, $DatabaseName, $PortNumber, $Socket);
  67.  
  68.  
  69.             //If our link to the database is not successful, escape this sequence and do not put the link in our list of linked databases.
  70.             if (!$link){
  71.                 echo "No Successful link to '" . $DatabaseID . "' at hostname " . $HostName .".";
  72.                 echo '<br>';
  73.             }
  74.             //Otherwise, our link should be good, and we should add it to our list of database links.
  75.             else{
  76.                 echo "Connection to Database '" . $DatabaseID . "' at hostname " . $HostName ." was successful.";
  77.                 echo '<br>';
  78.                 array_push($DatabaseLinkList, $link);
  79.             }    
  80.         }
  81.     //After we finish iterating to generate a list of viable links, we can use these
  82.     //links to perform database operations.
  83.     return $DatabaseLinkList;
  84.     }
  85.    
  86.     function ConnectWithMysqli($NumArgs,$HostName, $dbUsername, $dbPassword, $DatabaseName, $PortNumber, $Socket){
  87.         switch($NumArgs) {
  88.             case 2:
  89.                 $link = mysqli_connect($HostName);
  90.                 //could not connect
  91.                 break;
  92.             case 3:
  93.                 $link = mysqli_connect($HostName,$dbUsername);
  94.                 break;
  95.             case 4:
  96.                 $link = mysql_connect($HostName,$dbUsername,$dbPassword);
  97.                 //$link = mysqli_connect($HostName,$dbUsername,$dbPassword);
  98.                 break;
  99.             case 5:
  100.                 print ($DatabaseName);
  101.                 $link = mysqli_connect($HostName,$dbUsername,$dbPassword,$DatabaseName);
  102.                 break;
  103.             case 6:
  104.                 $link =  mysqli_connect($HostName, $dbUsername, $dbPassword, $DatabaseName, $PortNumber, $Socket);
  105.                 break;
  106.             default:
  107.                 throw new FailedConnection;
  108.             }
  109.     try{
  110.         if(!$link){
  111.             throw new FailedConnection;
  112.         }
  113.     }
  114.     catch(FailedConnection $e){
  115.         return "Null";
  116.     }
  117.  
  118. return $link;
  119. }  
  120. //LIST OF DATABASE LINKS HAS BEEN GENERATED-----------------------------------//
  121.  
  122. /--------------------------------------------------------------/
  123.  
  124.  
  125. /-----------------ConnectionModuleTest.php---------------------/
  126.  
  127. <?php
  128. require_once 'C:\Users\bsnider\Documents\BrandonDepot\SourceControl\git\repos\GLS_DBSearchProject\Connection.php';
  129.  
  130. class ConnectionModuleTest extends PHPUnit_Framework_TestCase{
  131.     public function setUp(){}
  132.     public function tearDown(){}
  133.    
  134.     public function testDataGetDBSetLinks_GoodConnection(){
  135.         $ConnectionManager = new ConnectionManager;
  136.         //Valid Connection Parameters
  137.         $DBSet = array('MainDatabase,192.168.1.41,root,colt45', 'MainDatabase,192.168.1.41,root,colt45');
  138.         $Result = $ConnectionManager->GetDBSetLinks($DBSet); //$Result
  139.         $this->assertNotEmpty($Result);
  140.     }
  141.    
  142.     public function testDataGetDBSetLinks_BadHostName(){
  143.         $ConnectionManager = new ConnectionManager;
  144.         //Invalid Connection Parameters
  145.         $DBSet = array('MainDatabase,192.168.1.20,root,colt45', 'MainDatabase,192.168.1.20,root,colt45');
  146.         $Result = $ConnectionManager->GetDBSetLinks($DBSet); //$Result
  147.         $this->assertEmpty($Result);
  148.     }
  149.    
  150.     public function testDataGetDBSetLinks_BadPassword(){
  151.         $ConnectionManager = new ConnectionManager;
  152.         //Invalid Connection Parameters
  153.         $DBSet = array('MainDatabase,192.168.1.41,root,badpassword', 'MainDatabase,192.168.1.41,root,badpassword');
  154.         $Result = $ConnectionManager->GetDBSetLinks($DBSet); //$Result
  155.         $this->assertEmpty($Result);        
  156.         }
  157.     public function testConnectWithMysqli(){
  158.        
  159.     }
  160. }
  161.  
  162.  
  163. /--------------------------------------------------------------/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement