diff --git a/App/personApp.php b/App/personApp.php index 11932d0..39fc567 100644 --- a/App/personApp.php +++ b/App/personApp.php @@ -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(); @@ -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; @@ -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; @@ -56,6 +61,7 @@ public function setName($name){ * Display Person * @return string human readable display. */ + public function display(){ $name=''; if(!\is_null($this->name)){ @@ -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; @@ -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){ @@ -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(); } - } - ?> diff --git a/App/personTest.php b/App/personTest.php index 49bcaa2..1d62470 100644 --- a/App/personTest.php +++ b/App/personTest.php @@ -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')) {