From af7eb573e922e2aef8e87b9b1a1f1c7bea5768d2 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 21 Sep 2017 02:23:50 +0200 Subject: [PATCH] [kahlan:api/core/Request] tests + fixed Request + ->checkParams~coverage:33.3% --- build/api/core/Request.php | 38 +++++++++---- spec/build/api/core/RequestSpec.php | 83 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 10 deletions(-) diff --git a/build/api/core/Request.php b/build/api/core/Request.php index cbdf5a9..8a066a6 100644 --- a/build/api/core/Request.php +++ b/build/api/core/Request.php @@ -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,13 +574,16 @@ /* (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']); return false; } + } + } /* [3] Gestion du retour, si tout s'est bien passe diff --git a/spec/build/api/core/RequestSpec.php b/spec/build/api/core/RequestSpec.php index 265263d..0d5f700 100644 --- a/spec/build/api/core/RequestSpec.php +++ b/spec/build/api/core/RequestSpec.php @@ -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 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 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 + + }); + }); }); }); \ No newline at end of file