add exception management, correct mistakes in error management in Image.php and add function to simplify parameters using in tests

This commit is contained in:
Yoggzo 2016-03-17 11:34:56 +01:00
parent e01936a73e
commit fe0eb3a9e2
4 changed files with 338 additions and 289 deletions

View file

@ -89,10 +89,16 @@ class AppTest{
}
public function getPostParam($name){
return $this->postParams[$name];
}
public function setPostParam($name, $value){
$this->postParams[$name] = $value;
}
public function setOutput($key, $out){

View file

@ -14,55 +14,39 @@ $opt['minDisk'] = 1;
$opt['protected'] = false;
$opt['minRam'] = 10;
//$new_image = $image->createImage($opt);
//$App->setPostParam('id', 'sdfihlus154dfhj');
$err = $image->action("createImage");
//Liste des images
$image->action("listImage");
//$images = $image->listImage();
$im = $App->show();
$images = json_decode($im, true)["Images"];
if(isset($images)){
echo "Images présentes :";
$images = json_decode($im, true)["Images"];
$recup;
echo "Images présentes :";
echo "</br>";
foreach($images as $i){
$recup = $i;
echo $recup['name'];
echo "</br>";
foreach($images as $i){
echo $i['name'];
echo "</br>";
}
echo "</br>";
if(isset($list)){
foreach ($list as $l) {
echo $l;
echo "</br>";
}
}
}
else{
echo "Aucune image présente\n";
}
//echo $recup['id'];
}
echo "</br>";
echo "Erreur capturée: ";
echo "</br>";
/*
//$App->setPostParam('id', $recup['id']);
$App->setPostParam('id', 'sdfihlus154dfhj');
$err = $image->action("detailsImage");
$temp = $App->show();
$ret = json_decode($temp, true)["Images"];
echo $ret['id'];
*/
//$App->getPostParam("id");
// Détails Image
//$details = $image->imageDetails($id_image);
//$image->deleteImage('123456');
//$image->desactivateImage($id_image);
//$image->reactivateImage($id_image);
//$file_name = "/home/yogg/Downloads/TinyCore-6.4.1.iso";
//$image->uploadImage($id_image, $file_name);
//$image->downloadImage($id_image);
/*
$opt_update = Array();
$opt_update['name'] = "Test";
$opt_update['tags'] = null;
$update = $image->updateImage($id_image, $opt_update);
echo $update->name;
*/
?>

View file

@ -32,6 +32,10 @@ Class errorManagement{
public function UserInputHandler($error){
}
public function OtherException($error){
$this->app->setOutput("Error", $error->getMessage);
}
}

View file

@ -131,6 +131,8 @@ class image implements Core{
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
$this->app->setOutput("Images", $image);
@ -156,6 +158,8 @@ class image implements Core{
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
$this->app->setOutput("Images", $result);
@ -171,26 +175,33 @@ class image implements Core{
*/
private function detailsImage(){
$id = $this->app->getPostParam("id");
if(!isset($id)){
$this->app->setOutput("Error", "Incorrect id parameter");
}
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$this->app->setOutput("Images", $image);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
else{
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
else{
echo 'toto';
$this->app->setOutput("Images", $image);
}
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
/**
@ -209,50 +220,53 @@ class image implements Core{
if(!isset($id)){
$this->app->setOutput("Error", "Incorrect id parameter");
}
if(!isset($opt)){
else if(!isset($opt)){
$this->app->setOutput("Error", "Incorrect opt parameter");
}
else{
try{
//vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
try{
//vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$options = Array();
$options = Array();
// Voir vérification des types
if(isset($opt['name'])){ //string
$options['name'] = $opt['name'];
}
if(isset($opt['minDisk'])){ //int
$options['minDisk'] = $opt['minDisk'];
}
if(isset($opt['minRam'])){ // int
$options['minRam'] = $opt['minRam'];
}
if(isset($opt['protected'])){ // boolean
$options['protected'] = $opt['protected'];
}
if(isset($opt['visibility'])){ // public, private
$options['visibility'] = $opt['visibility'];
}
if(isset($opt['tags'])){ // list
$options['tags'] = $opt['tags'];
}
$image->update($options);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$this->app->setOutput("Images", $image);
// Voir vérification des types
if(isset($opt['name'])){ //string
$options['name'] = $opt['name'];
}
if(isset($opt['minDisk'])){ //int
$options['minDisk'] = $opt['minDisk'];
}
if(isset($opt['minRam'])){ // int
$options['minRam'] = $opt['minRam'];
}
if(isset($opt['protected'])){ // boolean
$options['protected'] = $opt['protected'];
}
if(isset($opt['visibility'])){ // public, private
$options['visibility'] = $opt['visibility'];
}
if(isset($opt['tags'])){ // list
$options['tags'] = $opt['tags'];
}
$image->update($options);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
$this->app->setOutput("Images", $image);
}
}
/**
@ -269,23 +283,26 @@ class image implements Core{
if(!isset($id)){
$this->app->setOutput("Error", "Image doesn't exist");
}
try{
$service = $this->libClass;
$image = $this->libClass->getImage($id);
if($image == null){ // if the image doesn't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image->delete();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
else{
try{
$service = $this->libClass;
$image = $this->libClass->getImage($id);
if($image == null){ // if the image doesn't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image->delete();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
/**
@ -301,22 +318,27 @@ class image implements Core{
if(!isset($id)){
$this->app->setOutput("Error", "Incorrect parameter");
}
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image->reactivate();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
else
{
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image->reactivate();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
/**
@ -332,23 +354,28 @@ class image implements Core{
if(!isset($id)){
$this->app->setOutput("Error", "Incorrect parameter");
}
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image->deactivate();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
else
{
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image->deactivate();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
/**
@ -367,26 +394,30 @@ class image implements Core{
if(!isset($id)){
$this->app->setOutput("Error", "Incorrect id parameter");
}
if(!isset($file_name)){
else if(!isset($file_name)){
$this->app->setOutput("Error", "Incorrect file name parameter");
}
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$stream = \GuzzleHttp\Psr7\stream_for(fopen($file_name, 'r'));
$image->uploadData($stream);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
else{
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$stream = \GuzzleHttp\Psr7\stream_for(fopen($file_name, 'r'));
$image->uploadData($stream);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
@ -403,24 +434,28 @@ class image implements Core{
if(!isset($id)){
$this->app->setOutput("Error", "Incorrect id parameter");
}
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$stream = $image->downloadData();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$this->app->setOutput("Images", $stream);
else{
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$stream = $image->downloadData();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
$this->app->setOutput("Images", $stream);
}
}
/**
@ -438,27 +473,31 @@ class image implements Core{
if(!isset($image_id)){
$this->app->setOutput("Error", "Incorrect image id parameter");
}
if(!isset($member_id)){
else if(!isset($member_id)){
$this->app->setOutput("Error", "Incorrect member id parameter");
}
try{
$service = $this->libClass;
else{
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member_id = $image->addMember($member_id);
$this->app->setOutput("Images", $member_id);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member_id = $image->addMember($member_id);
$this->app->setOutput("Images", $member_id);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
@ -476,30 +515,34 @@ class image implements Core{
if(!isset($image_id)){
$this->app->setOutput("Error", "Incorrect image id parameter");
}
if(!isset($member_id)){
else if(!isset($member_id)){
$this->app->setOutput("Error", "Incorrect member id parameter");
}
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($image_id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$members = $image->listMembers();
if($members == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "No member");
}
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$this->app->setOutput("Images", $members);
else{
try{
// vérifier existence image
$service = $this->libClass;
$image = $service->getImage($image_id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$members = $image->listMembers();
if($members == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "No member");
}
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
$this->app->setOutput("Images", $members);
}
}
/**
@ -517,31 +560,35 @@ class image implements Core{
if(!isset($image_id)){
$this->app->setOutput("Error", "Incorrect image id parameter");
}
if(!isset($member_id)){
else if(!isset($member_id)){
$this->app->setOutput("Error", "Incorrect member id parameter");
}
try{
$service = $this->libClass;
else{
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member = $image->getMember($member_id);
if($member == null){ // if the member don't exists -> error
$this->app->setOutput("Error", "Member doesn't exist");
}
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$this->app->setOutput("Images", $member);
$member = $image->getMember($member_id);
if($member == null){ // if the member don't exists -> error
$this->app->setOutput("Error", "Member doesn't exist");
}
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
$this->app->setOutput("Images", $member);
}
}
/**
@ -559,30 +606,34 @@ class image implements Core{
if(!isset($image_id)){
$this->app->setOutput("Error", "Incorrect image id parameter");
}
if(!isset($member_id)){
else if(!isset($member_id)){
$this->app->setOutput("Error", "Incorrect member id parameter");
}
try{
$service = $this->libClass;
else{
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member = $image->getMember($member_id);
if($member == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Member doesn't exist");
}
$member->delete();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member = $image->getMember($member_id);
if($member == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Member doesn't exist");
}
$member->delete();
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
/**
@ -602,30 +653,34 @@ class image implements Core{
if(!isset($image_id)){
$this->app->setOutput("Error", "Incorrect image id parameter");
}
if(!isset($member_id)){
else if(!isset($member_id)){
$this->app->setOutput("Error", "Incorrect member id parameter");
}
try{
$service = $this->libClass;
else{
try{
$service = $this->libClass;
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member = $image->getMember($member_id);
if($member == null){ // if the member don't exists -> error
$this->app->setOutput("Error", "Member doesn't exist");
}
$member->updateStatus($status);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}
$image = $service->getImage($id);
if($image == null){ // if the image don't exists -> error
$this->app->setOutput("Error", "Image doesn't exist");
}
$member = $image->getMember($member_id);
if($member == null){ // if the member don't exists -> error
$this->app->setOutput("Error", "Member doesn't exist");
}
$member->updateStatus($status);
}catch(BadResponseError $e){
$this->app->getErrorInstance()->BadResponseHandler($e);
}catch(UserInputError $e){
$this->app->getErrorInstance()->UserInputHandler($e);
}catch(BaseError $e){
$this->app->getErrorInstance()->BaseErrorHandler($e);
}catch(NotImplementedError $e){
$this->app->getErrorInstance()->NotImplementedHandler($e);
}catch(Exception $e){
$this->app->getErrorInstance()->OtherException($e);
}
}
}
}