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

A2-workflow #27

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
182 changes: 182 additions & 0 deletions App/projectAPP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace WCS;

//include this file when running
require_once 'Work-Cell-Scheduler/Config/global.php';

class Person{

// build attribute for this class:
private $db = NULL;
public $person = NULL;
public $name = NULL;
public $rate = NULL;

// build constructor: automaticlly connect to the database when new object was built.there has a space behind funtion
function __construct(){
$this->db = new \mysqli(\WCS\Config::$dbhost,\WCS\Config::$dbuser,\WCS\Config::$dbpassword,\WCS\Config::$dbdatabase);

if($this->db===NULL){
die("cannot connect to the database!");
}

}

function __destruct(){

if($this->db!=NULL){

$this->db->close();

}

}

//build the operations for this class:

//operation : set the person
function setPerson($person){
// limit the enter type of the person
if(preg_match('/^[a-zA-Z0-9]+$/',$person)){

$this->person=$person;
return TRUE;
}

return FALSE;
}


// operation: set the name
function setName($name){
$this->name=$name;
return TRUE;
}

// opetation: set the rate
function setRate($rate){
$this->rate=$rate;
return TRUE;
}

function display(){

$name="";
$rate="";
if(!\is_null($this->name)){

$name=" name: $this->name";
}
if(!\is_null($this->rate)){
$rate=" rate: $this->rate";
}

return "{person: $this->person".$name.$rate."}";

}

//communicate with the database:

function Insert(){
$stmt=$this->db->prepare("INSERT INTO Person (person,name,rate) VALUES (?,?,?)");
if($stmt===FALSE){
error_log("WCS/Person.Insert> stmt:".$this->db->error);
return 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){
return FALSE;
}
error_log("WCS/Person.Insert> execute:".$this->db->error);
return FALSE;

}
return TRUE;
}


function Delete(){
$stmt=$this->db->prepare("DELETE FROM Person WHERE person=?");
if($stmt===FALSE){

error_log("WCS/Person.Delete> stmt:".$this->db->error);
return FALSE;

}

if($stmt->bind_param('s', $this->person)===FALSE){

error_log("WCS/Person.Delete> bind_param:".$this->db->error);
return FALSE;

}

if($stmt->execute()===FALSE){

error_log("WCS/Person.Delete> execute:".$this->db->error );
return FALSE;
}

return TRUE;


}

function get(){
$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;

}
if($stmt->bind_param( 's', $this->person)===FALSE){
error_log("WCS/Person.get> bind_param:".$this->db->error);
return FALSE;
}
if($stmt->bind_result($this->name,$this->rate)===FALSE){
error_log("WCS/Person.get> bind_result:".$this->db->error);
return FALSE;
}

if($stmt->execute()===FALSE){
error_log("WCS/Person.get> execute:".$this->db->error);
return FALSE;

}


return $stmt->fetch();


}





}





?>











44 changes: 44 additions & 0 deletions App/projectAPPtest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
require_once'Work-Cell-Scheduler/TDD/validator.php';
include 'Work-Cell-Scheduler/Config/local-linux.php';
require_once 'projectAPP.php';

class PersonTest extends WebIS\Validator{
//get the class name:


protected static $__CLASS__=__CLASS__;


function testPersonApp(){
$a=new \WCS\Person();
$this->assertFalse($a->setPerson("Jianghong Li"));
$this->assertTrue($a->setPerson("JianghongLi"));

$this->assertEquals("{person: JianghongLi}",$a->display());

$this->assertTrue($a->setName("Jianghong Li"));
$this->assertTrue($a->setRate("0.6"));

$this->assertEquals("{person: JianghongLi name: Jianghong Li rate: 0.6}", $a->display());

//can be delected twice, but cannot insert twice
$this->assertTrue($a->Delete());
$this->assertTrue($a->Delete());
$this->assertTrue($a->Insert());
$this->assertFalse($a->Insert());

$a=new \WCS\Person();
$this->assertTrue($a->setPerson("JianghongLi"));
$this->assertTrue($a->get());
echo "name: ".$a->name." "."rate: ".$a->rate;

}


}

if (!defined('PHPUnit_MAIN_METHOD')) {
PersonTest::main();
}
?>