class: DBUser & phpunit

This commit is contained in:
Yuan Chiu 2014-08-18 02:34:25 -07:00
parent 082dd5534f
commit 2ad3627c88
5 changed files with 188 additions and 32 deletions

View File

@ -23,5 +23,66 @@ require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php';
*/ */
class DBUser extends Database { class DBUser extends Database {
const FORM_USER = 'User';
/**
* 新增一個使用者
* @param string $uId 使用者名稱
* @param string $password 密碼
* @param string $gId 群組
* @param string $cId 班級
* @param string $enable 啟用此帳號
* @param string $l_mode 學習模式
* @param string $m_mode 教材模式
* @param string $nickName 暱稱
* @param string $realName 姓名
* @param string $email 電子郵件地址
* @param string $memo 備註
*/
public function insertUser($uId, $password, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo){
// 檢查是否有支援所設定的DBMS
if($this->db_type == 'mysql') {
//紀錄使用者帳號進資料庫
$sqlString = "INSERT INTO ".$this->table(self::FORM_USER). "(`UID`, `UPassword`, `GID`, `CID`, `UEnabled`, `UBuild_Time`, `LMode`, `MMode`, `UNickname`, `UReal_Name`, `UEmail`, `UMemo`) VALUES ( :id , :passwd, :gid , :cid , :enable , NOW() , :lmode , :mmode , :nickname , :realname , :email , :memo )";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $uId);
$query->bindParam(":passwd", $uPassword);
$query->bindParam(":gid", $gId);
$query->bindParam(":cid", $cId);
$query->bindParam(":enable", $enable);
$query->bindParam(":lmode", $l_mode);
$query->bindParam(":mmode", $m_mode);
$query->bindParam(":nickname", $nickName);
$query->bindParam(":realname", $realName);
$query->bindParam(":email", $email);
$query->bindParam(":memo", $memo);
$query->execute();
}
else {
throw new Exception\DatabaseNoSupportException($this->db_type);
}
}
/**
* 移除一位使用者
* @param string $uId 使用者名稱
*/
public function deleteUser($uId) {
if($this->db_type == 'mysql') {
$sqlString = "DELETE FROM ".$this->table(self::FORM_USER). " WHERE `UID` = :id ";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $uId);
$query->execute();
}
else {
throw new Exception\DatabaseNoSupportException($this->db_type);
}
}
} }

View File

@ -4,6 +4,7 @@
<testsuites> <testsuites>
<testsuite name="all"> <testsuite name="all">
<directory>tests</directory> <directory>tests</directory>
<exclude>tests/InstallTest.php</exclude>
</testsuite> </testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>

View File

@ -0,0 +1,61 @@
<?php
/**
* DBUserTest
*
* @package UElearning
* @author Yuan Chiu <chyuaner@gmail.com>
*/
namespace UElearning;
require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php';
require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php';
use UElearning\Database\DBUser;
use UElearning\Database\Exception;
class DBUserTest extends \PHPUnit_Framework_TestCase
{
protected $db;
protected function setUp(){
try {
// 建立資料庫管理物件
$this->db = new DBUser();
}
// 若設定的DBMS不被支援 則丟出例外
catch (Database\Exception\DatabaseNoSupportException $e) {
throw $e;
}
}
/**
* 測試建立使用者
*
* @dataProvider userDataProvider
*/
public function testCreateUser($uId, $uPassword, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo){
$this->db->insertUser($uId, $uPassword, $gId, $cId, $enable,
$l_mode, $m_mode,
$nickName, $realName, $email, $memo);
}
/**
* 測試移除使用者
*
* @dataProvider userDataProvider
*/
public function testDeleteUser($uId) {
$this->db->deleteUser($uId);
}
public function userDataProvider(){
return array(
array('yuan', 'pass123', 'admin', null, true, 'harf-line-learn', 1, '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null)
);
}
}

View File

@ -1,33 +1,33 @@
<?php <?php
/** ///**
* InstallTest // * InstallTest
* // *
* @package UElearning // * @package UElearning
* @author Yuan Chiu <chyuaner@gmail.com> // * @author Yuan Chiu <chyuaner@gmail.com>
*/ // */
namespace UElearning; //namespace UElearning;
//
require_once UELEARNING_LIB_ROOT.'/Database/DBAdmin.php'; //require_once UELEARNING_LIB_ROOT.'/Database/DBAdmin.php';
//
class InstallTest extends \PHPUnit_Framework_TestCase //class InstallTest extends \PHPUnit_Framework_TestCase
{ //{
//
/** // /**
* 測試安裝初始化資料庫 // * 測試安裝初始化資料庫
*/ // */
public function testInstallDatabase() // public function testInstallDatabase()
{ // {
//
try { // try {
// 建立資料庫管理物件 // // 建立資料庫管理物件
$dbAdmin = new Database\DBAdmin(); // $dbAdmin = new Database\DBAdmin();
//
// 建立所有所需的資料表 // // 建立所有所需的資料表
$dbAdmin->createAllTable(); // $dbAdmin->createAllTable();
} // }
// 若設定的DBMS不被支援 則丟出例外 // // 若設定的DBMS不被支援 則丟出例外
catch (Database\Exception\DatabaseNoSupportException $e) { // catch (Database\Exception\DatabaseNoSupportException $e) {
throw $e; // throw $e;
} // }
} // }
} //}

View File

@ -0,0 +1,33 @@
<?php
/**
* UserAdminTest.php
*
* @package UElearning
* @author Yuan Chiu <chyuaner@gmail.com>
*/
namespace UElearning;
require_once UELEARNING_LIB_ROOT.'/User/UserAdmin.php';
use UElearning\User\UserAdmin;
class UserAdminTest extends \PHPUnit_Framework_TestCase
{
/**
* 測試安裝初始化資料庫
*/
public function testCreateUser()
{
try {
// 建立資料庫管理物件
$dbAdmin = new UserAdmin();
// TODO: 建立使用者
}
// 若設定的DBMS不被支援 則丟出例外
catch (Database\Exception\DatabaseNoSupportException $e) {
throw $e;
}
}
}