[kahlan:api/core/Request] tests + fixed Request->checkParams~coverage:100% @2

This commit is contained in:
xdrm-brackets 2017-09-21 15:01:10 +02:00
parent 8d93f403cd
commit 28409ba579
2 changed files with 242 additions and 341 deletions

View File

@ -173,6 +173,7 @@
if( $this->options['download'] === true ) if( $this->options['download'] === true )
return $this->download(); return $this->download();
/* [1] On verifie qu'aucune erreur n'a ete signalee /* [1] On verifie qu'aucune erreur n'a ete signalee
=========================================================*/ =========================================================*/
if( $this->error->get() !== Err::Success ) // si il y a une erreur if( $this->error->get() !== Err::Success ) // si il y a une erreur
@ -188,6 +189,7 @@
return new Response($this->error); return new Response($this->error);
} }
/* [3] On verifie que la methode est amorcable /* [3] On verifie que la methode est amorcable
=========================================================*/ =========================================================*/
if( !is_callable([$instance, $this->getModuleMethod()]) ){ if( !is_callable([$instance, $this->getModuleMethod()]) ){
@ -606,7 +608,6 @@
/* (2) Par défaut on définit les options par défaut */ /* (2) Par défaut on définit les options par défaut */
$this->options = self::$default_options; $this->options = self::$default_options;
/* (3) On récupère les options données */ /* (3) On récupère les options données */
$options = $method['options']; $options = $method['options'];

View File

@ -457,18 +457,30 @@
describe('checkParams(@params)', function(){ describe('checkParams(@params)', function(){
it('fail if @params is not an array', function(){ beforeEach(function(){
// bypass checkers // bypass checkers
allow(Request::class)->toReceive('checkPermission')->andReturn(true); allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true); allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('is_array')->toBeCalled()->andReturn(false);
allow('json_decode')->toBeCalled()->andReturn([ $this->json = [
'moduleA' => [ 'moduleA' => [
'POST::methodA' => [] 'POST::methodA' => []
] ]
]); ];
// dispatch() -> return $this->params
allow(Request::class)->toReceive('dispatch')->andRun(function(){ return $this->params; });
});
context('config error', function(){
it('fail if @params is not an array', function(){
allow('is_array')->toBeCalled()->andReturn(false);
allow('json_decode')->toBeCalled()->andReturn($this->json);
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
@ -477,15 +489,7 @@
it('fail if array \'parameters\' is missing in config', function(){ it('fail if array \'parameters\' is missing in config', function(){
// bypass checkers allow('json_decode')->toBeCalled()->andReturn($this->json);
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'); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError); expect($req->error->get())->toBe(Err::ConfigError);
@ -494,19 +498,10 @@
it('fail if \'parameters\' has no <string> name', function(){ it('fail if \'parameters\' has no <string> name', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
1 => [] 1 => []
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError); expect($req->error->get())->toBe(Err::ConfigError);
@ -515,19 +510,10 @@
it('fail if \'parameters\' has no <array> specification', function(){ it('fail if \'parameters\' has no <array> specification', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
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 'paramName' => 1
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError); expect($req->error->get())->toBe(Err::ConfigError);
@ -536,21 +522,12 @@
it('fail if one \'parameters\' have no \'type\' clause', function(){ it('fail if one \'parameters\' have no \'type\' clause', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => false 'optional' => false
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError); expect($req->error->get())->toBe(Err::ConfigError);
@ -559,51 +536,32 @@
it('fail if one \'parameters\' have incorrect \'type\' clause', function(){ it('fail if one \'parameters\' have incorrect \'type\' clause', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => false, 'optional' => false,
'type' => 12 'type' => 12
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError); expect($req->error->get())->toBe(Err::ConfigError);
}); });
it('pass if missing optional parameter -> set to null by default', function(){
// bypass checkers
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
// check param is now null
allow(Request::class)->toReceive('dispatch')->andRun(function(){
return $this->params;
}); });
allow('json_decode')->toBeCalled()->andReturn([ context('optional/required parameters', function(){
'moduleA' => [
'POST::methodA' => [ it('pass if missing optional parameter -> set to null by default', function(){
'parameters' => [
$this->json['moduleA']['POST::methodA']['parameters'] = [
'paramName' => [ 'paramName' => [
'optional' => true, 'optional' => true,
'type' => 'text' 'type' => 'text'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::Success); expect($req->error->get())->toBe(Err::Success);
@ -617,22 +575,13 @@
it('fail if optional param wrong type ', function(){ it('fail if optional param wrong type ', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => true, 'optional' => true,
'type' => 'id' 'type' => 'id'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
expect(Checker::class)->toReceive('::run')->with('id', 'bla'); expect(Checker::class)->toReceive('::run')->with('id', 'bla');
$req = new Request('moduleA/methodA', ['paramName' => 'bla']); $req = new Request('moduleA/methodA', ['paramName' => 'bla']);
@ -642,22 +591,13 @@
it('pass if optional param matching type ', function(){ it('pass if optional param matching type ', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => true, 'optional' => true,
'type' => 'id' 'type' => 'id'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
expect(Checker::class)->toReceive('::run')->with('id', '12'); expect(Checker::class)->toReceive('::run')->with('id', '12');
$req = new Request('moduleA/methodA', ['paramName' => '12']); $req = new Request('moduleA/methodA', ['paramName' => '12']);
@ -667,22 +607,13 @@
it('fail if required param missing ', function(){ it('fail if required param missing ', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => false, 'optional' => false,
'type' => 'id' 'type' => 'id'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$req = new Request('moduleA/methodA', []); $req = new Request('moduleA/methodA', []);
expect($req->error->get())->toBe(Err::MissingParam); expect($req->error->get())->toBe(Err::MissingParam);
@ -691,22 +622,13 @@
it('fail if required param wrong type ', function(){ it('fail if required param wrong type ', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => false, 'optional' => false,
'type' => 'id' 'type' => 'id'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
expect(Checker::class)->toReceive('::run')->with('id', 'bla'); expect(Checker::class)->toReceive('::run')->with('id', 'bla');
$req = new Request('moduleA/methodA', ['paramName' => 'bla']); $req = new Request('moduleA/methodA', ['paramName' => 'bla']);
@ -716,22 +638,13 @@
it('pass if required param matching type ', function(){ it('pass if required param matching type ', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => false, 'optional' => false,
'type' => 'id' 'type' => 'id'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
expect(Checker::class)->toReceive('::run')->with('id', '12'); expect(Checker::class)->toReceive('::run')->with('id', '12');
$req = new Request('moduleA/methodA', ['paramName' => '12']); $req = new Request('moduleA/methodA', ['paramName' => '12']);
@ -739,28 +652,18 @@
}); });
it('fail if FILE param required + file does not exist', function(){
// bypass checkers
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
// check param is now null
allow(Request::class)->toReceive('dispatch')->andRun(function(){
return $this->params;
}); });
allow('json_decode')->toBeCalled()->andReturn([ context('file parameters', function(){
'moduleA' => [
'POST::methodA' => [ it('fail if FILE param required + file does not exist', function(){
'parameters' => [
$this->json['moduleA']['POST::methodA']['parameters'] = [
'paramName' => [ 'paramName' => [
'type' => 'FILE' 'type' => 'FILE'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$_FILES = []; $_FILES = [];
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
@ -770,26 +673,12 @@
it('pass if FILE param required + file exists -> create ref', function(){ it('pass if FILE param required + file exists -> create ref', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
// check param is now null
allow(Request::class)->toReceive('dispatch')->andRun(function(){
return $this->params;
});
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'type' => 'FILE' 'type' => 'FILE'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$_FILES = [ 'paramName' => 'some_file_value' ]; $_FILES = [ 'paramName' => 'some_file_value' ];
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
@ -804,27 +693,13 @@
it('pass if FILE optional param missing -> null', function(){ it('pass if FILE optional param missing -> null', function(){
// bypass checkers $this->json['moduleA']['POST::methodA']['parameters'] = [
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
// check param is now null
allow(Request::class)->toReceive('dispatch')->andRun(function(){
return $this->params;
});
allow('json_decode')->toBeCalled()->andReturn([
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [ 'paramName' => [
'optional' => true, 'optional' => true,
'type' => 'FILE' 'type' => 'FILE'
] ]
] ];
] allow('json_decode')->toBeCalled()->andReturn($this->json);
]
]);
$_FILES = [ ]; $_FILES = [ ];
$req = new Request('moduleA/methodA'); $req = new Request('moduleA/methodA');
@ -837,7 +712,32 @@
}); });
it('pass if FILE optional param + file exists -> create ref', function(){
$this->json['moduleA']['POST::methodA']['parameters'] = [
'paramName' => [
'optional' => true,
'type' => 'FILE'
]
];
allow('json_decode')->toBeCalled()->andReturn($this->json);
$_FILES = [ 'paramName' => 'some_file_value' ];
$req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::Success);
// check param created with 'null' value
$params = $req->dispatch();
expect($params)->toContainKey('paramName');
expect($params['paramName'])->toBe('some_file_value');
});
});
});
describe('dispatch()', function(){
}); });
}); });