[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,387 +457,287 @@
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' => []
] ]
]); ];
$req = new Request('moduleA/methodA'); // dispatch() -> return $this->params
allow(Request::class)->toReceive('dispatch')->andRun(function(){ return $this->params; });
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(){ context('config error', function(){
// bypass checkers it('fail if @params is not an array', function(){
allow(Request::class)->toReceive('checkPermission')->andReturn(true);
allow(Request::class)->toReceive('buildOptions')->andReturn(true);
allow('json_decode')->toBeCalled()->andReturn([ allow('is_array')->toBeCalled()->andReturn(false);
'moduleA' => [
'POST::methodA' => [
'parameters' => [
1 => []
]
]
]
]);
$req = new Request('moduleA/methodA'); allow('json_decode')->toBeCalled()->andReturn($this->json);
expect($req->error->get())->toBe(Err::ConfigError);
}); $req = new Request('moduleA/methodA');
it('fail if \'parameters\' has no <array> specification', function(){ expect($req->error->get())->toBe(Err::MissingParam);
// 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);
});
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' => [
'paramName' => [
'optional' => false
]
]
]
]
]);
$req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError);
});
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' => [
'paramName' => [
'optional' => false,
'type' => 12
]
]
]
]
]);
$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([ it('fail if array \'parameters\' is missing in config', function(){
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [
'optional' => true,
'type' => 'text'
]
]
]
]
]);
$req = new Request('moduleA/methodA'); allow('json_decode')->toBeCalled()->andReturn($this->json);
expect($req->error->get())->toBe(Err::Success);
// check param created with 'null' value $req = new Request('moduleA/methodA');
$params = $req->dispatch(); expect($req->error->get())->toBe(Err::ConfigError);
expect($params)->toContainKey('paramName');
expect($params['paramName'])->toBeNull();
});
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' => [
'paramName' => [
'optional' => true,
'type' => 'id'
]
]
]
]
]);
expect(Checker::class)->toReceive('::run')->with('id', 'bla');
$req = new Request('moduleA/methodA', ['paramName' => 'bla']);
expect($req->error->get())->toBe(Err::WrongParam);
});
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' => [
'paramName' => [
'optional' => true,
'type' => 'id'
]
]
]
]
]);
expect(Checker::class)->toReceive('::run')->with('id', '12');
$req = new Request('moduleA/methodA', ['paramName' => '12']);
expect($req->error->get())->toBe(Err::Success);
});
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' => [
'paramName' => [
'optional' => false,
'type' => 'id'
]
]
]
]
]);
$req = new Request('moduleA/methodA', []);
expect($req->error->get())->toBe(Err::MissingParam);
});
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' => [
'paramName' => [
'optional' => false,
'type' => 'id'
]
]
]
]
]);
expect(Checker::class)->toReceive('::run')->with('id', 'bla');
$req = new Request('moduleA/methodA', ['paramName' => 'bla']);
expect($req->error->get())->toBe(Err::WrongParam);
});
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' => [
'paramName' => [
'optional' => false,
'type' => 'id'
]
]
]
]
]);
expect(Checker::class)->toReceive('::run')->with('id', '12');
$req = new Request('moduleA/methodA', ['paramName' => '12']);
expect($req->error->get())->toBe(Err::Success);
});
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([ it('fail if \'parameters\' has no <string> name', function(){
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [
'type' => 'FILE'
]
]
]
]
]);
$_FILES = []; $this->json['moduleA']['POST::methodA']['parameters'] = [
$req = new Request('moduleA/methodA'); 1 => []
expect($req->error->get())->toBe(Err::MissingParam); ];
allow('json_decode')->toBeCalled()->andReturn($this->json);
}); $req = new Request('moduleA/methodA');
expect($req->error->get())->toBe(Err::ConfigError);
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([ it('fail if \'parameters\' has no <array> specification', function(){
'moduleA' => [
'POST::methodA' => [
'parameters' => [
'paramName' => [
'type' => 'FILE'
]
]
]
]
]);
$_FILES = [ 'paramName' => 'some_file_value' ]; $this->json['moduleA']['POST::methodA']['parameters'] = [
$req = new Request('moduleA/methodA'); 'paramName' => 1
expect($req->error->get())->toBe(Err::Success); ];
allow('json_decode')->toBeCalled()->andReturn($this->json);
// check param created with 'null' value $req = new Request('moduleA/methodA');
$params = $req->dispatch(); expect($req->error->get())->toBe(Err::ConfigError);
expect($params)->toContainKey('paramName');
expect($params['paramName'])->toBe('some_file_value');
});
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([ it('fail if one \'parameters\' have no \'type\' clause', function(){
'moduleA' => [
'POST::methodA' => [ $this->json['moduleA']['POST::methodA']['parameters'] = [
'parameters' => [ 'paramName' => [
'paramName' => [ 'optional' => false
'optional' => true,
'type' => 'FILE'
]
]
] ]
] ];
]); allow('json_decode')->toBeCalled()->andReturn($this->json);
$_FILES = [ ]; $req = new Request('moduleA/methodA');
$req = new Request('moduleA/methodA'); expect($req->error->get())->toBe(Err::ConfigError);
expect($req->error->get())->toBe(Err::Success);
// check param created with 'null' value });
$params = $req->dispatch();
expect($params)->toContainKey('paramName'); it('fail if one \'parameters\' have incorrect \'type\' clause', function(){
expect($params['paramName'])->toBeNull();
$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);
});
}); });
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);
// check param created with 'null' value
$params = $req->dispatch();
expect($params)->toContainKey('paramName');
expect($params['paramName'])->toBeNull();
});
it('fail if optional param wrong type ', function(){
$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']);
expect($req->error->get())->toBe(Err::WrongParam);
});
it('pass if optional param matching type ', function(){
$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']);
expect($req->error->get())->toBe(Err::Success);
});
it('fail if required param missing ', function(){
$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);
});
it('fail if required param wrong type ', function(){
$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']);
expect($req->error->get())->toBe(Err::WrongParam);
});
it('pass if required param matching type ', function(){
$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']);
expect($req->error->get())->toBe(Err::Success);
});
});
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');
expect($req->error->get())->toBe(Err::MissingParam);
});
it('pass if FILE param required + file exists -> create ref', function(){
$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');
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');
});
it('pass if FILE optional param missing -> null', function(){
$this->json['moduleA']['POST::methodA']['parameters'] = [
'paramName' => [
'optional' => true,
'type' => 'FILE'
]
];
allow('json_decode')->toBeCalled()->andReturn($this->json);
$_FILES = [ ];
$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'])->toBeNull();
});
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(){
}); });
}); });