php - How to test this function? -


public static function flush_cache() {     static::$_cached_objects[get_class()] = array(); } 

i don't know how test phpunit? project fuelphp framework . can give advice instead of trampling on question? lot

there no "correct" way test this. reason is: have global state!

testing static values difficult because share state between unit-tests (during 1 test-run). 1 unit-test can interfere , execution-order of unit tests becomes important.

instead of using static value (which nothing else global state), should pass cache function (like dependency injection). case, have this:

public static function flush_cache($cache) {     $cache = array(); }  // test function public function testflushcache() {     $mycache = $array();     // uses cache, filling values.     flush_cache($mycache);     $this->assertequals(count($mycache), 0); } 

of course, have still take care instantiating cache in application.

but suggest avoid global state, static cache (having static function okay though). having global state makes testing pita, , can source of weird bugs because each function tapping global variable becomes non-deterministic.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -