[kahlan:api/core/Request|api/core/ModuleFactory] tests + fixed Request and ModuleFactory~coverage:100%

This commit is contained in:
xdrm-brackets 2017-09-21 15:29:36 +02:00
parent 28409ba579
commit ce9fa0705c
4 changed files with 141 additions and 6 deletions

View File

@ -2,6 +2,8 @@
namespace api\core; namespace api\core;
use \error\core\Error;
use \error\core\Err;
class ModuleFactory{ class ModuleFactory{
@ -14,18 +16,36 @@
* *
*/ */
public static function getModule($module, $arguments=[]){ 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 <string> */
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 */ /* (1) On vérifie que la classe existe */
if( !file_exists(__BUILD__."/api/module/$module.php") ) if( !file_exists(__BUILD__."/api/module/$module.php") )
return false; return new Error(Err::UncallableModule);
/* (2) On récupère la classe */ /* (2) On récupère la classe */
$class_name = "\\api\\module\\$module"; $class_name = "\\api\\module\\$module";
/* (3) On retourne une instance */ /* (3) On essaie d'instancier */
return new $class_name($arguments); try{
$instance = new $class_name($arguments);
}catch(\Error $e){
return new Error(Err::UncallableModule);
}
/* (4) On retourne l' instance */
return $instance;
} }
} }

View File

@ -184,7 +184,7 @@
=========================================================*/ =========================================================*/
$instance = ModuleFactory::getModule($this->path['module']); $instance = ModuleFactory::getModule($this->path['module']);
if( $instance === false ){ if( $instance instanceof Error ){
$this->error->set(Err::UncallableModule, $this->path['module']); $this->error->set(Err::UncallableModule, $this->path['module']);
return new Response($this->error); return new Response($this->error);
} }

View File

@ -0,0 +1,82 @@
<?php
use Kahlan\Plugin\Stub;
use Kahlan\Plugin\Monkey;
use api\core\ModuleFactory;
use error\core\Error;
use error\core\Err;
describe('api', function(){
describe('core', function(){
describe('ModuleFactory', function(){
context('param error', function(){
it('fail if @module is not a <string>', function(){
$err = ModuleFactory::getModule(11);
expect($err)->toBeAnInstanceOf('\\error\\core\\Error');
expect($err->get())->toBe(Err::WrongParam);
});
it('fail if @arguments is not an <array>', 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');
});
});
});
});
});

View File

@ -738,6 +738,39 @@
}); });
describe('dispatch()', function(){ 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);
});
}); });
}); });