[kahlan:api/core/Request] tests + fixed Request + ->checkParams~coverage:33.3%
This commit is contained in:
parent
f9e95ee501
commit
af7eb573e9
|
@ -518,18 +518,35 @@
|
|||
private function checkParams(&$params){
|
||||
/* [1] On verifie qu'il ne manque aucun parametre
|
||||
=========================================================*/
|
||||
// Si @params n'est pas un tableau
|
||||
if( !is_array($params) ){
|
||||
$this->error->set(Err::ConfigError);
|
||||
return false;
|
||||
}
|
||||
/* (1) Si @params n'est pas un tableau */
|
||||
if( !is_array($params) )
|
||||
return $this->error->set(Err::MissingParam);
|
||||
|
||||
/* (2) On récupère les données de la méthode */
|
||||
$method = $this->modules[$this->path['module']][$this->path['method']];
|
||||
|
||||
/* (3) Si pas 'parameters' dans la config */
|
||||
if( !isset($method['parameters']) || !is_array($method['parameters']) )
|
||||
return $this->error->set(Err::ConfigError);
|
||||
|
||||
|
||||
/* [2] Si le type est defini, pour chaque param, on teste
|
||||
=========================================================*/
|
||||
foreach($method['parameters'] as $name=>$paramsdata){
|
||||
|
||||
/* (1) Vérification des données
|
||||
---------------------------------------------------------*/
|
||||
/* (1) Si @name n'est pas une string */
|
||||
if( !is_string($name) )
|
||||
return $this->error->set(Err::ConfigError);
|
||||
|
||||
/* (2) Si @paramsdata n'est pas un tableau */
|
||||
if( !is_array($paramsdata) )
|
||||
return $this->error->set(Err::ConfigError);
|
||||
|
||||
|
||||
/* (2) Gestion des spécifications
|
||||
---------------------------------------------------------*/
|
||||
/* (1) On récupère si le paramètre est optionnel ou pas */
|
||||
$optional = isset($paramsdata['optional']) && $paramsdata['optional'] === true;
|
||||
|
||||
|
@ -537,10 +554,8 @@
|
|||
$isFile = isset($paramsdata['type']) && $paramsdata['type'] == 'FILE' && isset($_FILES[$name]);
|
||||
|
||||
/* (3) Si le paramètre est obligatoire et qu'il n'est pas donné -> erreur */
|
||||
if( !isset($params[$name]) && !$optional && !$isFile ){
|
||||
$this->error->set(Err::MissingParam, $name);
|
||||
return false;
|
||||
}
|
||||
if( !isset($params[$name]) && !$optional && !$isFile )
|
||||
return $this->error->set(Err::MissingParam, $name);
|
||||
|
||||
/* (4) Si le type n'est pas defini, on a pas besoin de le vérifier */
|
||||
if( !isset($paramsdata['type']) )
|
||||
|
@ -559,7 +574,8 @@
|
|||
|
||||
|
||||
/* (6) Si le paramètre est renseigné */
|
||||
}else
|
||||
}else{
|
||||
|
||||
// Si la verification est fausse, on retourne faux
|
||||
if( !Checker::run($paramsdata['type'], $params[$name]) ){
|
||||
$this->error->set(Err::WrongParam, $name, $paramsdata['type']);
|
||||
|
@ -568,6 +584,8 @@
|
|||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* [3] Gestion du retour, si tout s'est bien passe
|
||||
=========================================================*/
|
||||
return true;
|
||||
|
|
|
@ -454,6 +454,89 @@
|
|||
|
||||
});
|
||||
|
||||
describe('checkParams(@params)', function(){
|
||||
|
||||
it('fail if @params is not an array', function(){
|
||||
|
||||
// bypass checkers
|
||||
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
|
||||
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
|
||||
allow('is_array')->toBeCalled()->andReturn(false);
|
||||
|
||||
allow('json_decode')->toBeCalled()->andReturn([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => []
|
||||
]
|
||||
]);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
|
||||
expect($req->error->get())->toBe(Err::MissingParam);
|
||||
});
|
||||
|
||||
it('fail if array \'parameters\' is missing in config', function(){
|
||||
|
||||
// bypass checkers
|
||||
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
|
||||
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
|
||||
|
||||
allow('json_decode')->toBeCalled()->andReturn([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => []
|
||||
]
|
||||
]);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
||||
});
|
||||
|
||||
it('fail if \'parameters\' has no <string> name', function(){
|
||||
|
||||
// bypass checkers
|
||||
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
|
||||
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
|
||||
|
||||
allow('json_decode')->toBeCalled()->andReturn([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => [
|
||||
'parameters' => [
|
||||
1 => []
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
||||
});
|
||||
|
||||
it('fail if \'parameters\' has no <array> specification', function(){
|
||||
|
||||
// bypass checkers
|
||||
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
|
||||
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
|
||||
|
||||
allow('json_decode')->toBeCalled()->andReturn([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => [
|
||||
'parameters' => [
|
||||
'paramName' => 1
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
||||
});
|
||||
|
||||
//TODO: blabla
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue