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

Topic a2 #29

Open
wants to merge 6 commits 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
141 changes: 141 additions & 0 deletions App/a2personApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
namespace WCS;
require_once 'Work-Cell-Scheduler/Config/global.php';

class a2person {

/**
* Database handle
* @var \mysqli
*/

private $db=NULL;

private $a2person_whitelist=NULL;
private $a2person_name=NULL;
private $a2person_email=NULL;

function __construct(){
$this->db = new \mysqli(\WCS\Config::$dbhost,\WCS\Config::$dbuser,\WCS\Config::$dbpassword,\WCS\Config::$dbdatabase);
If($this->db===NULL){
die('Error database failed to construct');
}
}


function __destruct(){
if ($this->db!=NULL){
$this->db->close();
}
}

/**
* Set a2person_whitelist
* @param string $a2person_whitelist alphabetic [a-zA-Z]
* @return boolean a2person_whitelist
*/

public function seta2person_whitelist($a2person_whitelist){
//print "seta2person_whitelist: ".$a2person_whitelist;
if (preg_match('/^[a-zA-Z]+$/',$a2person_whitelist)){
$this->a2person_whitelist=$a2person_whitelist;
Return TRUE;
}
Return FALSE;
}

/**
* Set a2person_name
* @param string $a2person_name is person's name
* @return boolean a2person_name set
*/
public function seta2person_name($a2person_name){
$this->a2person_name=$a2person_name;
return TRUE;
}

/**
* Display a2person
* @return string human readable display.
*/
public function display(){
$a2person_name='';
if(!\is_null($this->a2person_name)){
$a2person_name=" name: $this->a2person_name";
}
return "{person: $this->a2person_whitelist".$a2person_name."}";
}


public function a2insert() {
$query = "INSERT INTO Person (person,name,rate) VALUES (?,?,?)";
$stmt = $this->db->prepare($query);
if($stmt===FALSE){
error_log("WCS/a2person.a2insert> stmt:".$this->db->error);
return FALSE;
}
if($stmt->bind_param("ssd",$this->a2person_whitelist,$this->a2person_name,$this->a2person_email)===FALSE){
error_log("WCS/a2person.a2insert> bind_param:".$this->db->error);
return FALSE;
}
if($stmt->execute()===FALSE){
if($this->db->errno==1062){ // Duplicate.
return FALSE;
}
error_log("WCS/a2person.a2insert> execute:".$this->db->errno." ".$this->db->error);
return FALSE;
}
return TRUE;
}

/**
* Remove Person
* @return boolean TRUE
*/
public function a2delete(){
$stmt=$this->db->prepare("DELETE FROM Person WHERE person=?");
if ($stmt===FALSE){
error_log("WCS/a2person.a2delete> stmt:".$this->db->error);
return FALSE;
}
if ($stmt->bind_param('s',$this->a2person_whitelist)===FALSE){
error_log("WCS/a2person.a2delete> bind_param:".$this->db->error);
return FALSE;
}
if ($stmt->execute()===FALSE){
error_log("WCS/a2person.a2delete> execute:".$this->db->errno." ".$this->db->error);
return FALSE;
}
return TRUE;

}

public function a2get(){
$query = "SELECT name,rate FROM Person WHERE person=?";
$stmt = $this->db->prepare($query);
if ($stmt===FALSE){
echo "WCS/a2person.a2get> stmt error";
return FALSE;
}

if ($stmt->bind_param('s',$this->a2person_whitelist)===FALSE){
echo "WCS/a2person.a2get> bind_param error";
return FALSE;
}

if ($stmt->bind_result($this->a2person_name,$this->a2person_email)===FALSE){
echo "WCS/a2person.a2get> bind_result error";
return FALSE;
}

if ($stmt->execute()===FALSE){
echo "WCS/a2person.a2get> execute error";
return FALSE;
}

return $stmt->fetch();
}

}

?>
33 changes: 33 additions & 0 deletions App/a2personTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

// Person Test Copyright 2014 by WebIS Spring 2014 License Apache 2.0
require_once 'Work-Cell-Scheduler/TDD/validator.php';
include 'Work-Cell-Scheduler/Config/local.php';
require_once 'a2personApp.php';

class a2personTestCase extends WebIS\Validator {

protected static $__CLASS__=__CLASS__;

function testa2personApp() {
$n=new \WCS\a2person();
$this->AssertTrue($n->seta2person_whitelist("MikeGroene"),"this is an error message");
$this->AssertFalse($n->seta2person_whitelist("Mike...Groene"));
$this->AssertEquals("{person: MikeGroene}", $n->display());
$this->AssertTrue($n->seta2person_name("Mike Groene"));
$this->AssertEquals("{person: MikeGroene name: Mike Groene}",$n->display());
$this->AssertTrue($n->a2delete(),"first delete test");
$this->AssertTrue($n->a2delete(), "second delete test");
$this->AssertTrue($n->a2insert(),"first insert test");
$this->AssertFalse($n->a2insert(),"second insert test");
$n=new \WCS\a2person();
$this->AssertTrue($n->seta2person_whitelist("MikeGroene"));
$this->AssertTrue($n->seta2person_name("Mike Groene"));
$this->AssertEquals("{person: MikeGroene name: Mike Groene}",$n->display());
}
}

if (!defined('PHPUnit_MAIN_METHOD')) {
a2personTestCase::main();
}
?>
4 changes: 3 additions & 1 deletion App/personApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function __destruct() {
* @return bool person set.
*/
public function setPerson($person){
print "Person.setPerson:".$person;
if(preg_match('/^[a-zA-Z0-9]+$/',$person)){
$this->person=$person;
return TRUE;
Expand All @@ -59,7 +60,8 @@ public function setName($name){
public function display(){
$name='';
if(!\is_null($this->name)){
$name=" name: $this->name";
$name=" name: $this->n
ame";
}
return "{person: $this->person".$name."}";
}
Expand Down
1 change: 1 addition & 0 deletions Config/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

-- Person
DROP TABLE IF EXISTS Person;
DROP TABLE IF EXISTS a2person;
CREATE TABLE Person (
person VARCHAR(30),
name VARCHAR(128),
Expand Down
1 change: 1 addition & 0 deletions Web/topic-a2web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
10 changes: 8 additions & 2 deletions phpunit.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<?php // Unit test entire project.
header("Content-type: text/plain");
require_once 'PHPUnit/Autoload.php';
foreach(array('TDD','Test','WCS','App') as $dir) {
echo "Work-Cell-Scheduler TDD\n";
$tests=array('TDD','Test','WCS','App');
$passed=0;
foreach($tests as $dir) {
$command = new PHPUnit_TextUI_Command;
$ret = $command->run(array('phpunit.php',$dir), FALSE);
if($ret){
echo "Test $dir failed";
exit($ret);
}else{
$passed+=1;
}
}
exit(0);
echo "\nWork-Cell-Scheduler TDD $passed of ",sizeof($tests)," tests passed.\n";
exit(sizeof($tests)-$passed);
?>