diff --git a/build/api/core/Request.php b/build/api/core/Request.php index 588409c..90c686e 100644 --- a/build/api/core/Request.php +++ b/build/api/core/Request.php @@ -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']; diff --git a/spec/build/api/core/RequestSpec.php b/spec/build/api/core/RequestSpec.php index 431aedc..c53da85 100644 --- a/spec/build/api/core/RequestSpec.php +++ b/spec/build/api/core/RequestSpec.php @@ -457,387 +457,287 @@ 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' => [] ] - ]); + ]; - $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); + // dispatch() -> return $this->params + allow(Request::class)->toReceive('dispatch')->andRun(function(){ return $this->params; }); }); - it('fail if \'parameters\' has no name', function(){ + context('config error', function(){ - // bypass checkers - allow(Request::class)->toReceive('checkPermission')->andReturn(true); - allow(Request::class)->toReceive('buildOptions')->andReturn(true); + it('fail if @params is not an array', function(){ - allow('json_decode')->toBeCalled()->andReturn([ - 'moduleA' => [ - 'POST::methodA' => [ - 'parameters' => [ - 1 => [] - ] - ] - ] - ]); + allow('is_array')->toBeCalled()->andReturn(false); - $req = new Request('moduleA/methodA'); - expect($req->error->get())->toBe(Err::ConfigError); + allow('json_decode')->toBeCalled()->andReturn($this->json); - }); + $req = new Request('moduleA/methodA'); - 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); - - }); - - 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; + expect($req->error->get())->toBe(Err::MissingParam); }); - allow('json_decode')->toBeCalled()->andReturn([ - 'moduleA' => [ - 'POST::methodA' => [ - 'parameters' => [ - 'paramName' => [ - 'optional' => true, - 'type' => 'text' - ] - ] - ] - ] - ]); + it('fail if array \'parameters\' is missing in config', function(){ - $req = new Request('moduleA/methodA'); - expect($req->error->get())->toBe(Err::Success); + allow('json_decode')->toBeCalled()->andReturn($this->json); - // check param created with 'null' value - $params = $req->dispatch(); - expect($params)->toContainKey('paramName'); - expect($params['paramName'])->toBeNull(); + $req = new Request('moduleA/methodA'); + expect($req->error->get())->toBe(Err::ConfigError); - }); - - 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([ - 'moduleA' => [ - 'POST::methodA' => [ - 'parameters' => [ - 'paramName' => [ - 'type' => 'FILE' - ] - ] - ] - ] - ]); + it('fail if \'parameters\' has no name', function(){ - $_FILES = []; - $req = new Request('moduleA/methodA'); - expect($req->error->get())->toBe(Err::MissingParam); + $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); - 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' => [ - 'paramName' => [ - 'type' => 'FILE' - ] - ] - ] - ] - ]); + it('fail if \'parameters\' has no specification', function(){ - $_FILES = [ 'paramName' => 'some_file_value' ]; - $req = new Request('moduleA/methodA'); - expect($req->error->get())->toBe(Err::Success); + $this->json['moduleA']['POST::methodA']['parameters'] = [ + 'paramName' => 1 + ]; + allow('json_decode')->toBeCalled()->andReturn($this->json); - // check param created with 'null' value - $params = $req->dispatch(); - expect($params)->toContainKey('paramName'); - expect($params['paramName'])->toBe('some_file_value'); + $req = new Request('moduleA/methodA'); + expect($req->error->get())->toBe(Err::ConfigError); - }); - - 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' => [ - 'paramName' => [ - 'optional' => true, - 'type' => 'FILE' - ] - ] + it('fail if one \'parameters\' have no \'type\' clause', function(){ + + $this->json['moduleA']['POST::methodA']['parameters'] = [ + 'paramName' => [ + 'optional' => false ] - ] - ]); + ]; + allow('json_decode')->toBeCalled()->andReturn($this->json); - $_FILES = [ ]; - $req = new Request('moduleA/methodA'); - expect($req->error->get())->toBe(Err::Success); + $req = new Request('moduleA/methodA'); + expect($req->error->get())->toBe(Err::ConfigError); - // check param created with 'null' value - $params = $req->dispatch(); - expect($params)->toContainKey('paramName'); - expect($params['paramName'])->toBeNull(); + }); + + it('fail if one \'parameters\' have incorrect \'type\' clause', function(){ + + $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(){ }); });