Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebIS A2. Reimplementation of PersonApp and PersonTest #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions App/personApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ class Person {
* Database handle
* @var \mysqli
*/
private $db=NULL;

private $db=NULL;
public $person=NULL;
public $name=NULL;
public $rate=NULL;

//Construct Connection to Database

function __construct(){
//print "WCS/PersonApp>";
function __construct()
{
$this->db = new \mysqli(\WCS\Config::$dbhost,\WCS\Config::$dbuser,\WCS\Config::$dbpassword,\WCS\Config::$dbdatabase);
if($this->db===NULL){
die("Error unable to connect to database");
}
}

//Destruct Database Connection

function __destruct() {
if($this->db!=NULL){
$this->db->close();
Expand All @@ -34,6 +36,8 @@ function __destruct() {
* @param string $person Alphanumeric username [a-zA-Z0-9]
* @return bool person set.
*/
//Use a regular expression to put character constraint on string $person

public function setPerson($person){
if(preg_match('/^[a-zA-Z0-9]+$/',$person)){
$this->person=$person;
Expand All @@ -47,6 +51,7 @@ public function setPerson($person){
* @param string $name Persons name
* @return bool name set.
*/

public function setName($name){
$this->name=$name;
return TRUE;
Expand All @@ -56,6 +61,7 @@ public function setName($name){
* Display Person
* @return string human readable display.
*/

public function display(){
$name='';
if(!\is_null($this->name)){
Expand All @@ -70,13 +76,14 @@ public function insert() {
error_log("WCS/Person.insert> stmt:".$this->db->error);
return FALSE;
}
if($stmt->bind_param('ssd',$this->person,$this->name,$this->rate)===FALSE){
if($stmt->bind_param('ssd',$this->person,$this->name,$this->rate)===FALSE){
error_log("WCS/Person.insert> bind_param:".$this->db->error);
return FALSE;
}
if($stmt->execute()===FALSE){
if($this->db->errno==1062){ // Duplicate.
if($this->db->errno==1062){
return FALSE;
//errno==1062 is the error for a duplicate
}
error_log("WCS/Person.insert> execute:".$this->db->errno." ".$this->db->error);
return FALSE;
Expand All @@ -88,6 +95,7 @@ public function insert() {
* Remove Person
* @return bool TRUE on success (even if record did not exist);
*/

public function delete() {
$stmt=$this->db->prepare("DELETE FROM Person WHERE person=?");
if($stmt===FALSE){
Expand All @@ -106,27 +114,29 @@ public function delete() {
}

public function get() {
//1. Prepare MySQL Statement
$stmt=$this->db->prepare("SELECT name,rate FROM Person WHERE person=?");
if($stmt===FALSE){
error_log("WCS/Person.get> stmt:".$this->db->error);
return FALSE;
}

//2. Bind parameters
if($stmt->bind_param('s',$this->person)===FALSE){
error_log("WCS/Person.get> bind_param:".$this->db->error);
return FALSE;
}
//3. Bind the result
if($stmt->bind_result($this->name,$this->rate)===FALSE){
error_log("WCS/Person.get> bind_result:".$this->db->error);
return FALSE;
}
//4. Execute statement
if($stmt->execute()===FALSE){
error_log("WCS/Person.get> bind_result:".$this->db->error);
return FALSE;
}
//5. Return statement
return $stmt->fetch();
}

}

?>
18 changes: 9 additions & 9 deletions App/personTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class PersonTestCase extends WebIS\Validator {

function testTrainingApp() {
$p=new \WCS\Person();
$this->assertTrue($p->setPerson("DrMiddelkoop"));
$this->assertFalse($p->setPerson("Dr.Middelkoop"));
$this->assertEquals("{person: DrMiddelkoop}",$p->display());
$this->assertTrue($p->setName("Dr. Middelkoop"));
$this->assertEquals("{person: DrMiddelkoop name: Dr. Middelkoop}",$p->display());
$this->assertTrue($p->setPerson("LanceMarkert"));
$this->assertFalse($p->setPerson("Lance Markert"));
$this->assertEquals("{person: LanceMarkert}",$p->display());
$this->assertTrue($p->setName("Lance Markert"));
$this->assertEquals("{person: LanceMarkert name: Lance Markert}",$p->display());
$this->assertTrue($p->delete());
$this->assertTrue($p->delete()); // second one should also succeed.
// Use assertTrue($p->delete()) again to return true if the record does not exist
$this->assertTrue($p->delete());
$this->assertTrue($p->insert());
$this->assertFalse($p->insert(),"record exists, should return false");
$p=new \WCS\Person();
$this->assertTrue($p->setPerson("DrMiddelkoop"));
$this->assertTrue($p->setPerson("LanceMarkert"));
$this->assertTrue($p->get());
$this->assertEquals("{person: DrMiddelkoop name: Dr. Middelkoop}",$p->display());
$this->assertEquals("{person: LanceMarkert name: Lance Markert}",$p->display());
}

}

if (!defined('PHPUnit_MAIN_METHOD')) {
Expand Down