Fix: tdd for connect_db.php

This commit is contained in:
2019-07-10 09:00:51 +09:00
parent b25b661032
commit abd22d5156
3 changed files with 48 additions and 19 deletions
+19 -17
View File
@@ -15,7 +15,7 @@ class DBConnector
{ {
global $db_host, $db_user, $db_passwd, $db_name; global $db_host, $db_user, $db_passwd, $db_name;
if (isset($db_host) && $db_host !== null ) { if (isset($db_host) && $db_host !== null) {
// echo "db_host variable\n"; // echo "db_host variable\n";
$this->db_host = $db_host; $this->db_host = $db_host;
$this->db_user = $db_user; $this->db_user = $db_user;
@@ -28,14 +28,10 @@ class DBConnector
$this->db_passwd = "sEobMPuJ2A8KTfwU"; $this->db_passwd = "sEobMPuJ2A8KTfwU";
$this->db_name = "chocomae"; $this->db_name = "chocomae";
} }
$this->printDBVariables();
$this->connectDB();
} }
function __destruct() function __destruct()
{ {
$this->closeDB();
} }
public function connectDB() public function connectDB()
@@ -43,30 +39,36 @@ class DBConnector
// connect db // connect db
$this->db_conn = new mysqli($this->db_host, $this->db_user, $this->db_passwd, $this->db_name); $this->db_conn = new mysqli($this->db_host, $this->db_user, $this->db_passwd, $this->db_name);
if ($this->db_conn->connect_error) { if ($this->db_conn->connect_error) {
// set_error_message("Failed to connect DB: ".$this->db_conn->connect_error); return false;
// send_result_fail(); }
echo "connection failed\n";
exit;
} else {
echo "connection succeeded\n";
$query = "USE chocomae"; $query = "USE chocomae";
$result = mysqli_query($this->db_conn, $query); $result = mysqli_query($this->db_conn, $query);
} if (!$result)
return false;
return true;
} }
public function closeDB() public function closeDB()
{ {
if (isset($this->db_conn) || $this->db_conn === null) {
return;
}
if ($this->db_conn->connect_error) { if ($this->db_conn->connect_error) {
$this->db_conn->close(); $this->db_conn->close();
} }
} }
private function printDBVariables() public function getDBVariables()
{ {
echo "host : ".$this->db_host."\n"; $variables = array();
echo "user : ".$this->db_user."\n"; $variables["host"] = $this->db_host;
echo "passwd : ".$this->db_passwd."\n"; $variables["user"] = $this->db_user;
echo "name : ".$this->db_name."\n"; $variables["passwd"] = $this->db_passwd;
$variables["name"] = $this->db_name;
return $variables;
} }
} }
+8 -1
View File
@@ -3,8 +3,15 @@ header('Content-Type: application/json');
include "./../lib/connect_db.php"; include "./../lib/connect_db.php";
$jsonReply = array();
$db_conn = new DBConnector(); $db_conn = new DBConnector();
// $db_conn->setup(); $isConnected = $db_conn->connectDB();
$jsonReply["isConnected"] = $isConnected;
$jsonReply["dbVariables"] = $db_conn->getDBVariables();
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
/* /*
+20
View File
@@ -1,3 +1,23 @@
//////////////////////////////////////////////////
// DBConnector
QUnit.test( "DBConnector", function( assert ) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "./../src/web/php/writing/player_list.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
var replyJSON = JSON.parse(xhr.responseText);
console.log(replyJSON);
}
};
xhr.send();
assert.equal(1, 0, "DBConnector");
});
////////////////////////////////////////////////// //////////////////////////////////////////////////
// StringUtil // StringUtil