Unit testing a PHP Framework -
i have custom lightweight php framework our company uses. start, don't want suggestion use third-party framework. trying write unit tests have stumbled upon problem. relatively new unit testing, of code covered.
i have loader class require files can't loaded class auto loader (e.g. configuration files). has kind of fallback system if don't specify module load particular resource from, try guess based on current module, default module. has required me (so far can see) check existence of particular file before falling default.
the problem i'm running writing test class doesn't depend on files existing (which desirable). i've read virtual file systems , testing, i'm having trouble seeing how can utilize solve problem.
i can write abstraction of filesystem inject class, class have similar problem.
in pseudo code: if file exists in module, load it; if not, load default.
any suggestions appreciated.
edit:
here's excerpt of code:
class base implements loader { ... protected function _include( $type, $name ){ $identifier = $this->parsename($name); $path = '/'.$this->resource_types[$type]['directory'].'/'.$this->resource_types[$type]['file_prefix'].$identifier['name'].'.php'; if( $identifier['module'] && is_file(core::basepath().'/'.constant::get('module_path').'/'.$identifier['module'].$path) ){ core::requireroot('/'.constant::get('module_path').'/'.$identifier['module'].$path); } else if( is_file(core::basepath().'/'.constant::get('core_path').$path) ){ core::requireroot('/'.constant::get('core_path').$path); } else { throw new exceptions\load(ucwords($type).' '.$name.' not found'); } } .... }
this low-level class, has few static calls substitutes or wrappers around native php functions. core::requireroot
wrapper around native require
function. constant::get
custom implementation of constants (an alternative define
, using constant directly). keep in mind i'm not testing method directly. have public methods such loadhooks
testing call method behind scenes.
thanks both @hek2mgl , (to lesser extent) @deceze (who correct static calls), have solution problem. not kind of solution hoped for, think it's best can done. here goes.
i going use temporary files, per @hek2mgl's suggestion here (github.com/metashock/jm_autoloader). don't think there way around this, filesystem abstraction object. said, am going use filesystem abstraction object i'll inject class, because make easier re-use temporary file substitution in other contexts.
i going remove static calls in function (which knew better do, can easy convince "this such low-level class doesn't matter"). instead create getter , setter methods core_path , modules_path, , revert native call require. not need done new filesystem object, think lot cleaner , more flexible, , more loosely coupled.
Comments
Post a Comment