I'm very new to Unit testing; I need to use PHPUnit test to write test cases! Since I have not seen any test cases, I do not know exactly how I can test, how I can write test cases... If you can help me to test one of my small methods, I can get a good vision of testing and understand the logic. So I was wondering if you would mind helping me with testing the following method (preferably with PHPUnit, but any other test cases can help me to understand the logic as well):
$app->get('/image/:image_id', function ($param1)use($app) {
$app->response->headers->set('Access-Control-Allow-Origin','*');
$imageId = $param1;
$eject = array('-png','-gif','-jpg');
$imageName = str_replace($eject , '', $imageId);
$src = temporaryDir."/".$imageName;
$src1 = permanentDir."/".$imageName;
$type = array('.png','.gif','.jpg');
$src2 = '';
$i = 0;
while($i<3)
{
if(file_exists($src.$type[$i]))
{
$src2 = $src.$type[$i];
}
elseif(file_exists($src1.$type[$i]))
{
$src2 = $src1.$type[$i];
}
$i++;
}
if(file_exists($src2)){
$a = getimagesize($src2);
$image_type = $a[2];
switch ($image_type) {
case 1:
$format = "image/gif";
break;
case 2:
$format = "image/jpg";
break;
case 3:
$format = "image/png";
break;
default:
break;
}
$app->response->headers->set('Content-Type', $format);
echo "Ok";
}
else{
$app->halt(404, 'Not found');
}
});
If you need more clarification, please let me know and your help is totally appreciated!
Aucun commentaire:
Enregistrer un commentaire