[kahlan:api/core/Request] tests + fixed Request->checkParams~coverage:100% @2
This commit is contained in:
parent
8d93f403cd
commit
28409ba579
|
@ -173,6 +173,7 @@
|
|||
if( $this->options['download'] === true )
|
||||
return $this->download();
|
||||
|
||||
|
||||
/* [1] On verifie qu'aucune erreur n'a ete signalee
|
||||
=========================================================*/
|
||||
if( $this->error->get() !== Err::Success ) // si il y a une erreur
|
||||
|
@ -188,6 +189,7 @@
|
|||
return new Response($this->error);
|
||||
}
|
||||
|
||||
|
||||
/* [3] On verifie que la methode est amorcable
|
||||
=========================================================*/
|
||||
if( !is_callable([$instance, $this->getModuleMethod()]) ){
|
||||
|
@ -606,7 +608,6 @@
|
|||
/* (2) Par défaut on définit les options par défaut */
|
||||
$this->options = self::$default_options;
|
||||
|
||||
|
||||
/* (3) On récupère les options données */
|
||||
$options = $method['options'];
|
||||
|
||||
|
|
|
@ -457,18 +457,30 @@
|
|||
|
||||
describe('checkParams(@params)', function(){
|
||||
|
||||
it('fail if @params is not an array', function(){
|
||||
beforeEach(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([
|
||||
$this->json = [
|
||||
'moduleA' => [
|
||||
'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');
|
||||
|
||||
|
@ -477,15 +489,7 @@
|
|||
|
||||
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' => []
|
||||
]
|
||||
]);
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
@ -494,19 +498,10 @@
|
|||
|
||||
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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
1 => []
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
@ -515,19 +510,10 @@
|
|||
|
||||
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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => 1
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
@ -536,21 +522,12 @@
|
|||
|
||||
it('fail if one \'parameters\' have no \'type\' clause', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => false
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::ConfigError);
|
||||
|
@ -559,51 +536,32 @@
|
|||
|
||||
it('fail if one \'parameters\' have incorrect \'type\' clause', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => false,
|
||||
'type' => 12
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
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([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => [
|
||||
'parameters' => [
|
||||
context('optional/required parameters', function(){
|
||||
|
||||
it('pass if missing optional parameter -> set to null by default', function(){
|
||||
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => true,
|
||||
'type' => 'text'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA');
|
||||
expect($req->error->get())->toBe(Err::Success);
|
||||
|
@ -617,22 +575,13 @@
|
|||
|
||||
it('fail if optional param wrong type ', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => true,
|
||||
'type' => 'id'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
expect(Checker::class)->toReceive('::run')->with('id', 'bla');
|
||||
$req = new Request('moduleA/methodA', ['paramName' => 'bla']);
|
||||
|
@ -642,22 +591,13 @@
|
|||
|
||||
it('pass if optional param matching type ', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => true,
|
||||
'type' => 'id'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
expect(Checker::class)->toReceive('::run')->with('id', '12');
|
||||
$req = new Request('moduleA/methodA', ['paramName' => '12']);
|
||||
|
@ -667,22 +607,13 @@
|
|||
|
||||
it('fail if required param missing ', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => false,
|
||||
'type' => 'id'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$req = new Request('moduleA/methodA', []);
|
||||
expect($req->error->get())->toBe(Err::MissingParam);
|
||||
|
@ -691,22 +622,13 @@
|
|||
|
||||
it('fail if required param wrong type ', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => false,
|
||||
'type' => 'id'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
expect(Checker::class)->toReceive('::run')->with('id', 'bla');
|
||||
$req = new Request('moduleA/methodA', ['paramName' => 'bla']);
|
||||
|
@ -716,22 +638,13 @@
|
|||
|
||||
it('pass if required param matching type ', 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' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => false,
|
||||
'type' => 'id'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
expect(Checker::class)->toReceive('::run')->with('id', '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([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => [
|
||||
'parameters' => [
|
||||
context('file parameters', function(){
|
||||
|
||||
it('fail if FILE param required + file does not exist', function(){
|
||||
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'type' => 'FILE'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$_FILES = [];
|
||||
$req = new Request('moduleA/methodA');
|
||||
|
@ -770,26 +673,12 @@
|
|||
|
||||
it('pass if FILE param required + file exists -> create ref', 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([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => [
|
||||
'parameters' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'type' => 'FILE'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$_FILES = [ 'paramName' => 'some_file_value' ];
|
||||
$req = new Request('moduleA/methodA');
|
||||
|
@ -804,27 +693,13 @@
|
|||
|
||||
it('pass if FILE optional param missing -> null', 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([
|
||||
'moduleA' => [
|
||||
'POST::methodA' => [
|
||||
'parameters' => [
|
||||
$this->json['moduleA']['POST::methodA']['parameters'] = [
|
||||
'paramName' => [
|
||||
'optional' => true,
|
||||
'type' => 'FILE'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
];
|
||||
allow('json_decode')->toBeCalled()->andReturn($this->json);
|
||||
|
||||
$_FILES = [ ];
|
||||
$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(){
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue