diff --git a/build/api/core/ModuleFactory.php b/build/api/core/ModuleFactory.php index 527d5e9..d0bc24e 100644 --- a/build/api/core/ModuleFactory.php +++ b/build/api/core/ModuleFactory.php @@ -2,6 +2,8 @@ namespace api\core; + use \error\core\Error; + use \error\core\Err; class ModuleFactory{ @@ -14,18 +16,36 @@ * */ public static function getModule($module, $arguments=[]){ - /* (1) On gère les arguments */ - $arguments = is_array($arguments) ? $arguments : []; + /* [1] Vérification des arguments + =========================================================*/ + /* (1) @module n'est pas une */ + if( !is_string($module) ) + return new Error(Err::WrongParam); + + /* (2) @arguments n'est pas un tableau */ + if( !is_array($arguments) ) + return new Error(Err::WrongParam); + + + /* [2] Gestion de la classe + =========================================================*/ /* (1) On vérifie que la classe existe */ if( !file_exists(__BUILD__."/api/module/$module.php") ) - return false; + return new Error(Err::UncallableModule); /* (2) On récupère la classe */ $class_name = "\\api\\module\\$module"; - /* (3) On retourne une instance */ - return new $class_name($arguments); + /* (3) On essaie d'instancier */ + try{ + $instance = new $class_name($arguments); + }catch(\Error $e){ + return new Error(Err::UncallableModule); + } + + /* (4) On retourne l' instance */ + return $instance; } } diff --git a/build/api/core/Request.php b/build/api/core/Request.php index 90c686e..f652c5c 100644 --- a/build/api/core/Request.php +++ b/build/api/core/Request.php @@ -184,7 +184,7 @@ =========================================================*/ $instance = ModuleFactory::getModule($this->path['module']); - if( $instance === false ){ + if( $instance instanceof Error ){ $this->error->set(Err::UncallableModule, $this->path['module']); return new Response($this->error); } diff --git a/spec/build/api/core/ModuleFactorySpec.php b/spec/build/api/core/ModuleFactorySpec.php new file mode 100644 index 0000000..07342e4 --- /dev/null +++ b/spec/build/api/core/ModuleFactorySpec.php @@ -0,0 +1,82 @@ +', function(){ + + $err = ModuleFactory::getModule(11); + + expect($err)->toBeAnInstanceOf('\\error\\core\\Error'); + expect($err->get())->toBe(Err::WrongParam); + + }); + + it('fail if @arguments is not an ', function(){ + + $err = ModuleFactory::getModule('someMod', 12); + + expect($err)->toBeAnInstanceOf('\\error\\core\\Error'); + expect($err->get())->toBe(Err::WrongParam); + + }); + + it('pass if @arguments not given (default value)', function(){ + + $err = ModuleFactory::getModule('bla'); + + expect($err)->toBeAnInstanceOf('\\error\\core\\Error'); + expect($err->get())->not->toBe(Err::WrongParam); + + }); + + it('fail if @module implementation file does not exist', function(){ + + allow('file_exists')->toBeCalled()->andReturn(false); + + $err = ModuleFactory::getModule('bla'); + + expect($err)->toBeAnInstanceOf('\\error\\core\\Error'); + expect($err->get())->toBe(Err::UncallableModule); + + }); + + it('fail if @module implementation file exists but instanciation fails', function(){ + + allow('file_exists')->toBeCalled()->andReturn(true); + + $err = ModuleFactory::getModule('bla'); + + expect($err)->toBeAnInstanceOf('\\error\\core\\Error'); + expect($err->get())->toBe(Err::UncallableModule); + + }); + + it('pass if @module implementation file exists and instanciation succeeds', function(){ + + allow('file_exists')->toBeCalled()->andReturn(true); + + // normally the 'module' module is always here for testing purposes + $err = ModuleFactory::getModule('module'); + + expect($err)->not->toBeAnInstanceOf('\\error\\core\\Error'); + + }); + + + + }); + + }); + }); + }); \ No newline at end of file diff --git a/spec/build/api/core/RequestSpec.php b/spec/build/api/core/RequestSpec.php index c53da85..7332de4 100644 --- a/spec/build/api/core/RequestSpec.php +++ b/spec/build/api/core/RequestSpec.php @@ -738,6 +738,39 @@ }); describe('dispatch()', function(){ + + it('pass when option[\'download\'] == true -> launch download()', function(){ + + // bypass constructor + allow(Request::class)->toReceive('buildRequestObject')->andRun(function(){ + $this->options['download'] = true; + + $this->error = new Error(Err::Success); + }); + + // bypass download() + allow(Request::class)->toReceive('download')->andReturn(true); + + $req = new Request(); + expect(Request::class)->toReceive('download')->once(); + $req->dispatch(); + + }); + + it('fail when request have an error != Err::Success', function(){ + + // bypass constructor + allow(Request::class)->toReceive('buildRequestObject')->andRun(function(){ + $this->error = new Error(Err::UnknownError); + }); + + $req = new Request(); + $res = $req->dispatch(); + expect($res)->toBeAnInstanceOf('\\api\\core\\Response'); + expect($res->error->get())->toBe(Err::UnknownError); + + }); + }); });